Skip to content

Commit a219442

Browse files
committed
Every classical signer now assembles the signature component s through the constant-time BigIntegers.modMult, modAdd and the new modSubtract rather than BigInteger.multiply and mod, whose cost follows the quotient and which were being handed the signing key, covering ECDSA, SM2, DSA, GOST 3410, ECGOST 3410, DSTU 4145, EC-NR and BIP 340, with the public hash-derived value reduced on the way in wherever it could sit at or past the order, DSAPrivateKeyParameters and GOST3410PrivateKeyParameters now holding x to the [1, q-1] their own parameters imply, and the two GOST signers redrawing a nonce that is zero or at or past the order rather than leaving it to be folded by the reduction that followed.
1 parent acd2178 commit a219442

20 files changed

Lines changed: 531 additions & 25 deletions

File tree

‎CONTRIBUTORS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,4 @@ We also wish to acknowledge financial and collaborative support from [CISCO](htt
547547
- hannesa2 \<https://github.com/hannesa2\> - initial Dependabot configuration for the Gradle and GitHub Actions ecosystems (PR #883).
548548
- vladhuma \<https://github.com/vladhuma\> - initial implementation of server-side OCSP stapling for the BCJSSE provider, on behalf of Thales Group (PR #1740).
549549
- Bhargava Shastry \<bshastry&#064;posteo.de\> - reporting, with a self-checking reproducer, that the C509 validity fields were read as unbounded seconds and then given two meanings: displacing a type-3 certificate's validityNotAfter by 2^61 seconds left the reconstructed DER TBSCertificate and the issuer signature untouched, while C509CertificateHolder.isValidOn took the raw value and read an expired certificate as valid.
550+
- Rob Augustinus \<rob&#064;opensolutions.nl\> - a constant-time analysis of the elliptic-curve and AES code paths, with proof-of-concept code, which independently reached the variable-point scalar multiplication exposure addressed in 1.86.

‎core/src/main/java/org/bouncycastle/crypto/params/DSAPrivateKeyParameters.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ public DSAPrivateKeyParameters(
1313
{
1414
super(true, params);
1515

16-
this.x = x;
16+
this.x = validate(x, params);
1717
}
1818

19+
// x is multiplied and reduced modulo q when a signature is formed, in a constant-time form
20+
// that requires it already reduced, and FIPS 186-4 sec. 4.1 puts it in [1, q-1] in any case.
21+
// Unlike the ECCSI signing key, a DSA key carries the parameters that bound it, so the check
22+
// belongs here rather than at the point of use.
23+
private static BigInteger validate(BigInteger x, DSAParameters params)
24+
{
25+
if (x != null && params != null
26+
&& (x.signum() <= 0 || x.compareTo(params.getQ()) >= 0))
27+
{
28+
throw new IllegalArgumentException("x must be in [1, q-1]");
29+
}
30+
31+
return x;
32+
}
33+
1934
public BigInteger getX()
2035
{
2136
return x;

‎core/src/main/java/org/bouncycastle/crypto/params/GOST3410PrivateKeyParameters.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ public GOST3410PrivateKeyParameters(
1313
{
1414
super(true, params);
1515

16-
this.x = x;
16+
this.x = validate(x, params);
17+
}
18+
19+
// as with DSA, x is multiplied and reduced modulo q when a signature is formed, in a
20+
// constant-time form that requires it already reduced; the key carries the parameters that
21+
// bound it, so the check belongs here
22+
private static BigInteger validate(BigInteger x, GOST3410Parameters params)
23+
{
24+
if (x != null && params != null
25+
&& (x.signum() <= 0 || x.compareTo(params.getQ()) >= 0))
26+
{
27+
throw new IllegalArgumentException("x must be in [1, q-1]");
28+
}
29+
30+
return x;
1731
}
1832

1933
public BigInteger getX()

‎core/src/main/java/org/bouncycastle/crypto/signers/BIP340Signer.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ private static byte[] sign(ECPrivateKeyParameters privateKey, byte[] m, byte[] a
321321

322322
// Steps 11-12: e = int(H_challenge(bytes(R) || bytes(P) || m)) mod n; sig = bytes(R) || bytes((k + e*d) mod n).
323323
BigInteger e = challengeScalar(rBytes, pBytes, m);
324-
BigInteger s = k.add(e.multiply(d)).mod(n);
324+
// the secret d and nonce k are kept off BigInteger.mod, whose cost follows the quotient; both are in [1, n-1] after the conditional negations above, e is reduced by challengeScalar, and n is odd
325+
BigInteger s = BigIntegers.modAdd(n, k, BigIntegers.modMult(n, e, d));
325326

326327
byte[] sig = new byte[SIGNATURE_SIZE];
327328
System.arraycopy(rBytes, 0, sig, 0, X_SIZE);

‎core/src/main/java/org/bouncycastle/crypto/signers/DSASigner.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public BigInteger[] generateSignature(
9090
{
9191
DSAParameters params = key.getParameters();
9292
BigInteger q = params.getQ();
93-
BigInteger m = calculateE(q, message);
93+
// m is public, and calculateE can leave it past q, which modAdd below rejects rather than reduces
94+
BigInteger m = calculateE(q, message).mod(q);
9495
BigInteger x = ((DSAPrivateKeyParameters)key).getX();
9596

9697
if (kCalculator.isDeterministic())
@@ -107,9 +108,9 @@ public BigInteger[] generateSignature(
107108
// the randomizer is to conceal timing information related to k and x.
108109
BigInteger r = params.getG().modPow(BigIntegers.createBlindedExponent(k, q, CryptoServicesRegistrar.getSecureRandom(random)), params.getP()).mod(q);
109110

110-
k = BigIntegers.modOddInverse(q, k).multiply(m.add(x.multiply(r)));
111-
112-
BigInteger s = k.mod(q);
111+
// s = k^-1 * (m + x * r) mod q, with the secret x and k^-1 kept off BigInteger.mod, whose cost follows the quotient; x is in [1, q-1] by DSAPrivateKeyParameters, r is reduced, and q is prime and so odd
112+
BigInteger s = BigIntegers.modMult(q, BigIntegers.modOddInverse(q, k),
113+
BigIntegers.modAdd(q, m, BigIntegers.modMult(q, x, r)));
113114

114115
return new BigInteger[]{ r, s };
115116
}

‎core/src/main/java/org/bouncycastle/crypto/signers/DSTU4145Signer.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ public BigInteger[] generateSignature(byte[] message)
9999
}
100100
while (r.signum() == 0);
101101

102-
s = r.multiply(d).add(e).mod(n);
102+
// the secret d and ephemeral e are kept off BigInteger.mod, whose cost follows the quotient; d is in [1, n-1] by validatePrivateScalar, e is drawn in that range and r is truncated below 2^(n.bitLength()-1), and n is odd
103+
s = BigIntegers.modAdd(n, BigIntegers.modMult(n, r, d), e);
103104
}
104105
while (s.signum() == 0);
105106

‎core/src/main/java/org/bouncycastle/crypto/signers/ECDSASigner.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public BigInteger[] generateSignature(
9797
{
9898
ECDomainParameters ec = key.getParameters();
9999
BigInteger n = ec.getN();
100-
BigInteger e = calculateE(n, message);
100+
// e is public, and calculateE can leave it past n, which modAdd below rejects rather than reduces
101+
BigInteger e = calculateE(n, message).mod(n);
101102
BigInteger d = ((ECPrivateKeyParameters)key).getD();
102103

103104
if (kCalculator.isDeterministic())
@@ -128,7 +129,9 @@ public BigInteger[] generateSignature(
128129
}
129130
while (r.equals(ZERO));
130131

131-
s = BigIntegers.modOddInverse(n, k).multiply(e.add(d.multiply(r))).mod(n);
132+
// s = k^-1 * (e + d * r) mod n, with the secret d and k^-1 kept off BigInteger.mod, whose cost follows the quotient; d is in [1, n-1] by validatePrivateScalar, r is reduced and non-zero, and n is odd
133+
s = BigIntegers.modMult(n, BigIntegers.modOddInverse(n, k),
134+
BigIntegers.modAdd(n, e, BigIntegers.modMult(n, d, r)));
132135
}
133136
while (s.equals(ZERO));
134137

‎core/src/main/java/org/bouncycastle/crypto/signers/ECGOST3410Signer.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ public BigInteger[] generateSignature(
6969
byte[] message)
7070
{
7171
byte[] mRev = Arrays.reverse(message); // conversion is little-endian
72-
BigInteger e = new BigInteger(1, mRev);
7372

7473
ECDomainParameters ec = key.getParameters();
7574
BigInteger n = ec.getN();
75+
// e is public, and the digest can exceed n, which modMult below rejects rather than reduces
76+
BigInteger e = new BigInteger(1, mRev).mod(n);
7677
BigInteger d = ((ECPrivateKeyParameters)key).getD();
7778

7879
BigInteger r, s;
@@ -88,15 +89,16 @@ public BigInteger[] generateSignature(
8889
{
8990
k = BigIntegers.createRandomBigInteger(n.bitLength(), random);
9091
}
91-
while (k.equals(ECConstants.ZERO));
92+
while (k.equals(ECConstants.ZERO) || k.compareTo(n) >= 0);
9293

9394
ECPoint p = basePointMultiplier.multiply(ec.getG(), k).normalize();
9495

9596
r = p.getAffineXCoord().toBigInteger().mod(n);
9697
}
9798
while (r.equals(ECConstants.ZERO));
9899

99-
s = (k.multiply(e)).add(d.multiply(r)).mod(n);
100+
// the secret d and nonce k are kept off BigInteger.mod, whose cost follows the quotient; d is in [1, n-1] by validatePrivateScalar, k is now redrawn until it is below n, r is reduced, and n is odd
101+
s = BigIntegers.modAdd(n, BigIntegers.modMult(n, k, e), BigIntegers.modMult(n, d, r));
100102
}
101103
while (s.equals(ECConstants.ZERO));
102104

‎core/src/main/java/org/bouncycastle/crypto/signers/ECNRSigner.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public BigInteger[] generateSignature(
122122
// generate s
123123
BigInteger x = privKey.getD(); // private key value
124124
BigInteger u = ((ECPrivateKeyParameters)tempPair.getPrivate()).getD(); // temp's private key value
125-
s = u.subtract(r.multiply(x)).mod(n);
125+
// the secret x and the ephemeral u are kept off BigInteger.mod, whose cost follows the quotient; x is in [1, n-1] by validatePrivateScalar, u comes from a generated key pair and r is reduced and non-zero, and n is odd
126+
s = BigIntegers.modSubtract(n, u, BigIntegers.modMult(n, r, x));
126127

127128
BigInteger[] res = new BigInteger[2];
128129
res[0] = r;

‎core/src/main/java/org/bouncycastle/crypto/signers/GOST3410Signer.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,16 @@ public BigInteger[] generateSignature(
6464
byte[] message)
6565
{
6666
byte[] mRev = Arrays.reverse(message); // conversion is little-endian
67-
BigInteger m = new BigInteger(1, mRev);
6867
GOST3410Parameters params = key.getParameters();
68+
// m is public, and the digest can exceed q, which modMult below rejects rather than reduces
69+
BigInteger m = new BigInteger(1, mRev).mod(params.getQ());
6970
BigInteger k;
7071

7172
do
7273
{
7374
k = BigIntegers.createRandomBigInteger(params.getQ().bitLength(), random);
7475
}
75-
while (k.compareTo(params.getQ()) >= 0);
76+
while (k.signum() == 0 || k.compareTo(params.getQ()) >= 0);
7677

7778
// the randomizer conceals timing information related to k, which is what s below would
7879
// give up the private key from if it leaked. a has order q by construction, so a^q = 1
@@ -82,9 +83,10 @@ public BigInteger[] generateSignature(
8283

8384
BigInteger r = params.getA().modPow(blindedK, params.getP()).mod(params.getQ());
8485

85-
BigInteger s = k.multiply(m).
86-
add(((GOST3410PrivateKeyParameters)key).getX().multiply(r)).
87-
mod(params.getQ());
86+
// the secret x and nonce k are kept off BigInteger.mod, whose cost follows the quotient; x is in [1, q-1] by GOST3410PrivateKeyParameters, k is drawn below q above, r is reduced, and q is prime and so odd
87+
BigInteger s = BigIntegers.modAdd(params.getQ(),
88+
BigIntegers.modMult(params.getQ(), k, m),
89+
BigIntegers.modMult(params.getQ(), ((GOST3410PrivateKeyParameters)key).getX(), r));
8890

8991
BigInteger[] res = new BigInteger[2];
9092

0 commit comments

Comments
 (0)