7. Authenticated Encryption
Confidentiality and integrity in a single pass
In the block-modes article, we ended on an uncomfortable note: CTR mode lets an attacker flip bits in the ciphertext and predictably corrupt the corresponding plaintext bits — without knowing the key. CBC mode is vulnerable to padding oracle attacks when decryption errors are observable. The root cause in both cases is the same: encryption gives you confidentiality, but it says nothing about integrity.
An attacker who can modify your ciphertext in transit is a realistic threat. TLS connections pass through routers. Files sit on untrusted storage. API payloads travel across hostile networks. Without integrity protection, encryption alone is a lock on a screen door — it keeps honest people out, but a determined attacker can reach around it.
Authenticated Encryption with Associated Data (AEAD) solves this by combining encryption and a MAC into a single primitive with a clean, hard-to-misuse interface. One key. One operation. Confidentiality and integrity together.
The Doom Principle
Before AEAD existed, developers combined encryption and authentication manually. There are three obvious ways to do it:
Three ways to combine encryption and MAC
Encrypt-and-MAC: C = Enc(K₁, P), T = MAC(K₂, P)
— MAC is over the plaintext. Leaks plaintext structure through T.
MAC-then-Encrypt: T = MAC(K₂, P), C = Enc(K₁, P || T)
— MAC is encrypted. Decryption must happen before MAC verification.
Encrypt-then-MAC: C = Enc(K₁, P), T = MAC(K₂, C)
— MAC is over the ciphertext. Verify T before touching C.
Only Encrypt-then-MAC is correct. The security researcher Moxie Marlinspike captured why in what became known as the Doom Principle:
The Doom Principle
"If you have to perform any cryptographic operation before verifying the MAC on a message you've received, it will somehow inevitably lead to doom." — Moxie Marlinspike
MAC-then-Encrypt requires you to decrypt first, then verify. That gap — the decryption that happens before the MAC check — is exactly where padding oracle attacks live. The decryption operation leaks timing or error information that an attacker can exploit before the MAC ever gets checked.
MAC-then-Encrypt (wrong)
Decrypt first, then verify the MAC. Every decryption error is an oracle. Attackers can observe error timing or messages to learn about the plaintext. This is how BEAST, POODLE, and Lucky13 work.
Encrypt-then-MAC (correct)
Verify the MAC first. If it fails, discard the message immediately — never even attempt decryption. The attacker gets no feedback about the ciphertext structure. This is what AEAD primitives do internally.
Getting this ordering right manually is error-prone. Forgetting to use separate keys for encryption and MAC, or reusing a MAC key, or checking the MAC in a way that leaks timing — all of these are real vulnerabilities found in real deployed systems. AEAD exists so that you don't have to get it right yourself.
The AEAD Interface
A well-designed AEAD primitive exposes exactly two operations:
The AEAD API
Encrypt(key, nonce, plaintext, aad) → (ciphertext, tag)
Decrypt(key, nonce, ciphertext, tag, aad) → plaintext
→ ERROR (if tampered)
The inputs:
- key — the shared secret, typically 128 or 256 bits.
- nonce — a number used once. Never reuse the same nonce with the same key. More on this shortly.
- plaintext — the data to encrypt.
- aad — Associated Authenticated Data. Data you want authenticated but not encrypted.
The outputs:
- ciphertext — the encrypted data, same length as the plaintext.
- tag — a short authentication code, typically 128 bits, covering both the ciphertext and the AAD.
The AAD is the subtlest part. Consider a network packet: the payload should be encrypted, but the routing header needs to remain readable by routers in transit. You still need to know the header hasn't been tampered with. AAD lets you include the header in the authentication without encrypting it — the tag covers both the encrypted payload and the plaintext header.
AAD in practice
TLS record:
Header: [content_type=23, version=0x0303, length=256] ← AAD (authenticated, visible)
Body: [encrypted application data ...] ← ciphertext (encrypted + authenticated)
Tag: [16-byte GCM tag] ← covers both header and body
An attacker who changes the content_type in the header
will cause the tag verification to fail at the receiver.
On decryption, if anything has been modified — a single bit of ciphertext, a byte of AAD, or the tag itself — Decrypt returns an error and produces no output. The application never sees potentially tampered data.
AES-GCM Deep Dive
GCM (Galois/Counter Mode) is the most widely deployed AEAD construction. We introduced it in the block-modes article; here we go deeper.
GCM is built from two components running in parallel:
GCM structure
Key
│
┌───────────┴───────────┐
│ │
▼ ▼
CTR encryption GHASH authentication
│ │
Nonce‖1 → [AES_K] → ⊕ ← P₁ → C₁ ─────────────────┐
Nonce‖2 → [AES_K] → ⊕ ← P₂ → C₂ ────────────────┐ │
Nonce‖3 → [AES_K] → ⊕ ← P₃ → C₃ ───────────────┐│ │
▼▼ ▼
Nonce‖0 → [AES_K] → H (hash key) AAD, C₁, C₂, C₃, lengths
│
GHASH(H, ...)
│
Nonce‖0 → [AES_K] ─────────────────────────⊕
│
Tag
CTR encryption works exactly as described in the block-modes article: generate keystream blocks by encrypting a counter, XOR with the plaintext. Fast, parallelizable, no padding needed.
GHASH is a polynomial MAC over the field GF(2¹²⁸). The hash key H is computed by encrypting a block of all zeros with the key: H = AES_K(0). Then GHASH processes the AAD and ciphertext as a sequence of 128-bit blocks:
GHASH computation
H = AES_K(0¹²⁸) ← hash subkey (never reuse key!)
X₀ = 0
X₁ = (X₀ ⊕ AAD_block₁) · H ← multiplication in GF(2¹²⁸)
X₂ = (X₁ ⊕ AAD_block₂) · H
...
Xₙ = (Xₙ₋₁ ⊕ C_block₁) · H
...
Xₘ = (Xₘ₋₁ ⊕ len(AAD)‖len(C)) · H ← final block encodes lengths
GHASH output = Xₘ
Tag = GHASH_output ⊕ AES_K(Nonce‖0) ← mask with encrypted nonce
The multiplication · H in GF(2¹²⁸) is what binds together every block of AAD and ciphertext into a single 128-bit value. Change any bit anywhere and the polynomial evaluates differently, producing a different GHASH and a tag mismatch. The length encoding at the end ensures that different-length messages can't collide by accident.
The final XOR with AES_K(Nonce‖0) masks the GHASH output so that an attacker who sees multiple tags can't use them to learn anything about H. This matters because GHASH, unlike HMAC, is not a pseudorandom function by itself — it's a polynomial, and if you ever saw raw GHASH output you could potentially recover H. The AES masking step prevents that.
ChaCha20-Poly1305
ChaCha20-Poly1305 pairs the ChaCha20 stream cipher (covered in the stream-ciphers article) with Poly1305, a fast one-time MAC. It was designed by Daniel Bernstein and has become the preferred alternative to AES-GCM in environments where AES hardware acceleration isn't available.
ChaCha20-Poly1305 structure
ChaCha20(key, nonce, counter=0) → first 32 bytes = Poly1305 key (r, s)
ChaCha20(key, nonce, counter=1) → keystream for plaintext block 1
ChaCha20(key, nonce, counter=2) → keystream for plaintext block 2
...
ciphertext = plaintext ⊕ keystream
Poly1305 key is used once, then discarded:
tag = Poly1305(r, s, AAD || padding || ciphertext || padding || lengths)
Poly1305 evaluates a polynomial modulo the prime . The message is split into 17-byte chunks (each chunk is a little-endian integer with a leading 0x01 byte to ensure it's nonzero), and the MAC is computed as:
Where r and s are the two halves of the 32-byte one-time key generated by ChaCha20 counter block 0. The r value is used as the polynomial evaluation point; s is added at the end to mask the polynomial output.
Why the key must be one-time
Poly1305 is a one-time MAC. If you reuse the same (r, s)
key for two different messages, an attacker can recover r algebraically —
they have two polynomial evaluations at the same point and can solve for it. ChaCha20
derives a fresh (r, s) for every single message from the nonce, so the
key is never reused as long as nonces are unique.
The appeal of ChaCha20-Poly1305 over AES-GCM:
- No AES-NI required. On ARM microcontrollers, embedded devices, and older mobile CPUs without AES hardware, ChaCha20 is significantly faster than AES in software.
- Naturally constant-time. ChaCha20 and Poly1305 are designed to run in constant time without hardware support. AES in software is notoriously difficult to implement without timing side channels (cache-timing attacks like Bernstein's 2005 cache-timing attack on AES).
- Nonce misuse is less catastrophic. We'll return to this below.
AES-GCM vs ChaCha20-Poly1305
| Property | AES-GCM | ChaCha20-Poly1305 |
|---|---|---|
| Encryption | AES in CTR mode | ChaCha20 stream cipher |
| Authentication | GHASH polynomial MAC | Poly1305 one-time MAC |
| Tag size | 128 bits | 128 bits |
| Nonce size | 96 bits | 96 bits |
| Key sizes | 128 or 256 bits | 256 bits |
| Speed (with AES-NI) | Very fast | Fast |
| Speed (without AES-NI) | Slow | Very fast |
| Nonce reuse impact | Catastrophic (H leaks) | Catastrophic (r leaks) |
| Constant-time | Requires AES-NI | Yes, by design |
| TLS 1.3 | Yes | Yes |
| Recommended for | Servers with AES-NI | Mobile/embedded/no AES-NI |
Both are excellent choices. TLS 1.3 mandates support for both, and implementations negotiate based on hardware capability. If you're writing application code, use whichever one your library provides as the default — both provide equivalent security margins.
Nonce Misuse
The single most common mistake with AEAD is reusing a nonce. The consequences range from bad to catastrophic.
What nonce reuse reveals
If you encrypt two messages with the same (key, nonce):
C₁ = P₁ ⊕ keystream(key, nonce)
C₂ = P₂ ⊕ keystream(key, nonce)
C₁ ⊕ C₂ = P₁ ⊕ P₂
The attacker XORs the two ciphertexts and gets the XOR of the two
plaintexts directly. With any known plaintext, the other is recovered
immediately. This is the same catastrophe as OTP key reuse.
For AES-GCM, nonce reuse has an additional, more severe consequence: the GHASH subkey H = AES_K(0) can be recovered. Once an attacker has H, they can forge authentication tags for arbitrary messages. GCM's authentication is entirely broken, not just weakened.
Nonce Reuse in GCM Is Catastrophic
With nonce reuse in AES-GCM, an attacker who observes two ciphertexts can recover the
hash subkey H and forge valid authentication tags for any message
they choose. Both confidentiality and integrity are completely destroyed. This
happened in practice with Sony's PlayStation 3 firmware distribution, which reused
nonces in a GCM-like construction.
SIV Mode: Nonce-Misuse Resistance
SIV (Synthetic IV) mode is designed for situations where generating a reliable random nonce is difficult — low-entropy early-boot environments, deterministic protocols, or systems that have historically mismanaged nonces.
SIV mode construction (AES-SIV)
Step 1: Compute a synthetic IV by running S2V (a MAC) over AAD and plaintext:
SIV = S2V(K₁, AAD₁, AAD₂, ..., plaintext)
Step 2: Encrypt using the SIV as the CTR nonce:
ciphertext = AES-CTR(K₂, SIV, plaintext)
Output: (SIV, ciphertext)
The SIV serves as both the authentication tag and the encryption nonce.
SIV has a useful property: if the caller passes the same plaintext and AAD twice, it produces identical ciphertext. This is a leak — an attacker learns that two messages are identical — but that's far less damaging than leaking the plaintexts or forging tags. For most use cases, standard AES-GCM or ChaCha20-Poly1305 with proper nonce management is preferred. SIV is for specific contexts where nonce misuse is a realistic operational risk (e.g., keys exported from a hardware security module).
Real-World Usage
| Context | AEAD scheme |
|---|---|
| TLS 1.3 | AES-128-GCM, AES-256-GCM, ChaCha20-Poly1305 |
| WireGuard | ChaCha20-Poly1305 |
| SSH (OpenSSH) | AES-256-GCM or ChaCha20-Poly1305 |
| Signal protocol | AES-256-GCM |
| Age (file encryption) | ChaCha20-Poly1305 |
| Google QUIC / HTTP/3 | AES-128-GCM or ChaCha20-Poly1305 |
| iOS Secure Enclave | AES-256-GCM |
| Noise Protocol Framework | AES-256-GCM or ChaCha20-Poly1305 |
Notice that TLS 1.2 with AES-CBC is absent — it was retired specifically because CBC without careful authentication led to a long string of attacks (BEAST, Lucky13, POODLE). Every modern protocol uses AEAD.
Try It — AEAD Encrypt / Decrypt
Encrypt a plaintext message with optional associated data. Then tamper with the ciphertext, the AAD, or the tag and attempt decryption — the authentication tag will catch every modification.
Tampering Detector
This visualizer shows how AEAD catches every modification. Encrypt the message, then flip bits in different parts of the payload and observe the result. Any change anywhere — ciphertext, AAD, or tag — is detected.
Key Takeaways
- Encryption alone does not protect integrity — an attacker can tamper with ciphertext without knowing the key
- Encrypt-then-MAC is the only safe manual composition; MAC-then-Encrypt opens the door to padding oracle attacks
- AEAD combines encryption and authentication into a single, hard-to-misuse primitive with a clean two-function API
- AAD lets you authenticate data that must remain readable in transit (headers, metadata) without encrypting it
- AES-GCM = CTR encryption + GHASH polynomial MAC; fast on hardware with AES-NI
- ChaCha20-Poly1305 = ChaCha20 stream cipher + one-time Poly1305 MAC; preferred on devices without AES hardware
- Nonce reuse with AES-GCM is catastrophic: it leaks the GHASH key and allows tag forgery
- SIV mode provides nonce-misuse resistance at the cost of revealing message equality; use it only when nonce management is genuinely unreliable
- Every modern protocol — TLS 1.3, WireGuard, Signal, SSH — uses AEAD; never use unauthenticated encryption in new systems
Symmetric cryptography is now complete: we have fast, authenticated encryption that protects both confidentiality and integrity. But all of it rests on a shared secret — and we've never addressed the hardest question: how do two parties who have never met agree on that secret in the first place? That question requires a fundamentally different kind of cryptography, one where you can publish your encryption key to the entire world. Next up: RSA and the invention of public-key cryptography.