Block Ciphers and AES
Almost all symmetric encryption today starts with one function: AES, the Advanced Encryption Standard. It is a block cipher: it encrypts exactly 16 bytes at a time under a key. On its own that is not enough to encrypt a file or a network stream (that is what modes are for, next lesson), but everything else is built on its one guarantee: without the key, AES looks like a random permutation of 16-byte blocks.
What a block cipher is
A block cipher is a pair of functions E(K, P) and D(K, C) on fixed-size blocks, with D(K, E(K, P)) = P. For each key it is a permutation: every one of the 2^128 possible 16-byte inputs maps to a different output. The security goal is that it behaves like a pseudorandom permutation: an attacker who can ask for encryptions and decryptions of blocks of their choice can't tell E(K, ·) from a permutation picked at random, unless they find the key, which should take about 2^128 work for AES-128.
| AES-128 | AES-192 | AES-256 | |
|---|---|---|---|
| key | 16 bytes | 24 bytes | 32 bytes |
| block | 16 bytes | 16 bytes | 16 bytes |
| rounds | 10 | 12 | 14 |
AES is the algorithm Rijndael by Joan Daemen and Vincent Rijmen, selected by NIST in an open competition (1997–2000) and published as FIPS 197 in 2001 (updated in 2023 without changing the algorithm). It has been attacked publicly for a quarter century; the best known key-recovery attacks on full AES are only slightly faster than brute force and nowhere near practical.
Inside a round, at a high level
AES arranges the 16 bytes as a 4 × 4 grid, the state, and applies ten (or 12, 14) rounds of four steps:
- SubBytes replaces every byte through a fixed 256-entry table, the S-box, built from inversion in the field GF(2^8). It is the only non-linear step, and it is what defeats linear and differential cryptanalysis (confusion).
- ShiftRows rotates row r of the grid left by r positions, so bytes from one column spread to all four.
- MixColumns multiplies each column by a fixed matrix over GF(2^8), so every output byte of a column depends on all four input bytes (diffusion). After two rounds every state byte depends on every input byte.
- AddRoundKey XORs in a 16-byte round key derived from the cipher key by the key schedule.
The field arithmetic is worked through in AES and the Field GF(2^8). You never implement these steps yourself in production; the point is to know why AES is designed this way and what the round count buys: a few rounds can be broken, so the standard has a safety margin.
One block, checked against the standard
The test below encrypts the plaintext of FIPS 197 Appendix C.1 with its published test key and compares the result. Using modes.ECB() for one block is the standard way to call the raw block function in cryptography; using it for more than one block is the mistake of the next lesson.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
key = bytes.fromhex("000102030405060708090a0b0c0d0e0f") # FIPS-197 Appendix C.1 test key
pt = bytes.fromhex("00112233445566778899aabbccddeeff")
def enc_block(k, block):
e = Cipher(algorithms.AES(k), modes.ECB()).encryptor() # one raw block: ECB is fine for ONE block
return e.update(block) + e.finalize()
def dec_block(k, block):
d = Cipher(algorithms.AES(k), modes.ECB()).decryptor()
return d.update(block) + d.finalize()
ct = enc_block(key, pt)
print("AES-128(K, P) =", ct.hex())
print("matches FIPS-197:", ct.hex() == "69c4e0d86a7b0430d8cdb78070b4c55a")
print("decrypts back :", dec_block(key, ct) == pt)
def bits_diff(a, b): return sum(bin(x ^ y).count("1") for x, y in zip(a, b))
pt2 = bytearray(pt); pt2[15] ^= 1
k2 = bytearray(key); k2[0] ^= 0x80
print("1 plaintext bit flipped ->", bits_diff(ct, enc_block(key, bytes(pt2))), "of 128 output bits change")
print("1 key bit flipped ->", bits_diff(ct, enc_block(bytes(k2), pt)), "of 128 output bits change")
for ks in (16, 24, 32):
print(f"AES-{ks*8}: {ks}-byte key, rounds = {ks//4 + 6}")AES-128(K, P) = 69c4e0d86a7b0430d8cdb78070b4c55a
matches FIPS-197: True
decrypts back : True
1 plaintext bit flipped -> 62 of 128 output bits change
1 key bit flipped -> 63 of 128 output bits change
AES-128: 16-byte key, rounds = 10
AES-192: 24-byte key, rounds = 12
AES-256: 32-byte key, rounds = 14
Flipping a single input bit or key bit changes about half of the 128 output bits (62 and 63 here, where a random permutation would average 64). That is diffusion measured: the output gives no hint of which input bit changed.
Testing against published vectors like this is how implementations are validated (NIST's CAVP programme runs thousands of them), and it is the first thing to check when an embedded device ships "its own AES".
AES-128 or AES-256?
Both are unbroken. AES-128 has a 128-bit security level against classical attacks. Against a future quantum computer, Grover's algorithm would in theory reduce brute force to about the square root of the key space, so AES-256 keeps a comfortable margin; that is why the NSA's CNSA 2.0 suite requires AES-256 and some national post-quantum plans recommend it. NIST itself still rates AES-128 as post-quantum security category 1 (NIST IR 8547), because Grover's search parallelises badly. AES-256 costs about 40 % more rounds (14 versus 10), negligible on CPUs with AES instructions (AES-NI on x86, the ARMv8 crypto extensions).
Where AES implementations fail
The algorithm is sound; implementations can leak.
- Timing and cache side channels: table-based software AES looks up S-box entries at addresses that depend on key bytes; an attacker sharing the CPU cache can measure which lines were touched (Bernstein's 2005 cache-timing attack, later attacks across virtual machines). Hardware AES instructions and bit-sliced software implementations run in constant time.
- Power and electromagnetic analysis on smart cards and microcontrollers: measuring power draw during the first round recovers key bytes. Countermeasures are masking and hardware designed for it.
- Fault injection: glitching the clock or voltage during the last rounds produces faulty ciphertexts from which the key can be computed.
How weak block-cipher use is detected
- Configuration and code review: anything other than AES (or ChaCha20) for new designs; key sizes; a "custom AES" or hand-written S-box tables in application code.
- Known-answer tests: run the FIPS 197 vectors against the implementation, as above.
- Platform checks: whether the library uses hardware AES (
grep -o aes /proc/cpuinfoon x86 Linux; OpenSSL'sopenssl speed -evp aes-128-gcmshows the accelerated speed).
How it is fixed
- Use AES through a maintained library (OpenSSL, BoringSSL, libsodium,
cryptography), which uses constant-time hardware paths. - Never call the raw block function for data; always use an authenticated mode (AES-GCM) or ChaCha20-Poly1305.
- Use AES-256 where policy or long-term confidentiality requires it; AES-128 remains fine for most current uses.
Pitfalls
- Treating "AES-256" as a security guarantee on its own: the mode, the key source and the nonce handling decide the real security.
- Confusing key size with block size: all three AES variants have 128-bit blocks.
- Writing AES yourself for a product. It is a good exercise, and a bad product, because of side channels.
Practice
1. A device vendor says its firmware implements AES-128 in 60 lines of C with lookup tables. What do you test first?
Correctness with the FIPS 197 known-answer tests, then timing behaviour: table-based AES is vulnerable to cache-timing attacks if other code shares the CPU. Ask whether a hardware AES engine or a constant-time implementation is available.
2. How many rounds does AES-256 use, and how many bytes is its block?
14 rounds; the block is 16 bytes, as for every AES variant.
3. In the example, change k2[0] ^= 0x80 to flip a different key bit. What count do you expect?
Around 64 (anything from roughly 50 to 78 is normal for a single block): every key bit affects every output bit after a few rounds.
Test yourself
What is the block size of AES-256?
- 32 bytes
- 16 bytes
- 8 bytes
- It depends on the mode
Which AES step is the only non-linear one?
- ShiftRows
- MixColumns
- SubBytes
- AddRoundKey
Flipping one plaintext bit changed 62 of 128 output bits. What does this show?
- A bug in AES
- Diffusion: about half the output bits change, as for a random permutation
- That the key is weak
- That only the last byte matters
What is the most common real-world weakness of software AES on shared hardware?
- Too few rounds
- Cache-timing side channels in table-based implementations
- The 128-bit block size
- The key schedule is public