Skip to content
KALANBLON
Show contents

Chapter 4 of 6 · 20 min

Side channels and the quantum threat

By the end of this chapter, you'll be able to

  • Exploit a timing leak to recover a secret character by character
  • Show that a square-and-multiply exponentiation leaks the bits of the exponent
  • Write an exponentiation with a constant sequence of operations (Montgomery ladder)
  • Assess the urgency of the post-quantum migration and name ML-KEM and ML-DSA

A timing leak, exploited

A server checks an access token with a comparison that stops at the first wrong character. We simulate the response time by the number of characters compared (in real life, the attacker takes thousands of measurements and uses statistics to remove the network noise).

import string

SECRET = "k7q2zp"                       # the server's secret token

def response_time(guess):
    """Simulates the duration of a comparison that stops at the first wrong character."""
    duration = 0
    for a, b in zip(SECRET, guess):
        duration += 1
        if a != b:
            return duration
    return duration + (1 if len(guess) == len(SECRET) else 0)

# The attacker guesses the token position by position, keeping the "slowest" character.
found = ""
for position in range(6):
    best = max(string.ascii_lowercase + string.digits,
               key=lambda c: response_time(found + c + "?" * (5 - position)))
    found += best
    print(f"position {position}: {found}")
print("attempts needed:", 6 * 36, "instead of", 36 ** 6)
position 0: k
position 1: k7
position 2: k7q
position 3: k7q2
position 4: k7q2z
position 5: k7q2zp
attempts needed: 216 instead of 2176782336

The attack turns a search over 2 billion possibilities into 216 attempts. The fix is well known: a constant-time comparison (hmac.compare_digest), which always goes through the whole string.

Execution traces

The classic modular exponentiation, "square and multiply", goes through the bits of the exponent: a square for every bit, plus a multiplication when the bit is 1. If an attacker observes the sequence of operations (through timing, power consumption or the processor's cache), they read the secret exponent, bit by bit. That is the subject of the pitfall below.

The quantum threat

A large enough quantum computer would run Shor's algorithm, which factors and computes discrete logarithms in polynomial time: RSA, Diffie-Hellman and elliptic curves would fall. Grover's algorithm also speeds up exhaustive search, but only by a square root: AES-256 keeps a comfortable margin. Nobody knows when such a machine will exist; the immediate risk is called "harvest now, decrypt later": an adversary records encrypted exchanges today to read them later.

In August 2024, NIST published the first post-quantum standards: ML-KEM (FIPS 203) for key exchange, ML-DSA (FIPS 204) and SLH-DSA (FIPS 205) for signatures. Browsers and large services already use a hybrid exchange (X25519 + ML-KEM-768): both must be broken to read the conversation.

Cryptographic agility lets you replace an algorithm without rewriting the application.

Step 5 of 5
Text description of the animation
  1. We list each use, its algorithm and the library that provides it.
  2. Data that must stay secret for a long time comes first.
  3. The secret combines both: security does not go backwards if one of the two is broken.
  4. Post-quantum keys and signatures are bigger: check slow networks and old equipment.
  5. Cryptographic agility lets you replace an algorithm without rewriting the application.

To decide how urgent it is, we use Mosca's inequality: if x is the number of years a piece of data must stay secret, y the duration of the migration and z the time until a quantum computer able to break today's cryptography, there is a problem as soon as x + y > z.

migration = 5          # years to migrate (y), the team's estimate
for z in (10, 15, 20):                       # assumptions about the arrival of quantum computers
    print(f"if quantum arrives in {z} years:")
    for data, x in [("session token", 0), ("business contract", 10), ("medical record", 25)]:
        verdict = "URGENT" if x + migration > z else "ok"
        print(f"   {data:<18} x={x:>2} → {verdict}")
if quantum arrives in 10 years:
   session token      x= 0 → ok
   business contract  x=10 → URGENT
   medical record     x=25 → URGENT
if quantum arrives in 15 years:
   session token      x= 0 → ok
   business contract  x=10 → ok
   medical record     x=25 → URGENT
if quantum arrives in 20 years:
   session token      x= 0 → ok
   business contract  x=10 → ok
   medical record     x=25 → URGENT

Even under the most optimistic assumption, long-lived data is already concerned: that is why the migration starts now, beginning with key exchange ("harvest now" does not threaten signatures that have already been verified).

Mini-lab: blurring the leakPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: the Montgomery ladder

Write ladder(base, exponent, modulus, trace) that computes base^exponent mod modulus while keeping two values R0 and R1 (initially 1 and base). For each bit: if the bit is 0, R1 = R0·R1 then R0 = R0²; if it is 1, R0 = R0·R1 then R1 = R1². Add "M" then "S" to the trace for every bit. Check that the result equals pow, and that two exponents of the same length give the same trace.

Hint 1
Start with R0, R1 = 1, base. The invariant is R1 = R0 × base at every step.
Hint 2
Both branches do exactly one multiplication and one squaring: only the destination changes. In real code, even the branch is avoided by swapping the two values with a condition-free computation.
Show the solution and common mistakes
def ladder(base, exponent, modulus, trace):
    R0, R1 = 1, base % modulus
    for bit in bin(exponent)[2:]:
        if bit == "0":
            R1 = R0 * R1 % modulus
            R0 = R0 * R0 % modulus
        else:
            R0 = R0 * R1 % modulus
            R1 = R1 * R1 % modulus
        trace += ["M", "S"]
    return R0

for d in (0b1011001110001011, 0b1000000000000001):
    trace = []
    result = ladder(7, d, 1_000_003, trace)
    print(f"d = {d:>5}: correct = {result == pow(7, d, 1_000_003)}, trace = {''.join(trace)[:16]}…")
d = 45963: correct = True, trace = MSMSMSMSMSMSMSMS…
d = 32769: correct = True, trace = MSMSMSMSMSMSMSMS…

The two exponents, very different, produce exactly the same sequence of operations. The Montgomery ladder is the method X25519 uses for scalar multiplication.

Common mistakes:

  • Believing an identical trace is enough: in Python, the time of a multiplication still depends on the size of the numbers; constant time is guaranteed in C or assembly, with verification tools.
  • Adding "dummy" operations whose result is ignored: a compiler may remove them, and power consumption may tell them apart.
  • Skipping the first iteration when the top bit is 1: that reveals the real length of the exponent.

Summary

  1. 1A side channel (timing, power, cache) can reveal a secret even if the algorithm is solid.
  2. 2Any branch or memory access that depends on a secret bit is a potential leak.
  3. 3The Montgomery ladder performs the same sequence of operations whatever the exponent.
  4. 4Shor would break RSA and elliptic curves: migrate to ML-KEM and ML-DSA, in hybrid mode, starting with long-lived data.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.