Speed

A CBC Bit Flip Garbles a Block

Flip one bit of a CBC ciphertext block and decryption still runs: the plaintext block that belongs to it comes out as unrelated bytes, and the next plaintext block has exactly that bit flipped.

The question

CBC decryption chains blocks just as encryption does. SP 800-38A section 6.2 gives it as P_1 = CIPH^-1(C_1) xor IV and P_j = CIPH^-1(C_j) xor C_(j-1). If someone changes one bit of a ciphertext block, what happens to the plaintext that comes out, and does decryption notice?

This lesson flips one bit in a ciphertext made with real AES-128 under a test key, decrypts both versions with OpenSSL, and compares the plaintexts byte by byte. The forgery that uses this to rewrite a known message is in a-bit-flip-changes-the-message; this page shows what the flip does to the blocks.

Work it by hand

The demo message is the 48-byte text of the ECB lesson, YELLOW SUBMARINE twice and then sixteen more byt, encrypted in CBC with the test key and the IV 00010203 04050607 08090a0b 0c0d0e0f. Padding makes it four blocks; the fourth holds only padding. Bit 0 of byte 0 of the ciphertext is flipped, so C_1 changes in one bit.

P_1 = CIPH^-1(C_1) xor IV: the block cipher gets a changed input, so the output is unrelated to the old one. P_2 = CIPH^-1(C_2) xor C_1: C_2 is unchanged, so the first term is the same, and C_1 differs from the old value in one bit, so P_2 differs in that same bit. P_3 and P_4 use unchanged blocks.

Byte 0 of block 2 is Y, 59 in hex, and changing bit 0 makes it 58, X.

Run it

The program takes the plaintext of the clean ciphertext, clean, and the plaintext of the flipped one, flipped, both as hex. They were made once with the native enc command: encrypt the 48 bytes in CBC, flip the bit, then decrypt both with enc -d -aes-128-cbc -nopad, which writes every block including the padding.

The program compares the two byte by byte and returns the positions that differ. Line 1 takes the length, the loop of lines 3 to 5 collects the positions and line 6 returns them. The picture marks the changed bytes in red.

The 64 bytes of the clean plaintext in four lines, and the 64 bytes after the flip: 15 bytes of the first line and the first byte of the second line are red, the rest are unchanged.
let n = str_len(clean) / 2;
let diff = int_list();
for (i in range(0, n)) {
  if (byte_at(clean, i) != byte_at(flipped, i)) { diff.add(i); }
}
let answer = diff;
{
  "answer": [
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
    12,
    13,
    14,
    16
  ]
}

You can change both inputs to any hex. The program goes over as many bytes as clean has; a shorter flipped is read as zero bytes, so nothing stops the run, and an empty clean gives an empty list.

Your turn

The command below is meant to decrypt the ciphertext in in/cipher.bin that was made in CBC mode with the test key and IV, and to write all of the plaintext blocks to out.bin, the padding block included, whatever the blocks contain. Run it, read what it does, and edit it until it matches. Check runs it on the file shown and on four others.

The first 16 bytes of the 64-byte ciphertext, and the 64 bytes that the shown command wrote to out.bin, with their count.

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-ctr -K 2b7e151628aed2a6abf7158809cf4f3c \
  -iv 000102030405060708090a0b0c0d0e0f -in in/cipher.bin -out out.bin
{
  "answer": "78982d09b446d62d64b185c924cad3685b2ba5dbd363730f373d3db959ea17022e90610600765710361e31713c2a0f0f1384ac0c98a3d81a0823b9d7543cf8f4",
  "file": "out.bin",
  "bytes": 64
}

in/cipher.bin holds 64 bytes made with AES-128 in CBC mode, key 2b7e151628aed2a6abf7158809cf4f3c and IV 000102030405060708090a0b0c0d0e0f, and possibly changed afterwards. Write one openssl command that decrypts it and writes all 64 plaintext bytes to out.bin, padding block included. Check runs your command on the file shown and on four others.

Read the result carefully

Decryption with -nopad succeeds with exit code 0 for every ciphertext here: nothing checks that the message is the one that was sent. A flip in block 1 garbles block 1 and flips a bit in block 2; a flip in the last block has no next block to carry it on. The padding rule can notice some damage, which the next lesson shows.

Bytes of the garbled block can be equal to the old ones by accident; the demo has one such byte, position 15 of block 1, so do not read the list of positions as "all 16 bytes change". The key and IV are public test material. The page does not show how the flip would be chosen on purpose.

Recap

  • CBC decryption: P_j = CIPH^-1(C_j) xor C_(j-1) (SP 800-38A, section 6.2).
  • One flipped bit in C_j garbles the whole plaintext block j and flips the same bit in block j + 1.
  • Decryption still ends with exit code 0, so the change is not noticed.
  • Bytes of the garbled block may match the old ones by coincidence.