Skip to content

Commit ef33f9d

Browse files
committed
State in createBlindedExponent's javadoc what its seven bits of exponent blinding do and do not cover - that it removes the exponent bit-length and bit-pattern leak from the variable-time modPow but leaves 128 enumerable multipliers for an adversary averaging many operations under one long-term exponent, and randomises only the exponent so base-dependent leakage is untouched - mirrored into the jdk1.1 overlay, and replace the two non-ASCII em dashes left in areSecretValuesEqual's javadoc.
1 parent 7b510fd commit ef33f9d

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎core/src/main/java/org/bouncycastle/util/BigIntegers.java‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ public static long longValueExact(BigInteger x)
218218
* Fixed-width encoding drops the two's-complement sign byte that {@link BigInteger#toByteArray()}
219219
* prepends when the most-significant bit is set: without that, two values could serialise to
220220
* different lengths (e.g. 256 vs 257 bytes for a 2048-bit value) purely according to that high
221-
* bit, and the variable-time comparison would leak it. This is not perfectly constant-time —
221+
* bit, and the variable-time comparison would leak it. This is not perfectly constant-time -
222222
* {@code java.math.BigInteger} has no constant-time serialisation, so the underlying
223-
* {@code toByteArray()} still runs in time proportional to each value's magnitude — but the
223+
* {@code toByteArray()} still runs in time proportional to each value's magnitude - but the
224224
* comparison no longer differs in length on the value's high bit. Intended for secret key
225225
* material (RSA {@code d} and the CRT factors, DSA/DH/ElGamal/GOST {@code x}, EC {@code d}),
226226
* which are all non-negative; the temporary unsigned encodings holding that secret material are
@@ -594,8 +594,30 @@ public static BigInteger createRandomBigInteger(int bitLength, SecureRandom rand
594594
* multiple - for a safe prime that is roughly half of the range-valid bases - and no known-answer
595595
* test will catch it, since the randomisation is otherwise result-preserving.
596596
* <p>
597-
* The multiple is small (between 128 and 255 times groupOrder), so it lengthens the exponent by
598-
* about eight bits rather than doubling the work.
597+
* <b>What this does and does not buy.</b> The multiple is small - between 128 and 255 times
598+
* groupOrder, drawn from seven random bits - so it lengthens the exponent by exactly eight bits
599+
* rather than doubling the work, and every call raises a different exponent of the same length.
600+
* That is aimed squarely at what {@link BigInteger#modPow(BigInteger, BigInteger)} leaks about
601+
* the exponent it is handed: the bit length and the bit pattern, which drive the number of
602+
* squarings and multiplications. After blinding, no single operation is performed on the private
603+
* exponent itself, so an adversary reading one trace - or a handful - no longer reads its bits.
604+
* <p>
605+
* It is not, however, a constant-time exponentiation, and it is not a substitute for one. Two
606+
* limits are worth being explicit about:
607+
* <ul>
608+
* <li>Seven bits give only 128 distinct blinded exponents for a given exponent and order. An
609+
* adversary who can measure <em>many</em> operations under the same long-term exponent can
610+
* average over that space, which is small enough to enumerate. This raises the cost of a
611+
* repeated-measurement attack; it does not remove it. Where an exponent is long lived and the
612+
* operation can be triggered and timed at will - static-static agreement, or signing on
613+
* demand - treat that as the residual risk, and prefer a primitive with a real constant-time
614+
* guarantee if one exists for the algorithm.</li>
615+
* <li>Only the exponent is randomised. The base and the modulus reach modPow unchanged, so any
616+
* leakage that depends on them - the windowing table modPow builds from the base, for one - is
617+
* unaffected. That is the right trade for the protocols this is used in, where the base is a
618+
* public or peer-supplied value and the exponent is the secret, but it is not a general
619+
* side-channel defence for the exponentiation.</li>
620+
* </ul>
599621
*
600622
* @param exponent the private exponent to randomise.
601623
* @param groupOrder an order the base is raised to giving 1 - see above.

‎core/src/main/jdk1.1/org/bouncycastle/util/BigIntegers.java‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,30 @@ public static BigInteger createRandomBigInteger(int bitLength, SecureRandom rand
462462
* multiple - for a safe prime that is roughly half of the range-valid bases - and no known-answer
463463
* test will catch it, since the randomisation is otherwise result-preserving.
464464
* <p>
465-
* The multiple is small (between 128 and 255 times groupOrder), so it lengthens the exponent by
466-
* about eight bits rather than doubling the work.
465+
* <b>What this does and does not buy.</b> The multiple is small - between 128 and 255 times
466+
* groupOrder, drawn from seven random bits - so it lengthens the exponent by exactly eight bits
467+
* rather than doubling the work, and every call raises a different exponent of the same length.
468+
* That is aimed squarely at what {@link BigInteger#modPow(BigInteger, BigInteger)} leaks about
469+
* the exponent it is handed: the bit length and the bit pattern, which drive the number of
470+
* squarings and multiplications. After blinding, no single operation is performed on the private
471+
* exponent itself, so an adversary reading one trace - or a handful - no longer reads its bits.
472+
* <p>
473+
* It is not, however, a constant-time exponentiation, and it is not a substitute for one. Two
474+
* limits are worth being explicit about:
475+
* <ul>
476+
* <li>Seven bits give only 128 distinct blinded exponents for a given exponent and order. An
477+
* adversary who can measure <em>many</em> operations under the same long-term exponent can
478+
* average over that space, which is small enough to enumerate. This raises the cost of a
479+
* repeated-measurement attack; it does not remove it. Where an exponent is long lived and the
480+
* operation can be triggered and timed at will - static-static agreement, or signing on
481+
* demand - treat that as the residual risk, and prefer a primitive with a real constant-time
482+
* guarantee if one exists for the algorithm.</li>
483+
* <li>Only the exponent is randomised. The base and the modulus reach modPow unchanged, so any
484+
* leakage that depends on them - the windowing table modPow builds from the base, for one - is
485+
* unaffected. That is the right trade for the protocols this is used in, where the base is a
486+
* public or peer-supplied value and the exponent is the secret, but it is not a general
487+
* side-channel defence for the exponentiation.</li>
488+
* </ul>
467489
*
468490
* @param exponent the private exponent to randomise.
469491
* @param groupOrder an order the base is raised to giving 1 - see above.

0 commit comments

Comments
 (0)