Skip to content

Commit e25e94a

Browse files
committed
Add cofactor validation after point decompression
1 parent 42e43cf commit e25e94a

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,12 @@ protected ECPoint decompressPoint(int yTilde, BigInteger X1)
472472
y = y.negate();
473473
}
474474

475-
return this.createRawPoint(x, y, true);
475+
ECPoint p = this.createRawPoint(x, y, true);
476+
if (!p.satisfiesCofactor())
477+
{
478+
throw new IllegalArgumentException("Invalid point");
479+
}
480+
return p;
476481
}
477482
}
478483

@@ -974,14 +979,14 @@ synchronized BigInteger[] getSi()
974979
*/
975980
protected ECPoint decompressPoint(int yTilde, BigInteger X1)
976981
{
977-
ECFieldElement xp = fromBigInteger(X1), yp = null;
978-
if (xp.isZero())
982+
ECFieldElement x = fromBigInteger(X1), y = null;
983+
if (x.isZero())
979984
{
980-
yp = b.sqrt();
985+
y = b.sqrt();
981986
}
982987
else
983988
{
984-
ECFieldElement beta = xp.square().invert().multiply(b).add(a).add(xp);
989+
ECFieldElement beta = x.square().invert().multiply(b).add(a).add(x);
985990
ECFieldElement z = solveQuadraticEquation(beta);
986991
if (z != null)
987992
{
@@ -995,24 +1000,30 @@ protected ECPoint decompressPoint(int yTilde, BigInteger X1)
9951000
case COORD_LAMBDA_AFFINE:
9961001
case COORD_LAMBDA_PROJECTIVE:
9971002
{
998-
yp = z.add(xp);
1003+
y = z.add(x);
9991004
break;
10001005
}
10011006
default:
10021007
{
1003-
yp = z.multiply(xp);
1008+
y = z.multiply(x);
10041009
break;
10051010
}
10061011
}
10071012
}
10081013
}
10091014

1010-
if (yp == null)
1015+
if (y == null)
10111016
{
10121017
throw new IllegalArgumentException("Invalid point compression");
10131018
}
10141019

1015-
return createRawPoint(xp, yp, true);
1020+
ECPoint p = this.createRawPoint(x, y, true);
1021+
if (!p.satisfiesCofactor())
1022+
{
1023+
throw new IllegalArgumentException("Invalid point");
1024+
}
1025+
1026+
return p;
10161027
}
10171028

10181029
/**

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ protected ECPoint(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElem
6464
this.zs = zs;
6565
}
6666

67+
protected boolean satisfiesCofactor()
68+
{
69+
BigInteger h = curve.getCofactor();
70+
return h == null || h.equals(ECConstants.ONE) || !ECAlgorithms.referenceMultiply(this, h).isInfinity();
71+
}
72+
6773
protected abstract boolean satisfiesCurveEquation();
6874

6975
public final ECPoint getDetachedPoint()
@@ -303,9 +309,7 @@ public boolean isValid()
303309
return false;
304310
}
305311

306-
BigInteger h = curve.getCofactor();
307-
if (h != null && !h.equals(ECConstants.ONE)
308-
&& ECAlgorithms.referenceMultiply(this, h).isInfinity())
312+
if (!satisfiesCofactor())
309313
{
310314
return false;
311315
}

0 commit comments

Comments
 (0)