Explain a timing attack on RSA. If square-and-multiply isn't constant-time, how can an attacker infer private key bits by measuring decryption execution time for different ciphertexts?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Non-constant-time square-and-multiply leaks RSA private exponent bits because multiplication only occurs when an exponent bit is 1, causing measurable timing differences. By adaptively submitting crafted ciphertexts and measuring decryption time, the attacker statistically infers each bit of the exponent from these timing variations.
What an RSA implementation does when it “decrypts” a block is compute
[
m = c^d \bmod N
]
with the secret exponent (d). The obvious way of doing that on a general–purpose CPU is the left‑to‑right “square–and–multiply” algorithm:
code
x := 1
for bit in bits(d) from MSB to LSB:
x := x·x mod N // square
if bit = 1:
x := x·c mod N // multiply
return x
Two properties of this code are crucial for the attack Kocher described in 1996:
It is not constant time. The inner if is only taken when a bit of (d) is one, and a modular multiplication followed by a reduction typically takes a different number of cycles depending on whether there is a carry or an extra reduction. A typical Montgomery multiply is noticeably slower when the product exceeds (N), for example.
The value you multiply by in the if is under the attacker’s control: it’s the ciphertext (c).
If you can get the machine to execute this code on ciphertexts of your choice and measure how long it takes, you get information about the bits of (d). At the grossest level, the total running time is proportional to the Hamming weight of (d); implementations with an early‑exit test on the top bit leak the position of the top‑bit. Kocher’s full attack goes further and recovers the bits one by one.
The trick is that, by the time you reach bit (i), the current accumulator value (x) is a deterministic function of the higher‑order bits of (d) and the chosen ciphertext. If you already know those higher‑order bits, you can predict (x) for a given ciphertext. Under the hypothesis “bit (i) = 0” the next step will be just a square; under the hypothesis “bit (i) = 1” there will be an extra multiply by (c). Because the cost of that multiply/reduction depends on the operands, the distribution of execution times is different in the two cases.
In practice the attacker proceeds as follows:
Recover the most significant bit (which is always 1).
For each subsequent bit position (i):
– Use the bits recovered so far to simulate the left‑to‑right exponentiation on many random ciphertexts (c_j) up to position (i).
– For each (c_j), compute the intermediate value (x_j) that the implementation will have before processing bit (i). If bit (i) were 1 the next operation would be (x_j\cdot c_j\bmod N); if it were 0 it would just be (x_j^2\bmod N).
– Submit the (c_j) to the target, measure the decryption times, and correlate them with whether (x_j\cdot c_j) would trigger an extra reduction. One hypothesis will produce timings with lots of slow multiplications, the other won’t.
– The hypothesis with timings that match your measurements is the correct value of bit (i).
Repeating this loop recovers all the bits of (d). The attack works because the branch and the data‑dependent cost of modular multiplication leak a few clock cycles per bit; by averaging over many chosen ciphertexts those tiny differences become visible. A “constant‑time” exponentiation routine removes the branch and makes every multiply/reduction take the same time, denying the attacker this side channel.