Speed

CBC Chains the Blocks

In CBC mode each plaintext block is XORed with the previous ciphertext block before it is encrypted, and the first one with the IV, so equal plaintext blocks no longer give equal ciphertext blocks.

The question

ECB showed that equal plaintext blocks give equal ciphertext blocks. How can a mode avoid that while still using only the block cipher? CBC, the cipher block chaining mode, mixes each plaintext block with something that differs from block to block before the cipher sees it.

SP 800-38A section 6.2 defines it: C_1 = CIPH_K(P_1 xor IV) and C_j = CIPH_K(P_j xor C_(j-1)) for j from 2 on. The IV, the initialization vector, takes the place of a ciphertext block before the first block. This lesson shows the XOR step with the first published vector of that standard, and then a command does the whole encryption.

Work it by hand

Appendix F.2.1 of SP 800-38A gives the test material key 2b7e1516 28aed2a6 abf71588 09cf4f3c, the IV 00010203 04050607 08090a0b 0c0d0e0f and two plaintext blocks. The first block starts 6b c1 be e2 and the IV starts 00 01 02 03. XOR byte by byte: 6b with 00 is 6b, c1 with 01 is c0, be with 02 is bc, e2 with 03 is e1. The first block that AES sees starts 6b c0 bc e1.

The second block does not use the IV. It starts ae 2d 8a 57 and is XORed with the first ciphertext block, which starts 76 49 ab ac: ae with 76 is d8, 2d with 49 is 64, 8a with ab is 21, 57 with ac is fb. The input of block 2 starts d8 64 21 fb.

Run it

The program takes the two plaintext blocks p1 and p2, the IV iv and the first ciphertext block c1 of that vector, all as hex. Lines 1 to 7 define a function that XORs two hex texts byte by byte. Line 8 XORs the first plaintext block with the IV; line 9 XORs the second with c1. Line 10 returns the second result as an extra value and line 11 returns the first, the block that goes into AES.

The picture shows each plaintext block, what it is XORed with and the result, with the bytes that changed marked. The program shows the input of AES, not AES: the ciphertext block c1 is a published value that the page does not compute.

Plaintext block 1 starting 6b c1 be e2, the IV starting 00 01 02 03, the AES input 6b c0 bc e1 with every byte but the first marked red, plaintext block 2, ciphertext block 1 and the AES input d8 64 21 fb with its bytes marked red.
fn xor_hex(a: string, b: string) -> string {
  let r = "";
  for (i in range(0, str_len(a) / 2)) {
    r = r + hex_byte(xor_byte(byte_at(a, i), byte_at(b, i)));
  }
  return r;
}
let mixed = xor_hex(p1, iv);
let chained = xor_hex(p2, c1);
lmcode_return_put("second", chained);
let answer = mixed;
{
  "answer": "6bc0bce12a459991e134741a7f9e1925",
  "second": "d86421fb9f1a1eda505ee1375746972c"
}

You can change all four inputs. The program XORs as many bytes as the first operand has, and a shorter second operand counts as zero bytes, so nothing stops the run. If you change p1 or iv, c1 is no longer the ciphertext of the new input, because the page does not run AES.

Your turn

Write one command that encrypts the file in/message.bin with AES-128 in CBC mode, with the key and the IV of the vector above, and writes the ciphertext to out.bin. The command below runs and writes a ciphertext. Read what it does and edit it until it matches the question. Check runs it on the message shown and on five others.

The 21 message bytes in hex, starting 73 65 6e 64, and the first 32 of the 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.

# Encrypts in/message.bin under the test key, result in out.bin.
# The key and the IV are public test material.
openssl enc -aes-128-cbc -K 2b7e151628aed2a6abf7158809cf4f3c \
  -iv 00000000000000000000000000000000 -nosalt \
  -in in/message.bin -out out.bin
{
  "answer": "deadf93ccbce249050747f9e54f2934f94a799c299c92de1e3e8dcce96a31dbe",
  "file": "out.bin",
  "bytes": 32
}

Write one openssl command that encrypts the file in/message.bin with AES-128 in CBC mode, key 2b7e151628aed2a6abf7158809cf4f3c and IV 000102030405060708090a0b0c0d0e0f (SP 800-38A section 6.2), with the standard padding and no salt header, and writes the ciphertext to out.bin. Check runs your command on the message shown and on five others.

Read the result carefully

The key and the IV in the command are literals, public test material from the standard; Check changes only the message. The page does not show how to choose an IV; SP 800-38A section 6.2 says it need not be secret but must be unpredictable, and the next lesson shows what repeating it does.

Run the command on the 48-byte text of the ECB lesson and the two repeated blocks come out different. OpenSSL fills a key or an IV that is too short with zero bytes and exits with code 0, so check the length of each literal.

Recap

  • CBC: C_1 = CIPH_K(P_1 xor IV), C_j = CIPH_K(P_j xor C_(j-1)) (SP 800-38A, section 6.2).
  • The first AES input of the published vector starts 6b c0 bc e1; the second starts d8 64 21 fb.
  • Each block depends on all blocks before it, so equal plaintext blocks give different ciphertext blocks.
  • The program shows the XOR before AES; the command does the AES.