Skip to content

Commit 761d964

Browse files
committed
Add getQ to ECCurve.AbstractFp
1 parent 35eabaa commit 761d964

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

core/src/main/j2me/org/bouncycastle/math/ec/ECCurve.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,14 @@ protected AbstractFp(BigInteger q)
593593
super(FiniteFields.getPrimeField(q));
594594
}
595595

596+
public BigInteger getQ()
597+
{
598+
return getField().getCharacteristic();
599+
}
600+
596601
public boolean isValidFieldElement(BigInteger x)
597602
{
598-
return x != null && x.signum() >= 0 && x.compareTo(this.getField().getCharacteristic()) < 0;
603+
return x != null && x.signum() >= 0 && x.compareTo(this.getQ()) < 0;
599604
}
600605

601606
public ECFieldElement randomFieldElement(SecureRandom r)
@@ -604,7 +609,7 @@ public ECFieldElement randomFieldElement(SecureRandom r)
604609
* NOTE: BigInteger comparisons in the rejection sampling are not constant-time, so we
605610
* use the product of two independent elements to mitigate side-channels.
606611
*/
607-
BigInteger p = this.getField().getCharacteristic();
612+
BigInteger p = this.getQ();
608613
ECFieldElement fe1 = this.fromBigInteger(implRandomFieldElement(r, p));
609614
ECFieldElement fe2 = this.fromBigInteger(implRandomFieldElement(r, p));
610615
return fe1.multiply(fe2);
@@ -616,7 +621,7 @@ public ECFieldElement randomFieldElementMult(SecureRandom r)
616621
* NOTE: BigInteger comparisons in the rejection sampling are not constant-time, so we
617622
* use the product of two independent elements to mitigate side-channels.
618623
*/
619-
BigInteger p = this.getField().getCharacteristic();
624+
BigInteger p = this.getQ();
620625
ECFieldElement fe1 = this.fromBigInteger(implRandomFieldElementMult(r, p));
621626
ECFieldElement fe2 = this.fromBigInteger(implRandomFieldElementMult(r, p));
622627
return fe1.multiply(fe2);
@@ -699,12 +704,11 @@ public Fp(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger
699704

700705
if (isInternal)
701706
{
702-
this.q = q;
703707
knownQs.add(q);
704708
}
705709
else if (knownQs.contains(q) || validatedQs.contains(q))
706710
{
707-
this.q = q;
711+
// No need to validate
708712
}
709713
else
710714
{
@@ -724,10 +728,9 @@ else if (knownQs.contains(q) || validatedQs.contains(q))
724728
}
725729

726730
validatedQs.add(q);
727-
728-
this.q = q;
729731
}
730732

733+
this.q = q;
731734
this.r = ECFieldElement.Fp.calculateResidue(q);
732735
this.infinity = new ECPoint.Fp(this, null, null);
733736

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public BigInteger getH()
159159

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

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

core/src/main/java/org/bouncycastle/math/ec/ECCurve.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,14 @@ protected AbstractFp(BigInteger q)
596596
super(FiniteFields.getPrimeField(q));
597597
}
598598

599+
public BigInteger getQ()
600+
{
601+
return getField().getCharacteristic();
602+
}
603+
599604
public boolean isValidFieldElement(BigInteger x)
600605
{
601-
return x != null && x.signum() >= 0 && x.compareTo(this.getField().getCharacteristic()) < 0;
606+
return x != null && x.signum() >= 0 && x.compareTo(getQ()) < 0;
602607
}
603608

604609
public ECFieldElement randomFieldElement(SecureRandom r)
@@ -607,7 +612,7 @@ public ECFieldElement randomFieldElement(SecureRandom r)
607612
* NOTE: BigInteger comparisons in the rejection sampling are not constant-time, so we
608613
* use the product of two independent elements to mitigate side-channels.
609614
*/
610-
BigInteger p = getField().getCharacteristic();
615+
BigInteger p = getQ();
611616
ECFieldElement fe1 = fromBigInteger(implRandomFieldElement(r, p));
612617
ECFieldElement fe2 = fromBigInteger(implRandomFieldElement(r, p));
613618
return fe1.multiply(fe2);
@@ -619,7 +624,7 @@ public ECFieldElement randomFieldElementMult(SecureRandom r)
619624
* NOTE: BigInteger comparisons in the rejection sampling are not constant-time, so we
620625
* use the product of two independent elements to mitigate side-channels.
621626
*/
622-
BigInteger p = getField().getCharacteristic();
627+
BigInteger p = getQ();
623628
ECFieldElement fe1 = fromBigInteger(implRandomFieldElementMult(r, p));
624629
ECFieldElement fe2 = fromBigInteger(implRandomFieldElementMult(r, p));
625630
return fe1.multiply(fe2);
@@ -702,12 +707,11 @@ public Fp(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger
702707

703708
if (isInternal)
704709
{
705-
this.q = q;
706710
knownQs.add(q);
707711
}
708712
else if (knownQs.contains(q) || validatedQs.contains(q))
709713
{
710-
this.q = q;
714+
// No need to validate
711715
}
712716
else
713717
{
@@ -727,10 +731,9 @@ else if (knownQs.contains(q) || validatedQs.contains(q))
727731
}
728732

729733
validatedQs.add(q);
730-
731-
this.q = q;
732734
}
733735

736+
this.q = q;
734737
this.r = ECFieldElement.Fp.calculateResidue(q);
735738
this.infinity = new ECPoint.Fp(this, null, null);
736739

core/src/main/jdk1.2/org/bouncycastle/math/ec/ECCurve.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,14 @@ protected AbstractFp(BigInteger q)
596596
super(FiniteFields.getPrimeField(q));
597597
}
598598

599+
public BigInteger getQ()
600+
{
601+
return getField().getCharacteristic();
602+
}
603+
599604
public boolean isValidFieldElement(BigInteger x)
600605
{
601-
return x != null && x.signum() >= 0 && x.compareTo(this.getField().getCharacteristic()) < 0;
606+
return x != null && x.signum() >= 0 && x.compareTo(this.getQ()) < 0;
602607
}
603608

604609
public ECFieldElement randomFieldElement(SecureRandom r)
@@ -607,7 +612,7 @@ public ECFieldElement randomFieldElement(SecureRandom r)
607612
* NOTE: BigInteger comparisons in the rejection sampling are not constant-time, so we
608613
* use the product of two independent elements to mitigate side-channels.
609614
*/
610-
BigInteger p = this.getField().getCharacteristic();
615+
BigInteger p = this.getQ();
611616
ECFieldElement fe1 = this.fromBigInteger(implRandomFieldElement(r, p));
612617
ECFieldElement fe2 = this.fromBigInteger(implRandomFieldElement(r, p));
613618
return fe1.multiply(fe2);
@@ -619,7 +624,7 @@ public ECFieldElement randomFieldElementMult(SecureRandom r)
619624
* NOTE: BigInteger comparisons in the rejection sampling are not constant-time, so we
620625
* use the product of two independent elements to mitigate side-channels.
621626
*/
622-
BigInteger p = this.getField().getCharacteristic();
627+
BigInteger p = this.getQ();
623628
ECFieldElement fe1 = this.fromBigInteger(implRandomFieldElementMult(r, p));
624629
ECFieldElement fe2 = this.fromBigInteger(implRandomFieldElementMult(r, p));
625630
return fe1.multiply(fe2);
@@ -702,12 +707,11 @@ public Fp(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger
702707

703708
if (isInternal)
704709
{
705-
this.q = q;
706710
knownQs.add(q);
707711
}
708712
else if (knownQs.contains(q) || validatedQs.contains(q))
709713
{
710-
this.q = q;
714+
// No need to validate
711715
}
712716
else
713717
{
@@ -727,10 +731,9 @@ else if (knownQs.contains(q) || validatedQs.contains(q))
727731
}
728732

729733
validatedQs.add(q);
730-
731-
this.q = q;
732734
}
733735

736+
this.q = q;
734737
this.r = ECFieldElement.Fp.calculateResidue(q);
735738
this.infinity = new ECPoint.Fp(this, null, null);
736739

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public PublicKeyDataObject getPublicKeyDataObject(ASN1ObjectIdentifier usage, Pu
132132

133133
return new ECDSAPublicKey(
134134
usage,
135-
curve.getField().getCharacteristic(),
135+
curve.getQ(),
136136
curve.getA().toBigInteger(),
137137
curve.getB().toBigInteger(),
138138
params.getG().getEncoded(false),

prov/src/test/java/org/bouncycastle/jce/provider/test/DetDSATest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import org.bouncycastle.asn1.ASN1Integer;
1616
import org.bouncycastle.asn1.ASN1Sequence;
17-
import org.bouncycastle.asn1.nist.NISTNamedCurves;
1817
import org.bouncycastle.asn1.x9.X9ECParameters;
18+
import org.bouncycastle.crypto.ec.CustomNamedCurves;
1919
import org.bouncycastle.jce.provider.BouncyCastleProvider;
2020
import org.bouncycastle.math.ec.ECCurve;
2121
import org.bouncycastle.util.encoders.Hex;
@@ -78,13 +78,16 @@ private void doTestHMACDetDSATest(String algName, PrivateKey privKey, BigInteger
7878
private void testECHMacDeterministic()
7979
throws Exception
8080
{
81-
X9ECParameters x9ECParameters = NISTNamedCurves.getByName("P-192");
82-
ECCurve curve = x9ECParameters.getCurve();
81+
X9ECParameters x9ECParameters = CustomNamedCurves.getByName("P-192");
82+
ECCurve.AbstractFp curve = (ECCurve.AbstractFp)x9ECParameters.getCurve();
83+
BigInteger q = curve.getQ();
84+
85+
org.bouncycastle.math.ec.ECPoint g = x9ECParameters.getG().normalize();
8386

8487
ECPrivateKeySpec privKeySpec = new ECPrivateKeySpec(new BigInteger("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
8588
new ECParameterSpec(
86-
new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null),
87-
new ECPoint(x9ECParameters.getG().getXCoord().toBigInteger(), x9ECParameters.getG().getYCoord().toBigInteger()),
89+
new EllipticCurve(new ECFieldFp(q), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null),
90+
new ECPoint(g.getAffineXCoord().toBigInteger(), g.getAffineYCoord().toBigInteger()),
8891
x9ECParameters.getN(), x9ECParameters.getH().intValue())
8992
);
9093

0 commit comments

Comments
 (0)