PostHeaderIcon Where Timing Leaks: Branches, Caches, Early Returns, Data-Dependent Delays

Once the measurement budget is known, the next question is what to measure. Timing does not leak from a single place, and the mechanisms differ enough that a technique which finds one will miss the others entirely.

Four mechanisms account for almost every practical leak. They are listed here in order of how easy they are to find, which is roughly the inverse of how much information they carry.

Mechanism one: the early return

This is the classic and the easiest to exploit. A comparison loop that exits at the first mismatching byte takes an amount of time proportional to the length of the matching prefix, and that length is the secret.

“ for (i = 0; i < 16; i++) { if (a[i] != b[i]) return 0; // leaks the prefix length } “

The magnitude is large: each iteration costs one load, one compare and one branch, which is a handful of cycles, so a sixteen-byte comparison spans perhaps a hundred cycles between the fastest and slowest case. On a 16 MHz core that is six microseconds, which is enormous compared with the noise floor of a well-triggered measurement.

The measurement is a single-timing histogram: run the comparison ten thousand times with the first mismatch at each position, and the means separate. No special equipment is needed beyond a trigger, because the effect is bigger than the noise.

The information per measurement is high. Recovering one byte costs 256 attempts, so a sixteen-byte tag costs 4,096 rather than 2^128. That ratio is why this mechanism is the one that appears in every textbook.

Mechanism two: the branch

A conditional branch on a secret bit produces two paths of different length. The magnitude is usually one to a few cycles, which is three orders of magnitude smaller than the early return.

“ if (key_bit) { x = rotate_left(x, 1); } else { x = x ^ constant; } “

On a simple core, the branch itself is the cost: a taken branch flushes the prefetch buffer, and the flush costs a few cycles. On a core with branch prediction, the cost becomes harder to predict but does not vanish — the misprediction rate depends on the pattern, and the pattern depends on the secret.

The measurement requirement is different from the first case. A three-cycle difference against a noise floor of a hundred nanoseconds on a 16 MHz core is a ratio of two to one in favour of the noise, so thousands of samples are needed and the trigger has to be precise.

Mechanism Typical Δ Noise immunity Samples needed
Early return 1–10 µs Very good Hundreds
Branch on secret 2–10 cycles Poor 10,000+
Cache or wait state 10–100 cycles Moderate 1,000+
Data-dependent arithmetic 5–50 cycles Moderate 5,000+

Mechanism three: the cache or the wait state

A table lookup indexed by a secret touches a cache line determined by the secret. If the table is larger than the cache, the hit rate depends on which entries are used, and the timing reflects the access pattern.

On microcontrollers this mechanism shows up in two forms, and the second is often the dominant one.

The first is a genuine data cache, found on higher-end parts. A table of a few kilobytes against a 32-byte line size gives a measurable hit-rate difference, and the classic attack on table-based AES recovers the key from it.

The second is flash wait states. On parts running above the flash’s rated frequency, the flash controller inserts wait states, and the number depends on whether the access is sequential and whether the prefetch buffer hits. A table lookup that breaks a sequential run costs more than one that continues it. This mechanism exists on parts with no cache at all, and it is frequently overlooked because the datasheet describes wait states as a fixed configuration rather than a data-dependent cost.

Sub-mechanism Where it exists Magnitude
Data cache miss High-end MCU with cache Tens of cycles
Flash wait state plus prefetch Most MCUs above 24 MHz 1–3 cycles per access
TCM versus flash placement Parts with tightly coupled memory Large, if code moves

The third row is a design detail that becomes an attack: code or tables placed in fast memory run at a different speed from the same code in flash, and if the placement depends on a configuration value, the timing reveals the configuration.

Mechanism four: data-dependent arithmetic

Some operations take an amount of time that depends on their operands. Integer division is the canonical example: the number of shift-subtract iterations depends on the magnitude of the divisor and the dividend. Floating-point operations on soft-float libraries are worse, because the library implements normalisation loops whose iteration count depends on the exponent.

Software delay loops of the form for (i = 0; i < n; i++) where n derives from the secret are the crudest version and are still written, usually as a “security delay” that inadvertently publishes the value it is protecting.

Operation Dependence On a core without hardware divide
Integer divide Operand magnitude Up to 32 iterations
Soft float normalise Exponent Up to 24 shifts
Multiply-accumulate chain Loop count Proportional
Software delay loop The secret directly Fully proportional

A routine usually has more than one

The mechanisms above are not alternatives. A single authentication routine typically contains an early return in the comparison, a branch on the result, a table lookup in the key schedule and a division in the certificate check. Four leaks in one function, with magnitudes spanning three orders of magnitude.

The practical consequence is that fixing the obvious one changes which mechanism dominates. After the early return is removed, the next largest effect is the branch, and after the branch is removed, the table lookup becomes visible. An audit that stops after the first fix has removed the loudest leak and left the quiet ones, which is exactly the state most “hardened” libraries are in.

This is also why the defence is a rewrite rather than a patch. Removing four mechanisms from a routine one at a time produces code that is constant in four dimensions and probably not in a fifth that nobody looked for. The disciplined approach is to specify constant time as a property of the whole path, which is the subject of preventing timing attacks in code.

What time does not see

The four mechanisms above all describe time. There is a fifth channel that is not time at all, and knowing its boundary is what stops a timing audit from being mistaken for a security audit.

Every operation the core performs draws current, and the current depends on which transistors switched. A multiply that processes a secret-dependent operand draws a different current profile from one that does not, regardless of how many cycles it takes. So a routine that is perfectly constant in time can still be a strong emitter, and the measurement that finds it is a current or electromagnetic probe rather than a timer.

The relationship between the two channels is worth stating precisely, because it is frequently confused.

Property Timing channel Power or EM channel
Observable Arrival time of the response Current or field during execution
Resolution needed Nanoseconds Megasamples per second
Removed by constant-time code Yes No
Removed by quantised response Yes No
Requires physical access to the die No Usually not, but close proximity

The fourth row is the one that surprises people. A device that quantises its response has hidden the timing completely and hidden nothing else, because the energy consumed during the operation is still proportional to the work done, and the work done is still proportional to the secret.

This matters for assessment because a customer who has been told their device is constant time may reasonably conclude that side channels are handled. They are not; one of the two channels is handled, and which one was addressed determines what remains.

Identifying which one applies

Identification is an experiment, not an inspection. Change one input bit at a time, hold everything else fixed, and measure. The shape of the response identifies the mechanism.

A response that is flat until a byte boundary and then steps is an early return. A response that alternates with individual bits is a branch. A response that depends on the index modulo a power of two is a cache or a wait state. A response that grows smoothly with a numeric value is arithmetic.

That mapping is worth building once per target, because it determines both the attack and the fix. The budget that decides whether any of it is measurable on the actual board is in before a timing attack, and the system-level defences that make the measurement moot are in preventing timing attacks at system level.

In assessment work this is the first analysis we perform: a per-bit response map of the target routine, classified by mechanism, with the magnitude of each. It takes a day and it tells the customer which of their four leaks is the one that matters. Where the map shows a single dominant mechanism and a good trigger, the route to a recovered credential is the one in where the secret key lives; where it shows four comparable mechanisms, the honest report is that the routine needs a rewrite before any measurement is worth taking.

Comments are closed.