Part I Chapter 4

Hiding and masking

Contents
  1. Hiding: making the leak harder to measure
  2. Masking: removing the secret from every value
  3. Why one sample says nothing
  4. Where the leak goes
  5. What masking stops, and what it doesn’t
  6. Using both
  7. References

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.
Shuffling moves the leak to a different slot every trace trace 1 trace 2 trace 3 trace 4 fixed order slot the attack reads shuffled slot the attack reads S-box lookup of the attacked byte; gray: the other 15 bytes
Shuffling, drawn for four traces. In a fixed order, the attacked byte's lookup sits in the same slot every time, and a sample-by-sample correlation finds it. Shuffled, it lands in the slot the attack reads only one trace in 16.

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 lookupsAttackTraces
FixedCPA on the attacked byte’s slotabout 12
ShuffledCPA on the same slotabout 5,000
ShuffledCPA on the sum of all 16 slotsabout 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:

  1. Mask the input. The chip handles p ⊕ m_in instead of p.
  2. XOR the key. XOR lets the mask pass through: (p ⊕ m_in) ⊕ k = x ⊕ m_in. The chip gets the masked x without ever forming x.
  3. 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_out for all 256 values of a. Looking up x ⊕ m_in in that table returns Sbox[x] ⊕ m_out = y ⊕ m_out: the right output, masked with m_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].

The first S-box, unmasked and masked unmasked masked p 0xc5 (HW 4) x = p ⊕ k 0xd2 (HW 4) y = Sbox[x] 0xb5 (HW 5) ⊕ k Sbox p ⊕ m_in 0x9f (HW 6) x ⊕ m_in 0x88 (HW 2) y ⊕ m_out 0x23 (HW 3) ⊕ k Sbox′ fresh random masks m_in=0x5a, m_out=0x96 masked table, rebuilt every encryption Sbox′[a] = Sbox[a ⊕ m_in] ⊕ m_out
The first S-box of the Chapter 1 example, unmasked and masked with m_in = 0x5a and m_out = 0x96. Red: values an attacker computes from a key guess, as in Chapter 3. The masked row never handles them.

Run the same byte twice with fresh masks and the chip handles a different S-box output each time:

m_inm_outOutputHW
Unmasked0xb55
Masked, run 10x5a0x960x233
Masked, run 20xe10x470xf25

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.

What the power sees, unmasked and masked unmasked: the chip handles y 0% 50% 100% 0 1 2 3 4 5 6 7 8 Hamming weight of the value handled masked: the chip handles y ⊕ m 0% 10% 20% 30% 0 1 2 3 4 5 6 7 8 Hamming weight of the value handled secret y = 0x00 secret y = 0xff
Hamming weight at the S-box output for the two most different secrets, 0x00 and 0xff. Unmasked, the chip handles them as weight 0 and weight 8. Masked, over all 256 values of the mask, both give the same spread: weight 4 for 70 of the 256 masks, weight 0 for one.

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.

The secret is in the pair of samples secret y = 0x00 0 0 4 4 8 8 the two are equal sample where it handles y ⊕ m sample where it handles m secret y = 0xff 0 0 4 4 8 8 they add up to 8 sample where it handles y ⊕ m sample where it handles m
Simulated: 400 encryptions, each reduced to two samples, the Hamming weights of m and of y ⊕ m, plus a little noise. For y = 0x00 the masked value equals the mask, so the two samples rise together. For y = 0xff every bit is flipped, so they add up to 8. Either axis on its own has the same spread in both panels.

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, CPAMasked chip, CPA on the product
0.510150
115200
2301,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 m with y ⊕ m in the same register, the bits that flip are the Hamming distance between the two (Chapter 2), and HD(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

  1. 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.
  2. 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.
  3. 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.
  4. L. Goubin, J. Patarin, “DES and Differential Power Analysis: The ‘Duplication’ Method,” CHES 1999 (opens in a new tab). LNCS 1717, pp. 158–172.
  5. 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.
  6. 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.
  7. 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.
  8. M. Rivain, E. Prouff, “Provably Secure Higher-Order Masking of AES,” CHES 2010 (opens in a new tab). LNCS 6225, pp. 413–427.
  9. 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.
  10. S. Mangard, N. Pramstaller, E. Oswald, “Successfully Attacking Masked AES Hardware Implementations,” CHES 2005 (opens in a new tab). LNCS 3659, pp. 157–171.
  11. 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.