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-AES | AES | |
|---|---|---|
| Block | 16 bits | 128 bits |
| Key | 16 bits | 128, 192 or 256 bits |
| Rounds | 2 | 10, 12 or 14 (for the three key sizes) |
| State | 2×2 grid of nibbles | 4×4 grid of bytes |
| S-box | 16 entries | 256 entries |
| Shift rows | swap the bottom row | shift the rows by 0, 1, 2 and 3 places |
| Mix columns | 1’s and 4’s, on nibbles | 1’s, 2’s and 3’s, on bytes |
| Last round | no mix columns | no 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 34key:
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 and column is number r + 4 * c in the list.
def from_hex(text):
# '32 43 F6 ...' -> a list of 16 bytes
return list(bytes.fromhex(text))
def to_hex(state):
return ' '.join(f'{byte:02X}' for byte in state)
def print_state(state):
for row in range(4):
print(' '.join(f'{state[row + 4 * column]:02X}' for column in range(4)))
plaintext = from_hex('32 43 F6 A8 88 5A 30 8D 31 31 98 A2 E0 37 07 34')
key = from_hex('2B 7E 15 16 28 AE D2 A6 AB F7 15 88 09 CF 4F 3C')
print_state(plaintext)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¶
That’s nine full rounds plus a shorter last round. AES-128 uses eleven round keys, to , all made from the one key. 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:
This is still the only step that uses the key.
def add_round_key(state, round_key):
return [state[i] ^ round_key[i] for i in range(16)]
state = add_round_key(plaintext, key)
print_state(state)19 A0 9A E9
3D F4 C6 F8
E3 E2 8D 48
BE 2B 2A 08
Bytes as Polynomials: ¶
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 .
Adding is XOR, just as for nibbles.
Multiplying by (by 2) is a shift left.
The wrap-around rule changes. AES counts as zero, so . 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 zero | ||
| wrap rule | ||
| in bits | spill? XOR 0011 | spill? XOR 1B |
| name |
Some examples:
:
0101 0111→1010 1110. No spill, so the answer isAE.:
1010 1110→1 0101 1100. A 1 spilled, so drop it and XOR1B:0101 1100⊕0001 1011=0100 0111=47.: since , multiply by 2 and then XOR the byte itself:
AE⊕57=F9.
def xtime(byte):
# Multiply a byte by 2 (by x): shift left, and if a 1 spills off the left, drop it and XOR 1B
byte = byte << 1
if byte & 0x100:
byte = (byte & 0xFF) ^ 0x1B
return byte
def gf_multiply(a, b):
# Multiply two bytes: add (XOR) a, 2a, 4a, ... for each 1 bit of b
result = 0
while b > 0:
if b & 1:
result ^= a
a = xtime(a)
b = b >> 1
return result
print(f'2·57 = {xtime(0x57):02X}, 2·AE = {xtime(0xAE):02X}, 3·57 = {gf_multiply(3, 0x57):02X}')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.
Replace the byte with its inverse in : the byte you multiply it by to get
01. (The byte00has no inverse and stays00.) For example, , so the inverse of53isCA.Apply a fixed mix of the bits, then XOR
63. This is called the affine step. Bit of the output is the XOR of bits , , , and of the input (counting around mod 8, with bit 0 on the right), and then bit of63.
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.
def gf_inverse(byte):
if byte == 0:
return 0
for candidate in range(1, 256):
if gf_multiply(byte, candidate) == 1:
return candidate
def bit_at(byte, i):
return (byte >> (i % 8)) & 1
def s_box_entry(byte):
inverse = gf_inverse(byte)
result = 0
for i in range(8):
bit = (bit_at(inverse, i) ^ bit_at(inverse, i + 4) ^ bit_at(inverse, i + 5)
^ bit_at(inverse, i + 6) ^ bit_at(inverse, i + 7) ^ bit_at(0x63, i))
result = result | (bit << i)
return result
S_BOX = [s_box_entry(byte) for byte in range(256)]
print(f'inverse of 53 = {gf_inverse(0x53):02X}, S(53) = {S_BOX[0x53]:02X}, S(19) = {S_BOX[0x19]:02X}')
print()
print(' ' + ' '.join(f'{column:X}' for column in range(16)))
for row in range(16):
print(f'{row:X} ' + ' '.join(f'{S_BOX[16 * row + column]:02X}' for column in range(16)))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
def sub_bytes(state, box=S_BOX):
return [box[byte] for byte in state]
state = sub_bytes(state)
print_state(state)D4 E0 B8 1E
27 BF B4 41
11 98 5D 52
AE F1 E5 30
Step 3: Shift Rows¶
Row shifts to the left by 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.
def shift_rows(state):
# The byte in row r, column c comes from row r, column c + r
new_state = [0] * 16
for row in range(4):
for column in range(4):
new_state[row + 4 * column] = state[row + 4 * ((column + row) % 4)]
return new_state
state = shift_rows(state)
print_state(state)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 , , , from top to bottom:
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
:
1101 0100→1 1010 1000, a 1 spilled, so XOR1Bto get1011 0011=B3..
=
0000 0100=04.
The whole column becomes 04 66 81 E5.
MIX = [[2, 3, 1, 1],
[1, 2, 3, 1],
[1, 1, 2, 3],
[3, 1, 1, 2]]
def mix_columns(state, matrix=MIX):
new_state = []
for column in range(4):
old = state[4 * column: 4 * column + 4]
for row in range(4):
byte = 0
for k in range(4):
byte ^= gf_multiply(matrix[row][k], old[k])
new_state.append(byte)
return new_state
state = mix_columns(state)
print_state(state)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, to , which make up . Each new word is
except at the start of each round key (when is a multiple of 4), where
AES-128 needs 11 round keys of 4 words each, so 44 words, to . Round key is to .
The function 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 = [0x01]
while len(ROUND_CONSTANTS) < 10:
ROUND_CONSTANTS.append(xtime(ROUND_CONSTANTS[-1]))
print('round constants:', to_hex(ROUND_CONSTANTS))
def g(word, round_number):
rotated = word[1:] + word[:1]
substituted = [S_BOX[byte] for byte in rotated]
substituted[0] ^= ROUND_CONSTANTS[round_number - 1]
return substituted
def key_expansion(key):
# Returns the 11 round keys K0 to K10, each a list of 16 bytes
words = [key[4 * i: 4 * i + 4] for i in range(4)]
for i in range(4, 44):
previous = words[i - 1]
if i % 4 == 0:
previous = g(previous, i // 4)
words.append([words[i - 4][j] ^ previous[j] for j in range(4)])
return [words[4 * r] + words[4 * r + 1] + words[4 * r + 2] + words[4 * r + 3] for r in range(11)]round constants: 01 02 04 08 10 20 40 80 1B 36
For the example key, here is round key , the words to .
| Step | Word (hex) |
|---|---|
| Start: | 09 CF 4F 3C |
| Rotate (first byte to the end) | CF 4F 3C 09 |
| Substitute each byte | 8A 84 EB 01 |
| XOR the round constant | 01 00 00 00 |
| 8B 84 EB 01 | |
| A0 FA FE 17 | |
| 88 54 2C B1 | |
| 23 A3 39 39 | |
| 2A 6C 76 05 |
round_keys = key_expansion(key)
print('g(w3):', to_hex(g(key[12:16], 1)))
for r in range(11):
print(f'K{r:<2}', to_hex(round_keys[r]))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.
def aes_encrypt(plaintext, key, trace=False):
round_keys = key_expansion(key)
state = add_round_key(plaintext, round_keys[0])
if trace:
print(f'{"plaintext":10}', to_hex(plaintext))
print(f'{"add K0":10}', to_hex(state))
for r in range(1, 11):
state = sub_bytes(state)
state = shift_rows(state)
if r < 10: # the last round has no mix columns
state = mix_columns(state)
state = add_round_key(state, round_keys[r])
if trace:
print(f'{"round " + str(r):10}', to_hex(state))
return state
ciphertext = aes_encrypt(plaintext, key, trace=True)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.
print('Appendix B matches: ', to_hex(ciphertext) == '39 25 84 1D 02 DC 09 FB DC 11 85 97 19 6A 0B 32')
test = aes_encrypt(from_hex('00112233445566778899aabbccddeeff'), from_hex('000102030405060708090a0b0c0d0e0f'))
print('Appendix C.1 matches:', to_hex(test) == '69 C4 E0 D8 6A 7B 04 30 D8 CD B7 80 70 B4 C5 5A')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 down to .
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,0Dand09in place of 2, 3, 1 and 1.
INVERSE_S_BOX = [S_BOX.index(byte) for byte in range(256)]
INVERSE_MIX = [[0x0E, 0x0B, 0x0D, 0x09],
[0x09, 0x0E, 0x0B, 0x0D],
[0x0D, 0x09, 0x0E, 0x0B],
[0x0B, 0x0D, 0x09, 0x0E]]
def inverse_shift_rows(state):
# The byte in row r, column c goes back to row r, column c + r
new_state = [0] * 16
for row in range(4):
for column in range(4):
new_state[row + 4 * ((column + row) % 4)] = state[row + 4 * column]
return new_state
def aes_decrypt(ciphertext, key):
round_keys = key_expansion(key)
state = add_round_key(ciphertext, round_keys[10])
for r in range(9, -1, -1):
state = inverse_shift_rows(state)
state = sub_bytes(state, INVERSE_S_BOX)
state = add_round_key(state, round_keys[r])
if r > 0:
state = mix_columns(state, INVERSE_MIX)
return state
to_hex(aes_decrypt(ciphertext, key))'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.
import random
random.seed(4200)
def round_states(plaintext, key):
# The state after add K0 and at the end of each of the 10 rounds
round_keys = key_expansion(key)
state = add_round_key(plaintext, round_keys[0])
states = [state]
for r in range(1, 11):
state = shift_rows(sub_bytes(state))
if r < 10:
state = mix_columns(state)
state = add_round_key(state, round_keys[r])
states.append(state)
return states
def bits_different(state1, state2):
return sum(bin(a ^ b).count('1') for a, b in zip(state1, state2))
totals = [0] * 11
trials = 0
for _ in range(10):
p = [random.randrange(256) for _ in range(16)]
states = round_states(p, key)
for bit in range(128):
q = p.copy()
q[bit // 8] ^= 1 << (bit % 8)
flipped = round_states(q, key)
for i in range(11):
totals[i] += bits_different(states[i], flipped[i])
trials += 1
print('after ', ' '.join(f'{name:>4}' for name in ['K0'] + [f'R{r}' for r in range(1, 11)]))
print('bits changed ', ' '.join(f'{total / trials:4.0f}' for total in totals))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.
keys = 2 ** 128
seconds = keys / 10**18
years = seconds / (365.25 * 24 * 60 * 60)
age_of_universe = 13.8e9 # years
print(f'number of keys: {keys:.2e}')
print(f'years to try them all: {years:.2e}')
print(f'times the age of universe: {years / age_of_universe:.0f}')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.