Posts Tagged ‘mcu memory attack’
Preventing Timing Attacks at System Level: Why Random Delays Fail
When a timing leak cannot be fixed in the code — because the code is in a vendor library, or because the leak is in the platform, or because the constant-time rewrite is too expensive — the defence moves up a layer. Three options exist at that layer, and they are not variations on a theme. Two of them multiply the attacker’s work by a constant. One of them removes the observable.
The distinction matters because the first two are what most products implement, and they are the reason most products are still attackable.
Option one: random delays
The idea is intuitive. If the response time leaks information, add a random delay so that the response time no longer means anything.
The arithmetic that kills it is short. A uniform random delay over an interval of width D has a standard deviation of about 0.29 D. Averaging N independent observations reduces the standard deviation by √N, so after N observations the residual noise is 0.29 D / √N. To hide a real signal of magnitude S, the residual noise must exceed S, which requires:
N > (0.29 D / S)²
Put in numbers for a realistic embedded case: a delay uniform over one millisecond, and a signal of twenty nanoseconds.
| Delay width D | Signal S | Samples needed | At 100 ms each |
|---|---|---|---|
| 100 µs | 20 ns | 2,100 | 3.5 min |
| 1 ms | 20 ns | 210,000 | 5.8 hours |
| 10 ms | 20 ns | 21,000,000 | 24 days |
| 100 ms | 20 ns | 2.1 × 10^9 | 6.6 years |
The first two rows are the ones that matter, because delays of a hundred microseconds to a millisecond are what a firmware engineer considers a reasonable randomisation. Two thousand samples is a coffee break and two hundred thousand is a long weekend. Neither is a defence.
Only at the bottom of the table does the delay become a real obstacle, and a device that randomly delays every authentication response by up to a hundred milliseconds is a device with a hundred-millisecond latency, which the product owner will reject.
There is a second problem. The random delay has to be genuinely random. A delay derived from a linear congruential generator seeded at boot, with the same seed on every unit, is a fixed sequence, and an attacker who recovers the sequence removes the defence entirely. This is common in low-cost firmware, where the RNG is a timer reading.
Option two: a fixed time base
The second option abandons randomness and makes the response time constant. The routine runs to completion, then waits until a fixed deadline before responding.
This works, and it works completely, because the observable becomes independent of the secret rather than noisy. The difference from randomisation is categorical: after averaging, a randomised response converges to the true value plus a constant, while a fixed-time response converges to the constant.
The cost is the worst-case latency. If the operation takes between one and three milliseconds depending on the input, the deadline must be three milliseconds, and every response takes three. That is acceptable for an authentication attempt, which happens once per session, and unacceptable for a data path that runs thousands of times per second.
| Defence | Observable after averaging | Cost | Works |
|---|---|---|---|
| Random delay | Signal plus a constant | Average latency | No |
| Fixed deadline | A constant | Worst-case latency | Yes |
| Fixed-period polling | A quantised constant | Slot granularity | Yes |
Option three: fixed-period polling
The third option is the most elegant and the least used. The device does not respond when it finishes; it responds at the next tick of a free-running timer.
The implementation is a queue. The routine places its result in a slot, and a timer interrupt at a fixed period drains the slot and transmits. Because the transmission instant is decided by the timer rather than by the routine, the observable is quantised to the tick period, and every response that lands in the same tick is indistinguishable.
This is strictly better than a fixed deadline in one respect: it does not require the routine to be padded to its worst case, only that the worst case be shorter than the tick period. A routine that takes one to three milliseconds behind a five-millisecond tick produces responses that all appear at the same instant, and the routine’s internal variance is invisible.
The cost is granularity. A response that arrives at a tick boundary is quantised to five milliseconds, so the maximum throughput is two hundred responses per second regardless of how fast the routine is. For a command interface that is fine; for a bulk data path it is not.
Quantisation is the key property and it is worth stating precisely. A quantised observable takes one of a small number of values, and the number of values is the same regardless of the secret. That is a stronger guarantee than any amount of noise, because noise leaves the underlying value recoverable in the limit while quantisation leaves it unrecoverable at any sample count.
Which one to use
The decision is made by where the leak is and how often the operation runs.
If the operation is rare — authentication, key derivation, a service unlock — use a fixed deadline or a tick. The latency cost is invisible to the user and the defence is complete.
If the operation is frequent and the latency matters, the defence has to move back into the code, because no system-level trick can hide a leak on a path that must respond in microseconds. That is the case for the constant-time rewrite described in preventing timing attacks in code.
If the operation is in a library that cannot be changed, wrap it: run it, store the result, and release the result on a tick. The wrapper does not need to know what the library does, only that its output can be delayed.
| Situation | Recommended | Why |
|---|---|---|
| Rare, latency tolerant | Fixed tick | Complete and cheap |
| Frequent, latency critical | Constant-time code | Only option that works |
| Third-party library | Tick wrapper | No source access needed |
| Vendor crypto with own timing | Tick plus audit | Wrapper hides, audit finds |
Measuring whether the defence holds
A quantised response is a claim, and it is a cheap claim to test.
The test is to run the same operation a thousand times with the same input, record the arrival time of each response, and look at the distribution. A defended device produces one of two shapes: a single spike, where every response lands on the same tick, or a distribution truncated at the tick boundary, where the fastest responses are delayed to a common floor and the slowest are not.
The second shape is the interesting failure. It means the padding covers the fast path but not the slow one, so the slow cases still leak. This happens when the deadline is set from the typical execution time rather than the worst case, which is a natural mistake when the worst case is rare.
The second half of the test is to vary the input across the range that would matter to an attacker and confirm the response time does not move. Ten thousand samples per input class, means compared. If the means are identical to within the tick resolution, the quantisation is complete; if they differ by a fraction of a tick, the padding is partial and the residual leak is visible to anyone who averages.
| Test | Passes when | Typical failure |
|---|---|---|
| Repeated identical input | One spike at the tick | Jitter larger than the tick |
| Worst-case input | Arrives at the same tick | Deadline set from the mean |
| Input sweep | Means identical | Partial padding |
| Current probe | No data-dependent steps | Leak moved to the supply |
The fourth row is not a timing test at all, and it is included because a defence that quantises the response can leave the current draw untouched. A device that takes a variable amount of charge to complete an operation leaks that variation through its supply even when its digital response is perfectly quantised.
The mistake that undoes all three
All three defences are defeated by one implementation error: allowing a second observable to escape.
A device that quantises its response but logs the failure time to a flash page has moved the leak to the flash write. A device that pads its response but returns a different error code for a malformed input has kept the leak in the code. A device that uses a fixed deadline but blinks an LED inside the routine has published the timing optically.
The audit question is therefore not “is the response time constant” but “what else can be observed”. Current draw, indicator pins, bus traffic, log writes and the response itself are all observables, and a defence that covers one of them is a defence against a specific measurement rather than against the class.
What the assessment delivers
The service at this layer is an observable inventory rather than a code review. We list every channel that carries timing information out of the device — response latency, current, indicator pins, bus activity, logs — measure the magnitude on each, and report which ones are quantised and which are not.
That inventory is what tells a customer whether their randomisation is worth anything. In most cases the answer is that it is not, the arithmetic in the table above shows why, and the fix is a tick rather than a delay. The measurement budget that decides whether a residual leak is exploitable at all is set out in before a timing attack, and the shapes a leak takes in the first place are in where timing leaks. Where the leak is in the silicon rather than the software, the defence is a different discussion entirely, and the five-generation history in the smartcard arms race is the honest summary of how that goes.