Posts Tagged ‘mcu timing attack’

PostHeaderIcon Before a Timing Attack: The Observability Budget

Timing attacks acquire a reputation for being either trivial or impossible, and the reputation comes from people attempting them without doing the arithmetic first. The arithmetic takes ten minutes and answers the question definitively. Everything after that is execution.

Four quantities have to be known before the first measurement. They are not independent, and the relationship between them is the whole analysis.

One: can the input be controlled

A timing attack needs the attacker to choose the input and to know exactly when the target operation begins. Both halves matter.

Choosing the input is usually easy: a command over a serial port, a keypad entry, a value written to a register. It becomes hard when the input passes through a state machine that transforms it, or when the target only accepts one attempt per power cycle, or when the input is a physical quantity such as a supply voltage that the attacker can only influence within a range.

Knowing when the operation begins is the part that is frequently underestimated. A trigger has to be derived from something the device does at the start of the operation: a GPIO toggling, a bus transaction, a current step, or an instruction fetch on an address bus. Without a trigger, the measurement window is the whole command-response interval, which is milliseconds long, and the timing difference of twenty nanoseconds is lost in the placement error.

The practical test is whether the trigger jitters. A trigger derived from a GPIO that the firmware sets at the start of the routine jitters by the GPIO write latency, which is one or two cycles, so the placement error is a few nanoseconds. A trigger derived from the first byte of a UART reply jitters by the baud-rate quantisation, which is 8.7 µs at 115200 baud. That difference decides whether the attack is possible, and it has nothing to do with the target’s protection.

Two: what resolution is available

The measurement has to resolve the effect, and the effect is typically one to fifty nanoseconds.

Measurement method Resolution Placement jitter
Oscilloscope, 1 GS/s, hardware trigger 1 ns 1–5 ns
Oscilloscope, 100 MS/s 10 ns 5–20 ns
MCU cycle counter around the call 1 cycle (62 ns at 16 MHz) Trigger overhead
GPIO toggle plus logic analyser 1 sample (2–10 ns) 1–2 cycles
Software timestamp read before and after 1 µs or worse Interrupt dependent

The fifth row is where most home-grown attempts fail. Reading a timer before and after the call measures the call plus the timestamp read, and the timestamp read itself is affected by interrupts, by flash wait states, and by whether the counter is in a peripheral that requires a bus wait. The measured value is dominated by that overhead, and the effect being sought is smaller than the overhead’s variance.

The second row is the common bench situation and it is marginal: a 10 ns resolution against a 20 ns effect means the effect is two quantisation steps, which is detectable with enough averaging but leaves little margin for anything that shrinks the effect.

Three: what the noise is

Noise is the largest term and the least often measured. It has four sources, and they differ in how they respond to averaging.

Noise source Magnitude Reduced by averaging
Thermal and quantisation Sub-nanosecond Yes, as 1/√N
Clock and PLL jitter 1–20 ns Partially, if uncorrelated
Interrupt and scheduler jitter 100 ns–10 µs Poorly, if periodic
Host scheduling (USB, OS) 1–10 ms No

The last row is fatal and is the reason USB-connected instruments are unsuitable for this work at nanosecond scale. USB transfers are scheduled in one-millisecond frames, so the arrival time of a command has a one-millisecond uncertainty. Averaging does not reduce it, because it is not random: it is a quantisation that repeats.

The third row is the one that catches embedded targets. A timer interrupt that fires every millisecond and takes two microseconds perturbs the measurement whenever it lands inside the window. Averaging helps, but slowly, and the perturbation is correlated with the loop’s own timing if the loop is periodic.

Four: how many repetitions are affordable

The formula that ties the three quantities together is the standard one. To resolve a difference Δ against a noise standard deviation σ, the number of samples needed is roughly (σ/Δ)² per class, doubled for two classes.

Putting numbers to it makes the decision concrete.

Δ (effect) σ (noise) N per class Time at 100 ms/attempt
20 ns 20 ns 4 0.4 s
20 ns 200 ns 200 20 s
20 ns 2 µs 20,000 33 min
20 ns 20 µs 2,000,000 55 hours
1 ns 200 ns 80,000 2.2 hours

The fourth row is where a technically possible attack stops being a practical one. Two million attempts at a tenth of a second each is fifty-five hours of continuous measurement on a single device, and any interruption — a watchdog, a thermal shutdown, a bus error — invalidates the run.

The fifth row is the case that looks impossible on paper and is fine in practice, because the effect there is one nanosecond and the noise is two hundred, but a device that can be measured at a hundred microseconds per attempt makes the required eighty thousand samples a two-hour job.

The decision

With the four quantities measured, the answer is one of three.

If N is below a thousand, the attack is a morning’s work and the only remaining question is which byte to attack first. Start with the byte whose effect is largest, which is usually the first and last byte of a comparison rather than a middle one.

If N is between a thousand and a million, the attack is viable but has to be planned: the device must be able to run for hours without intervention, the trigger must be stable, and the measurement chain must not depend on a host operating system.

If N is above a million, the honest answer is that the measurement budget does not permit it, and the effort should move to a different side channel or to a physical route. Saying this early is the most valuable thing an assessment can produce.

What changes the budget

Two levers can move a marginal case into the viable range, and both are about the target rather than the equipment.

Raising the signal: some implementations amplify the effect without any change to the attacker’s setup. A comparison that processes more bytes per iteration, or that includes a checksum over the whole buffer, produces a larger Δ than one that exits early. Finding an input that maximises the difference is worth more than a better oscilloscope.

Lowering the noise: removing the device’s own interrupts is sometimes possible by choosing an operating mode — a bootloader path instead of the application, a service mode instead of normal operation. A bootloader often has interrupts disabled and a simple polling loop, which reduces σ by an order of magnitude. This is a legitimate and often decisive step.

Where the analysis sits in the work

This budget is computed before anything else, and it is what makes the difference between an assessment and a campaign. The four leak shapes an attacker is looking for are described in where timing leaks, and the code-level defence that removes them is in preventing timing attacks in code. When the budget is insufficient, the alternative is usually a fault injection route, whose own feasibility test is in glitch attacks as a search problem.

The service at this layer is a measurement report: the trigger jitter, the noise floor, the achievable Δ, and the resulting sample count, with a recommendation. Customers are sometimes surprised that the answer is a number rather than an opinion, and that is the point. Where the number is too large, we say so, and the work moves to a route that has a budget of its own — for instance the physical read in extracting IC flash. What we will not do is spend a month averaging in the hope that a budget which does not close will close later.