Side-channel attacks: timing, cache, power, and why constant-time code matters

Side channels leak secrets through how long, how loudly, or how much power a computation takes. Timing leaks in string comparison, cache attacks such as Spectre and Meltdown, power analysis on devices, and the coding and patching habits that defend.

Lesson 3 of 10 · 11 min read · practice only with authorization

Diagram

A timing leak in a token check

Follow the path. One example at a time.

Step 1 of 4

Compare

A naive check stops at the first mismatched byte.

Illustration: Compare
def check(token, expected):
    return token == expected      # may return early

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Timing in application code

    What fails
    Early-exit comparisons and secret-dependent branches leak timing.
    Who is affected
    Tokens, MACs, and passwords compared in code.
    What you would notice
    Use of == on secrets in review.
    What you change
    Use constant-time comparison functions.
  2. CPU side channels

    What fails
    Spectre and Meltdown (2018) read memory through speculative execution and cache timing.
    Who is affected
    Shared hosts and browsers.
    What you would notice
    Missing microcode or OS mitigations.
    What you change
    Patch OS, firmware, and browsers; isolate sensitive workloads.
  3. Hardware devices

    What fails
    Power and electromagnetic analysis read keys from smart cards and embedded chips.
    Who is affected
    Device makers and users.
    What you would notice
    Embedded crypto without countermeasures.
    What you change
    Use certified secure elements for keys.

Use libraries that already care

Well-maintained crypto libraries (libsodium, BoringSSL, Go’s crypto packages) are written to avoid secret-dependent timing. Writing your own primitives almost always reintroduces leaks.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

Why can token == expected leak information?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapCryptography basics: hashes, symmetric and public-key encryption, and signaturesThe four primitives security work depends on, what each guarantees, and the common mistakes: encoding mistaken for encryption, fast hashes for passwords, and encryption without integrity.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.