Skip to content

Commit 2c18c6d

Browse files
author
gefeili
committed
Merge branch 'main' into 1958-aead-parameters
2 parents 4127c2f + 9706d9a commit 2c18c6d

File tree

15 files changed

+385
-102
lines changed

15 files changed

+385
-102
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public class ECJPAKECurve
2020
{
21-
private final ECCurve.Fp curve;
21+
private final ECCurve.AbstractFp curve;
2222
private final ECPoint g;
2323

2424
/**
@@ -116,7 +116,7 @@ public ECJPAKECurve(BigInteger q, BigInteger a, BigInteger b, BigInteger n, BigI
116116
* groups in {@link ECJPAKECurves}.
117117
* These pre-approved curves can avoid the expensive checks.
118118
*/
119-
ECJPAKECurve(ECCurve.Fp curve, ECPoint g)
119+
ECJPAKECurve(ECCurve.AbstractFp curve, ECPoint g)
120120
{
121121
ECJPAKEUtil.validateNotNull(curve, "curve");
122122
ECJPAKEUtil.validateNotNull(g, "g");
@@ -127,7 +127,7 @@ public ECJPAKECurve(BigInteger q, BigInteger a, BigInteger b, BigInteger n, BigI
127127
this.g = g;
128128
}
129129

130-
public ECCurve.Fp getCurve()
130+
public ECCurve.AbstractFp getCurve()
131131
{
132132
return curve;
133133
}
@@ -159,7 +159,7 @@ public BigInteger getH()
159159

160160
public BigInteger getQ()
161161
{
162-
return curve.getQ();
162+
return curve.getField().getCharacteristic();
163163
}
164164

165165
private static BigInteger calculateDeterminant(BigInteger q, BigInteger a, BigInteger b)

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

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

3-
import org.bouncycastle.asn1.nist.NISTNamedCurves;
43
import org.bouncycastle.asn1.x9.X9ECParameters;
4+
import org.bouncycastle.crypto.ec.CustomNamedCurves;
55
import org.bouncycastle.math.ec.ECCurve;
66

77
/**
@@ -37,13 +37,14 @@ public class ECJPAKECurves
3737

3838
static
3939
{
40-
NIST_P256 = fromX9ECParameters(NISTNamedCurves.getByName("P-256"));
41-
NIST_P384 = fromX9ECParameters(NISTNamedCurves.getByName("P-384"));
42-
NIST_P521 = fromX9ECParameters(NISTNamedCurves.getByName("P-521"));
40+
NIST_P256 = getCurve("P-256");
41+
NIST_P384 = getCurve("P-384");
42+
NIST_P521 = getCurve("P-521");
4343
}
4444

45-
private static ECJPAKECurve fromX9ECParameters(X9ECParameters x9)
45+
private static ECJPAKECurve getCurve(String curveName)
4646
{
47-
return new ECJPAKECurve((ECCurve.Fp)x9.getCurve(), x9.getG());
47+
X9ECParameters x9 = CustomNamedCurves.getByName(curveName);
48+
return new ECJPAKECurve((ECCurve.AbstractFp)x9.getCurve(), x9.getG());
4849
}
4950
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ public class ECJPAKEParticipant
107107
*/
108108
private String partnerParticipantId;
109109

110-
private ECCurve.Fp ecCurve;
111-
private BigInteger ecca;
112-
private BigInteger eccb;
110+
private ECCurve.AbstractFp ecCurve;
113111
private BigInteger q;
114112
private BigInteger h;
115113
private BigInteger n;
@@ -255,8 +253,6 @@ public ECJPAKEParticipant(
255253
this.password = Arrays.copyOf(password, password.length);
256254

257255
this.ecCurve = curve.getCurve();
258-
this.ecca = curve.getA();
259-
this.eccb = curve.getB();
260256
this.g = curve.getG();
261257
this.h = curve.getH();
262258
this.n = curve.getN();

core/src/main/java/org/bouncycastle/crypto/prng/drbg/DualECSP800DRBG.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.math.BigInteger;
44

5-
import org.bouncycastle.asn1.nist.NISTNamedCurves;
65
import org.bouncycastle.crypto.Digest;
6+
import org.bouncycastle.crypto.ec.CustomNamedCurves;
77
import org.bouncycastle.crypto.prng.EntropySource;
88
import org.bouncycastle.math.ec.ECCurve;
99
import org.bouncycastle.math.ec.ECMultiplier;
@@ -36,23 +36,18 @@ public class DualECSP800DRBG
3636
private static final BigInteger p521_Qx = new BigInteger("1b9fa3e518d683c6b65763694ac8efbaec6fab44f2276171a42726507dd08add4c3b3f4c1ebc5b1222ddba077f722943b24c3edfa0f85fe24d0c8c01591f0be6f63", 16);
3737
private static final BigInteger p521_Qy = new BigInteger("1f3bdba585295d9a1110d1df1f9430ef8442c5018976ff3437ef91b81dc0b8132c8d5c39c32d0e004a3092b7d327c0e7a4d26d2c7b69b58f9066652911e457779de", 16);
3838

39-
private static final DualECPoints[] nistPoints;
40-
41-
static
39+
private static final DualECPoints[] nistPoints = new DualECPoints[]
4240
{
43-
nistPoints = new DualECPoints[3];
44-
45-
ECCurve.Fp curve = (ECCurve.Fp)NISTNamedCurves.getByNameLazy("P-256").getCurve();
46-
47-
nistPoints[0] = new DualECPoints(128, curve.createPoint(p256_Px, p256_Py), curve.createPoint(p256_Qx, p256_Qy), 1);
48-
49-
curve = (ECCurve.Fp)NISTNamedCurves.getByNameLazy("P-384").getCurve();
50-
51-
nistPoints[1] = new DualECPoints(192, curve.createPoint(p384_Px, p384_Py), curve.createPoint(p384_Qx, p384_Qy), 1);
41+
createDualECPoints("P-256", 128, p256_Px, p256_Py, p256_Qx, p256_Qy, 1),
42+
createDualECPoints("P-384", 192, p384_Px, p384_Py, p384_Qx, p384_Qy, 1),
43+
createDualECPoints("P-521", 256, p521_Px, p521_Py, p521_Qx, p521_Qy, 1),
44+
};
5245

53-
curve = (ECCurve.Fp)NISTNamedCurves.getByNameLazy("P-521").getCurve();
54-
55-
nistPoints[2] = new DualECPoints(256, curve.createPoint(p521_Px, p521_Py), curve.createPoint(p521_Qx, p521_Qy), 1);
46+
private static DualECPoints createDualECPoints(String curveName, int securityStrength, BigInteger Px,
47+
BigInteger Py, BigInteger Qx, BigInteger Qy, int cofactor)
48+
{
49+
ECCurve.AbstractFp c = (ECCurve.AbstractFp)CustomNamedCurves.getByNameLazy(curveName).getCurve();
50+
return new DualECPoints(securityStrength, c.createPoint(Px, Py), c.createPoint(Qx, Qy), cofactor);
5651
}
5752

5853

@@ -67,7 +62,6 @@ public class DualECSP800DRBG
6762
private int _securityStrength;
6863
private int _seedlen;
6964
private int _outlen;
70-
private ECCurve.Fp _curve;
7165
private ECPoint _P;
7266
private ECPoint _Q;
7367
private byte[] _s;
@@ -210,11 +204,9 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
210204
{
211205
s = getScalarMultipleXCoord(_P, s);
212206

213-
//System.err.println("S: " + new String(Hex.encode(_s)));
214-
215207
byte[] r = getScalarMultipleXCoord(_Q, s).toByteArray();
216208

217-
if (r.length > _outlen)
209+
if (r.length >= _outlen)
218210
{
219211
System.arraycopy(r, r.length - _outlen, output, outOffset, _outlen);
220212
}
@@ -223,7 +215,6 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
223215
System.arraycopy(r, 0, output, outOffset + (_outlen - r.length), r.length);
224216
}
225217

226-
//System.err.println("R: " + new String(Hex.encode(r)));
227218
outOffset += _outlen;
228219

229220
_reseedCounter++;
@@ -237,13 +228,17 @@ public int generate(byte[] output, byte[] additionalInput, boolean predictionRes
237228

238229
int required = output.length - outOffset;
239230

240-
if (r.length > _outlen)
231+
if (r.length >= _outlen)
241232
{
242233
System.arraycopy(r, r.length - _outlen, output, outOffset, required);
243234
}
244235
else
245236
{
246-
System.arraycopy(r, 0, output, outOffset + (_outlen - r.length), required);
237+
int outPos = _outlen - r.length;
238+
if (outPos < required)
239+
{
240+
System.arraycopy(r, 0, output, outOffset + outPos, required - outPos);
241+
}
247242
}
248243

249244
_reseedCounter++;

core/src/main/java/org/bouncycastle/pqc/crypto/util/PrivateKeyInfoFactory.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.bouncycastle.pqc.legacy.crypto.mceliece.McElieceCCA2PrivateKeyParameters;
5454
import org.bouncycastle.pqc.legacy.crypto.qtesla.QTESLAPrivateKeyParameters;
5555
import org.bouncycastle.util.Pack;
56+
import org.bouncycastle.util.Properties;
5657

5758
/**
5859
* Factory to create ASN.1 private key info objects from lightweight private keys.
@@ -247,18 +248,17 @@ else if (privateKey instanceof MLKEMPrivateKeyParameters)
247248
MLKEMPrivateKeyParameters params = (MLKEMPrivateKeyParameters)privateKey;
248249

249250
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mlkemOidLookup(params.getParameters()));
250-
251-
return new PrivateKeyInfo(algorithmIdentifier, getBasicPQCEncoding(params.getSeed(), params.getEncoded()), attributes);
252-
// byte[] seed = params.getSeed();
253-
//
254-
// if (seed == null)
255-
// {
256-
// return new PrivateKeyInfo(algorithmIdentifier, params.getEncoded(), attributes);
257-
// }
258-
// else
259-
// {
260-
// return new PrivateKeyInfo(algorithmIdentifier, seed, attributes);
261-
// }
251+
252+
byte[] seed = params.getSeed();
253+
if (Properties.isOverrideSet("org.bouncycastle.mlkem.seedOnly"))
254+
{
255+
if (seed == null) // very difficult to imagine, but...
256+
{
257+
throw new IOException("no seed available");
258+
}
259+
return new PrivateKeyInfo(algorithmIdentifier, seed, attributes);
260+
}
261+
return new PrivateKeyInfo(algorithmIdentifier, getBasicPQCEncoding(seed, params.getEncoded()), attributes);
262262
}
263263
else if (privateKey instanceof NTRULPRimePrivateKeyParameters)
264264
{
@@ -297,20 +297,16 @@ else if (privateKey instanceof MLDSAPrivateKeyParameters)
297297

298298
AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(Utils.mldsaOidLookup(params.getParameters()));
299299

300+
byte[] seed = params.getSeed();
301+
if (Properties.isOverrideSet("org.bouncycastle.mldsa.seedOnly"))
302+
{
303+
if (seed == null) // very difficult to imagine, but...
304+
{
305+
throw new IOException("no seed available");
306+
}
307+
return new PrivateKeyInfo(algorithmIdentifier, seed, attributes);
308+
}
300309
return new PrivateKeyInfo(algorithmIdentifier, getBasicPQCEncoding(params.getSeed(), params.getEncoded()), attributes);
301-
// byte[] seed = params.getSeed();
302-
// if (seed == null)
303-
// {
304-
// MLDSAPublicKeyParameters pubParams = params.getPublicKeyParameters();
305-
//
306-
// return new PrivateKeyInfo(algorithmIdentifier, params.getEncoded(), attributes, pubParams.getEncoded());
307-
// }
308-
// else
309-
// {
310-
// MLDSAPublicKeyParameters pubParams = params.getPublicKeyParameters();
311-
//
312-
// return new PrivateKeyInfo(algorithmIdentifier, seed, attributes, pubParams.getEncoded());
313-
// }
314310
}
315311
else if (privateKey instanceof DilithiumPrivateKeyParameters)
316312
{

core/src/test/java/org/bouncycastle/crypto/agreement/test/ECJPAKEUtilTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
public class ECJPAKEUtilTest
1919
extends TestCase
2020
{
21-
private static final BigInteger TEN = BigInteger.valueOf(10);
2221
private static final BigInteger ONE = BigInteger.valueOf(1);
2322

2423
public void testValidateParticipantIdsDiffer()
@@ -217,7 +216,7 @@ public void testValidateZeroKnowledgeProof()
217216
}
218217

219218
// (x,y) elements for Gx are not in Fq ie: not in [0,q-1]
220-
ECCurve.Fp curve = (ECCurve.Fp)curve1.getCurve();
219+
ECCurve.AbstractFp curve = curve1.getCurve();
221220
try
222221
{
223222
ECPoint invalidGx_1 = curve.createPoint(ONE.negate(), ONE);

pg/src/main/java/org/bouncycastle/bcpg/ArmoredInputStream.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,16 @@ private boolean parseHeaders()
268268
}
269269
if (c == '\r' || (last != '\r' && c == '\n'))
270270
{
271-
String line = Strings.fromUTF8ByteArray(buf.toByteArray());
271+
String line;
272+
273+
try
274+
{
275+
line = Strings.fromUTF8ByteArray(buf.toByteArray());
276+
}
277+
catch (Exception e)
278+
{
279+
throw new ArmoredInputException(e.getMessage());
280+
}
272281
if (line.trim().length() == 0)
273282
{
274283
break;

0 commit comments

Comments
 (0)