Modes Do Not Authenticate
ECB, CBC and CTR all decrypt a changed ciphertext without complaint; they differ only in what the change does to the plaintext. ECB garbles one block, CBC garbles one and flips a bit in the next, CTR flips exactly the bit that was flipped.
The question
A ciphertext travels over a network and one byte of it is changed on the way. The receiver decrypts. Does decryption notice, and what does the plaintext look like afterwards?
None of ECB, CBC and CTR contains a check of any kind, so none notices; what differs is the damage. This lesson takes the same 48-byte message, encrypts it in each mode with real AES-128 under a test key, flips one bit of ciphertext byte 17 and counts the plaintext blocks that changed. The rule for each mode comes first; the run then checks it on this one example.
Work it by hand
Number the blocks from 0, as the program does. Byte 17 is in block 1, which holds bytes 16 to 31. Ciphertext block 1 is changed.
ECB decrypts each block alone, P_j = CIPH^-1(C_j), so block 1 comes out as unrelated bytes and no other block changes. CBC decrypts P_j = CIPH^-1(C_j) xor C_(j-1), so block 1 is garbled, and block 2, which XORs with the changed block, has the same bit flipped. CTR computes P_j = C_j xor O_j, and the keystream block O_j does not depend on the ciphertext, so one bit of block 1 changes and nothing else.
The prediction is blocks 1; 1 and 2; 1.
Run it
The program takes the clean plaintext clean and the plaintexts that came out of decrypting the three flipped ciphertexts, ecb, cbc and ctr, all as hex. They were made once with the native enc command under the test key: encrypt in each mode (ECB and CBC with -nopad, the message being whole blocks), flip bit 0 of byte 17, decrypt with the same command. The test key and IVs are the ones of the previous lessons.
The function of lines 1 to 7 counts the blocks that differ. Lines 8 to 11 collect the three counts and lines 12 to 15 build the table; line 16 returns the table and line 17 returns the counts.

fn changed(a: string, b: string) -> int {
let n = 0;
for (i in range(0, str_len(a) / 32)) {
if (hex_slice(a, i * 16, 16) != hex_slice(b, i * 16, 16)) { n = n + 1; }
}
return n;
}
let counts = int_list();
counts.add(changed(clean, ecb));
counts.add(changed(clean, cbc));
counts.add(changed(clean, ctr));
let rows = str_list();
rows.add("ECB|" + str(counts.at(0)));
rows.add("CBC|" + str(counts.at(1)));
rows.add("CTR|" + str(counts.at(2)));
lmcode_return_put("rows", rows);
let answer = counts;
{
"answer": [
1,
2,
1
],
"rows": [
"ECB|1",
"CBC|2",
"CTR|1"
]
}You can change all four inputs to any hex of whole bytes; the function compares as many whole blocks as clean has, and a second input with fewer bytes than that stops the run, for example with "bytes 16 to 31 are past the end of the data" when clean has two blocks and the other input one.
Your turn
The program below is meant to return the plaintext block numbers, counted from 0, that change when byte pos of a ciphertext of length bytes is flipped; mode is ecb, cbc or ctr. The rule is the one above: ECB and CTR change the block that holds the byte, CBC that block and the next one if there is one. Decide whether the program follows it, and find the candidates that do. Check runs each candidate on the data shown and on nine others.

let blocks = length / 16;
let i = pos / 16;
let answer = int_list();
answer.add(i);
if (mode == "cbc") { answer.add(i + 1); }
{
"answer": [
2,
3
]
}A ciphertext of length bytes (a multiple of 16) is made with AES-128 in mode ecb, cbc or ctr, and byte pos of it is flipped before decryption. Return the list of plaintext block numbers, counted from 0, that change: for ecb and ctr the block that holds the byte; for cbc that block and the next one, if there is a next one. The list is in increasing order. Check runs each candidate on the data shown and on nine others.
Read the result carefully
The rule above was checked by the author on the 48-byte example of the demo, with one flipped bit of byte 17 in each mode; the exercise applies it to other positions and lengths and does not run AES. A different byte or a different message would need its own run.
Counting changed blocks says nothing about whether the receiver would notice: none of the three modes adds a tag or any check. A tag, which an authenticated cipher adds, is the subject of the book authenticated-encryption. The key and IVs are test material.
Recap
- ECB, CBC and CTR have no integrity check: decryption of a changed ciphertext succeeds.
- ECB: the block that holds the changed byte is garbled, nothing else changes.
- CBC: that block is garbled and the next block has the same bit flipped.
- CTR: exactly the flipped bit changes in the plaintext.