Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Why a New Standard?

DES became the US standard in 1977 with a 56-bit key. By the 1990s, computers were fast enough to try every key. In 1998 the Electronic Frontier Foundation’s machine Deep Crack found a DES key by brute force in 56 hours. A replacement was needed.

This time the US National Institute of Standards and Technology (NIST) held an open competition.

  • 2 January 1997: NIST announced that it would choose a successor to DES, and on 12 September 1997 it asked for designs.

  • 1998: 15 designs from around the world were submitted and discussed in public.

  • August 1999: NIST chose five finalists: MARS, RC6, Rijndael, Serpent and Twofish.

  • 2 October 2000: NIST announced the winner, Rijndael, designed by the Belgian cryptographers Joan Daemen and Vincent Rijmen.

  • 26 November 2001: Rijndael was published as the Advanced Encryption Standard (AES), in the standard FIPS-197. It took effect on 26 May 2002.

The process was the opposite of DES’s. When DES was designed, the NSA changed the S-boxes and for years nobody outside knew why. For AES, every candidate was published, and anyone in the world could try to break them. Every part of AES comes from a published rule, so there’s nowhere to hide a back door. This is Kerckhoffs’s principle at work: the design is public, and only the key is secret.

AES is now everywhere: websites using HTTPS, Wi-Fi (WPA2 and WPA3), encrypted storage on phones and laptops, and messaging apps. Many processors made since about 2010 have AES instructions built in.

From S-AES to AES

AES uses the same four steps as Simplified AES, on a much bigger block.

S-AESAES
Block16 bits128 bits
Key16 bits128, 192 or 256 bits
Rounds210, 12 or 14 (for the three key sizes)
State2×2 grid of nibbles4×4 grid of bytes
S-box16 entries256 entries
Shift rowsswap the bottom rowshift the rows by 0, 1, 2 and 3 places
Mix columns1’s and 4’s, on nibbles1’s, 2’s and 3’s, on bytes
Last roundno mix columnsno mix columns

This page covers AES-128: a 128-bit key and 10 rounds. The code on this page is checked against the examples in the AES standard.

Bytes and the State

A byte is 8 bits, which is two hex digits. For example, 0011 0010 is 32. A 128-bit block is 16 bytes, or 32 hex digits.

The state is a 4×4 grid of bytes, filled down the columns, just like S-AES. Bytes 1 to 4 fill the first column from top to bottom, bytes 5 to 8 fill the second column, and so on.

Our example is the worked example in Appendix B of the AES standard, FIPS-197.

  • plaintext: 32 43 F6 A8 88 5A 30 8D 31 31 98 A2 E0 37 07 34

  • key: 2B 7E 15 16 28 AE D2 A6 AB F7 15 88 09 CF 4F 3C

In Python we’ll keep the state as a list of 16 bytes in that order. The byte in row rr and column cc is number r + 4 * c in the list.

32 88 31 E0
43 5A 31 37
F6 30 98 07
A8 8D A2 34

32 43 F6 A8 is the first column.

The Big Picture

plaintext→add K0→sub bytes→shift rows→mix columns→add Kr⏟rounds r=1 to 9→sub bytes→shift rows→add K10⏟round 10 (no mix columns)→ciphertext\text{plaintext} \rightarrow \text{add } K_0 \rightarrow \underbrace{\text{sub bytes} \rightarrow \text{shift rows} \rightarrow \text{mix columns} \rightarrow \text{add } K_r}_{\text{rounds } r = 1 \text{ to } 9} \rightarrow \underbrace{\text{sub bytes} \rightarrow \text{shift rows} \rightarrow \text{add } K_{10}}_{\text{round 10 (no mix columns)}} \rightarrow \text{ciphertext}

That’s nine full rounds plus a shorter last round. AES-128 uses eleven round keys, K0K_0 to K10K_{10}, all made from the one key. K0K_0 is the key itself.

Step 1: Add Round Key

XOR each byte of the state with the matching byte of the round key. To XOR two bytes, XOR their bits:

32=0011 0010⊕ 2B=0010 101119=0001 1001\begin{array}{rl} \texttt{32} = & \texttt{0011 0010} \\ \oplus\ \texttt{2B} = & \texttt{0010 1011} \\ \hline \texttt{19} = & \texttt{0001 1001} \end{array}

This is still the only step that uses the key.

19 A0 9A E9
3D F4 C6 F8
E3 E2 8D 48
BE 2B 2A 08

Bytes as Polynomials: GF(256)GF(256)

Sub bytes and mix columns both need arithmetic on bytes. It works just like the nibble arithmetic of S-AES, with 8 bits instead of 4.

A byte is a polynomial whose coefficients are its bits. For example, 57 = 0101 0111 =x6+x4+x2+x+1= x^6 + x^4 + x^2 + x + 1.

  • Adding is XOR, just as for nibbles.

  • Multiplying by xx (by 2) is a shift left.

  • The wrap-around rule changes. AES counts x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1 as zero, so x8=x4+x3+x+1x^8 = x^4 + x^3 + x + 1. In bits: if a 1 spills off the left of a byte, drop it and XOR 0001 1011 = 1B.

S-AES (nibbles)AES (bytes)
counts as zerox4+x+1x^4 + x + 1x8+x4+x3+x+1x^8 + x^4 + x^3 + x + 1
wrap rulex4=x+1x^4 = x + 1x8=x4+x3+x+1x^8 = x^4 + x^3 + x + 1
in bitsspill? XOR 0011spill? XOR 1B
nameGF(16)GF(16)GF(256)GF(256)

Some examples:

  • 2⋅572 \cdot \texttt{57}: 0101 0111 → 1010 1110. No spill, so the answer is AE.

  • 2⋅AE2 \cdot \texttt{AE}: 1010 1110 → 1 0101 1100. A 1 spilled, so drop it and XOR 1B: 0101 1100 ⊕ 0001 1011 = 0100 0111 = 47.

  • 3⋅573 \cdot \texttt{57}: since 3=2+13 = 2 + 1, multiply by 2 and then XOR the byte itself: AE ⊕ 57 = F9.

2·57 = AE,  2·AE = 47,  3·57 = F9

Step 2: Sub Bytes

Replace every byte with its entry in the 256-entry S-box. Read the table with the byte’s two hex digits: the first digit picks the row and the second picks the column. For example, 19 is in row 1, column 9, so S(19) = D4.

Where the S-box comes from

Unlike the S-AES S-box, the AES S-box isn’t just a list someone made up. It comes from a public rule with two steps.

  1. Replace the byte with its inverse in GF(256)GF(256): the byte you multiply it by to get 01. (The byte 00 has no inverse and stays 00.) For example, 53⋅CA=01\texttt{53} \cdot \texttt{CA} = \texttt{01}, so the inverse of 53 is CA.

  2. Apply a fixed mix of the bits, then XOR 63. This is called the affine step. Bit ii of the output is the XOR of bits ii, i+4i+4, i+5i+5, i+6i+6 and i+7i+7 of the input (counting around mod 8, with bit 0 on the right), and then bit ii of 63.

So S(53) = ED. Taking the inverse is about as far from linear as a function can be, so this is the confusion step. And because the recipe is public, anyone can rebuild the table and check that nothing is hidden in it. The code below builds the whole table from the rule.

inverse of 53 = CA,  S(53) = ED,  S(19) = D4

    0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
0  63 7C 77 7B F2 6B 6F C5 30 01 67 2B FE D7 AB 76
1  CA 82 C9 7D FA 59 47 F0 AD D4 A2 AF 9C A4 72 C0
2  B7 FD 93 26 36 3F F7 CC 34 A5 E5 F1 71 D8 31 15
3  04 C7 23 C3 18 96 05 9A 07 12 80 E2 EB 27 B2 75
4  09 83 2C 1A 1B 6E 5A A0 52 3B D6 B3 29 E3 2F 84
5  53 D1 00 ED 20 FC B1 5B 6A CB BE 39 4A 4C 58 CF
6  D0 EF AA FB 43 4D 33 85 45 F9 02 7F 50 3C 9F A8
7  51 A3 40 8F 92 9D 38 F5 BC B6 DA 21 10 FF F3 D2
8  CD 0C 13 EC 5F 97 44 17 C4 A7 7E 3D 64 5D 19 73
9  60 81 4F DC 22 2A 90 88 46 EE B8 14 DE 5E 0B DB
A  E0 32 3A 0A 49 06 24 5C C2 D3 AC 62 91 95 E4 79
B  E7 C8 37 6D 8D D5 4E A9 6C 56 F4 EA 65 7A AE 08
C  BA 78 25 2E 1C A6 B4 C6 E8 DD 74 1F 4B BD 8B 8A
D  70 3E B5 66 48 03 F6 0E 61 35 57 B9 86 C1 1D 9E
E  E1 F8 98 11 69 D9 8E 94 9B 1E 87 E9 CE 55 28 DF
F  8C A1 89 0D BF E6 42 68 41 99 2D 0F B0 54 BB 16
D4 E0 B8 1E
27 BF B4 41
11 98 5D 52
AE F1 E5 30

Step 3: Shift Rows

Row rr shifts to the left by rr places, wrapping around. The top row (row 0) stays put, row 1 moves 1 place, row 2 moves 2 places and row 3 moves 3 places. Afterwards, each column holds one byte from every original column.

D4 E0 B8 1E
BF B4 41 27
5D 52 11 98
30 AE F1 E5

Step 4: Mix Columns

For each column, with bytes aa, bb, cc, dd from top to bottom:

new a=2a⊕3b⊕c⊕dnew b=a⊕2b⊕3c⊕dnew c=a⊕b⊕2c⊕3dnew d=3a⊕b⊕c⊕2dor[2311123111233112]\begin{aligned} \text{new } a &= 2a \oplus 3b \oplus c \oplus d \\ \text{new } b &= a \oplus 2b \oplus 3c \oplus d \\ \text{new } c &= a \oplus b \oplus 2c \oplus 3d \\ \text{new } d &= 3a \oplus b \oplus c \oplus 2d \end{aligned} \qquad\text{or}\qquad \begin{bmatrix} 2 & 3 & 1 & 1 \\ 1 & 2 & 3 & 1 \\ 1 & 1 & 2 & 3 \\ 3 & 1 & 1 & 2 \end{bmatrix}

Each row of the matrix is the one above it, shifted one place to the right.

The first column after shift rows is D4 BF 5D 30. Its new top byte is

2⋅D4⊕3⋅BF⊕5D⊕302 \cdot \texttt{D4} \oplus 3 \cdot \texttt{BF} \oplus \texttt{5D} \oplus \texttt{30}
  • 2⋅D42 \cdot \texttt{D4}: 1101 0100 → 1 1010 1000, a 1 spilled, so XOR 1B to get 1011 0011 = B3.

  • 3⋅BF=2⋅BF⊕BF=65⊕BF=DA3 \cdot \texttt{BF} = 2 \cdot \texttt{BF} \oplus \texttt{BF} = \texttt{65} \oplus \texttt{BF} = \texttt{DA}.

  • B3⊕DA⊕5D⊕30\texttt{B3} \oplus \texttt{DA} \oplus \texttt{5D} \oplus \texttt{30} = 0000 0100 = 04.

The whole column becomes 04 66 81 E5.

04 E0 48 28
66 CB F8 06
81 19 D3 26
E5 9A 7A 4C

Mix columns spreads a change in one byte through its whole column. Shift rows then sends the bytes of that column to four different columns, so in the next round the change spreads everywhere.

The Key Schedule

The key schedule works with words of 4 bytes (one column). The key gives the first four words, w0w_0 to w3w_3, which make up K0K_0. Each new word is

wi=wi−4⊕wi−1w_i = w_{i-4} \oplus w_{i-1}

except at the start of each round key (when ii is a multiple of 4), where

wi=wi−4⊕g(wi−1)w_i = w_{i-4} \oplus g(w_{i-1})

AES-128 needs 11 round keys of 4 words each, so 44 words, w0w_0 to w43w_{43}. Round key KrK_r is w4rw_{4r} to w4r+3w_{4r+3}.

The function gg does three things to a word.

The round constants are 01, 02, 04, 08, 10, 20, 40, 80, 1B, 36. Each one is the last one times 2. After 80, a bit spills off, so the next one is 1B, and then 36.

round constants: 01 02 04 08 10 20 40 80 1B 36

For the example key, here is round key K1K_1, the words w4w_4 to w7w_7.

StepWord (hex)
Start: w3w_309 CF 4F 3C
Rotate (first byte to the end)CF 4F 3C 09
Substitute each byte8A 84 EB 01
XOR the round constant01 00 00 00
g(w3)g(w_3)8B 84 EB 01
w4=w0⊕g(w3)w_4 = w_0 \oplus g(w_3)A0 FA FE 17
w5=w4⊕w1w_5 = w_4 \oplus w_188 54 2C B1
w6=w5⊕w2w_6 = w_5 \oplus w_223 A3 39 39
w7=w6⊕w3w_7 = w_6 \oplus w_32A 6C 76 05
g(w3): 8B 84 EB 01
K0  2B 7E 15 16 28 AE D2 A6 AB F7 15 88 09 CF 4F 3C
K1  A0 FA FE 17 88 54 2C B1 23 A3 39 39 2A 6C 76 05
K2  F2 C2 95 F2 7A 96 B9 43 59 35 80 7A 73 59 F6 7F
K3  3D 80 47 7D 47 16 FE 3E 1E 23 7E 44 6D 7A 88 3B
K4  EF 44 A5 41 A8 52 5B 7F B6 71 25 3B DB 0B AD 00
K5  D4 D1 C6 F8 7C 83 9D 87 CA F2 B8 BC 11 F9 15 BC
K6  6D 88 A3 7A 11 0B 3E FD DB F9 86 41 CA 00 93 FD
K7  4E 54 F7 0E 5F 5F C9 F3 84 A6 4F B2 4E A6 DC 4F
K8  EA D2 73 21 B5 8D BA D2 31 2B F5 60 7F 8D 29 2F
K9  AC 77 66 F3 19 FA DC 21 28 D1 29 41 57 5C 00 6E
K10 D0 14 F9 A8 C9 EE 25 89 E1 3F 0C C8 B6 63 0C A6

The Whole Cipher

Putting the steps together gives AES-128.

plaintext  32 43 F6 A8 88 5A 30 8D 31 31 98 A2 E0 37 07 34
add K0     19 3D E3 BE A0 F4 E2 2B 9A C6 8D 2A E9 F8 48 08
round 1    A4 9C 7F F2 68 9F 35 2B 6B 5B EA 43 02 6A 50 49
round 2    AA 8F 5F 03 61 DD E3 EF 82 D2 4A D2 68 32 46 9A
round 3    48 6C 4E EE 67 1D 9D 0D 4D E3 B1 38 D6 5F 58 E7
round 4    E0 92 7F E8 C8 63 63 C0 D9 B1 35 50 85 B8 BE 01
round 5    F1 00 6F 55 C1 92 4C EF 7C C8 8B 32 5D B5 D5 0C
round 6    26 0E 2E 17 3D 41 B7 7D E8 64 72 A9 FD D2 8B 25
round 7    5A 41 42 B1 19 49 DC 1F A3 E0 19 65 7A 8C 04 0C
round 8    EA 83 5C F0 04 45 33 2D 65 5D 98 AD 85 96 B0 C5
round 9    EB 40 F2 1E 59 2E 38 84 8B A1 13 E7 1B C3 42 D2
round 10   39 25 84 1D 02 DC 09 FB DC 11 85 97 19 6A 0B 32

These match the round-by-round values in Appendix B of FIPS-197, which lists each one as the input to the next round, and the ciphertext matches the standard’s answer, 39 25 84 1D 02 DC 09 FB DC 11 85 97 19 6A 0B 32. The standard has a second example in its Appendix C.1, which we can check too.

Appendix B matches:   True
Appendix C.1 matches: True

Decrypting AES

Like S-AES, and unlike BabyBlock, every step of AES needs its own undo. Decryption runs the steps backwards, with the round keys from K10K_{10} down to K0K_0.

  • Add round key: XOR the same round key again.

  • Shift rows: shift right by 0, 1, 2 and 3 places.

  • Sub bytes: use the inverse S-box.

  • Mix columns: use the inverse matrix, which has the entries 0E, 0B, 0D and 09 in place of 2, 3, 1 and 1.

'32 43 F6 A8 88 5A 30 8D 31 31 98 A2 E0 37 07 34'

How Fast Does a Change Spread?

Flip one bit of the plaintext and count how many bits of the state are different at the end of each round. The code below does this for all 128 bit positions of 10 random plaintexts, with the example key.

after           K0    R1    R2    R3    R4    R5    R6    R7    R8    R9   R10
bits changed     1    16    64    64    64    64    64    64    64    64    64

After round 1, one flipped bit has spread through one column, about 16 bits. After round 2 it has reached every byte, and about 64 of the 128 bits (half) are different. It stays at about half from then on.

So AES reaches full diffusion after just 2 rounds. The other 8 rounds are a safety margin. Cryptanalysts have found attacks on shortened versions of AES, but the best ones only work on about 7 rounds of AES-128. The full 10 rounds leave room to spare.

How Strong Is AES-128?

There are 2128 keys. Suppose a computer could test a billion billion (1018) keys every second.

number of keys:            3.40e+38
years to try them all:     1.08e+13
times the age of universe: 781

Trying every key would take about 1013 years, roughly 780 times the age of the universe. AES-192 and AES-256 add even more margin.

  • The best known attack. In 2011, Andrey Bogdanov, Dmitry Khovratovich and Christian Rechberger found an attack on the full AES-128, called a biclique attack. It needs about 2126 steps. That’s only about 4 times faster than trying every key, so it’s still impossible in practice.

  • Quantum computers. Grover’s algorithm could, in theory, search 2128 keys in about 264 steps. But those steps have to run one after another, and spreading the work over many quantum computers helps much less than it does for ordinary computers. Experts expect AES-128 to stay safe. In fact, NIST uses “at least as hard to break as AES-128” as the lowest security level for its new quantum-resistant standards.

AES still needs a mode

AES encrypts one 16-byte block. A longer message still needs a mode of operation. ECB mode leaks repeated blocks no matter how strong the cipher is. Real systems use CBC, counter mode (CTR) or GCM, and GCM adds a tag that catches tampering.