liveBook · Manning

Padding oracle

As explained in Hacking Cryptography

A padding oracle is a decryption process that reveals whether supplied ciphertext has valid padding without directly revealing the plaintext.

A padding oracle is a decryption process that reveals whether submitted ciphertext has valid padding without directly revealing its plaintext. That single yes-or-no signal can enable recovery of the entire message. section 5.4

A typical failure appears when a server decrypts AES-CBC data, validates PKCS#7 padding, and returns distinguishable errors for invalid padding, invalid ciphertext, or malformed timestamps. Troubleshooting information becomes a decryption signal. section 5.4.1

Listing 5.1 impl_padding_oracle.go
func PadWithPKCS7(data []byte, blockSize int) ([]byte, error) {
  if blockSize <= 0 {
    return nil, fmt.Errorf("invalid block size")
  }
  if data == nil {
    return nil, fmt.Errorf("input data is nil")
  }

  padding := blockSize - len(data)%blockSize 
  padtext := bytes.Repeat([]byte{byte(padding)}, padding)  #1
  return append(data, padtext...), nil 
}

PKCS#7 padding appends bytes whose value equals the number of padding bytes; validation checks that the final bytes repeat that value. section 5.4.1

A CBC padding-oracle attack proceeds by manipulating the block preceding a target block:

  • Submit modified ciphertexts while changing the final byte of the preceding block until the target plaintext ends in 0x01. section 5.4.2
  • Repeat with padding values 0x02 through the block size, then process preceding blocks until the plaintext is recovered. section 5.4.2

Encrypted timestamp service

A service returns an encrypted timestamp and later accepts it to calculate elapsed time. Its padding-specific error enables an exploit that recovers a plaintext such as “Fri Jun 9 14:39:01 PDT 2023.”

How Hacking Cryptography covers it

How deep it goes
Explains PKCS#7 validation, CBC XOR equations, bytewise brute force, and a complete recovery implementation.
Examples
Demonstrates an AES-CBC encrypted timestamp API, Go padding routines, bytewise manipulation, and recovered timestamp output.
What it assumes
Assumes block-cipher and CBC concepts while developing PKCS#7 padding and validation through implementation.
What you can do after
Diagnose distinguishable cryptographic errors and reproduce plaintext recovery against a CBC padding oracle.
How it connects
Connects PKCS#7 padding, CBC mode, initialization vectors, error handling, and plaintext recovery.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.