Side-Channel Attack

Difficulty: Advanced

Question

What is a timing attack, how does it fit into the broader family of side-channel attacks, and how do you defend against it?

Answer

A timing attack is a side-channel attack where the attacker learns secret information by measuring how long an operation takes. The computation itself is correct and the output reveals nothing directly, but the time it takes leaks information about the secret. A side channel is any observable physical or timing property of a system, as opposed to a flaw in the algorithm on paper.

How a timing attack works

Many naive comparisons return as soon as they find a mismatch. If a password, API token or HMAC is compared byte by byte and the function exits on the first wrong byte, then a guess with a correct first byte takes slightly longer than one that is wrong immediately. By sending many guesses and measuring tiny timing differences, an attacker can recover a secret one byte at a time. The same idea applies to cryptographic code whose running time depends on secret key bits, such as a modular exponentiation that branches on key values.

The broader side-channel family

ChannelWhat leaksExample
TimingExecution durationNon-constant-time string compare
CacheMemory access patternsSpectre and Meltdown reading across boundaries
Power analysisPower draw during cryptoExtracting keys from a smart card
ElectromagneticEM emissionsReading a device's operations from nearby
AcousticSound of componentsInferring keystrokes or CPU activity
Error/oracleDiffering error responsesPadding oracle attacks on CBC

Side channels are powerful because they bypass the mathematics. An algorithm can be provably secure yet still leak its key through how it runs on real hardware.

Defences

  • Constant-time comparison for secrets: always compare the full length and combine results, so the time does not depend on where the first mismatch is. Use library functions like crypto.timingSafeEqual in Node, hmac.compare_digest in Python, or subtle.ConstantTimeCompare in Go.
  • Constant-time cryptography: use vetted libraries whose code paths and memory accesses do not branch on secret data. Do not roll your own.
  • Blinding: randomise inputs to cryptographic operations so timing cannot be correlated to the key.
  • Reduce precision and add noise where feasible, though this is weaker than removing the dependency.
  • Isolation: for cache attacks, separate tenants and apply CPU microcode and OS mitigations.

Why it matters for web developers

You rarely implement ciphers, but you do compare secrets: verifying an HMAC signature on a webhook, checking an API token, or comparing a password reset token. Using == there is a real, exploitable timing leak. Always use a constant-time comparison for anything secret.

Common mistakes

  • Comparing tokens or HMACs with == or ===, which short-circuits on the first mismatch.
  • Writing your own crypto with data-dependent branches.
  • Assuming network jitter hides timing differences; attackers average over many samples to cancel noise.
  • Returning different error messages or response times for valid versus invalid input, creating an oracle.
Recovering a token one byte at a time. A comparison that exits on the first mismatch leaks progress through its response time.
  1. Attacker → Server: Guess token starting 'a...'
  2. Server → Attacker: Fast reject (first byte wrong)
  3. Attacker → Server: Guess token starting 'b...'
  4. Server → Attacker: Slightly slower (first byte right)
  5. Attacker → Server: Keep the prefix, vary the next byte

Code examples

Timing-safe comparison vs a leaky one

import { timingSafeEqual } from 'node:crypto';

// LEAKY: === short-circuits on the first differing character
function unsafeCheck(a, b) {
  return a === b;
}

// SAFE: constant-time, independent of where bytes differ
function safeCheck(a, b) {
  const bufA = Buffer.from(a);
  const bufB = Buffer.from(b);
  // lengths must match; compare a fixed-length digest if they vary
  if (bufA.length !== bufB.length) return false;
  return timingSafeEqual(bufA, bufB);
}

timingSafeEqual always scans the whole buffer, so the runtime does not reveal how many leading bytes matched. For variable-length secrets, compare fixed-length HMACs of both values instead.

Key points

  • A timing attack recovers secrets by measuring how long an operation takes.
  • Side channels (timing, cache, power, EM, acoustic, error oracles) bypass the algorithm's math.
  • Early-exit comparisons leak secrets one byte at a time.
  • Use constant-time comparison functions for tokens, HMACs and reset codes.
  • Use vetted crypto libraries; do not write data-dependent branches on secrets.

Common follow-up questions

  • How does timingSafeEqual differ from === and why does it matter for HMAC checks?
  • What are Spectre and Meltdown and which side channel do they exploit?
  • What is a padding oracle attack?
  • How does blinding protect RSA from timing attacks?

Interview tips

  • Give the concrete web example of comparing an API token or HMAC with ==; it makes the risk real.
  • Name a constant-time function for the language you know; it shows you can fix it, not just name it.
  • Place timing attacks in the wider side-channel family to show breadth.

Concepts covered

Side-Channel, Timing Attack, Constant-Time Comparison, Cryptography

More Web Security (OWASP, JWT, XSS, CSRF) questions