• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.bouncycastle.jcajce.provider.symmetric.util;
2 
3 import java.security.spec.InvalidKeySpecException;
4 import java.security.spec.KeySpec;
5 
6 import javax.crypto.SecretKey;
7 import javax.crypto.spec.PBEKeySpec;
8 
9 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
10 import org.bouncycastle.crypto.CipherParameters;
11 
12 public class PBESecretKeyFactory
13     extends BaseSecretKeyFactory
14     implements PBE
15 {
16     private boolean forCipher;
17     private int scheme;
18     private int digest;
19     private int keySize;
20     private int ivSize;
21 
PBESecretKeyFactory( String algorithm, ASN1ObjectIdentifier oid, boolean forCipher, int scheme, int digest, int keySize, int ivSize)22     public PBESecretKeyFactory(
23         String algorithm,
24         ASN1ObjectIdentifier oid,
25         boolean forCipher,
26         int scheme,
27         int digest,
28         int keySize,
29         int ivSize)
30     {
31         super(algorithm, oid);
32 
33         this.forCipher = forCipher;
34         this.scheme = scheme;
35         this.digest = digest;
36         this.keySize = keySize;
37         this.ivSize = ivSize;
38     }
39 
engineGenerateSecret( KeySpec keySpec)40     protected SecretKey engineGenerateSecret(
41         KeySpec keySpec)
42         throws InvalidKeySpecException
43     {
44         if (keySpec instanceof PBEKeySpec)
45         {
46             PBEKeySpec pbeSpec = (PBEKeySpec)keySpec;
47             CipherParameters param;
48 
49             if (pbeSpec.getSalt() == null)
50             {
51                 return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, null);
52             }
53 
54             if (forCipher)
55             {
56                 param = PBE.Util.makePBEParameters(pbeSpec, scheme, digest, keySize, ivSize);
57             }
58             else
59             {
60                 param = PBE.Util.makePBEMacParameters(pbeSpec, scheme, digest, keySize);
61             }
62 
63             return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, param);
64         }
65 
66         throw new InvalidKeySpecException("Invalid KeySpec");
67     }
68 }
69