Section 15.4

AES round structure

Full name: Advanced Encryption Standard (AES) round structure.

Definition

The AES round structure is the sequence of steps by which AES (Advanced Encryption Standard) encrypts one 16-byte block. AES is the block cipher §15.3, a keyed invertible map on blocks, that NIST standardized in FIPS 197 (2001). It holds the block as a 4-by-4 array §14.5 of bytes, the state, and transforms the state in rounds. A full round applies four steps: SubBytes, ShiftRows, MixColumns and AddRoundKey; the last round omits MixColumns. Keys of 128, 192 or 256 bits give 10, 12 or 14 rounds; the block is always 128 bits.

The idea

AES must turn 16 bytes into 16 bytes so that every output bit depends on every key bit and every input bit in a complicated way, and yet a key holder can undo the transformation exactly. No single simple step achieves this: a step simple enough to analyze and invert is too regular to be secure on its own. AES therefore combines a few simple, invertible steps into a round and repeats the round.

Claude Shannon named the two jobs in 1949. Confusion makes the relation between key and ciphertext complicated. In AES the SubBytes step provides it. SubBytes is the only nonlinear step, meaning the only step whose output bits are not XOR (exclusive or) combinations of its input bits and key bits. Diffusion spreads the influence of each input byte over many output bytes; ShiftRows and MixColumns provide it. AddRoundKey brings in the key. After two rounds, every byte of the state depends on all 16 input bytes. Further rounds add a safety margin: the published attacks that are far faster than trying every key work only against versions of AES with fewer rounds.

Changing the key length changes how many rounds run and how the round keys are made. The block, the state and the four steps stay the same.

How it works

Notation. Bytes are written in hexadecimal, for example 57. AES treats each byte as an element of the finite field §19.1.2 GF(28)\mathrm{GF}(2^8), the Galois field with 256 elements. In this field, addition is XOR. Multiplication multiplies the two bytes as polynomials whose coefficients are their bits, with coefficient arithmetic done by XOR, and keeps the remainder after division by x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1. Every nonzero byte has a multiplicative inverse. Multiplying by 02 is the easy case: shift the byte left by one bit, and if a 1 bit fell off the top, XOR the result with 1b. Multiplying by 03 means multiplying by 02 and then taking the XOR of the result and the original byte.

The state. The 16 input bytes are written into the state column by column: bytes 0 to 3 fill column 0 from top to bottom, bytes 4 to 7 fill column 1, and so on. Row rr and column cc of the state, each numbered from 0 to 3, hold input byte r+4cr + 4c. The output is read out in the same order.

The four steps.

The order. Let NrN_r be the number of rounds: 10, 12 or 14.

  1. The key expansion, also called the key schedule, turns the key into Nr+1N_r + 1 round keys of 16 bytes each.

  2. AddRoundKey with round key 0.

  3. Rounds 1 to Nr−1N_r - 1: SubBytes, ShiftRows, MixColumns, then AddRoundKey with that round’s key.

  4. The final round NrN_r: SubBytes, ShiftRows, then AddRoundKey with round key NrN_r.

Decryption applies the inverse steps in reverse order, with the round keys in reverse order. Each step can be undone: XOR with the same round key undoes AddRoundKey, the inverse table undoes the S-box, rotating right undoes ShiftRows, and the inverse matrix undoes MixColumns.

Why the final round omits MixColumns. MixColumns is linear: for any states xx and zz, MixColumns(x⊕z)=MixColumns(x)⊕MixColumns(z)\mathrm{MixColumns}(x \oplus z) = \mathrm{MixColumns}(x) \oplus \mathrm{MixColumns}(z). Taking z=MixColumns−1(y)z = \mathrm{MixColumns}^{-1}(y), where MixColumns−1\mathrm{MixColumns}^{-1} is the inverse step, gives MixColumns(x)⊕y=MixColumns(x⊕MixColumns−1(y))\mathrm{MixColumns}(x) \oplus y = \mathrm{MixColumns}(x \oplus \mathrm{MixColumns}^{-1}(y)) for any state yy. A MixColumns placed just before the last AddRoundKey could therefore be moved past the key addition, changing only the round key, and would then be a fixed public step applied to the ciphertext. An attacker could undo that step without the key, so it would add no security. Leaving it out also makes the decryption algorithm §14.1 resemble the encryption algorithm in structure.

Worked example

This example follows the first column of the state through round 1 of the AES-128 example in Appendix C.1 of FIPS 197, where AES-128 means AES with a 128-bit key. In hexadecimal, the key KK and the plaintext PP are

KK = 000102030405060708090a0b0c0d0e0f
PP = 00112233445566778899aabbccddeeff

Initial AddRoundKey. Round key 0 is the key itself. Column 0 holds the plaintext bytes 00 11 22 33, and the matching key bytes are 00 01 02 03. Byte by byte: 𝟶𝟶⊕𝟶𝟶=𝟶𝟶\texttt{00} \oplus \texttt{00} = \texttt{00}; 𝟷𝟷⊕𝟶𝟷=𝟷𝟶\texttt{11} \oplus \texttt{01} = \texttt{10}, since in binary 𝟶𝟶𝟶𝟷𝟶𝟶𝟶𝟷⊕𝟶𝟶𝟶𝟶𝟶𝟶𝟶𝟷=𝟶𝟶𝟶𝟷𝟶𝟶𝟶𝟶\texttt{00010001} \oplus \texttt{00000001} = \texttt{00010000}; 𝟸𝟸⊕𝟶𝟸=𝟸𝟶\texttt{22} \oplus \texttt{02} = \texttt{20}; 𝟹𝟹⊕𝟶𝟹=𝟹𝟶\texttt{33} \oplus \texttt{03} = \texttt{30}. The other columns are treated the same way, and the whole state, read column by column, is now

00102030405060708090a0b0c0d0e0f0

SubBytes. The S-box gives 00 →\to 63, 10 →\to ca, 20 →\to b7 and 30 →\to 04. The whole state becomes

63cab7040953d051cd60e0e7ba70e18c

ShiftRows. Read by rows, that state is: row 0 63 09 cd ba, row 1 ca 53 60 70, row 2 b7 d0 e0 e1, row 3 04 51 e7 8c. Rotating row 1 left by one position gives 53 60 70 ca, row 2 by two gives e0 e1 b7 d0, and row 3 by three gives 8c 04 51 e7. The new column 0 is the first byte of each row: 63 53 e0 8c. Its four bytes come from four different columns of the previous state.

MixColumns. The products needed are: 𝟶𝟸⋅𝟼𝟹=𝚌𝟼\texttt{02} \cdot \texttt{63} = \texttt{c6}, since 01100011 shifted left is 11000110 and no 1 bit fell off; 𝟶𝟹⋅𝟼𝟹=𝚌𝟼⊕𝟼𝟹=𝚊𝟻\texttt{03} \cdot \texttt{63} = \texttt{c6} \oplus \texttt{63} = \texttt{a5}; 𝟶𝟸⋅𝟻𝟹=𝚊𝟼\texttt{02} \cdot \texttt{53} = \texttt{a6}; 𝟶𝟹⋅𝟻𝟹=𝚊𝟼⊕𝟻𝟹=𝚏𝟻\texttt{03} \cdot \texttt{53} = \texttt{a6} \oplus \texttt{53} = \texttt{f5}; 𝟶𝟸⋅𝚎𝟶=𝚍𝚋\texttt{02} \cdot \texttt{e0} = \texttt{db}, since 11100000 shifted left is 11000000 (c0) with a 1 bit fallen off, and 𝚌𝟶⊕𝟷𝚋=𝚍𝚋\texttt{c0} \oplus \texttt{1b} = \texttt{db}; 𝟶𝟹⋅𝚎𝟶=𝚍𝚋⊕𝚎𝟶=𝟹𝚋\texttt{03} \cdot \texttt{e0} = \texttt{db} \oplus \texttt{e0} = \texttt{3b}; 𝟶𝟸⋅𝟾𝚌=𝟷𝟾⊕𝟷𝚋=𝟶𝟹\texttt{02} \cdot \texttt{8c} = \texttt{18} \oplus \texttt{1b} = \texttt{03}, since 10001100 shifted left is 00011000 (18) with a 1 bit fallen off; 𝟶𝟹⋅𝟾𝚌=𝟶𝟹⊕𝟾𝚌=𝟾𝚏\texttt{03} \cdot \texttt{8c} = \texttt{03} \oplus \texttt{8c} = \texttt{8f}. The matrix rows are (02, 03, 01, 01), (01, 02, 03, 01), (01, 01, 02, 03) and (03, 01, 01, 02), so the new column is:

AddRoundKey with round key 1. The key expansion makes the first word of round key 1 from the last key word 0c 0d 0e 0f. Rotate it to 0d 0e 0f 0c, apply the S-box to each byte to get d7 ab 76 fe, XOR the round constant 01 00 00 00 to get d6 ab 76 fe, and XOR the first key word 00 01 02 03 to get d6 aa 74 fd. Then 𝟻𝚏⊕𝚍𝟼=𝟾𝟿\texttt{5f} \oplus \texttt{d6} = \texttt{89}, 𝟽𝟸⊕𝚊𝚊=𝚍𝟾\texttt{72} \oplus \texttt{aa} = \texttt{d8}, 𝟼𝟺⊕𝟽𝟺=𝟷𝟶\texttt{64} \oplus \texttt{74} = \texttt{10} and 𝟷𝟻⊕𝚏𝚍=𝚎𝟾\texttt{15} \oplus \texttt{fd} = \texttt{e8}. Column 0 after round 1 is 89 d8 10 e8, as printed in FIPS 197.

All rounds, three key lengths. Encrypt the same plaintext under three keys: the 128-bit key above, the 192-bit key whose 24 bytes are 00, 01, and so on up to 17, and the 256-bit key whose 32 bytes are 00 up to 1f. They take 10, 12 and 14 rounds and give these ciphertexts, printed in Appendix C of FIPS 197:

128-bit key: 69c4e0d86a7b0430d8cdb78070b4c55a
192-bit key: dda97ca4864cdfe06eaf70a0ec0d7191
256-bit key: 8ea2b7ca516745bfeafc49904b496089

All three ciphertexts are 16 bytes long. The round keys occupy 16×11=17616 \times 11 = 176, 16×13=20816 \times 13 = 208 and 16×15=24016 \times 15 = 240 bytes, and the 256-bit version does 14/10=1.414 / 10 = 1.4 times the round work of the 128-bit version.

Stated precisely

Let NkN_k be the key length in 32-bit words (4, 6 or 8) and NrN_r the number of rounds. FIPS 197 fixes Nr=Nk+6N_r = N_k + 6, which gives 10, 12 or 14. The key expansion produces 4(Nr+1)4(N_r + 1) words w0,w1,…w_0, w_1, \dots of 4 bytes each, and round key KrK_r is the four words w4r,…,w4r+3w_{4r}, \dots, w_{4r+3} joined in order. Writing ss for the state, encryption is s←AddRoundKey(input,K0),s←AddRoundKey(MixColumns(ShiftRows(SubBytes(s))),Kr)for r=1,…,Nr−1,output=AddRoundKey(ShiftRows(SubBytes(s)),KNr).\begin{aligned} s &\leftarrow \mathrm{AddRoundKey}(\text{input}, K_0), \\ s &\leftarrow \mathrm{AddRoundKey}(\mathrm{MixColumns}(\mathrm{ShiftRows}(\mathrm{SubBytes}(s))), K_r) \quad \text{for } r = 1, \dots, N_r - 1, \\ \text{output} &= \mathrm{AddRoundKey}(\mathrm{ShiftRows}(\mathrm{SubBytes}(s)), K_{N_r}). \end{aligned}

Key expansion. The first NkN_k words are the key. For Nk≤i<4(Nr+1)N_k \le i < 4(N_r + 1), wi=wi−Nk⊕Tiw_i = w_{i - N_k} \oplus T_i, where Ti={SubWord(RotWord(wi−1))⊕Rconi/Nkif i is a multiple of Nk,SubWord(wi−1)if Nk=8 and imod8=4,wi−1otherwise.T_i = \begin{cases} \mathrm{SubWord}(\mathrm{RotWord}(w_{i-1})) \oplus \mathrm{Rcon}_{i / N_k} & \text{if } i \text{ is a multiple of } N_k, \\ \mathrm{SubWord}(w_{i-1}) & \text{if } N_k = 8 \text{ and } i \bmod 8 = 4, \\ w_{i-1} & \text{otherwise.} \end{cases} Here RotWord\mathrm{RotWord} moves the first byte of a word to the end, SubWord\mathrm{SubWord} applies the S-box to each of the four bytes, and the round constant Rconj\mathrm{Rcon}_j is the word whose first byte is xj−1x^{j-1} computed in GF(28)\mathrm{GF}(2^8) (01, 02, 04, 08, 10, 20, 40, 80, 1b, 36) and whose other three bytes are 0. The middle case is used only with 256-bit keys.

The steps. SubBytes computes S(a)=Ma−1⊕𝟼𝟹S(a) = M a^{-1} \oplus \mathtt{63} for each byte aa, where a−1a^{-1} is the inverse of aa in GF(28)\mathrm{GF}(2^8), with 0−10^{-1} taken as 0, and MM is a fixed invertible 8-by-8 matrix of bits. ShiftRows moves the byte in row rr, column cc to row rr, column (c−r)mod4(c - r) \bmod 4. MixColumns maps each column (a0,a1,a2,a3)(a_0, a_1, a_2, a_3) to (b0b1b2b3)=(𝟶𝟸𝟶𝟹𝟶𝟷𝟶𝟷𝟶𝟷𝟶𝟸𝟶𝟹𝟶𝟷𝟶𝟷𝟶𝟷𝟶𝟸𝟶𝟹𝟶𝟹𝟶𝟷𝟶𝟷𝟶𝟸)(a0a1a2a3),\left(\begin{array}{c} b_0 \\ b_1 \\ b_2 \\ b_3 \end{array}\right) = \left(\begin{array}{cccc} \mathtt{02} & \mathtt{03} & \mathtt{01} & \mathtt{01} \\ \mathtt{01} & \mathtt{02} & \mathtt{03} & \mathtt{01} \\ \mathtt{01} & \mathtt{01} & \mathtt{02} & \mathtt{03} \\ \mathtt{03} & \mathtt{01} & \mathtt{01} & \mathtt{02} \end{array}\right) \left(\begin{array}{c} a_0 \\ a_1 \\ a_2 \\ a_3 \end{array}\right), with additions and multiplications in GF(28)\mathrm{GF}(2^8) modulo x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1. AddRoundKey computes s⊕Krs \oplus K_r.

What the design proves. AES has no proof of security. Its designers, Joan Daemen and Vincent Rijmen, who submitted it under the name Rijndael, proved a narrower fact about one family of attacks [520]. In differential cryptanalysis an attacker follows how a difference between two plaintexts propagates through the rounds. A path of differences through the rounds is called a trail, and an S-box is active in a trail when its input difference is nonzero. Every trail over four rounds of AES has at least 25 active S-boxes, and the S-box turns a given nonzero input difference into a given output difference with probability at most 4/256=2−64/256 = 2^{-6}. Every four-round trail therefore has probability at most (2−6)25=2−150(2^{-6})^{25} = 2^{-150}.

Exhaustive key search tries every key: 21282^{128}, 21922^{192} or 22562^{256} of them. The best published attack on the full cipher, the biclique attack of 2011 [366], needs about 21262^{126} operations for a 128-bit key, a gain of only a factor of about 4.

Limits and common mistakes

The key length changes neither the block length nor anything that depends on it. Every AES variant has 128-bit blocks, so the data that a mode may process under one key before block repeats become likely, about 2642^{64} blocks by the birthday bound §5.6, is the same for all three key lengths. Nonce §5.18 lengths and tag lengths are set by the mode, not by the key length.

A 256-bit key does not make a whole system 256-bit secure. A 128-bit tag, a short random nonce, or a key derived from a guessable secret sets the real limit.

The key schedules of AES-192 and AES-256 allow related-key attacks (Biryukov and Khovratovich, 2009 [367]): attacks that need encryptions under several keys whose relations the attacker chooses. A protocol that uses independently generated keys never gives an attacker such related keys; a design that makes keys by changing a few bits of one key would.

Versions with fewer rounds are not AES. Attacks far faster than exhaustive key search are published only for versions with fewer rounds, and they do not carry over to the full cipher.

An implementation can leak what the mathematics hides. Fast software often merges SubBytes, ShiftRows and MixColumns into lookups in tables indexed by secret bytes. The processor’s memory cache then reveals through timing which entries were read, and so reveals key bytes. Use dedicated processor instructions for AES or a constant-time implementation §5.36, code whose timing and memory accesses do not depend on secret data.

The state is filled column by column. An implementation that fills it row by row computes a different function; the test vectors in Appendix C of FIPS 197 catch such mistakes.

AES encrypts one block. Messages need a mode of operation, and the mode’s rules on nonces and on authentication §1.6 apply whatever the key length.

Questions answered

What changes when the AES key length changes?

When the key length of AES (Advanced Encryption Standard) changes, two things change: the number of rounds and the key expansion. Keys of 128, 192 or 256 bits give 10, 12 or 14 rounds. The key expansion, which turns the key into one 16-byte round key per round plus one more, reads 4, 6 or 8 four-byte words of key and produces 176, 208 or 240 bytes of round keys. For 256-bit keys the expansion also applies the S-box, the fixed byte-substitution table, to the previous word when computing words 12, 20, 28 and so on (counting the words from 0), the positions that are 4 more than a multiple of 8. Two consequences follow. Exhaustive key search must try about 21282^{128}, 21922^{192} or 22562^{256} keys, and encryption costs about 1.2 or 1.4 times as much with 192-bit or 256-bit keys as with 128-bit keys.

Nothing else changes. For every key length the block is 128 bits (16 bytes), and the state, the S-box, the four round steps and the ciphertext length stay the same. The mode of operation’s requirements on nonces and on the amount of data per key stay the same as well.

Related entries: Block cipher (§15.3), Authenticated encryption (AEAD) (§5.16), Constant-time cryptographic implementation (§5.36), Pseudorandom permutation (PRP) (§15.2), Finite field and modular inverse (§19.1.2). Further reading: [47, 65, 366, 367, 520].