TIMING ATTACK
The clock leaks your secrets
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.
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.function checkToken(input, secret) {
if (input.length !== secret.length) return false;
let diff = 0;
for (let i = 0; i < input.length; i++) {
diff |= input.charCodeAt(i) ^ secret.charCodeAt(i); // no branch on data
}
return diff === 0;
}
// Same loop count whatever matches. In Node use crypto.timingSafeEqual;
// in Go subtle.ConstantTimeCompare; in Python hmac.compare_digest. 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.