Skip to content

Commit f13def6

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents 0aa9d1c + 3559d88 commit f13def6

File tree

6 files changed

+36
-43
lines changed

6 files changed

+36
-43
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/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);

pkix/src/main/jdk1.4/org/bouncycastle/eac/jcajce/JcaPublicKeyConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ public PublicKeyDataObject getPublicKeyDataObject(ASN1ObjectIdentifier usage, Pu
128128
ECPublicKey pubKey = (ECPublicKey)publicKey;
129129
ECParameterSpec params = pubKey.getParameters();
130130

131+
ECCurve.AbstractFp curve = (ECCurve.AbstractFp)params.getCurve();
132+
131133
return new ECDSAPublicKey(
132134
usage,
133-
((ECCurve.Fp)params.getCurve()).getQ(),
134-
((ECFieldElement.Fp)params.getCurve().getA()).toBigInteger(),
135-
((ECFieldElement.Fp)params.getCurve().getB()).toBigInteger(),
135+
curve.getField().getCharacteristic(),
136+
curve.getA().toBigInteger(),
137+
curve.getB().toBigInteger(),
136138
params.getG().getEncoded(false),
137139
params.getN(),
138140
pubKey.getQ().getEncoded(false),

0 commit comments

Comments
 (0)