Speed

The IV Must Not Repeat

With one key, CBC encryptions that use the same IV and start with the same plaintext blocks start with the same ciphertext blocks, so a repeated IV shows where two messages first differ; a new IV makes the whole ciphertext unrelated.

The question

The IV, the initialization vector, is the value that CBC XORs into the first plaintext block. It travels with the message and need not be secret. SP 800-38A section 5.3 says that for CBC it must be unpredictable. Why does it matter if two messages under one key use the same IV?

CBC makes every block depend on all the blocks before it, starting at the IV. If two messages start with the same blocks and use the same IV, the chain is the same for those blocks. This lesson compares ciphertexts of made-up messages, made once with real AES-128 under a test key.

Work it by hand

Take three plaintexts of three 16-byte blocks each. The first two blocks are north and south (padded with dots); the third block is east in the first plaintext and west in the second. The third plaintext is the first one again.

Encrypt the first two with the same IV. Block 1 is CIPH(P_1 xor IV): same input, same output. Block 2 is CIPH(P_2 xor C_1): same input again. Block 3 has different plaintext blocks, so its input differs, and every later block differs with it. Encrypt the third plaintext with another IV: block 1 already differs, and then the whole chain does.

So the first ciphertext and the second must agree in blocks 0 and 1 only, and the first and third in none.

Run it

The program takes three ciphertexts as hex: first, second (the same IV, the third block of plaintext changed) and third (the first plaintext under another IV). Lines 1 and 2 take the number of whole blocks that all three have. The loop of lines 5 to 12 compares each block of first with the block of second and of third at the same position, writes a row for the table and keeps the positions that agree with second.

Line 13 returns the table and line 14 returns the list of positions where first and second agree. The test key and both IVs are test material; the ciphertexts were made once with the native enc command.

A table of four blocks, 0 to 3: against the second ciphertext, blocks 0 and 1 are equal and blocks 2 and 3 differ; against the third ciphertext, every block differs.

Public test material, generated for this lesson. Never use it for anything real.

let m = math_min(hex_len(second), hex_len(third));
let n = math_min(hex_len(first), m) / 16;
let same = int_list();
let rows = str_list();
for (i in range(0, n)) {
  let a = hex_slice(first, i * 16, 16);
  let s = "differ";
  let t = "differ";
  if (a == hex_slice(second, i * 16, 16)) { s = "equal"; same.add(i); }
  if (a == hex_slice(third, i * 16, 16)) { t = "equal"; }
  rows.add(str(i) + "|" + s + "|" + t);
}
lmcode_return_put("rows", rows);
let answer = same;
{
  "answer": [
    0,
    1
  ],
  "rows": [
    "0|equal|differ",
    "1|equal|differ",
    "2|differ|differ",
    "3|differ|differ"
  ]
}

You can change the three ciphertexts to any hex of whole bytes. The program compares as many whole blocks as the shortest has, and a shorter input gives fewer rows. The table shows at most 16 rows; the returned list is not cut.

Your turn

The program below is meant to return how many leading 16-byte blocks two ciphertexts have in common: the number of blocks, counted from the start, up to the first position where they differ. Both ciphertexts were made with AES-128 under the same test key. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six others.

The first 16 bytes of each of the two ciphertexts, which are the same bytes, and the number that the shown program returns.

Public test material, generated for this lesson. Never use it for anything real.

let a = hex_slice(cipher_a, 0, 16);
let b = hex_slice(cipher_b, 0, 16);
let answer = 0;
if (a == b) { answer = 1; }
{
  "answer": 1
}

cipher_a and cipher_b are the hex text of two ciphertexts made with AES-128 under the same key, each of whole 16-byte blocks. Return the number of leading 16-byte blocks that are equal in both: count from the first block and stop at the first position where they differ. Check runs each candidate on the data shown and on six others.

Read the result carefully

What the comparison shows is where two plaintexts first differ, not what they say. Someone who sees both ciphertexts and the IVs learns that much, and this is why a new, unpredictable IV is used for every message.

The program compares bytes and does not know the mode: it counts leading blocks of any two ciphertexts. The page does not show how an IV is made or sent, and it does not show timing or any attack beyond the comparison. Equal IVs on a page are test material, not advice. None of this detects a change in a message; the last lesson of this book shows what a change does.

Recap

  • CBC output depends on the IV and on every block before; same IV and same start give the same first blocks.
  • Two ciphertexts under one IV that agree in k blocks show that their plaintexts agree in k blocks and differ in the next.
  • A different IV makes the whole ciphertext different, even for the same message.
  • SP 800-38A section 5.3: for CBC the IV need not be secret but must be unpredictable.