Skip to content

Commit 175a269

Browse files
committed
Merge branch 'java-25-kdf' into 'main'
Java 25 KDF See merge request root/bc-java!120
2 parents d9da0ba + 91ed2b0 commit 175a269

File tree

21 files changed

+959
-27
lines changed

21 files changed

+959
-27
lines changed

core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecT571K1Point.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bouncycastle.math.ec.ECPoint.AbstractF2m;
88
import org.bouncycastle.math.raw.Nat576;
99

10+
@SuppressWarnings("AssignmentExpression")
1011
public class SecT571K1Point extends AbstractF2m
1112
{
1213
SecT571K1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)
@@ -58,7 +59,7 @@ protected boolean getCompressionYTilde()
5859
// Y is actually Lambda (X + Y/X) here
5960
return Y.testBitZero() != X.testBitZero();
6061
}
61-
62+
6263
public ECPoint add(ECPoint b)
6364
{
6465
if (this.isInfinity())

core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecT571R1Point.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bouncycastle.math.raw.Nat;
99
import org.bouncycastle.math.raw.Nat576;
1010

11+
@SuppressWarnings("AssignmentExpression")
1112
public class SecT571R1Point extends AbstractF2m
1213
{
1314
SecT571R1Point(ECCurve curve, ECFieldElement x, ECFieldElement y)

pkix/src/main/java/org/bouncycastle/cert/ocsp/BasicOCSPRespBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static class ResponseObject
4343
ASN1GeneralizedTime nextUpdate;
4444
Extensions extensions;
4545

46-
public ResponseObject(
46+
ResponseObject(
4747
CertificateID certId,
4848
CertificateStatus certStatus,
4949
Date thisUpdate,
@@ -90,7 +90,7 @@ else if (certStatus instanceof UnknownStatus)
9090
this.extensions = extensions;
9191
}
9292

93-
public SingleResponse toResponse()
93+
SingleResponse toResponse()
9494
throws Exception
9595
{
9696
return new SingleResponse(certId.toASN1Primitive(), certStatus, thisUpdate, nextUpdate, extensions);

pkix/src/main/java/org/bouncycastle/cert/ocsp/OCSPReqBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ private static class RequestObject
3131
CertificateID certId;
3232
Extensions extensions;
3333

34-
public RequestObject(
34+
RequestObject(
3535
CertificateID certId,
3636
Extensions extensions)
3737
{
3838
this.certId = certId;
3939
this.extensions = extensions;
4040
}
4141

42-
public Request toRequest()
42+
Request toRequest()
4343
throws Exception
4444
{
4545
return new Request(certId.toASN1Primitive(), extensions);

pkix/src/main/java/org/bouncycastle/cert/selector/MSOutlookKeyIdCalculator.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private static abstract class GeneralDigest
4444
/**
4545
* Standard constructor
4646
*/
47-
protected GeneralDigest()
47+
GeneralDigest()
4848
{
4949
xBuf = new byte[4];
5050
xBufOff = 0;
@@ -55,22 +55,22 @@ protected GeneralDigest()
5555
* of the Object.clone() interface as this interface is not
5656
* supported by J2ME.
5757
*/
58-
protected GeneralDigest(GeneralDigest t)
58+
GeneralDigest(GeneralDigest t)
5959
{
6060
xBuf = new byte[t.xBuf.length];
6161

6262
copyIn(t);
6363
}
6464

65-
protected void copyIn(GeneralDigest t)
65+
void copyIn(GeneralDigest t)
6666
{
6767
System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);
6868

6969
xBufOff = t.xBufOff;
7070
byteCount = t.byteCount;
7171
}
7272

73-
public void update(
73+
void update(
7474
byte in)
7575
{
7676
xBuf[xBufOff++] = in;
@@ -84,7 +84,7 @@ public void update(
8484
byteCount++;
8585
}
8686

87-
public void update(
87+
void update(
8888
byte[] in,
8989
int inOff,
9090
int len)
@@ -124,7 +124,7 @@ public void update(
124124
}
125125
}
126126

127-
public void finish()
127+
void finish()
128128
{
129129
long bitLength = (byteCount << 3);
130130

@@ -143,7 +143,7 @@ public void finish()
143143
processBlock();
144144
}
145145

146-
public void reset()
146+
void reset()
147147
{
148148
byteCount = 0;
149149

@@ -154,11 +154,11 @@ public void reset()
154154
}
155155
}
156156

157-
protected abstract void processWord(byte[] in, int inOff);
157+
abstract void processWord(byte[] in, int inOff);
158158

159-
protected abstract void processLength(long bitLength);
159+
abstract void processLength(long bitLength);
160160

161-
protected abstract void processBlock();
161+
abstract void processBlock();
162162
}
163163

164164
private static class SHA1Digest
@@ -174,22 +174,22 @@ private static class SHA1Digest
174174
/**
175175
* Standard constructor
176176
*/
177-
public SHA1Digest()
177+
SHA1Digest()
178178
{
179179
reset();
180180
}
181181

182-
public String getAlgorithmName()
182+
String getAlgorithmName()
183183
{
184184
return "SHA-1";
185185
}
186186

187-
public int getDigestSize()
187+
int getDigestSize()
188188
{
189189
return DIGEST_LENGTH;
190190
}
191191

192-
protected void processWord(
192+
void processWord(
193193
byte[] in,
194194
int inOff)
195195
{
@@ -207,7 +207,7 @@ protected void processWord(
207207
}
208208
}
209209

210-
protected void processLength(
210+
void processLength(
211211
long bitLength)
212212
{
213213
if (xOff > 14)
@@ -219,7 +219,7 @@ protected void processLength(
219219
X[15] = (int)(bitLength & 0xffffffff);
220220
}
221221

222-
public int doFinal(
222+
int doFinal(
223223
byte[] out,
224224
int outOff)
225225
{
@@ -239,7 +239,7 @@ public int doFinal(
239239
/**
240240
* reset the chaining variables
241241
*/
242-
public void reset()
242+
void reset()
243243
{
244244
super.reset();
245245

@@ -288,7 +288,7 @@ private int g(
288288
return ((u & v) | (u & w) | (v & w));
289289
}
290290

291-
protected void processBlock()
291+
void processBlock()
292292
{
293293
//
294294
// expand 16 word block into 80 word block.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.bouncycastle.jcajce.provider.symmetric;
2+
3+
import org.bouncycastle.crypto.CipherParameters;
4+
import org.bouncycastle.crypto.DerivationFunction;
5+
import org.bouncycastle.crypto.Digest;
6+
import org.bouncycastle.crypto.digests.SHA256Digest;
7+
import org.bouncycastle.crypto.digests.SHA384Digest;
8+
import org.bouncycastle.crypto.digests.SHA512Digest;
9+
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
10+
import org.bouncycastle.crypto.params.HKDFParameters;
11+
import org.bouncycastle.crypto.params.KeyParameter;
12+
import org.bouncycastle.internal.asn1.misc.MiscObjectIdentifiers;
13+
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
14+
import org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey;
15+
import org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
16+
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+
{
26+
protected String algName;
27+
protected HKDFBytesGenerator hkdf;
28+
29+
30+
private HKDF(String algName, Digest digest)
31+
{
32+
this.algName = algName;
33+
this.hkdf = new HKDFBytesGenerator(digest);
34+
}
35+
36+
@Override
37+
protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException
38+
{
39+
HKDFParameters hkdfParameters = (HKDFParameters) keySpec;
40+
int derivedDataLength = hkdfParameters.getIKM().length;
41+
hkdf.init(hkdfParameters);
42+
43+
byte[] derivedData = new byte[derivedDataLength];
44+
hkdf.generateBytes(derivedData, 0, derivedDataLength);
45+
46+
CipherParameters param = new KeyParameter(derivedData);
47+
48+
return new BCPBEKey(this.algName, param) ;
49+
}
50+
51+
52+
@Override
53+
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec) throws InvalidKeySpecException
54+
{
55+
return null;
56+
}
57+
58+
@Override
59+
protected SecretKey engineTranslateKey(SecretKey key) throws InvalidKeyException
60+
{
61+
return null;
62+
}
63+
64+
65+
public static class Mappings
66+
extends AlgorithmProvider
67+
{
68+
private static final String PREFIX = SCRYPT.class.getName();
69+
70+
public Mappings()
71+
{
72+
}
73+
74+
public void configure(ConfigurableProvider provider)
75+
{
76+
provider.addAlgorithm("SecretKeyFactory.HKDF", PREFIX + "HKDF");
77+
provider.addAlgorithm("SecretKeyFactory.HKDF-SHA256", PREFIX + "$HKDFwithSHA256");
78+
provider.addAlgorithm("SecretKeyFactory.HKDF-SHA384", PREFIX + "$HKDFwithSHA384");
79+
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+
}
104+
}
105+
106+
}

prov/src/main/java/org/bouncycastle/jce/provider/BouncyCastleProvider.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ public final class BouncyCastleProvider extends Provider
163163
"DRBG"
164164
};
165165

166+
/*
167+
* Configurable kdfs
168+
*/
169+
private static final String KDF_PACKAGE = "org.bouncycastle.jcajce.provider.kdf.";
170+
private static final String[] KDFS =
171+
{
172+
"HKDF", "PBEPBKDF2", "SCRYPT"
173+
};
174+
175+
176+
166177
private Map<String, Service> serviceMap = new ConcurrentHashMap<String, Service>();
167178

168179
/**
@@ -202,6 +213,8 @@ private void setup()
202213

203214
loadAlgorithms(SECURE_RANDOM_PACKAGE, SECURE_RANDOMS);
204215

216+
loadAlgorithms(KDF_PACKAGE, KDFS);
217+
205218
loadPQCKeys(); // so we can handle certificates containing them.
206219

207220
//
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.bouncycastle.jcajce.provider.kdf;
2+
3+
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
4+
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
5+
import org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
6+
7+
class HKDF
8+
{
9+
private static final String PREFIX = "org.bouncycastle.jcajce.provider.kdf" + ".hkdf.";
10+
11+
public static class Mappings
12+
extends AlgorithmProvider
13+
{
14+
public Mappings()
15+
{
16+
}
17+
18+
public void configure(ConfigurableProvider provider)
19+
{
20+
21+
provider.addAlgorithm("KDF.HKDF", PREFIX + "HKDFSpi");
22+
provider.addAlgorithm("KDF.HKDF-SHA256", PREFIX + "HKDFSpi$HKDFwithSHA256");
23+
provider.addAlgorithm("KDF.HKDF-SHA384", PREFIX + "HKDFSpi$HKDFwithSHA384");
24+
provider.addAlgorithm("KDF.HKDF-SHA512", PREFIX + "HKDFSpi$HKDFwithSHA512");
25+
26+
// Use SymmetricAlgorithmProvider?
27+
//TODO: add PKCSObjectIdentifiers?
28+
//PKCSObjectIdentifiers.id_alg_hkdf_with_sha256
29+
//PKCSObjectIdentifiers.id_alg_hkdf_with_sha384
30+
//PKCSObjectIdentifiers.id_alg_hkdf_with_sha512
31+
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)