18. Padding Oracle Attacks

How a single leaked bit can break encryption

Padding oracle attacks are one of the most elegant and devastating attacks in all of cryptography. They don't exploit a weakness in AES itself. They don't require brute force. They exploit the way AES-CBC is used — specifically, what a server reveals when it tries to remove padding from a decrypted message. The result: a completely passive attacker, with no knowledge of the key, can decrypt an entire CBC-encrypted ciphertext by sending a few thousand specially crafted requests.

In the block modes article, we noted that CBC without authentication is vulnerable to this attack. Here is exactly why.

Prerequisites: PKCS#7 Padding and CBC Decryption

CBC mode requires plaintexts to be a multiple of 16 bytes. If the last block is short, it is padded using PKCS#7: each padding byte has a value equal to the number of padding bytes added.

PKCS#7 padding examples (16-byte blocks)

Message: "HELLO" (5 bytes) → needs 11 bytes of padding

48 45 4C 4C 4F 0B 0B 0B 0B 0B 0B 0B 0B 0B 0B 0B H E L L O ← 11 bytes, each with value 0x0B (11) →

Message: "YELLOW SUBMARINE" (16 bytes, exactly one block) → add a full extra block: 16 bytes each with value 0x10 (16)

Valid padding patterns (last N bytes must all equal N): ... 01 ← 1 byte of padding ... 02 02 ← 2 bytes of padding ... 03 03 03 ← 3 bytes of padding ... 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 ← 16 bytes (full block)

After decryption, the receiver strips the padding bytes. If the padding is invalid — say, the last byte is 0x03 but the two bytes before it aren't both 0x03 — decryption fails. Typically the server returns an error.

CBC decryption works like this: each ciphertext block is passed through AES inverse, then XORed with the previous ciphertext block (or the IV for the first block).

CBC decryption formula

C₁ ──→ [AES⁻¹_K] ──→ ⊕ ←── IV  ──→ P₁
C₂ ──→ [AES⁻¹_K] ──→ ⊕ ←── C₁ ──→ P₂
C₃ ──→ [AES⁻¹_K] ──→ ⊕ ←── C₂ ──→ P₃

P₂ = AES⁻¹_K(C₂) ⊕ C₁

The key observation: the attacker controls C₁. If the attacker sends a modified ciphertext, they control what P₂ looks like.

This is the structural weakness. The attacker doesn't need the key to influence the plaintext — they just need to modify the preceding ciphertext block.

The Oracle

An oracle, in cryptography, is any system that answers yes/no questions about hidden data. In a padding oracle attack, the oracle is a server that tells you whether a CBC-decrypted message had valid PKCS#7 padding.

In practice the oracle doesn't have to be explicit. It might be:

  • An HTTP 400 (Bad Request) vs. 500 (Internal Server Error) response
  • A faster response on valid padding vs. a slower one (timing oracle)
  • A different error message: "Invalid padding" vs. "Invalid MAC"
  • A connection reset vs. a clean error reply

Any observable difference in server behavior that correlates with padding validity is sufficient. The attacker doesn't need to see the plaintext — just whether decryption succeeded structurally.

Authentication Would Have Stopped This

If the server verified an authentication tag (HMAC or GCM tag) before attempting to decrypt, it would reject the attacker's modified ciphertext immediately — without ever checking padding. The oracle would be silenced. This is the fundamental lesson: always authenticate before decrypting.

The Attack, Step by Step

The goal: decrypt ciphertext block C₂ without knowing the key.

The attacker sends modified ciphertext to the server, watches the padding oracle's responses, and uses them to deduce the intermediate value AES⁻¹_K(C₂) — which, when XORed with the real C₁, gives the plaintext.

Recovering the last byte

The attack targets one byte at a time, starting from the end of the block.

  1. Isolate the target block

    Send just two blocks to the server: a manipulable "C₁-prime" (C₁′) of your choosing, followed by the real C₂. The server decrypts C₂ and XORs with C₁′ to get a trial plaintext P₂′, then checks whether P₂′ has valid padding.

  2. Brute-force the last byte

    Set the first 15 bytes of C₁′ to random garbage (they don't affect padding validity yet). Try all 256 values of C₁′[15] — the last byte. For 255 of them, the last byte of P₂′ will be garbage, and the oracle says "invalid." For one value, the last byte of P₂′ will be 0x01 — a valid one-byte padding — and the oracle says "valid."

  3. Solve for the intermediate byte

    When you find the value C₁′[15] that produces valid padding, you know:

    AES⁻¹_K(C₂)[15] ⊕ C₁′[15] = 0x01
    
    

    Therefore: AES⁻¹_K(C₂)[15] = C₁′[15] ⊕ 0x01

  4. Recover the plaintext byte

    XOR the intermediate byte with the original C₁ (not the modified one):

    P₂[15] = AES⁻¹_K(C₂)[15] ⊕ C₁[15]

Recovering the second-to-last byte

Now move left. To recover byte 14, you need the last byte of P₂′ to be 0x02 (so that a valid two-byte padding 02 02 can form).

Setting up for byte 14

We already know AES⁻¹_K(C₂)[15].

Set C₁′[15] to force P₂′[15] = 0x02: C₁′[15] = AES⁻¹_K(C₂)[15] ⊕ 0x02

Now try all 256 values of C₁′[14]. The oracle fires when P₂′[14] = 0x02 as well.

AES⁻¹_K(C₂)[14] = C₁′[14] ⊕ 0x02 P₂[14] = AES⁻¹_K(C₂)[14] ⊕ C₁[14]

Continue backwards through every byte of the block. For byte N from the end, set all already-solved bytes to produce padding value N, then brute-force the next byte.

A worked example

Let's decrypt a single block with concrete hex values. Suppose:

Concrete example: recovering one byte

C₁ (original, from the legitimate message):
  A3 14 2F 88 C0 5B 9E 71 D2 46 F3 AA 19 CE 82 5D

C₂ (the block we want to decrypt): 7F 3C A1 09 E8 4B D5 2A 60 91 FC 37 B8 0E 55 C4

We try all 256 values of C₁′[15] and find the oracle says "valid" when: C₁′[15] = 0x4E

This means: AES⁻¹_K(C₂)[15] = 0x4E ⊕ 0x01 = 0x4F

The original plaintext byte: P₂[15] = 0x4F ⊕ 0x5D = 0x12 ← a control character, part of the data

The attacker learned one plaintext byte using at most 256 requests — without the key, without factoring anything, without any mathematical heavy lifting.

The Attacker Never Touches the Key

The key is never involved in the attack. The attacker exploits the XOR relationship in CBC decryption: by controlling C₁′, they control what plaintext the server sees. The oracle answers questions about that plaintext indirectly, one bit of information at a time. It is a textbook example of a chosen-ciphertext attack.

Handling ambiguous results

One subtlety: occasionally two values of C₁′[15] produce a valid oracle response. This happens when a coincidental multi-byte padding like 02 02 or 03 03 03 forms in the garbage bytes to the left. The fix is easy — after finding a candidate byte, verify it by also flipping byte 14. If the oracle response changes, your candidate was a false positive; if it stays valid, you have the right answer.

Scaling Up: The Full Message

Recovering one block takes at most 256 × 16 = 4,096 oracle queries (and usually far fewer — on average 128 per byte, so about 2,048 per block). A 10-block ciphertext therefore takes roughly 20,000 requests. A web server handling thousands of requests per second can be drained of its plaintext in seconds.

Query budget per block

Bytes to recover:    16
Queries per byte:    128 on average (256 worst case)
Queries per block:   ~2,048 average, 4,096 worst case

A 160-byte message (10 blocks): ~20,480 average queries ~40,960 worst case

At 1,000 requests/second: fully decrypted in under a minute.

Each ciphertext block is decrypted independently against the block before it. For the first block (decrypted against the IV), the attacker needs the IV — but in many protocols, the IV is transmitted in the clear alongside the ciphertext.

Real-World Instances

This attack is not theoretical. It has broken real deployed systems.

POODLE (2014)

SSLv3 used CBC with non-deterministic padding (the last byte specified padding length, but intermediate bytes could be anything). Bodo Möller, Thai Duong, and Krzysztof Kotowicz showed that an attacker who could force SSLv3 fallback (by interfering with the TLS handshake) could then run a padding oracle against HTTPS cookies. SSLv3 was disabled across the internet within weeks.

Lucky Thirteen (2013)

Al Fardan and Paterson found a timing padding oracle in TLS 1.2 CBC-mode implementations. Even though TLS computed the MAC before stripping padding, the time taken to compute the MAC differed measurably based on padding length — leaking the same information through a side channel. Fixing it required constant-time MAC computation regardless of padding length.

ASP.NET padding oracle (Rizzo and Duong, 2010)

Thai Duong and Juliano Rizzo demonstrated a padding oracle against ASP.NET's encrypted ViewState and forms authentication cookies. The ASP.NET framework returned different HTTP error codes for padding failures versus MAC failures, providing a clear oracle. Their tool, POET (Padding Oracle Exploitation Tool), could decrypt a 16-byte ASP.NET authentication cookie in around 50 requests. Microsoft issued an emergency out-of-band patch — something they almost never do.

Discovery:  September 2010
Patch:      October 2010 (emergency, out-of-band)
Severity:   Critical — authentication bypass on millions of .NET web apps
Root cause: error handling differentiated padding failures from MAC failures
Attack Year Target Oracle type
Rizzo/Duong 2010 ASP.NET ViewState HTTP 500 vs. 200
BEAST 2011 TLS 1.0 CBC Timing + chosen-plaintext
Lucky Thirteen 2013 TLS 1.2 CBC Timing (MAC computation)
POODLE 2014 SSLv3 CBC Padding structure

The Fix: Authenticated Encryption

The permanent fix is simple: use AEAD. GCM and ChaCha20-Poly1305 both authenticate the ciphertext before any decryption attempt. A modified ciphertext fails authentication and is rejected immediately — the padding step is never reached, the oracle is never consulted, and the attack has nothing to work with.

Unauthenticated CBC (vulnerable)

Receive ciphertext → decrypt → check padding → strip padding → process plaintext. The padding check happens after decryption. Failure leaks timing or error information. The oracle exists.

AES-GCM (safe)

Receive ciphertext + tag → verify tag → if invalid, reject immediately. Decryption only happens after authentication passes. There is no padding, no oracle, and no information leaked on failure.

If you are stuck with CBC for legacy reasons (don't be, but sometimes you are), the mitigation is Encrypt-then-MAC: compute an HMAC over the ciphertext and send it alongside. The receiver verifies the HMAC first, in constant time, before touching the ciphertext. This silences the oracle without replacing the cipher mode. TLS 1.2 supports Encrypt-then-MAC via RFC 7366.

This Is Exactly Why We Built AEAD

Padding oracle attacks were a major motivation for the widespread adoption of authenticated encryption. TLS 1.3 dropped all unauthenticated CBC cipher suites entirely — they are not negotiable. Every TLS 1.3 connection uses GCM or ChaCha20-Poly1305. The lesson from decades of CBC attacks was clear: authentication and encryption must be inseparable.

Padding Oracle Simulator

This simulator walks through the attack one byte at a time. A simulated server decrypts your modified ciphertext and reports whether the padding is valid. Watch how each oracle response narrows down the intermediate byte value and reveals a plaintext byte.

Oracle Query Counter

Enter a plaintext message, and the tool encrypts it with CBC, then simulates the full padding oracle attack to decrypt it back — counting every query. See how the query cost scales with message length.

Key Takeaways

  • A padding oracle is any observable difference in server behavior that reveals whether CBC-decrypted padding is valid
  • By controlling the preceding ciphertext block, an attacker controls the decrypted plaintext — without the key
  • The attack recovers one byte per ~128 oracle queries; a full block costs ~2,048 queries on average
  • Real deployments were broken: ASP.NET (2010), Lucky Thirteen (2013), POODLE (2014)
  • The fix is AEAD — authentication before decryption makes the oracle unreachable
  • TLS 1.3 removed all unauthenticated CBC modes for exactly this reason
  • If stuck on legacy CBC: Encrypt-then-MAC (RFC 7366) silences the oracle without replacing the cipher

Padding oracle attacks show what happens when the gap between encryption and authentication is exploited through careful protocol manipulation. Next, we look at an attack that works at a more fundamental level — not by exploiting how a cipher is used, but by exploiting a basic mathematical property that every hash function must eventually run into: the birthday paradox.