Skip to main content
Be Bitwise

Padding Oracle Attacks

Applied Crypto & Common Pitfalls · Networking & Cryptography track

6 min read

In this module

When Error Messages Break Encryption

A padding oracle attack is one of the most elegant and ruinous attacks in applied cryptography. It exploits a seemingly innocuous piece of information — whether the padding on a decrypted message is valid — to recover the entire plaintext without ever knowing the key.

This attack is the textbook proof that cryptographic security is about more than the algorithm. The cipher can be flawless, the key unguessable, the implementation airtight. Leak a single bit of information about the decryption process and the whole edifice can collapse.

The Setup: CBC Mode with PKCS#7 Padding

Recall that CBC encrypts data in blocks. When the last block isn't full, it has to be padded. PKCS#7 padding fills the trailing bytes with the count of padding bytes required:

text
1 byte of padding: ...01
2 bytes of padding: ...02 02
3 bytes of padding: ...03 03 03
4 bytes of padding: ...04 04 04 04

After decryption, the receiver checks the padding. If it doesn't add up (last byte says 03 but the preceding bytes don't match), the decryption is rejected. That validation step is where the vulnerability lurks. The server's response to invalid padding hands the attacker information they can lean on.

The Oracle

A padding oracle is any system that tells the attacker whether the padding is valid. This could be:

  • A different error message ("Decryption failed" vs. "Invalid padding")
  • A different HTTP status code (200 vs. 400 vs. 500)
  • A different response time (processing takes longer when padding is valid)
  • Even subtle behavioural differences like whether a session is invalidated

A single bit of information — valid or invalid — is plenty to recover the whole plaintext. Sit with that for a moment: a binary yes/no, repeated enough times, breaks the encryption wide open.

Key Concept

A padding oracle attack only needs to know whether decrypted padding is valid. By systematically modifying ciphertext bytes and observing the oracle's response, the attacker can determine the plaintext one byte at a time. This recovers the entire message without knowing the encryption key.

How the Attack Works

In CBC decryption, each plaintext block is computed as:

text
P[i] = Decrypt(C[i]) XOR C[i-1]

The attacker controls C[i-1] (the previous ciphertext block). By modifying a byte in C[i-1], the attacker directly changes the corresponding byte in the decrypted plaintext P[i].

To recover the last byte of plaintext:

  1. Modify the last byte of C[i-1] to a guess value
  2. Send the modified ciphertext to the server
  3. If the server says "valid padding," the decrypted last byte is almost certainly 0x01 (valid single-byte padding)
  4. From the guess value and the valid padding byte, compute the actual plaintext byte

That "almost" matters. If the second-to-last plaintext byte happens to be 0x02, a guess that turns the last byte into 0x02 also gives valid padding (02 02). The attacker rules this out by changing the second-to-last byte of C[i-1] and asking again: if the padding is still valid, the last byte really was 0x01.

The attacker tries at most 256 values for the last byte. On average, 128 guesses do the trick. Then they pivot to the second-to-last byte (targeting padding 0x02 0x02), and so on, peeling the block apart. A 16-byte block needs at most 16 * 256 = 4,096 requests. Trivial for an automated tool. Coffee-break stuff.

The Maths

Let D be the raw decrypted value (before XOR with C[i-1]):

text
D[last] XOR C[i-1][last] = P[last] (original plaintext byte)
D[last] XOR C'[i-1][last] = 0x01 (when padding is valid)

From the second equation: D[last] = 0x01 XOR C'[i-1][last] Then: P[last] = D[last] XOR C[i-1][last] = 0x01 XOR C'[i-1][last] XOR C[i-1][last]

The attacker knows C[i-1][last] (the original ciphertext) and C'[i-1][last] (their modified value), so they can compute P[last]. XOR's signature trick: everything cancels cleanly, leaving the unknown plaintext byte alone in plain view.

Once the last byte is known, the attacker rigs the block to produce 0x02 0x02 as valid padding and brute-forces the next byte over. Same dance, every byte in the block, every block in the message.

Here's the attack on the last two bytes of one block. The middle rows show what the server computes, which the attacker never sees; the attacker only gets the valid or invalid answer. Before step 6, work out P[15] from the two equations above.

Padding oracle: recovering two bytes

C'[i-1], the block the attacker sends
…
3A
[14]
7C
[15]
server decrypts C[i] to D (attacker can'
…
??
[14]
??
[15]
server checks padding on D XOR C'[i-1]
…
6F
[14]
53
[15]
recovered plaintext P[i]
…
?
[14]
?
[15]
guess = -oracle = -requests = 0
The real C[i-1] ends in 3A 7C. The attacker sends a doctored copy in front of C[i] and watches only one thing: did the padding check pass? (Byte values are made up for this trace.)
Step 1/9

Historical Impact

Serge Vaudenay described the attack in 2002, and it took more than a decade of TLS breaks to drive the lesson home.

POODLE (2014): Exploited padding handling in SSL 3.0 to recover plaintext byte by byte. Many servers still supported SSL 3.0 as a fallback, so attackers could force a downgrade and then ride POODLE in.

Lucky Thirteen (2013): Exploited timing differences in TLS CBC padding validation. Even when error messages were identical, the time to process valid versus invalid padding differed by a measurable margin. The takeaway: hiding the oracle isn't enough — your timing has to be airtight too.

These attacks against TLS, the protocol securing all HTTPS traffic, dragged padding oracles out of academic papers and into the threat models of every serious operator on the internet.

A close relative from the same era, BEAST (2011), broke TLS 1.0 CBC without any padding oracle. It exploited predictable IVs: the last ciphertext block of each record doubled as the IV for the next, so a chosen-plaintext attack could recover session cookies one byte at a time.

Defences

Use AEAD ciphers: AES-GCM and ChaCha20-Poly1305 authenticate the ciphertext before decryption. If anyone has tampered with the ciphertext, the cipher rejects it before ever looking at padding. The oracle has nothing to leak.

Constant-time padding validation: If you're stuck with CBC, validate padding in constant time and return the same error whether padding or MAC failed. Doing this correctly is brutally hard. Lucky Thirteen made the point: even minute timing differences are exploitable.

TLS 1.3 binned CBC entirely: The spec only allows AEAD ciphers, making padding oracle attacks impossible against conforming implementations. A deliberate choice, baked in after a decade of padding oracle disasters.

The lesson is unambiguous: encryption without authentication is dangerous. Reach for authenticated encryption (AEAD) and the entire padding oracle problem evaporates. Not a hot take. The consensus of the whole cryptographic community, written straight into the latest protocol standards.