Block Cipher Modes
How to encrypt more than one block — ECB and the penguin problem, CBC with IVs and padding, CTR mode, GCM, nonce requirements and what happens when they're reused, padding oracle attacks, and XTS for disk encryption.
A block cipher like AES encrypts exactly one 128-bit block. A mode of operation defines how to encrypt messages of any length securely. The choice of mode matters as much as the cipher: AES in the wrong mode is insecure.
ECB: electronic codebook — don't
Encrypt each block independently with the same key:
Identical plaintext blocks produce identical ciphertext blocks, so patterns survive encryption. The famous demonstration: encrypt a bitmap of the Linux penguin with AES-ECB, and the penguin is still clearly visible. ECB also lets attackers reorder, delete or replay blocks undetected. Never use ECB for data (it's a common default in old APIs — e.g. Java's Cipher.getInstance("AES") historically defaulted to ECB).
CBC: cipher block chaining
XOR each plaintext block with the previous ciphertext block before encrypting; the first block uses a random initialisation vector (IV):
- Identical plaintexts now encrypt differently, as long as the IV is random and unpredictable for each message (it can be sent in the clear with the ciphertext).
- Encryption is sequential (each block depends on the previous); decryption can be parallel.
- Messages must be padded to a whole number of blocks (PKCS#7: append n bytes of value n).
- CBC provides no integrity, and that has led to devastating attacks.
The padding oracle attack
If a server decrypts attacker-supplied CBC ciphertext and reveals — through an error message, a status code, or even response timing — whether the padding was valid, an attacker can decrypt the entire message byte by byte (about 128 requests per byte) by tweaking the previous ciphertext block. This broke ASP.NET (2010), and variants (POODLE, Lucky Thirteen) hit SSL/TLS. The lesson: authenticate before you decrypt (encrypt-then-MAC) or, better, use an AEAD mode.
CTR: counter mode
Turn the block cipher into a stream cipher: encrypt successive counter values to produce a keystream, and XOR it with the plaintext:
| Pros | Cons |
|---|---|
| Fully parallel encryption and decryption; random access to any block | Nonce reuse is catastrophic: same keystream → |
| No padding needed | No integrity — bit flips in ciphertext flip the same plaintext bits |
| Only the encryption direction of the cipher is needed |
The malleability is dangerous: if an attacker knows a message says pay 0100 at a certain offset, they can XOR the ciphertext to make it decrypt as pay 9100 without knowing the key.
GCM: Galois/counter mode — the default
AES-GCM = CTR-mode encryption + a GHASH authenticator computed over the ciphertext and any associated data (headers, IDs that should be authenticated but not encrypted). It produces a 128-bit authentication tag; decryption verifies the tag before releasing any plaintext.
Inputs: key, 96-bit nonce, plaintext, associated data
Output: ciphertext (same length as plaintext) + 16-byte tagGCM is an AEAD (authenticated encryption with associated data) — confidentiality and integrity in one primitive (Authenticated encryption). It's fast (parallel, hardware-accelerated with AES-NI and carry-less multiply) and used in TLS, SSH, IPsec, and cloud encryption.
GCM's sharp edge: nonces
- The nonce must never repeat under the same key. A repeated nonce reveals the XOR of plaintexts and lets attackers recover the authentication key and forge messages.
- 96-bit random nonces are safe for about 2³² messages per key; beyond that, collisions become likely. Use a counter, rotate keys, or use XChaCha20-Poly1305 (192-bit nonces, safe to pick randomly) or the nonce-misuse-resistant AES-GCM-SIV.
Choosing a mode
| Mode | Integrity | Parallel | Nonce/IV requirement | Use |
|---|---|---|---|---|
| ECB | No | Yes | — | Never for data |
| CBC | No | Decrypt only | Random, unpredictable IV | Legacy; only with encrypt-then-MAC |
| CTR | No | Yes | Unique nonce | Building block; add a MAC |
| GCM | Yes | Yes | Unique nonce (never repeat) | Default for most uses |
| ChaCha20-Poly1305 | Yes | — | Unique nonce | Default without AES hardware |
| GCM-SIV | Yes | Yes | Nonce reuse only leaks equality | When unique nonces are hard to guarantee |
| XTS | No (but no expansion) | Yes | Sector number as tweak | Full-disk encryption (BitLocker, FileVault, LUKS) |
Disk encryption is a special case: ciphertext must be exactly the same size as each sector, leaving no room for an IV or tag. XTS uses the sector number as a "tweak" so identical sectors encrypt differently. It doesn't detect tampering — the threat model is a stolen laptop, not an active attacker.
Practical rules
# ✓ What you should actually write: an AEAD from a vetted library
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
aead = AESGCM(key)
nonce = os.urandom(12)
blob = nonce + aead.encrypt(nonce, plaintext, associated_data) # store nonce alongside ciphertext
# decrypt
nonce, ct = blob[:12], blob[12:]
plaintext = aead.decrypt(nonce, ct, associated_data) # raises if tampered- Use an AEAD mode — GCM or ChaCha20-Poly1305 — by default.
- Never reuse a nonce with the same key; store the nonce with the ciphertext.
- Never reveal why decryption failed (padding vs MAC) — return one generic error.
- Bind context with associated data (user ID, record ID, version) so ciphertexts can't be swapped between records.
- Modes extend a block cipher to arbitrary-length messages; the mode determines security.
- ECB leaks patterns (the penguin) — never use it.
- CBC needs random IVs and padding and has no integrity — vulnerable to padding oracle attacks.
- CTR turns a block cipher into a parallel stream cipher; nonce reuse is catastrophic and ciphertexts are malleable.
- GCM (and ChaCha20-Poly1305) are AEADs: encryption plus authentication — the default choice.
- XTS is for disk sectors; GCM-SIV or XChaCha20 when nonce uniqueness is hard.