Skip to content

Commit f68f00f

Browse files
J08nYnoloader
authored andcommitted
Fix ECDSA scalar multiplication leakage of bit-length. (GH #870)
This fixes the timing leakage of bit-length of nonces in ECDSA by essentially fixing the bit-length, by using a nonce equivalent modulo the subgroup order.
1 parent 739e579 commit f68f00f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pubkey.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,10 +1604,10 @@ class CRYPTOPP_NO_VTABLE DL_SignerBase : public DL_SignatureSchemeBase<PK_Signer
16041604
if (rng.CanIncorporateEntropy())
16051605
rng.IncorporateEntropy(representative, representative.size());
16061606

1607+
const Integer& q = params.GetSubgroupOrder();
16071608
Integer k;
16081609
if (alg.IsDeterministic())
16091610
{
1610-
const Integer& q = params.GetSubgroupOrder();
16111611
const Integer& x = key.GetPrivateExponent();
16121612
const DeterministicSignatureAlgorithm& det = dynamic_cast<const DeterministicSignatureAlgorithm&>(alg);
16131613
k = det.GenerateRandom(x, q, e);
@@ -1617,8 +1617,13 @@ class CRYPTOPP_NO_VTABLE DL_SignerBase : public DL_SignatureSchemeBase<PK_Signer
16171617
k.Randomize(rng, 1, params.GetSubgroupOrder()-1);
16181618
}
16191619

1620+
Integer ks = k + q;
1621+
if (ks.BitCount() == q.BitCount()) {
1622+
ks += q;
1623+
}
1624+
16201625
Integer r, s;
1621-
r = params.ConvertElementToInteger(params.ExponentiateBase(k));
1626+
r = params.ConvertElementToInteger(params.ExponentiateBase(ks));
16221627
alg.Sign(params, key.GetPrivateExponent(), k, e, r, s);
16231628

16241629
/*

0 commit comments

Comments
 (0)