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.
This is a great question about a real cryptographic vulnerability. Let me explain timing attacks on RSA in detail.
Timing Attacks on RSA
Background: RSA Decryption & Square-and-Multiply
RSA decryption computes: m = c^d mod n, where:
c = ciphertext
d = private key (exponent)
n = modulus
The standard algorithm for computing large exponentiations is the binary method or square-and-multiply:
code
for each bit in d (from most significant to least):
result = result^2 mod n
if bit == 1:
result = result * c mod n
The Vulnerability
The timing issue arises when multiplication takes significantly different time than just squaring:
If d[i] = 0: Only one modular squaring
If d[i] = 1: One modular squaring + one modular multiplication
If the modular multiplication operation takes noticeably longer than squaring, the total execution time for processing each bit leaks information about whether that bit is 0 or 1.
How the Attack Works
Step 1: Collect Timing Measurements
The attacker:
Sends many different ciphertexts c₁, c₂, c₃, ... to a server
Measures the decryption time T(c) for each
Records timing data correlated with ciphertext values
Step 2: Statistical Analysis
For different ciphertexts, the decryption process follows the same bit sequence (the private exponent d is constant). So the variation in timing across different ciphertexts comes from:
Different intermediate values during computation
Multiplication operates at different speeds depending on operands
Modular multiplication time varies based on:
Operand bit lengths
Number of carries in arithmetic
Cache behavior
Processor pipeline predictions
Step 3: Bit-by-Bit Recovery
The attacker incrementally reconstructs d by testing each candidate bit value. For each position, they send multiple ciphertexts and measure decryption times, then use statistical correlation to determine whether the bit is 0 or 1 based on which value produces timing patterns consistent with the observed data. This process repeats until the entire private exponent is recovered.
Concrete Example
When d begins with 1011... in binary, the attacker observes that certain bit choices correlate with measurable timing differences during the modular exponentiation. However, the attacker cannot directly control which intermediate values appear during decryption of a given ciphertext.
More Accurate Attack Description
Several attack variants exist depending on implementation details. One approach exploits timing variance in modular multiplication routines like Montgomery multiplication, where the computational cost varies based on operand values and processor behavior.
For a fixed private exponent d but varying ciphertexts c, the sequence of squaring and multiplication operations remains constant (determined by d's bit pattern), yet the intermediate values differ. These varying values produce measurable timing differences in the multiplication operations themselves. By analyzing statistical correlations between observed timings and known ciphertext differences, patterns in d gradually emerge.
Older implementations sometimes used early termination conditions or windowed exponentiation with variable loop counts, making timing leakage more direct. Cache behavior during multiplication versus squaring operations can also reveal structural information about the exponent.
The fundamental mechanism is that intermediate computational states during exponentiation depend on both the fixed private key and the varying ciphertext input, creating a side channel where timing measurements correlate with the unknown exponent's structure. of intermediate states. The next bit determines if we multiply or not. This future action, while not directly timing this operation, affects cache state and other side effects that influence subsequent operations.
With square-and-multiply, the timing differences become measurable. If multiplication takes roughly 100 cycles and squaring takes 60, then a 1024-bit exponent of all 1's requires about 160k cycles total, while all 0's needs only 61k cycles. These differences—around 99 microseconds—are easily distinguishable in practice, making the attack feasible even with modest measurement precision.
The attacker doesn't control the secret exponent, so they measure timing across many ciphertexts, partially decrypt using guessed bit sequences, and use statistical analysis to identify which pattern aligns with the observed timings.
Defenses include constant-time implementations that always perform both operations but conditionally use the result, blinded decryption that randomizes intermediate values by multiplying with a random value r and its inverse, and random delays or dummy operations that obscure the true execution time.
Timing Attacks on RSA
The Core Vulnerability
RSA decryption computes m = c^d mod n using square-and-multiply. The algorithm processes each bit of the private exponent d:
code
result = 1
for each bit in d (left to right):
result = result² mod n # Always squaring
if bit == 1:
result = result * c mod n # Conditional multiply
The problem: Modular multiplication is significantly slower than squaring, so:
d[i] = 1 → execution time includes multiplication
d[i] = 0 → execution time skips multiplication
Attack Mechanism
Why Timing Leaks Private Key Bits
Even though the attacker can't directly manipulate d, they can observe:
Different ciphertexts produce different timing patterns
Intermediate computational state varies with the ciphertext value
These state changes affect multiplication speed through:
Operand-dependent carry propagation
Data cache hits/misses
Branch prediction patterns
Processor pipeline behavior
Attack Steps
python
# Step 1: Gather timing measurements for many ciphertexts
timings = {}
for c in [c₁, c₂, c₃, ...]:
t_start = clock()
m = decrypt(c, d) # Attacker measures execution time
t_end = clock()
timings[c] = t_end - t_start
# Step 2: Incrementally recover d via statistical correlation
recovered_d = ""
for bit_position in range(1024): # For 1024-bit RSA
best_correlation = -∞
for candidate_bit in [0, 1]:
candidate_d = recovered_d + str(candidate_bit)
# Compute what intermediate states WOULD be
# if this bit were correct
correlation = 0
for ciphertext in all_ciphertexts:
# Statistical measure: correlation between
# predicted timing and actual timing
predicted_timing = model_timing(ciphertext, candidate_d)
actual_timing = timings[ciphertext]
correlation += predicted_timing * actual_timing
if correlation > best_correlation:
best_correlation = correlation
best_bit = candidate_bit
recovered_d += str(best_bit)
Why This Works: The Information Leak
code
Bit 0: c^(2x) mod n = FAST (only squaring)
Bit 1: c^(2x) * c mod n = SLOWER (squaring + multiply)
Real-World Timing Difference
For a 1024-bit RSA key:
Scenario
Operations
Time
All 0 bits
1024 squarings
~60ms
All 1 bits
1024 squarings + 1024 multiplies
~160ms
Mixed
Variable
Correlates with bit pattern
Key insight: With enough ciphertext samples, the attacker can statistically distinguish bit patterns even when individual measurements are noisy.
Concrete Example Attack
Suppose the first few bits of d are 1011...:
code
Ciphertext #1: c = 2
Bit 0 (=1): Timing includes multiplication ~100μs
Bit 1 (=0): Only squaring ~60μs
Bit 2 (=1): Timing includes multiplication ~100μs
Bit 3 (=1): Timing includes multiplication ~100μs
Total: ~360μs
Ciphertext #2: c = 3
Same bit sequence, but different values in intermediate states
Result: Different correlation with timing measurements
But pattern still correlates with "1011" structure
After ~1000 measurements, statistical noise averages out and the pattern emerges.
Prevention: Constant-Time Implementations
❌ Vulnerable (data-dependent timing)
python
result = 1
for bit in d:
result = result ^ 2 mod n
if bit == 1: # Conditional branch
result = result * c # Variable-time operation
✅ Protected Option 1: Montgomery Ladder
python
r0 = 0
r1 = 1
for bit in d:
if bit == 0:
r0, r1 = r0*r0, r0*r1 # Swap to maintain timing
else:
r0, r1 = r0*r1, r1*r1
# Constant operations per bit, no branch
return r0
✅ Protected Option 2: Always Multiply
python
result = 1
for bit in d:
result = result ^ 2 mod n
temp = result * c mod n
# Conditionally use result, but multiply always executes
result = select(result, temp, bit) # Constant-time select
✅ Protected Option 3: Blinding
python
r = random_blinding_factor()
c' = c * r^e mod n # Randomize ciphertext
m' = decrypt_unprotected(c', d)
m = m' / r mod n # Remove blinding
# Intermediate values are randomized, timing uncorrelated with d