Definition
Cipher block chaining (CBC) is a mode of operation for a block cipher §15.3: a rule for using a cipher that encrypts one fixed-size block, typically 16 bytes, to encrypt a message of many blocks. Before each plaintext block is encrypted, CBC combines it by XOR (exclusive or, bit-by-bit addition without carry) with the previous ciphertext block. The first block is combined with an initialization vector (IV), an unpredictable block sent with the ciphertext. CBC hides content but does not detect changes. NIST Special Publication (SP) 800-38A defines it.
The idea
A block cipher alone encrypts one block. The obvious way to encrypt a longer message is to cut it into blocks and encrypt each block separately, which is called electronic codebook (ECB) mode. ECB leaks patterns: equal plaintext blocks always give equal ciphertext blocks. A table in which many rows hold the same 16-byte value, or a picture with large areas of one color, stays recognizable after ECB encryption, because an observer sees where blocks repeat.
CBC hides the repeats by making the cipher input for each block depend on everything before it. Each plaintext block is combined by XOR with the previous ciphertext block, which looks random, so two equal plaintext blocks enter the cipher as different inputs and leave it as different ciphertext blocks. The IV stands in for the previous ciphertext block of the first block. A fresh IV for every message also makes two encryptions of the same message look unrelated.
The chaining has a second effect. The receiver combines each decrypted block by XOR with the previous ciphertext block, which travels in the clear, so whoever flips a bit of that ciphertext block flips the same bit of the recovered plaintext, and CBC cannot notice. A receiver that then reveals whether the result ends in valid padding answers a question about plaintext that the attacker steered, and enough answers reveal the plaintext.
How it works
The parts:
and : encryption and decryption of one block with the block cipher under the secret key . For AES (Advanced Encryption Standard), the most widely used block cipher, a block is 16 bytes.
: the plaintext blocks, after padding.
: the IV.
: the ciphertext blocks.
Encryption.
Pad the message to a whole number of blocks (padding rule below).
Choose a fresh, unpredictable IV and call it .
For in order, compute , where is bitwise XOR.
Send .
Encryption is sequential: block needs , so it cannot start before block is finished.
Decryption. For each , compute , then check and remove the padding. Each plaintext block needs only two ciphertext blocks, so all blocks can be decrypted in parallel.
Padding. CBC handles only whole blocks, so the sender pads the message; SP 800-38A leaves the padding rule to the application. The most common rule comes from Public-Key Cryptography §5.15 Standards (PKCS) number 7 and is called PKCS #7 padding: append bytes that each hold the value , choosing between 1 and 16 so that the length becomes a multiple of 16 bytes. A message that already fills whole blocks gets a full extra block of 16 bytes, each 0x10, so the last byte always says how much to remove. The receiver reads the last byte , checks that and that the last bytes all equal , and removes them. If the check fails, the padding is invalid.
The padding oracle attack. In cryptography an oracle is any party that answers questions of the attacker’s choosing. A padding oracle is a receiver that reveals, for any ciphertext the attacker submits, whether the decrypted plaintext ended in valid padding: through a distinct error message or code, a different response time §9.32, or a connection that closes in one case and stays open in the other. Number the bytes of a block 0 to 15 and write for byte of block . To read plaintext block , the attacker proceeds as follows.
Call the intermediate value, so that . The attacker knows but not .
Submit a block of the attacker’s choice followed by . The receiver treats as the IV, computes and checks its padding.
Try the 256 possible values of . The padding is valid when . Then and .
Set , so that the last decrypted byte is
0x02, and try values of until the padding is valid, which now means the ending0x02 0x02. Continue in the same way up to 16 bytes, each0x10.
Step 3 can also succeed when byte 14 of happens to be 0x02 and the guess made the last byte 0x02. The attacker detects this by changing and asking again: a true 0x01 ending stays valid.
Worked example
The cipher is AES with 16-byte blocks and PKCS #7 padding. The plaintext block is the 16 ASCII characters amount=000009500; because the message fills a whole block, a second block of 16 bytes, each 0x10, follows. The IV is
=
3f1a7c22d4e90b6851a0f2c48d779ec5,
so and . The attacker sees , and . The receiver answers every submitted ciphertext either with padding error or with some other error.
Since , the intermediate value is , whatever the key. The attacker does not know ; its last two bytes are shown here so that the reader can follow what the receiver computes. The character 0 is the byte 0x30, so and .
Byte 15. The attacker submits followed by , where is 15 zero bytes followed by a guess , for in turn. The last decrypted byte is . Byte 14 is , not 0x02, so the only valid padding is a last byte 0x01, reached at . That is the 245th question, because 0xf4 is 244 and the count started at 0. The attacker concludes and , the character 0. In binary the last step is .
Byte 14. The attacker sets , which makes the last decrypted byte 0x02, and tries values for . The padding becomes valid when byte 14 is also 0x02, that is when , so at , the 173rd question. Then and , again the character 0.
Two bytes cost questions. Fourteen more rounds of the same kind recover the rest of amount=000009500 without the key. A byte needs at most 256 questions, so a block needs at most , and every other block is attacked the same way with in place of the IV. Every forged ciphertext decrypts to garbage and is rejected; the attacker needs only to see which rejections were padding errors.
Stated precisely
Let be a block cipher with key and a block length of bits ( for AES), let be its inverse, bitwise XOR, the IV and the padded plaintext blocks. Then
Malleability. If an attacker replaces by , for a block of the attacker’s choice, the receiver obtains in place of . If , block also changes, to an unpredictable value, because it is decrypted from the changed block; all other blocks are unchanged. If , the changed block is the IV and only changes.
Confidentiality §1.8. Assume that is a pseudorandom permutation §15.2, meaning that no feasible computation can tell it apart from a permutation of -bit blocks chosen uniformly at random, and that the IV of each message is chosen uniformly at random. Then CBC is secure against chosen-plaintext attack: an attacker who can have plaintexts of the attacker’s choice encrypted still cannot tell which of two equal-length messages, also chosen by the attacker, a new ciphertext contains (Bellare, Desai, Jokipii and Rogaway, 1997 [369]). The attacker’s advantage grows with the square of the number of blocks encrypted under one key, divided by , the birthday bound §5.6. Equal ciphertext blocks mean equal cipher inputs, which gives , a fact about the plaintext computed from public values. For AES such blocks become likely after about blocks, so keys are changed long before.
Padding oracle. Let the oracle take blocks and and return 1 if ends in valid padding, 0 otherwise. Because the attacker chooses , each answer tests a property, selected by the attacker, of the fixed unknown block ; at most 256 answers determine each of its bytes. The attack uses no weakness of the block cipher. Serge Vaudenay published it in 2002 [365].
Limits and common mistakes
CBC does not provide integrity §1.9: a modified ciphertext decrypts without complaint, as the malleability property shows. The repair is to authenticate before decrypting. In encrypt-then-MAC, the sender computes a MAC (message authentication code §5.7), a key-dependent tag, over the IV and all ciphertext blocks, and the receiver verifies the tag before it decrypts anything, so a modified ciphertext is rejected before its padding is ever examined. Simpler still is authenticated encryption §5.16 (AEAD, authenticated encryption with associated data), such as AES-GCM (Galois/Counter Mode §15.5), which does both jobs in one algorithm §14.1.
The opposite order, MAC-then-encrypt, encrypts the MAC together with the plaintext. The receiver must decrypt and remove the padding before it can check the MAC, so the padding check runs on unauthenticated data. The CBC cipher suites of TLS (Transport Layer Security), the protocol that protects web traffic, used this order by default in the versions before 1.3.
Identical error messages are not enough. If a padding failure and a MAC failure take different amounts of time, the time is the oracle, a side channel §1.29. The Lucky Thirteen attack of 2013 [370] recovered plaintext from TLS connections through timing differences of this kind.
The IV must be unpredictable, not merely different each time. If an attacker knows the next IV before choosing the next plaintext, the attacker can test a guess for an earlier block by having encrypted: the cipher input is then , so the result equals exactly when . TLS 1.0 used the last ciphertext block of one record as the IV of the next and was attacked this way in 2011 [386].
The IV is not secret, but changing it changes at will, so the MAC must cover it. A CBC ciphertext also reveals the message length rounded up to a whole block.
Questions answered
Why can revealing detailed padding errors create an oracle?
Revealing whether a decrypted ciphertext had valid padding creates an oracle because each response answers a yes-or-no question about plaintext that the attacker chose, and the attacker can ask again with every ciphertext submitted. In cryptography an oracle is any party that answers an attacker’s chosen questions. In CBC (cipher block chaining) mode, the receiver recovers a plaintext block as : the block cipher’s decryption of ciphertext block , combined by XOR (exclusive or) with the previous ciphertext block . The attacker can put any block in place of , and so XOR any chosen value onto the unknown . The padding check then tests the last bytes of the result.
A response that tells bad padding apart from other failures therefore says whether the chosen value turned the last bytes into valid padding, such as the single byte 0x01. At most 256 tries reveal one byte of , and with it one byte of the plaintext; at most 4096 reveal a 16-byte block. The key is never needed. A distinct error message, a different error code, a different response time or a connection that closes in one case and not in the other all leak the same bit. The repair is to authenticate the ciphertext with a MAC (message authentication code) and verify it before decrypting, or to use authenticated encryption, so that every modified ciphertext is rejected before its padding is examined.
Related entries: Block cipher (§15.3), Authenticated encryption (AEAD) (§5.16), Authentication coverage (§5.41), Cipher-based MAC (CMAC) (§15.7), Side channel (§1.29). Further reading: [47, 137, 365, 369, 370, 386].