AES-128 and the S-box

How AES processes bytes in the first round, stated as in FIPS 197. The point for chapter 7 is a single intermediate: y = Sbox[p ⊕ k]. Once that sits in the real cipher, key candidates are just hypotheses about y.

1. What AES-128 is

AES is a block cipher. For AES-128 the block is 128 bits (16 bytes), the key is 128 bits (16 bytes), and encryption uses 10 rounds after an initial key mixing step. The algorithm is a fixed sequence of public operations on a 16-byte array called the state. Round keys are derived from the secret key by a public key schedule. Secrecy is only in the key material, not in the steps.

2. The state

The 16 plaintext bytes fill a 4×4 matrix column by column (FIPS 197). Cell labels below are byte indices 0…15:

0
4
8
12
1
5
9
13
2
6
10
14
3
7
11
15

Index 2 (highlighted) is the target byte in the ASCADr runs of this repository. SubBytes acts on each cell alone; ShiftRows and MixColumns mix cells afterward.

Figure 1. AES state, column-major byte indices.

3. Encryption structure

AES-128 encryption, as specified:

state ← plaintext (16 bytes)
state ← AddRoundKey(state, round_key[0])

for r = 1 … 9:
    state ← SubBytes(state)
    state ← ShiftRows(state)
    state ← MixColumns(state)
    state ← AddRoundKey(state, round_key[r])

state ← SubBytes(state)
state ← ShiftRows(state)
state ← AddRoundKey(state, round_key[10])
# final round: no MixColumns
ciphertext ← state

4. AddRoundKey

Each state byte is XOR-ed with the matching byte of the current round key. After the initial AddRoundKey, a cell with plaintext byte p and key byte k holds:

x = p ⊕ k

That step is linear. Nonlinearity enters at SubBytes.

5. SubBytes and the S-box

SubBytes replaces every state byte through a fixed 256-entry table: the S-box. The table is public and identical for every AES-128 implementation. The attacker does not recover it; they use it.

Lookup

For input x, the high nibble selects the table row and the low nibble the column. Example: x = 0xAC → row A, column C → Sbox[0xAC] = 0x91.

y = Sbox[x]

Construction (FIPS 197)

Each S-box entry is defined by:

  1. treat the byte as an element of GF(2⁸) (polynomials modulo the irreducible polynomial fixed in the standard),
  2. take the multiplicative inverse in that field (0 ↦ 0),
  3. apply a fixed affine transform over GF(2) (an 8×8 bit matrix plus a constant vector).

For side-channel work you rarely recompute that by hand. You do need the consequences: the map is public, nonlinear, and has strong avalanche — one input bit flip typically changes many output bits. Key candidates that differ by one bit therefore imply very different values of y.

6. ShiftRows and MixColumns

ShiftRows rotates each row by a fixed offset (0, 1, 2, 3 bytes). MixColumns multiplies each column by a fixed matrix over GF(2⁸). Together they diffuse one byte’s influence across the block. Profiling attacks often measure an intermediate that still depends on a single key byte in a simple algebraic way — typically the first-round SubBytes output — before that diffusion dominates.

7. The side-channel target

On many software AES implementations, the first-round SubBytes output at a chosen byte position is:

y = Sbox[p ⊕ k]
The network in this project predicts y (or a function of it, such as Hamming weight) from the trace. It does not output the S-box table or name k directly. The attacker recovers k by testing which of the 256 candidates produces a y that matches the model’s probabilities across many traces. Masking (ASCADr) weakens each single-trace vote, so accumulation is required — that is the rest of chapter 7.

8. ASCADr numbers

First attack-set illustration in the docs: plaintext byte p = 0x8e (142), true key byte k = 0x22 (34), at byte index 2.

x = 0x8e ⊕ 0x22 = 0xac = 172
y = Sbox[0xac] = 0x91 = 145
HW(y) = 3

Hamming weight HW(y) is the number of 1-bits in y (here 0x91 = 10010001₂). Many leakage models and some networks predict HW rather than the full byte; the table shows how a one-bit change in x moves both the S-box output and its HW.

xdecSbox[x]decHW(y)
0x8E1420x19253
0x8F1430x731155
0xAC1720x911453
0xAD1730x951494
0x711130xA31634

Figure 2. Fragment of the AES S-box (same table as the dataset loaders). Neighbours 0x8E and 0x8F differ by one input bit; outputs jump from 25 to 115 and HW from 3 to 5.

Candidate g = 0x00 predicts y = 25 (HW = 3); g = 0x01 predicts 115 (HW = 5); g = 0x22 predicts 145 (HW = 3). The model assigns probabilities to those y (or HW) values on each trace; the correct g is the hypothesis that wins in aggregate.