Inside AES: the S-box and MixColumns
AES, the world's standard block cipher, is usually presented as tables of magic bytes. They are not magic: the S-box is "invert in , then apply an affine map", and MixColumns is multiplication by a fixed matrix over . This lesson rebuilds both from the field arithmetic of the previous lesson.
The round structure in one paragraph
AES encrypts a 16-byte block arranged as a byte matrix. Each round applies SubBytes (the S-box on every byte: the only non-linear step), ShiftRows (rotate row by positions), MixColumns (mix the four bytes of each column) and AddRoundKey (XOR a round key). AES-128 uses 10 rounds. Confusion comes from the S-box, diffusion from ShiftRows and MixColumns.
The S-box
For each byte :
- Take the inverse in (with ).
- Apply the affine map over : (indices mod 8), with .
def gf_mul(a, b):
r = 0
while b:
if b & 1: r ^= a
b >>= 1; a <<= 1
if a & 0x100: a ^= 0x11B
return r
def gf_inv(a):
if a == 0:
return 0
r, e = 1, 254
while e:
if e & 1: r = gf_mul(r, a)
a = gf_mul(a, a); e >>= 1
return r
def rotl8(x, k):
return ((x << k) | (x >> (8 - k))) & 0xFF
def sbox(a):
b = gf_inv(a)
return b ^ rotl8(b, 1) ^ rotl8(b, 2) ^ rotl8(b, 3) ^ rotl8(b, 4) ^ 0x63
S = [sbox(a) for a in range(256)]
print("first row:", " ".join(f"{v:02x}" for v in S[:16]))
print("S(0x00) =", hex(S[0x00]), " S(0x53) =", hex(S[0x53]), " inverse of 0x53 =", hex(gf_inv(0x53)))
print("bijective:", len(set(S)) == 256, " fixed points:", [a for a in range(256) if S[a] == a])first row: 63 7c 77 7b f2 6b 6f c5 30 01 67 2b fe d7 ab 76
S(0x00) = 0x63 S(0x53) = 0xed inverse of 0x53 = 0xca
bijective: True fixed points: []
The first row 63 7c 77 7b ... matches the published table, and FIPS-197's worked example comes out.
Why the inverse?
Field inversion has excellent non-linearity: the best linear approximation and the best differential through it hold with probability only per S-box, which makes linear and differential cryptanalysis of the full cipher hopeless. The affine step removes the algebraic simplicity of (and its fixed points 0 and 1).
def gf_mul(a, b):
r = 0
while b:
if b & 1: r ^= a
b >>= 1; a <<= 1
if a & 0x100: a ^= 0x11B
return r
def gf_inv(a):
r, e = 1, 254
while e:
if e & 1: r = gf_mul(r, a)
a = gf_mul(a, a); e >>= 1
return r
rotl8 = lambda x, k: ((x << k) | (x >> (8 - k))) & 0xFF
def sbox(a):
b = gf_inv(a) if a else 0
return b ^ rotl8(b, 1) ^ rotl8(b, 2) ^ rotl8(b, 3) ^ rotl8(b, 4) ^ 0x63
S = [sbox(a) for a in range(256)]
# differential uniformity: max over dx != 0 of #{x : S(x ^ dx) ^ S(x) = dy}
best = 0
for dx in range(1, 256):
counts = [0] * 256
for x in range(256):
counts[S[x ^ dx] ^ S[x]] += 1
best = max(best, max(counts))
print("max differential count:", best, "of 256 (probability", best / 256, ")")max differential count: 4 of 256 (probability 0.015625 )
A count of 4 out of 256 () is the best possible for an 8-bit bijection known in practice.
MixColumns
Each column is multiplied by a fixed matrix over :
with sums as XOR and products in . Multiplying by 2 is one shift and a conditional XOR (xtime), by 3 is xtime(a) ^ a, so the step is cheap. The matrix is MDS (maximum distance separable): a change in any input bytes changes at least output bytes.
def xtime(a):
a <<= 1
return a ^ 0x11B if a & 0x100 else a
def mix_column(col):
a0, a1, a2, a3 = col
m2 = lambda x: xtime(x)
m3 = lambda x: xtime(x) ^ x
return [m2(a0) ^ m3(a1) ^ a2 ^ a3,
a0 ^ m2(a1) ^ m3(a2) ^ a3,
a0 ^ a1 ^ m2(a2) ^ m3(a3),
m3(a0) ^ a1 ^ a2 ^ m2(a3)]
print([hex(v) for v in mix_column([0xDB, 0x13, 0x53, 0x45])])
print([hex(v) for v in mix_column([0x01, 0x01, 0x01, 0x01])])
changed = mix_column([0xDB, 0x13, 0x53, 0x46])
print("one input byte changed -> output bytes changed:", sum(x != y for x, y in zip(changed, mix_column([0xDB, 0x13, 0x53, 0x45]))))['0x8e', '0x4d', '0xa1', '0xbc']
['0x1', '0x1', '0x1', '0x1']
one input byte changed -> output bytes changed: 4
The test vector db 13 53 45 to 8e 4d a1 bc is the standard one. A column of equal bytes is unchanged because each row of the matrix sums to .
In software
- Fast AES uses lookup tables combining S-box and MixColumns ("T-tables"), which leak through cache timing (Bernstein's 2005 attack); modern CPUs offer AES-NI and ARMv8 AES instructions that run in constant time.
- Bitsliced and constant-time software implementations compute the S-box as a Boolean circuit derived from the field inversion, not as a table.
Exercises
Exercise 1: Inverse step by hand
Check that in the AES field, using the program or by hand.
Answer
Yes, is the inverse of .
Solution
and . Their carry-less product reduced modulo is 1; gf_mul(0x53, 0xCA) confirms it.
Exercise 2: Why is MixColumns invertible?
What property of the matrix guarantees decryption is possible?
Answer
Its determinant over is nonzero; the inverse matrix has entries 14, 11, 13, 9.
Solution
A matrix over a field is invertible iff its determinant is nonzero. AES decryption uses InvMixColumns with rows rotated, which is why decryption is slightly slower in table-free software.
Exercise 3: Inverse S-box (code)
Build the inverse S-box from the S-box and check that InvS[S[a]] == a for all bytes. What is InvS[0xED]?
Answer
0x53.
Solution
def gf_mul(a, b):
r = 0
while b:
if b & 1: r ^= a
b >>= 1; a <<= 1
if a & 0x100: a ^= 0x11B
return r
def gf_inv(a):
r, e = 1, 254
while e:
if e & 1: r = gf_mul(r, a)
a = gf_mul(a, a); e >>= 1
return r
rotl8 = lambda x, k: ((x << k) | (x >> (8 - k))) & 0xFF
def sbox(a):
b = gf_inv(a) if a else 0
return b ^ rotl8(b, 1) ^ rotl8(b, 2) ^ rotl8(b, 3) ^ rotl8(b, 4) ^ 0x63
S = [sbox(a) for a in range(256)]
InvS = [0] * 256
for a, s in enumerate(S):
InvS[s] = a
print(all(InvS[S[a]] == a for a in range(256)), hex(InvS[0xED]))True 0x53
Test yourself
The AES S-box is built from…
- a random table
- inversion in followed by an affine map
- SHA-256
- modular exponentiation mod a prime
Which AES step is the only non-linear one?
- ShiftRows
- MixColumns
- SubBytes
- AddRoundKey
Multiplying by in the AES field is…
- a left shift, then XOR with 0x1B if the top bit was set
- adding 2
- a right shift
- a table lookup only