Field Note 003 / Crypto Engineering

Constant-time is a property of the binary.

Not of your source. A helpful compiler will happily reintroduce the branch you carefully removed. If you haven't read the emitted assembly, you don't have constant-time. You have an intention.

Why timing matters

If the time a cryptographic operation takes depends on a secret, an observer who can measure that time can recover the secret. This has broken real implementations of RSA, elliptic-curve signatures, and symmetric ciphers, sometimes over a network. The defense is code whose execution time, memory access pattern, and branch behavior do not depend on secret values.

Where the source lies to you

You write a branch-free selection using bit masks. The optimizer recognizes the pattern and turns it back into a conditional jump, because that is faster on average. You write a comparison loop that always runs to the end. The compiler adds an early exit. Nothing in the language standard forbids this, because timing is not part of what the language promises.

The same source can be safe with one compiler version, one optimization level, or one target processor, and leak with another. Some processors also have instructions, such as division on many cores, whose own duration depends on the operands.

What to do about it

Use a vetted library instead of writing your own primitives. When you must write or review such code, inspect the compiled output for the exact build you ship, and add tooling that checks for secret-dependent branches and memory access automatically. Run those checks in continuous integration, because the next toolchain upgrade can undo the property without changing a line of your code.

Related: Post-quantum cryptography →Penetration testing & audits →

← Note 002All field notesNote 004 →

Have this problem
for real?

Book a callSend a message