Section 15.6

Cipher block chaining (CBC)

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:

Encryption.

  1. Pad the message to a whole number of blocks (padding rule below).

  2. Choose a fresh, unpredictable IV and call it C0C_0.

  3. For i=1,2,…,mi = 1, 2, \dots, m in order, compute Ci=Ek(Pi⊕Ci−1)C_i = E_k(P_i \oplus C_{i-1}), where ⊕\oplus is bitwise XOR.

  4. Send C0,C1,…,CmC_0, C_1, \dots, C_m.

Encryption is sequential: block ii needs Ci−1C_{i-1}, so it cannot start before block i−1i - 1 is finished.

Decryption. For each ii, compute Pi=Dk(Ci)⊕Ci−1P_i = D_k(C_i) \oplus C_{i-1}, 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 nn bytes that each hold the value nn, choosing nn 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 nn, checks that 1≤n≤161 \le n \le 16 and that the last nn bytes all equal nn, 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 X[j]X[j] for byte jj of block XX. To read plaintext block PiP_i, the attacker proceeds as follows.

  1. Call I=Dk(Ci)I = D_k(C_i) the intermediate value, so that Pi=I⊕Ci−1P_i = I \oplus C_{i-1}. The attacker knows Ci−1C_{i-1} but not II.

  2. Submit a block RR of the attacker’s choice followed by CiC_i. The receiver treats RR as the IV, computes I⊕RI \oplus R and checks its padding.

  3. Try the 256 possible values gg of R[15]R[15]. The padding is valid when I[15]⊕g=𝟶𝚡𝟶𝟷I[15] \oplus g = \texttt{0x01}. Then I[15]=g⊕𝟶𝚡𝟶𝟷I[15] = g \oplus \texttt{0x01} and Pi[15]=I[15]⊕Ci−1[15]P_i[15] = I[15] \oplus C_{i-1}[15].

  4. Set R[15]=I[15]⊕𝟶𝚡𝟶𝟸R[15] = I[15] \oplus \texttt{0x02}, so that the last decrypted byte is 0x02, and try values of R[14]R[14] until the padding is valid, which now means the ending 0x02 0x02. Continue in the same way up to 16 bytes, each 0x10.

Step 3 can also succeed when byte 14 of I⊕RI \oplus R happens to be 0x02 and the guess made the last byte 0x02. The attacker detects this by changing R[14]R[14] 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 P1P_1 is the 16 ASCII characters amount=000009500; because the message fills a whole block, a second block P2P_2 of 16 bytes, each 0x10, follows. The IV is

C0C_0 = 3f1a7c22d4e90b6851a0f2c48d779ec5,

so C0[14]=𝟶𝚡𝟿𝚎C_0[14] = \texttt{0x9e} and C0[15]=𝟶𝚡𝚌𝟻C_0[15] = \texttt{0xc5}. The attacker sees C0C_0, C1C_1 and C2C_2. The receiver answers every submitted ciphertext either with padding error or with some other error.

Since C1=Ek(P1⊕C0)C_1 = E_k(P_1 \oplus C_0), the intermediate value is I=Dk(C1)=P1⊕C0I = D_k(C_1) = P_1 \oplus C_0, whatever the key. The attacker does not know II; 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 I[15]=𝟶𝚡𝟹𝟶⊕𝟶𝚡𝚌𝟻=𝟶𝚡𝚏𝟻I[15] = \texttt{0x30} \oplus \texttt{0xc5} = \texttt{0xf5} and I[14]=𝟶𝚡𝟹𝟶⊕𝟶𝚡𝟿𝚎=𝟶𝚡𝚊𝚎I[14] = \texttt{0x30} \oplus \texttt{0x9e} = \texttt{0xae}.

Byte 15. The attacker submits RR followed by C1C_1, where RR is 15 zero bytes followed by a guess gg, for g=𝟶𝚡𝟶𝟶,𝟶𝚡𝟶𝟷,…g = \texttt{0x00}, \texttt{0x01}, \dots in turn. The last decrypted byte is 𝟶𝚡𝚏𝟻⊕g\texttt{0xf5} \oplus g. Byte 14 is 𝟶𝚡𝚊𝚎⊕𝟶𝚡𝟶𝟶=𝟶𝚡𝚊𝚎\texttt{0xae} \oplus \texttt{0x00} = \texttt{0xae}, not 0x02, so the only valid padding is a last byte 0x01, reached at g=𝟶𝚡𝚏𝟺g = \texttt{0xf4}. That is the 245th question, because 0xf4 is 244 and the count started at 0. The attacker concludes I[15]=𝟶𝚡𝚏𝟺⊕𝟶𝚡𝟶𝟷=𝟶𝚡𝚏𝟻I[15] = \texttt{0xf4} \oplus \texttt{0x01} = \texttt{0xf5} and P1[15]=𝟶𝚡𝚏𝟻⊕𝟶𝚡𝚌𝟻=𝟶𝚡𝟹𝟶P_1[15] = \texttt{0xf5} \oplus \texttt{0xc5} = \texttt{0x30}, the character 0. In binary the last step is 𝟷𝟷𝟷𝟷𝟶𝟷𝟶𝟷⊕𝟷𝟷𝟶𝟶𝟶𝟷𝟶𝟷=𝟶𝟶𝟷𝟷𝟶𝟶𝟶𝟶\mathtt{11110101} \oplus \mathtt{11000101} = \mathtt{00110000}.

Byte 14. The attacker sets R[15]=𝟶𝚡𝚏𝟻⊕𝟶𝚡𝟶𝟸=𝟶𝚡𝚏𝟽R[15] = \texttt{0xf5} \oplus \texttt{0x02} = \texttt{0xf7}, which makes the last decrypted byte 0x02, and tries values hh for R[14]R[14]. The padding becomes valid when byte 14 is also 0x02, that is when 𝟶𝚡𝚊𝚎⊕h=𝟶𝚡𝟶𝟸\texttt{0xae} \oplus h = \texttt{0x02}, so at h=𝟶𝚡𝚊𝚌h = \texttt{0xac}, the 173rd question. Then I[14]=𝟶𝚡𝚊𝚌⊕𝟶𝚡𝟶𝟸=𝟶𝚡𝚊𝚎I[14] = \texttt{0xac} \oplus \texttt{0x02} = \texttt{0xae} and P1[14]=𝟶𝚡𝚊𝚎⊕𝟶𝚡𝟿𝚎=𝟶𝚡𝟹𝟶P_1[14] = \texttt{0xae} \oplus \texttt{0x9e} = \texttt{0x30}, again the character 0.

Two bytes cost 245+173=418245 + 173 = 418 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 16×256=409616 \times 256 = 4096, and every other block CiC_i is attacked the same way with Ci−1C_{i-1} 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 EkE_k be a block cipher with key kk and a block length of nn bits (n=128n = 128 for AES), let DkD_k be its inverse, ⊕\oplus bitwise XOR, C0C_0 the IV and P1,…,PmP_1, \dots, P_m the padded plaintext blocks. Then Ci=Ek(Pi⊕Ci−1),Pi=Dk(Ci)⊕Ci−1,1≤i≤m.C_i = E_k(P_i \oplus C_{i-1}), \qquad P_i = D_k(C_i) \oplus C_{i-1}, \qquad 1 \le i \le m.

Malleability. If an attacker replaces Ci−1C_{i-1} by Ci−1⊕ΔC_{i-1} \oplus \Delta, for a block Δ\Delta of the attacker’s choice, the receiver obtains Pi⊕ΔP_i \oplus \Delta in place of PiP_i. If i≥2i \ge 2, block Pi−1P_{i-1} also changes, to an unpredictable value, because it is decrypted from the changed block; all other blocks are unchanged. If i=1i = 1, the changed block is the IV and only P1P_1 changes.

Confidentiality §1.8. Assume that EkE_k is a pseudorandom permutation §15.2, meaning that no feasible computation can tell it apart from a permutation of nn-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 2n2^n, the birthday bound §5.6. Equal ciphertext blocks Ci=CjC_i = C_j mean equal cipher inputs, which gives Pi⊕Pj=Ci−1⊕Cj−1P_i \oplus P_j = C_{i-1} \oplus C_{j-1}, a fact about the plaintext computed from public values. For AES such blocks become likely after about 2642^{64} blocks, so keys are changed long before.

Padding oracle. Let the oracle take blocks RR and CC and return 1 if Dk(C)⊕RD_k(C) \oplus R ends in valid padding, 0 otherwise. Because the attacker chooses RR, each answer tests a property, selected by the attacker, of the fixed unknown block Dk(C)D_k(C); 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 VV before choosing the next plaintext, the attacker can test a guess GG for an earlier block PiP_i by having G⊕Ci−1⊕VG \oplus C_{i-1} \oplus V encrypted: the cipher input is then G⊕Ci−1G \oplus C_{i-1}, so the result equals CiC_i exactly when G=PiG = P_i. 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 P1P_1 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 Dk(Ci)⊕Ci−1D_k(C_i) \oplus C_{i-1}: the block cipher’s decryption of ciphertext block CiC_i, combined by XOR (exclusive or) with the previous ciphertext block Ci−1C_{i-1}. The attacker can put any block in place of Ci−1C_{i-1}, and so XOR any chosen value onto the unknown Dk(Ci)D_k(C_i). 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 Dk(Ci)D_k(C_i), 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].