TIMING ATTACK

The clock leaks your secrets

L215 MINSECURITY
PREDICT

The server compares a guessed token with the secret one character at a time and stops at the first mismatch. Each extra character compare adds 4 ticks. Network jitter adds up to ±10 ticks to every response. Can the attacker still find the first character?

The token check compares character by character and returns at the first mismatch. A guess with one correct leading character does one more compare before the “no”. That extra work is a few nanoseconds. It is still a signal.

The attacker tries all 16 candidates for the first position, many times each, and averages. The correct character is the slowest. Then the second position, and so on. Sixteen guesses per position instead of sixteen to the fourth. A constant-time compare does the same work for every input, so there is nothing to average.

▶ SIMULATION
ATTACKER
recovered: ____
requests sent: 0 · time spent: 0 ticks
—
SERVER
compare: —
cost: 100 + 4×compares ± 10 ticks
work seen: —
RELIABILITY
first character right in — independent attacks
at 6 trials per guess
✓ server work is the same whatever the number of matching characters
SCHEDULE
01[SERVER]server compares with an early-exit loop
02[ATTACKER]try all 16 characters at position 0, TRIALS times each; keep the slowest
03[ATTACKER]try all 16 characters at position 1, TRIALS times each; keep the slowest
04[ATTACKER]try all 16 characters at position 2, TRIALS times each; keep the slowest
05[ATTACKER]try all 16 characters at position 3, TRIALS times each; keep the slowest
06[ATTACKER]submit the recovered token
Start of early-exit compare scenario. 6 steps.
function checkToken(input, secret) {
  if (input.length !== secret.length) return false;  // length is not secret here
  for (let i = 0; i < input.length; i++) {
    if (input[i] !== secret[i]) return false;        // EARLY EXIT
  }
  return true;
}
// Each matching leading character adds one more loop iteration
// before the answer: a longer response for a better guess.

Watch “work seen” in the SERVER box and the invariant strip. EARLY-EXIT does 1 compare for a wrong first character and 2 for the right one, and the attacker walks out with B7E2. CONSTANT-TIME does 4 compares for every guess. The noise is identical, and the slowest mean is a different random character every round.

TUNE IT NOT YET

One extra compare costs 4 ticks; jitter is ±10 ticks per response. At 6 trials the attack works sometimes. Raise TRIALS PER GUESS until the first character is recovered in all 20 of 20 independent attacks.

GOAL: first character recovered in 20/20 attacks

FIX IT NOT YET

Write constantTimeEqual(a, b). a and b are byte buffers: .length and .get(i) (returns a number). The grader counts every get. For a given length, the count must be the same no matter where the inputs differ. It must return true only for equal contents, and false for different lengths. The lengths are public.

Mental model:

If the time a check takes depends on the secret, the check is answering more than yes or no. Noise does not save you; the attacker averages it away. Make the work independent of the data: read every byte, accumulate with XOR and OR, branch once at the end. Use the library’s constant-time compare, not ==.

IN PRODUCTION

Checking API keys, session tokens, password-reset codes, webhook signatures and HMACs. The bug is if (sig == expected) or memcmp on a secret. Use crypto.timingSafeEqual, hmac.compare_digest, subtle.ConstantTimeCompare or CRYPTO_memcmp.

IN INTERVIEWS

“What is wrong with comparing an HMAC with ==?” Explain the early exit, how an attacker recovers the value one byte at a time by averaging timings, why network noise only raises the sample count, and write the XOR-accumulate loop. Bonus: say that comparing hashes of both values, or a random-key HMAC of both, is another way to hide the prefix.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: HASHES, MACS & SIGNATURES →