Skip to content

Commit c9a1848

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents 785defe + 568ffae commit c9a1848

99 files changed

Lines changed: 1483 additions & 1112 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

core/src/main/java/org/bouncycastle/asn1/ASN1TaggedObject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static ASN1TaggedObject getInstance(ASN1TaggedObject taggedObject, int ta
7575
return ASN1Util.getExplicitBaseTagged(checkInstance(taggedObject, declaredExplicit), tagClass, tagNo);
7676
}
7777

78-
public static ASN1TaggedObject getOptional(ASN1Object element)
78+
public static ASN1TaggedObject getOptional(ASN1Encodable element)
7979
{
8080
if (element == null)
8181
{
@@ -90,7 +90,7 @@ public static ASN1TaggedObject getOptional(ASN1Object element)
9090
return null;
9191
}
9292

93-
public static ASN1TaggedObject getOptional(ASN1Object element, int tagClass)
93+
public static ASN1TaggedObject getOptional(ASN1Encodable element, int tagClass)
9494
{
9595
ASN1TaggedObject taggedObject = getOptional(element);
9696
if (taggedObject != null && taggedObject.hasTagClass(tagClass))
@@ -100,7 +100,7 @@ public static ASN1TaggedObject getOptional(ASN1Object element, int tagClass)
100100
return null;
101101
}
102102

103-
public static ASN1TaggedObject getOptional(ASN1Object element, int tagClass, int tagNo)
103+
public static ASN1TaggedObject getOptional(ASN1Encodable element, int tagClass, int tagNo)
104104
{
105105
ASN1TaggedObject taggedObject = getOptional(element);
106106
if (taggedObject != null && taggedObject.hasTag(tagClass, tagNo))

core/src/main/java/org/bouncycastle/asn1/x509/AuthorityKeyIdentifier.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@
3535
public class AuthorityKeyIdentifier
3636
extends ASN1Object
3737
{
38-
ASN1OctetString keyidentifier = null;
38+
ASN1OctetString keyIdentifier = null;
3939
GeneralNames certissuer = null;
4040
ASN1Integer certserno = null;
4141

42-
public static AuthorityKeyIdentifier getInstance(
43-
ASN1TaggedObject obj,
44-
boolean explicit)
42+
public static AuthorityKeyIdentifier getInstance(ASN1TaggedObject obj, boolean explicit)
4543
{
46-
return getInstance(ASN1Sequence.getInstance(obj, explicit));
44+
return new AuthorityKeyIdentifier(ASN1Sequence.getInstance(obj, explicit));
4745
}
4846

4947
public static AuthorityKeyIdentifier getInstance(
@@ -78,7 +76,7 @@ protected AuthorityKeyIdentifier(
7876
switch (o.getTagNo())
7977
{
8078
case 0:
81-
this.keyidentifier = ASN1OctetString.getInstance(o, false);
79+
this.keyIdentifier = ASN1OctetString.getInstance(o, false);
8280
break;
8381
case 1:
8482
this.certissuer = GeneralNames.getInstance(o, false);
@@ -128,7 +126,7 @@ public AuthorityKeyIdentifier(
128126
digest.update(bytes, 0, bytes.length);
129127
digest.doFinal(resBuf, 0);
130128

131-
this.keyidentifier = new DEROctetString(resBuf);
129+
this.keyIdentifier = new DEROctetString(resBuf);
132130
this.certissuer = name;
133131
this.certserno = (serialNumber != null) ? new ASN1Integer(serialNumber) : null;
134132
}
@@ -162,21 +160,34 @@ public AuthorityKeyIdentifier(
162160
GeneralNames name,
163161
BigInteger serialNumber)
164162
{
165-
this.keyidentifier = (keyIdentifier != null) ? new DEROctetString(Arrays.clone(keyIdentifier)) : null;
163+
this.keyIdentifier = (keyIdentifier != null) ? new DEROctetString(Arrays.clone(keyIdentifier)) : null;
166164
this.certissuer = name;
167165
this.certserno = (serialNumber != null) ? new ASN1Integer(serialNumber) : null;
168166
}
169-
167+
168+
/**
169+
* @deprecated Use {@link #getKeyIdentifierOctets()} instead.
170+
*/
170171
public byte[] getKeyIdentifier()
171172
{
172-
if (keyidentifier != null)
173+
return getKeyIdentifierOctets();
174+
}
175+
176+
public byte[] getKeyIdentifierOctets()
177+
{
178+
if (keyIdentifier != null)
173179
{
174-
return keyidentifier.getOctets();
180+
return keyIdentifier.getOctets();
175181
}
176182

177183
return null;
178184
}
179185

186+
public ASN1OctetString getKeyIdentifierObject()
187+
{
188+
return keyIdentifier;
189+
}
190+
180191
public GeneralNames getAuthorityCertIssuer()
181192
{
182193
return certissuer;
@@ -199,9 +210,9 @@ public ASN1Primitive toASN1Primitive()
199210
{
200211
ASN1EncodableVector v = new ASN1EncodableVector(3);
201212

202-
if (keyidentifier != null)
213+
if (keyIdentifier != null)
203214
{
204-
v.add(new DERTaggedObject(false, 0, keyidentifier));
215+
v.add(new DERTaggedObject(false, 0, keyIdentifier));
205216
}
206217

207218
if (certissuer != null)
@@ -220,7 +231,7 @@ public ASN1Primitive toASN1Primitive()
220231
public String toString()
221232
{
222233
// -DM Hex.toHexString
223-
String keyID = (keyidentifier != null) ? Hex.toHexString(keyidentifier.getOctets()) : "null";
234+
String keyID = (keyIdentifier != null) ? Hex.toHexString(keyIdentifier.getOctets()) : "null";
224235

225236
return "AuthorityKeyIdentifier: KeyID(" + keyID + ")";
226237
}

core/src/main/java/org/bouncycastle/crypto/agreement/ecjpake/ECJPAKEUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ public static void validateZeroKnowledgeProof(
219219

220220
ECPoint x_normalized = X.normalize();
221221
// 2. Check x and y coordinates are in Fq, i.e., x, y in [0, q-1]
222-
if (x_normalized.getAffineXCoord().toBigInteger().compareTo(BigInteger.ZERO) == -1 ||
223-
x_normalized.getAffineXCoord().toBigInteger().compareTo(q.subtract(BigInteger.ONE)) == 1 ||
224-
x_normalized.getAffineYCoord().toBigInteger().compareTo(BigInteger.ZERO) == -1 ||
225-
x_normalized.getAffineYCoord().toBigInteger().compareTo(q.subtract(BigInteger.ONE)) == 1)
222+
if (x_normalized.getAffineXCoord().toBigInteger().signum() < 0 ||
223+
x_normalized.getAffineXCoord().toBigInteger().compareTo(q) >= 0 ||
224+
x_normalized.getAffineYCoord().toBigInteger().signum() < 0 ||
225+
x_normalized.getAffineYCoord().toBigInteger().compareTo(q) >= 0)
226226
{
227227
throw new CryptoException("Zero-knowledge proof validation failed: x and y are not in the field");
228228
}

core/src/main/java/org/bouncycastle/crypto/agreement/jpake/JPAKEPrimeOrderGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public JPAKEPrimeOrderGroup(BigInteger p, BigInteger q, BigInteger g)
7070
{
7171
throw new IllegalArgumentException("p-1 must be evenly divisible by q");
7272
}
73-
if (g.compareTo(BigInteger.valueOf(2)) == -1 || g.compareTo(p.subtract(JPAKEUtil.ONE)) == 1)
73+
if (g.compareTo(BigInteger.valueOf(2)) < 0 || g.compareTo(p) >= 0)
7474
{
7575
throw new IllegalArgumentException("g must be in [2, p-1]");
7676
}

core/src/main/java/org/bouncycastle/crypto/agreement/jpake/JPAKEUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ public static void validateZeroKnowledgeProof(
252252
BigInteger r = zeroKnowledgeProof[1];
253253

254254
BigInteger h = calculateHashForZeroKnowledgeProof(g, gv, gx, participantId, digest);
255-
if (!(gx.compareTo(ZERO) == 1 && // g^x > 0
256-
gx.compareTo(p) == -1 && // g^x < p
257-
gx.modPow(q, p).compareTo(ONE) == 0 && // g^x^q mod q = 1
255+
if (!(gx.signum() > 0 && // g^x > 0
256+
gx.compareTo(p) < 0 && // g^x < p
257+
gx.modPow(q, p).equals(ONE) && // g^x^q mod q = 1
258258
/*
259259
* Below, I took an straightforward way to compute g^r * g^x^h,
260260
* which needs 2 exp. Using a simultaneous computation technique
261261
* would only need 1 exp.
262262
*/
263-
g.modPow(r, p).multiply(gx.modPow(h, p)).mod(p).compareTo(gv) == 0)) // g^v=g^r * g^x^h
263+
g.modPow(r, p).multiply(gx.modPow(h, p)).mod(p).equals(gv))) // g^v=g^r * g^x^h
264264
{
265265
throw new CryptoException("Zero-knowledge proof validation failed");
266266
}

core/src/main/java/org/bouncycastle/crypto/generators/KDFCounterBytesGenerator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.bouncycastle.crypto.generators;
22

3-
import java.math.BigInteger;
4-
53
import org.bouncycastle.crypto.DataLengthException;
64
import org.bouncycastle.crypto.DerivationParameters;
75
import org.bouncycastle.crypto.Mac;
86
import org.bouncycastle.crypto.MacDerivationFunction;
97
import org.bouncycastle.crypto.params.KDFCounterParameters;
108
import org.bouncycastle.crypto.params.KeyParameter;
9+
import org.bouncycastle.util.Integers;
1110

1211
/**
1312
* This KDF has been defined by the publicly available NIST SP 800-108 specification.
@@ -39,10 +38,6 @@
3938
public class KDFCounterBytesGenerator
4039
implements MacDerivationFunction
4140
{
42-
43-
private static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
44-
private static final BigInteger TWO = BigInteger.valueOf(2);
45-
4641
// please refer to the standard for the meaning of the variable names
4742
// all field lengths are in bytes, not in bits as specified by the standard
4843

@@ -92,9 +87,7 @@ public void init(DerivationParameters param)
9287
int r = kdfParams.getR();
9388
this.ios = new byte[r / 8];
9489

95-
BigInteger maxSize = TWO.pow(r).multiply(BigInteger.valueOf(h));
96-
this.maxSizeExcl = maxSize.compareTo(INTEGER_MAX) == 1 ?
97-
Integer.MAX_VALUE : maxSize.intValue();
90+
this.maxSizeExcl = r >= Integers.numberOfLeadingZeros(h) ? Integer.MAX_VALUE : h << r;
9891

9992
// --- set operational state ---
10093

core/src/main/java/org/bouncycastle/crypto/generators/KDFDoublePipelineIterationBytesGenerator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package org.bouncycastle.crypto.generators;
22

3-
import java.math.BigInteger;
4-
53
import org.bouncycastle.crypto.DataLengthException;
64
import org.bouncycastle.crypto.DerivationParameters;
75
import org.bouncycastle.crypto.Mac;
86
import org.bouncycastle.crypto.MacDerivationFunction;
97
import org.bouncycastle.crypto.params.KDFDoublePipelineIterationParameters;
108
import org.bouncycastle.crypto.params.KeyParameter;
9+
import org.bouncycastle.util.Integers;
1110

1211
/**
1312
* This KDF has been defined by the publicly available NIST SP 800-108 specification.
1413
*/
1514
public class KDFDoublePipelineIterationBytesGenerator
1615
implements MacDerivationFunction
1716
{
18-
19-
private static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
20-
private static final BigInteger TWO = BigInteger.valueOf(2);
21-
2217
// please refer to the standard for the meaning of the variable names
2318
// all field lengths are in bytes, not in bits as specified by the standard
2419

@@ -71,9 +66,7 @@ public void init(DerivationParameters params)
7166
if (dpiParams.useCounter())
7267
{
7368
// this is more conservative than the spec
74-
BigInteger maxSize = TWO.pow(r).multiply(BigInteger.valueOf(h));
75-
this.maxSizeExcl = maxSize.compareTo(INTEGER_MAX) == 1 ?
76-
Integer.MAX_VALUE : maxSize.intValue();
69+
this.maxSizeExcl = r >= Integers.numberOfLeadingZeros(h) ? Integer.MAX_VALUE : h << r;
7770
}
7871
else
7972
{

core/src/main/java/org/bouncycastle/crypto/generators/KDFFeedbackBytesGenerator.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package org.bouncycastle.crypto.generators;
22

3-
import java.math.BigInteger;
4-
53
import org.bouncycastle.crypto.DataLengthException;
64
import org.bouncycastle.crypto.DerivationParameters;
75
import org.bouncycastle.crypto.Mac;
86
import org.bouncycastle.crypto.MacDerivationFunction;
97
import org.bouncycastle.crypto.params.KDFFeedbackParameters;
108
import org.bouncycastle.crypto.params.KeyParameter;
9+
import org.bouncycastle.util.Integers;
1110

1211
/**
1312
* This KDF has been defined by the publicly available NIST SP 800-108 specification.
1413
*/
1514
public class KDFFeedbackBytesGenerator
1615
implements MacDerivationFunction
1716
{
18-
19-
private static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
20-
private static final BigInteger TWO = BigInteger.valueOf(2);
21-
2217
// please refer to the standard for the meaning of the variable names
2318
// all field lengths are in bytes, not in bits as specified by the standard
2419

@@ -70,9 +65,7 @@ public void init(DerivationParameters params)
7065
if (feedbackParams.useCounter())
7166
{
7267
// this is more conservative than the spec
73-
BigInteger maxSize = TWO.pow(r).multiply(BigInteger.valueOf(h));
74-
this.maxSizeExcl = maxSize.compareTo(INTEGER_MAX) == 1 ?
75-
Integer.MAX_VALUE : maxSize.intValue();
68+
this.maxSizeExcl = r >= Integers.numberOfLeadingZeros(h) ? Integer.MAX_VALUE : h << r;
7669
}
7770
else
7871
{

core/src/main/java/org/bouncycastle/crypto/modes/CCMModeCipher.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
public interface CCMModeCipher
44
extends AEADBlockCipher
55
{
6+
// TODO Add these so that all usages of CCMBlockCipher can be replaced by CCMModeCipher
7+
// byte[] processPacket(byte[] in, int inOff, int inLen)
8+
// throws IllegalStateException, InvalidCipherTextException;
9+
//
10+
// int processPacket(byte[] in, int inOff, int inLen, byte[] output, int outOff)
11+
// throws IllegalStateException, InvalidCipherTextException, DataLengthException;
612
}

core/src/main/java/org/bouncycastle/crypto/signers/SM2Signer.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.crypto.signers;
22

33
import java.math.BigInteger;
4+
import java.security.SecureRandom;
45

56
import org.bouncycastle.crypto.CipherParameters;
67
import org.bouncycastle.crypto.CryptoException;
@@ -92,35 +93,37 @@ public void init(boolean forSigning, CipherParameters param)
9293

9394
if (forSigning)
9495
{
96+
SecureRandom random = null;
9597
if (baseParam instanceof ParametersWithRandom)
9698
{
97-
ParametersWithRandom rParam = (ParametersWithRandom)baseParam;
98-
99-
ecKey = (ECKeyParameters)rParam.getParameters();
100-
ecParams = ecKey.getParameters();
101-
kCalculator.init(ecParams.getN(), rParam.getRandom());
102-
}
103-
else
104-
{
105-
ecKey = (ECKeyParameters)baseParam;
106-
ecParams = ecKey.getParameters();
107-
kCalculator.init(ecParams.getN(), CryptoServicesRegistrar.getSecureRandom());
99+
ParametersWithRandom withRandom = (ParametersWithRandom)baseParam;
100+
baseParam = withRandom.getParameters();
101+
random = withRandom.getRandom();
108102
}
109103

110-
BigInteger d = ((ECPrivateKeyParameters)ecKey).getD();
111-
BigInteger nSub1 = ecParams.getN().subtract(BigIntegers.ONE);
104+
ECPrivateKeyParameters ecPrivateKey = (ECPrivateKeyParameters)baseParam;
105+
106+
ecKey = ecPrivateKey;
107+
ecParams = ecPrivateKey.getParameters();
112108

113-
if (d.compareTo(ONE) < 0 || d.compareTo(nSub1) >= 0)
109+
BigInteger d = ecPrivateKey.getD();
110+
BigInteger n = ecParams.getN();
111+
112+
if (d.compareTo(ONE) < 0 || d.compareTo(n.subtract(ONE)) >= 0)
114113
{
115114
throw new IllegalArgumentException("SM2 private key out of range");
116115
}
116+
117+
kCalculator.init(n, CryptoServicesRegistrar.getSecureRandom(random));
117118
pubPoint = createBasePointMultiplier().multiply(ecParams.getG(), d).normalize();
118119
}
119120
else
120121
{
121-
ecKey = (ECKeyParameters)baseParam;
122-
ecParams = ecKey.getParameters();
123-
pubPoint = ((ECPublicKeyParameters)ecKey).getQ();
122+
ECPublicKeyParameters ecPublicKey = (ECPublicKeyParameters)baseParam;
123+
124+
ecKey = ecPublicKey;
125+
ecParams = ecPublicKey.getParameters();
126+
pubPoint = ecPublicKey.getQ();
124127
}
125128

126129
CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("ECNR", ecKey, forSigning));

0 commit comments

Comments
 (0)