TExcelTAD
Home › Number Theory & Cryptography › Inside AES: the S-box and MixColumns

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 GF(28)GF(2^8), then apply an affine map", and MixColumns is multiplication by a fixed matrix over GF(28)GF(2^8). 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 4×44 \times 4 byte matrix. Each round applies SubBytes (the S-box on every byte: the only non-linear step), ShiftRows (rotate row ii by ii 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 aa:

  1. Take the inverse b=a−1b = a^{-1} in GF(28)GF(2^8) (with 0↦00 \mapsto 0).
  2. Apply the affine map over GF(2)GF(2): si=bi⊕bi+4⊕bi+5⊕bi+6⊕bi+7⊕cis_i = b_i \oplus b_{i+4} \oplus b_{i+5} \oplus b_{i+6} \oplus b_{i+7} \oplus c_i (indices mod 8), with c=0x63c = 0x63.
python · try it yourself
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 S({53})={ed}S(\{53\}) = \{ed\} 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 2−62^{-6} per S-box, which makes linear and differential cryptanalysis of the full cipher hopeless. The affine step removes the algebraic simplicity of a↦a−1a \mapsto a^{-1} (and its fixed points 0 and 1).

python · try it yourself
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 (2−62^{-6}) is the best possible for an 8-bit bijection known in practice.

MixColumns

Each column (a0,a1,a2,a3)(a_0, a_1, a_2, a_3) is multiplied by a fixed matrix over GF(28)GF(2^8):

[b0b1b2b3]=[2311123111233112][a0a1a2a3]\begin{bmatrix} b_0 \\ b_1 \\ b_2 \\ b_3 \end{bmatrix} = \begin{bmatrix} 2 & 3 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 2 & 3 \\ 3 & 1 & 1 & 2 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \\ a_3 \end{bmatrix}

with sums as XOR and products in GF(28)GF(2^8). 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 kk input bytes changes at least 5−k5 - k output bytes.

python · try it yourself
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 2⊕3⊕1⊕1=12 \oplus 3 \oplus 1 \oplus 1 = 1.

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 {53}⋅{CA}={01}\{53\} \cdot \{CA\} = \{01\} in the AES field, using the program or by hand.

Answer

Yes, {CA}\{CA\} is the inverse of {53}\{53\}.

Solution

0x53=x6+x4+x+10x53 = x^6 + x^4 + x + 1 and 0xCA=x7+x6+x3+x0xCA = x^7 + x^6 + x^3 + x. Their carry-less product reduced modulo x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1 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 GF(28)GF(2^8) 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 (14,11,13,9)(14, 11, 13, 9) 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
python · try it yourself
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…

  1. a random table
  2. inversion in GF(28)GF(2^8) followed by an affine map
  3. SHA-256
  4. modular exponentiation mod a prime

Which AES step is the only non-linear one?

  1. ShiftRows
  2. MixColumns
  3. SubBytes
  4. AddRoundKey

Multiplying by {02}\{02\} in the AES field is…

  1. a left shift, then XOR with 0x1B if the top bit was set
  2. adding 2
  3. a right shift
  4. a table lookup only