|
1 | 1 | package org.bouncycastle.jcajce.provider.symmetric; |
2 | 2 |
|
| 3 | +import java.security.InvalidAlgorithmParameterException; |
| 4 | +import java.security.spec.InvalidKeySpecException; |
| 5 | +import java.security.spec.KeySpec; |
| 6 | + |
| 7 | +import javax.crypto.SecretKey; |
| 8 | + |
| 9 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 10 | +import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
3 | 11 | import org.bouncycastle.crypto.CipherParameters; |
4 | | -import org.bouncycastle.crypto.DerivationFunction; |
5 | 12 | import org.bouncycastle.crypto.Digest; |
6 | 13 | import org.bouncycastle.crypto.digests.SHA256Digest; |
7 | 14 | import org.bouncycastle.crypto.digests.SHA384Digest; |
8 | 15 | import org.bouncycastle.crypto.digests.SHA512Digest; |
9 | 16 | import org.bouncycastle.crypto.generators.HKDFBytesGenerator; |
10 | 17 | import org.bouncycastle.crypto.params.HKDFParameters; |
11 | 18 | import org.bouncycastle.crypto.params.KeyParameter; |
12 | | -import org.bouncycastle.internal.asn1.misc.MiscObjectIdentifiers; |
13 | 19 | import org.bouncycastle.jcajce.provider.config.ConfigurableProvider; |
14 | 20 | import org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey; |
| 21 | +import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory; |
15 | 22 | import org.bouncycastle.jcajce.provider.util.AlgorithmProvider; |
| 23 | +import org.bouncycastle.jcajce.spec.HKDFParameterSpec; |
16 | 24 |
|
17 | | -import javax.crypto.SecretKey; |
18 | | -import javax.crypto.SecretKeyFactorySpi; |
19 | | -import java.security.InvalidAlgorithmParameterException; |
20 | | -import java.security.InvalidKeyException; |
21 | | -import java.security.spec.InvalidKeySpecException; |
22 | | -import java.security.spec.KeySpec; |
23 | | - |
24 | | -public class HKDF extends SecretKeyFactorySpi |
| 25 | +public class HKDF |
25 | 26 | { |
26 | | - protected String algName; |
27 | | - protected HKDFBytesGenerator hkdf; |
28 | | - |
29 | | - |
30 | | - private HKDF(String algName, Digest digest) |
| 27 | + private HKDF() |
31 | 28 | { |
32 | | - this.algName = algName; |
33 | | - this.hkdf = new HKDFBytesGenerator(digest); |
| 29 | + |
34 | 30 | } |
35 | 31 |
|
36 | | - @Override |
37 | | - protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException |
| 32 | + public static class HKDFBase |
| 33 | + extends BaseSecretKeyFactory |
38 | 34 | { |
39 | | - HKDFParameters hkdfParameters = (HKDFParameters) keySpec; |
40 | | - int derivedDataLength = hkdfParameters.getIKM().length; |
41 | | - hkdf.init(hkdfParameters); |
| 35 | + protected String algName; |
| 36 | + protected HKDFBytesGenerator hkdf; |
42 | 37 |
|
43 | | - byte[] derivedData = new byte[derivedDataLength]; |
44 | | - hkdf.generateBytes(derivedData, 0, derivedDataLength); |
45 | 38 |
|
46 | | - CipherParameters param = new KeyParameter(derivedData); |
| 39 | + public HKDFBase(String algName, Digest digest, ASN1ObjectIdentifier oid) |
| 40 | + { |
| 41 | + super(algName, oid); |
| 42 | + this.algName = algName; |
| 43 | + this.hkdf = new HKDFBytesGenerator(digest); |
| 44 | + } |
47 | 45 |
|
48 | | - return new BCPBEKey(this.algName, param) ; |
49 | | - } |
| 46 | + @Override |
| 47 | + protected SecretKey engineGenerateSecret(KeySpec keySpec) |
| 48 | + throws InvalidKeySpecException |
| 49 | + { |
| 50 | + HKDFParameterSpec spec = (HKDFParameterSpec)keySpec; |
| 51 | + int derivedDataLength = spec.getOutputLength(); |
| 52 | + hkdf.init(new HKDFParameters(spec.getIKM(), spec.getSalt(), spec.getInfo())); |
| 53 | + |
| 54 | + byte[] derivedData = new byte[derivedDataLength]; |
| 55 | + hkdf.generateBytes(derivedData, 0, derivedDataLength); |
50 | 56 |
|
| 57 | + CipherParameters param = new KeyParameter(derivedData); |
51 | 58 |
|
52 | | - @Override |
53 | | - protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec) throws InvalidKeySpecException |
| 59 | + return new BCPBEKey(this.algName, param); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static class HKDFwithSHA256 |
| 64 | + extends HKDFBase |
54 | 65 | { |
55 | | - return null; |
| 66 | + public HKDFwithSHA256() throws InvalidAlgorithmParameterException |
| 67 | + { |
| 68 | + super("HKDF-SHA256", new SHA256Digest(), PKCSObjectIdentifiers.id_alg_hkdf_with_sha256); |
| 69 | + } |
56 | 70 | } |
57 | 71 |
|
58 | | - @Override |
59 | | - protected SecretKey engineTranslateKey(SecretKey key) throws InvalidKeyException |
| 72 | + public static class HKDFwithSHA384 |
| 73 | + extends HKDFBase |
60 | 74 | { |
61 | | - return null; |
| 75 | + public HKDFwithSHA384() throws InvalidAlgorithmParameterException |
| 76 | + { |
| 77 | + super("HKDF-SHA384", new SHA384Digest(), PKCSObjectIdentifiers.id_alg_hkdf_with_sha384); |
| 78 | + } |
62 | 79 | } |
63 | 80 |
|
| 81 | + public static class HKDFwithSHA512 |
| 82 | + extends HKDFBase |
| 83 | + { |
| 84 | + |
| 85 | + public HKDFwithSHA512() throws InvalidAlgorithmParameterException |
| 86 | + { |
| 87 | + super("HKDF-SHA512", new SHA512Digest(), PKCSObjectIdentifiers.id_alg_hkdf_with_sha512); |
| 88 | + } |
| 89 | + } |
64 | 90 |
|
65 | 91 | public static class Mappings |
66 | 92 | extends AlgorithmProvider |
67 | 93 | { |
68 | | - private static final String PREFIX = SCRYPT.class.getName(); |
| 94 | + private static final String PREFIX = HKDF.class.getName(); |
69 | 95 |
|
70 | 96 | public Mappings() |
71 | 97 | { |
72 | 98 | } |
73 | 99 |
|
74 | 100 | public void configure(ConfigurableProvider provider) |
75 | 101 | { |
76 | | - provider.addAlgorithm("SecretKeyFactory.HKDF", PREFIX + "HKDF"); |
77 | 102 | provider.addAlgorithm("SecretKeyFactory.HKDF-SHA256", PREFIX + "$HKDFwithSHA256"); |
78 | 103 | provider.addAlgorithm("SecretKeyFactory.HKDF-SHA384", PREFIX + "$HKDFwithSHA384"); |
79 | 104 | provider.addAlgorithm("SecretKeyFactory.HKDF-SHA512", PREFIX + "$HKDFwithSHA512"); |
80 | | - |
81 | | - } |
82 | | - } |
83 | | - public static class HKDFwithSHA256 extends HKDF |
84 | | - { |
85 | | - public HKDFwithSHA256() throws InvalidAlgorithmParameterException |
86 | | - { |
87 | | - super("HKDF-SHA256", new SHA256Digest()); |
88 | | - } |
89 | | - } |
90 | | - public static class HKDFwithSHA384 extends HKDF |
91 | | - { |
92 | | - public HKDFwithSHA384() throws InvalidAlgorithmParameterException |
93 | | - { |
94 | | - super("HKDF-SHA384", new SHA384Digest()); |
95 | | - } |
96 | | - } |
97 | | - public static class HKDFwithSHA512 extends HKDF |
98 | | - { |
99 | | - |
100 | | - public HKDFwithSHA512() throws InvalidAlgorithmParameterException |
101 | | - { |
102 | | - super("HKDF-SHA512", new SHA512Digest()); |
103 | 105 | } |
104 | 106 | } |
105 | | - |
106 | 107 | } |
0 commit comments