@@ -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.
0 commit comments