How Do Masking and Constant Time Stop Side Channels?
Every side-channel defense tries to break the link between the secret and what can be measured. Constant-time design makes timing independent of secrets. Masking splits each secret value into random shares, so no single intermediate value depends on the secret. Hiding reduces the signal or adds noise and randomness in time, so the leak is harder to measure. Real designs combine them, because each has a cost and a limit.
Before you start
This is defensive knowledge, for building and testing your own hardware or hardware you are authorized to test. It explains mechanisms and defenses with references to published research. It does not give step-by-step attacks on products or systems you don't own.
What you'll learn
- Constant-time comparison and why branches on secrets are dangerous
- Boolean masking, and why it works
- Hiding: noise, shuffling and balanced logic
- What each costs, and its limits
Constant time
Do the same operations in the same order whatever the secret is. For a comparison, combine every byte's difference and decide only at the end:
// Returns 1 if equal. Runs through all n bytes whatever the data.
int ct_equal(const unsigned char *a, const unsigned char *b, size_t n) {
unsigned char diff = 0;
for (size_t i = 0; i < n; i++)
diff |= a[i] ^ b[i];
return diff == 0;
}A mismatch in the first byte and in the last byte take the same 16 steps for a 16-byte value. The same rule applies to hardware: no secret-dependent early termination, no secret-dependent memory addresses (which leak through caches), and no secret-dependent number of clock cycles. Compilers can undo constant-time source code, so security libraries check the generated assembly.
Masking
Masking replaces a secret byte x with two shares: a fresh random mask m and x ⊕ m. Each share on its own is uniformly random: for any x, as m runs over all 256 values, x ⊕ m also takes every value exactly once, so the average Hamming weight of the masked value is 4 whatever x is. Power that depends on one share therefore reveals nothing about x.
Linear operations (XOR, shifts, bit permutations) can be done on each share separately. Nonlinear ones, such as the AES S-box, need special masked circuits that combine shares with extra randomness, and they are where implementations go wrong. Glitches in combinational logic can briefly combine both shares, so hardware masking schemes such as threshold implementations add rules on how shares may meet. First-order masking defeats attacks that look at one point in time; an attacker who combines two points (the two shares) can still succeed, at a much higher cost, which is why higher-order masking exists.
Hiding
- Noise: extra random switching activity lowers the signal-to-noise ratio, so more traces are needed.
- Shuffling: doing independent operations (such as the 16 S-boxes of an AES round) in a random order each time, so a given point in time no longer corresponds to one operation.
- Random delays and clock jitter, which blur alignment between traces.
- Balanced (dual-rail) logic, which switches the same number of nodes whatever the data, at roughly double the area and power.
Hiding raises the number of measurements needed rather than removing the leak, so it is usually combined with masking.
The costs
| Defense | Typical cost | Limit |
|---|---|---|
| Constant time | Slower worst-case-only code | Compilers and microarchitecture can break it |
| Masking | Randomness, area, and 2 to 3 times the logic for nonlinear parts | Higher-order attacks; glitches in hardware |
| Hiding | Power, area or speed | Only raises the number of traces needed |
Common mistakes
- Reusing a mask, which cancels it out when two masked values are combined.
- Checking constant time in C but not in the compiled code.
- Assuming a masked design is secure without measuring it.
- Masking the data but leaving the address of a table lookup secret-dependent.
Check your understanding
- Q1. Why is x ⊕ m safe to process when m is random? Answer: for any x, x ⊕ m is uniformly distributed, so its power profile does not depend on x.
- Q2. What does a constant-time compare avoid? Answer: returning early, so its time doesn't depend on how many bytes match.
- Q3. Why combine hiding with masking? Answer: hiding alone only raises the number of measurements needed; masking removes the first-order dependence.
The series
Related
Sources
Mangard, Oswald and Popp, Power Analysis Attacks, Springer, 2007, chapters 7 (hiding) and 9 (masking); Nikova, Rechberger and Rijmen, 'Threshold Implementations Against Side-Channel Attacks and Glitches', ICICS 2006.
Last verified
The masking distribution and the constant-time step counts are computed in the site's tests (lib/batch9Numbers.test.js) on 16 November 2026.