Part I Chapter 4
Hiding and masking
Contents
Chapter 3 recovered a key byte from 100 simulated traces. On a real unprotected microcontroller the equipment is an oscilloscope and a probe, and the statistics are the same. Chips that store keys for a living, such as bank cards, SIM cards and secure elements, are tested against these attacks before they’re certified, so they ship with countermeasures.
Chapter 3 ended on the two facts its correlation power analysis (CPA) relies on. The chip handles y = Sbox[p ⊕ k] itself, so its power follows the Hamming weight of y. And it does so at the same sample in every trace, so the correlation can compare traces sample by sample and average the noise out. Countermeasures take away one or the other. Hiding leaves the computation alone and makes the leak harder to measure, with more noise or a different sample in every trace. Masking changes the computation: the chip never handles y, only y combined with a fresh random byte.
Hiding: making the leak harder to measure
A chip that relies on hiding still computes y. The countermeasure works on the measurement, in one of two ways [1]:
- Amplitude: less signal, more noise. A noise generator runs alongside the cipher, or the circuit uses a logic style that draws about the same power whatever the data, such as dual-rail logic, where every bit travels with its complement. In the Chapter 3 widget this is the noise slider: raise σ and the attack needs more traces, but the true key still wins.
- Time: a different sample in every trace. The chip inserts random delays, runs on an unstable clock, or shuffles: it processes the 16 S-box lookups of a round in a random order.
On the simulated chip from Chapter 3 at σ = 1, with 16 lookups per trace, traces until the true key ranks first (median of 7 runs):
| Order of the lookups | Attack | Traces |
|---|---|---|
| Fixed | CPA on the attacked byte’s slot | about 12 |
| Shuffled | CPA on the same slot | about 5,000 |
| Shuffled | CPA on the sum of all 16 slots | about 180 |
Shuffling dilutes the correlation 16-fold at any one slot, and the traces needed grow roughly with the square of that dilution. Summing the slots puts the attacked lookup back in every trace, mixed with 15 others, and recovers most of the loss. In general, hiding multiplies the traces an attack needs by a factor the design sets. The attacker either pays it or preprocesses it away by averaging windows, realigning traces, or training a convolutional network, which tolerates shifted traces [2]. ASCAD ships copies of its traces shifted at random by up to 50 and 100 samples to test exactly that [3].
Masking: removing the secret from every value
Masking was proposed in 1999 [4, 5]. Before each encryption the chip draws fresh random bytes, the masks, and computes only on masked values. Each masked value is a random byte that changes every encryption, so no value the chip handles can be predicted from the plaintext and a key guess.
Take the first S-box from Chapter 1: plaintext byte p = 0xc5, key byte k = 0x17, so x = p ⊕ k = 0xd2 and y = Sbox[x] = 0xb5. An attacker who guesses k can compute both, and that’s what CPA does.
A masked AES draws two random bytes per encryption, m_in and m_out, and goes through three steps:
- Mask the input. The chip handles
p ⊕ m_ininstead ofp. - XOR the key. XOR lets the mask pass through:
(p ⊕ m_in) ⊕ k = x ⊕ m_in. The chip gets the maskedxwithout ever formingx. - Look up a masked table. Plain
Sbox[x ⊕ m_in]would be useless, so the chip first builds a masked copy of the S-box with this encryption’s masks:Sbox′[a] = Sbox[a ⊕ m_in] ⊕ m_outfor all 256 values ofa. Looking upx ⊕ m_inin that table returnsSbox[x] ⊕ m_out = y ⊕ m_out: the right output, masked withm_out.
The other AES steps are linear, so a mask goes through them in a form the chip can track, and the chip removes the masks at the end: the ciphertext is the same as without masking. The price is rebuilding a 256-entry table and drawing new random bytes for every encryption. ASCADr, the main dataset of this guide, comes from an implementation that works this way [3].
Run the same byte twice with fresh masks and the chip handles a different S-box output each time:
m_in | m_out | Output | HW | |
|---|---|---|---|---|
| Unmasked | 0xb5 | 5 | ||
| Masked, run 1 | 0x5a | 0x96 | 0x23 | 3 |
| Masked, run 2 | 0xe1 | 0x47 | 0xf2 | 5 |
Same plaintext, same key. For the right key, Chapter 3 predicts Hamming weight 5 at the S-box output. Run 2 matches it by chance; run 1 misses by two.
Why one sample says nothing
XOR-ing any byte with a uniformly random byte gives a uniformly random byte (Chapter 1). So every masked value is uniform whatever the secret is, and its Hamming weight follows the same distribution.
The correlation that found the key in Chapter 3 averages to zero, for the right guess as for every wrong one. On the simulated chip from Chapter 3 at noise σ = 1, CPA needs about 15 traces when the chip is unmasked. Mask the S-box output, and after 10,000 traces the true key still ranks anywhere from 35th to 222nd of 256 (five runs).
Where the leak goes
The secret is still in the trace, split between two values the chip handles at different moments. To keep the notation short, write m for m_out. The chip handles the mask m while it builds the masked table, and the masked output y ⊕ m during the lookup. Each one alone is a uniform byte; XOR the two and you get y back.
A second-order attack turns that pair into one number [6]. Subtract each sample’s average and multiply the two: under the Hamming-weight model, the product averages 2 − HW(y)/2. That number follows y again, so Chapter 3’s correlation works on it, with predictions from each key guess. Traces until the true key ranks first on the simulated chip, median of 15 runs:
| Noise σ | Unmasked chip, CPA | Masked chip, CPA on the product |
|---|---|---|
| 0.5 | 10 | 150 |
| 1 | 15 | 200 |
| 2 | 30 | 1,000 |
The gap grows with the noise, from 15 times at σ = 0.5 to 33 times at σ = 2, because multiplying two noisy samples multiplies their noise too. With more shares the attack has to multiply more samples, and with noisy measurements the traces it needs grow exponentially with the number of shares a secret is split into [5, 7].
The table assumes the attacker knows which two samples to multiply. On a real trace nobody says: a 15,000-sample CHES_CTF trace has about 1.1 × 108 pairs (Chapter 2). Finding the pair and the right way to combine it is the job this guide hands to a neural network.
What masking stops, and what it doesn’t
Two shares stop every attack that reads one sample at a time, whatever leakage model the attacker picks, because every single value the chip handles is uniform. Extra shares make the attacker’s job harder still, but the defender pays too: a masked AES S-box with d + 1 shares takes on the order of d2 operations [8], plus fresh random bytes for every share.
What breaks it:
- The pair still leaks. Two shares raise the attack’s cost but leave the information in the trace. Second-order attacks combine the pair by hand, and neural networks learn to: a network breaks masked ASCADr with 86 attack traces, as a later chapter shows.
- Weak randomness. The argument assumes every mask is fresh and uniform. A biased random number generator leaves the masked value correlated with
y, and a one-sample attack works again. - Transitions. If the chip overwrites
mwithy ⊕ min the same register, the bits that flip are the Hamming distance between the two (Chapter 2), andHD(m, y ⊕ m) = HW(y): the unmasked leak, in a single sample. Software has to keep the two shares out of the same register and bus [9]; in hardware, glitches can combine them even when the design keeps them apart [10].
Using both
Protected chips usually combine the two, because the costs multiply. Going from σ = 0.5 to σ = 2 took the unmasked attack from 10 traces to 30, and the second-order attack from 150 to 1,000. Noise hurts the masked chip’s attacker far more, and hiding is a source of noise; shuffling the masked operations pushes the cost up further [11]. The full ESHARD dataset comes from an AES that is masked and shuffled; the version in this guide keeps only the first round, without shuffling, so the masking is what’s left to beat.
All three datasets in this guide come from masked AES implementations. Chapter 5 opens them: what each file stores, and which ones also record the masks, something an attacker normally never sees.
References
- S. Mangard, E. Oswald, T. Popp, Power Analysis Attacks: Revealing the Secrets of Smart Cards, Springer 2007 (opens in a new tab). Chapters 7 and 9 cover hiding and masking.
- E. Cagli, C. Dumas, E. Prouff, “Convolutional Neural Networks with Data Augmentation Against Jitter-Based Countermeasures,” CHES 2017 (opens in a new tab). LNCS 10529, pp. 45–68.
- R. Benadjila, E. Prouff, R. Strullu, E. Cagli, C. Dumas, “Deep learning for side-channel analysis and introduction to ASCAD database,” Journal of Cryptographic Engineering 2020 (opens in a new tab). Vol. 10, pp. 163–188.
- L. Goubin, J. Patarin, “DES and Differential Power Analysis: The ‘Duplication’ Method,” CHES 1999 (opens in a new tab). LNCS 1717, pp. 158–172.
- S. Chari, C. S. Jutla, J. R. Rao, P. Rohatgi, “Towards Sound Approaches to Counteract Power-Analysis Attacks,” CRYPTO 1999 (opens in a new tab). LNCS 1666, pp. 398–412.
- T. S. Messerges, “Using Second-Order Power Analysis to Attack DPA Resistant Software,” CHES 2000 (opens in a new tab). LNCS 1965, pp. 238–251.
- E. Prouff, M. Rivain, “Masking against Side-Channel Attacks: A Formal Security Proof,” EUROCRYPT 2013 (opens in a new tab). LNCS 7881, pp. 142–159.
- M. Rivain, E. Prouff, “Provably Secure Higher-Order Masking of AES,” CHES 2010 (opens in a new tab). LNCS 6225, pp. 413–427.
- J. Balasch, B. Gierlichs, V. Grosso, O. Reparaz, F.-X. Standaert, “On the Cost of Lazy Engineering for Masked Software Implementations,” CARDIS 2014 (opens in a new tab). LNCS 8968, pp. 64–81.
- S. Mangard, N. Pramstaller, E. Oswald, “Successfully Attacking Masked AES Hardware Implementations,” CHES 2005 (opens in a new tab). LNCS 3659, pp. 157–171.
- M. Rivain, E. Prouff, J. Doget, “Higher-Order Masking and Shuffling for Software Implementations of Block Ciphers,” CHES 2009 (opens in a new tab). LNCS 5747, pp. 171–188.