Speed

Padding Errors Leak Information

CBC decryption checks the padding after it decrypts, and a failure is a yes or no that anyone who can send ciphertexts and see the result can use; this lesson shows the rule, the signal and a checker, and no way to exploit it.

The question

Padding was added when the message was encrypted, so decryption removes it. To do that safely it reads the last decrypted byte, n, and checks that the last n bytes all have the value n. What if they do not?

OpenSSL then stops with an error. That error is a yes or no about the decrypted data, and it is produced without the key being known to the person who sent the ciphertext. This lesson shows the rule on a real decryption, names the signal, and asks for the check itself. It contains no way to use the signal to recover a message. The key and the IV are test material.

Work it by hand

The demo message meet me at the old mill at nine. has 32 bytes. Under RFC 5652 section 6.3 the padding is a whole block of sixteen bytes with the value 16, hex 10: the message is a whole number of blocks, so a full block is added. The ciphertext has three blocks.

Decrypt without removing the padding, and the last block must be sixteen 10 bytes. The last byte is 16, which is between 1 and 16, and the last 16 bytes all equal 16, so the padding is valid and 16 bytes are removed, which leaves the 32 message bytes.

Run it

The command decrypts the three-block ciphertext in the data box with AES-128 in CBC mode and the option -nopad, which keeps the padding bytes in the output. The picture shows the ciphertext and the 48 bytes of plaintext; the last 16 are 10.

The 48 ciphertext bytes in three lines and the 48 plaintext bytes that the command wrote, starting 6d 65 65 74, whose third line is sixteen bytes of 10.

Public test material, generated for this lesson. Never use it for anything real.

# Decrypts in/cipher.bin under the test key, result in out.bin.
# The key and the IV are public test material.
openssl enc -d -aes-128-cbc -nopad -K 2b7e151628aed2a6abf7158809cf4f3c \
  -iv 000102030405060708090a0b0c0d0e0f \
  -in in/cipher.bin -out out.bin
{
  "answer": "6d656574206d6520617420746865206f6c64206d696c6c206174206e696e652e10101010101010101010101010101010",
  "file": "out.bin",
  "bytes": 48
}

You can change the ciphertext to any whole number of 16-byte blocks, written as hex; the empty ciphertext is accepted and writes an empty file, and any other length stops the command with "bad decrypt: wrong final block length", with or without -nopad.

To see the failure, also remove -nopad and change a byte of the last or the second-to-last block. OpenSSL then stops with the reason "bad decrypt" and exit code 1; measured here for a flipped last byte, a flipped byte of the block before it, and for another key. It had already written the first two blocks to the output file; for a flipped byte these are the 32 message bytes.

Your turn

The program below is meant to say whether the last decrypted block block (16 bytes, hex) ends with valid padding as RFC 5652 section 6.3 states it: the last byte n is from 1 to 16 and the last n bytes all have the value n. It has no AES; it only reads bytes. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on nine others.

The 16 bytes of the last decrypted block, 6d 79 20 74 and so on, ending 04 07 07 04.
let n = hex_u8(block, 15);
let answer = false;
if (n >= 1 && n <= 16) {
  if (hex_u8(block, 16 - n) == n) { answer = true; }
}
{
  "answer": true
}

block is the hex text of the last decrypted block, 16 bytes, of a CBC decryption that kept the padding. Return true if it ends with valid padding as in RFC 5652 section 6.3: the last byte n is from 1 to 16 and the last n bytes all have the value n; otherwise return false. Check runs each candidate on the data shown and on nine others.

Read the result carefully

Whether the padding is valid depends on the last byte and on as many bytes before it as the last byte says. A system that tells a sender "bad padding" or "good padding" for ciphertexts the sender chose gives away at most one bit about the plaintext for each try. That is why this is called a padding oracle. The page names it; it does not show how it is used, and it does not show timing, where the two outcomes can differ in how long they take.

The checker needs a block of 16 bytes: a shorter input stops it with "byte 15 is past the end of the data". Valid padding says nothing about whether the message is the one that was sent: the last lesson of this book shows that no mode detects a change.

Recap

  • Decryption reads the last byte n and checks that the last n bytes are all n (RFC 5652, section 6.3).
  • A message that is already whole blocks gets a whole block of padding, sixteen bytes with the value 16.
  • A failed check is reported by OpenSSL as "bad decrypt" with exit code 1.
  • That yes or no is a signal about the plaintext; this page names it and does not use it.