Part I Chapter 3

Attacking one key byte

Contents
  1. Guess, predict, compare
  2. A first attack, on simulated traces

Chapter 1 gave the target, the intermediate value Sbox[plaintext ⊕ key], and showed that each key byte can be attacked on its own. Chapter 2 showed that a chip’s current follows the Hamming weight of the values it handles. Put the two together and you have an attack on one key byte.

Guess, predict, compare

The attacker has many traces, each with a known plaintext byte p and the same unknown key byte k. Assume for now that we know which sample leaks; Chapter 6 shows how to find it. For each of the 256 possible values:

  1. Guess a key byte g.
  2. Predict what the chip would have handled if g were the key: the Hamming weight of Sbox[p ⊕ g], for every trace.
  3. Compare the predictions with the measurements. The guess whose predictions rise and fall with them is the key.
pMeasuredPredicted Hamming weight
true key 0x3Cwrong guess 0x3D
0x0e2.834
0x342.921
0x203.643
0x444.255
0x5f6.474
0xf16.664
Six traces from a simulated chip with key byte 0x3C and little noise (σ = 0.5), sorted by the measured sample; one trace per true weight from 2 to 7. The true key’s predictions climb with the measurement. Those of 0x3D, one bit away, don’t.

One bit of difference in the guess scrambles the prediction, because the S-box spreads it over about four bits of output (Chapter 1). A wrong guess can still match a few traces by luck, so an attack needs many.

The usual score for the compare step is the correlation between a guess’s predictions and the measurements: +1 when they rise and fall together, near 0 when they’re unrelated. On the six traces above, 0x3C scores 0.93 and 0x3D 0.44. Scoring every guess this way is correlation power analysis (CPA).

The prediction is called a label, and the rule that produces it a leakage model. Two are common: Identity (ID), the full S-box output (256 classes), and Hamming weight (HW), its number of 1 bits (9 classes, 0 to 8). For the first ASCADr attack trace used later in this guide (p = 0x8e, key byte 0x22), where popcount counts the 1 bits:

ID label  = Sbox[0x8e ⊕ 0x22] = 145
HW label  = popcount(145)     = 3    (145 = 0b10010001)

A first attack, on simulated traces

The six traces above had little noise. Simulate the same unprotected chip with noise σ = 2, enough to blur the whole 0–8 range of weights, and run CPA with more and more traces:

TracesRank (of 256)Correlation
true key 0x3Cbest wrong guess
10370.410.90
30120.340.55
10010.650.25
30010.550.29
1,00010.550.15
CPA on a simulated chip with key byte 0x3C and noise σ = 2; each row is a separate simulation with the same random seed. Below 100 traces a wrong guess beats the true key by chance. From 100 on, the true key leads and the best wrong guess sinks toward 0.

With 10 traces, some of the 255 wrong guesses line up with the measurements by luck, one of them at 0.90. More traces wash that luck out. The true key’s correlation doesn’t grow; it settles near 0.55, close to the 0.58 theory predicts for this much noise: the Hamming weight of a random byte has variance 2, so the expected correlation is √(2 / (2 + σ²)) = 0.58 at σ = 2.

Correlation per key guess, 100 simulated traces-0.400.40.8true key 0x3Ckey guess (0–255)correlation
Simulated, not measured: 100 traces whose leakage is the Hamming weight of Sbox[p ⊕ 0x3C] plus Gaussian noise (σ = 2). Each bar is the correlation between that leakage and one key guess’s predicted Hamming weights. The true key (red) stands out at 0.65; the best wrong guess reaches 0.25.

Try it on the same simulated chip. Start with little noise, find the key, then raise the noise to σ = 2 and compare 10 traces with 100.

key guess g = 0x00

Trace 1: Sbox[0x44 ⊕ 0x00] = 0x1b, 4 bits set → predicts 4, measured 5.1

Measured against predicted, guess 0x000481234567891011121314151617181920tracepowermeasuredpredicted by 0x00
match
-0.22
Match of every guess tried0.500x00match of each guess tried (0 of 256)0xff

Blue is what the chip drew; red is what your guess predicts. Move the slider until they match, or try all 256.

traces

Real attacks repeat this for every sample in the trace, since nobody knows in advance which samples leak, and then for each of the 16 key bytes.

Hamming weight is a model the attacker picks, and it doesn’t fit every chip. In the Chapter 2 widget, a register that starts at 0x00 flips as many bits as the new value’s Hamming weight. One that overwrites an older value flips the Hamming distance between the two instead. Against that chip, Hamming-weight predictions correlate more weakly, and the attacker switches models.

Every version of this attack rests on two facts about the chip: it handles Sbox[p ⊕ k] itself, and it does so at the same sample in every trace. Can a chip be built that gives up both?