Path: blob/main/foundations/03-galois-fields-aes/break/ecb-mode-pattern-leak.ipynb
483 views
Break: ECB Mode Pattern Leakage
Module 03 | Breaking Weak Parameters
Identical plaintext blocks produce identical ciphertext blocks. This is catastrophic.
Why This Matters
A block cipher like AES encrypts fixed-size blocks (16 bytes each). But real messages are longer than one block. A mode of operation defines how to apply the block cipher to multi-block messages.
The simplest mode is ECB (Electronic Codebook): encrypt each block independently with the same key. This sounds reasonable, but it has a fatal flaw:
Identical plaintext blocks produce identical ciphertext blocks. Any pattern in the plaintext is preserved in the ciphertext, even though the individual block values are scrambled. An attacker learns the structure of your message without decrypting a single byte.
The Scenario
We'll use a toy block cipher on 8-bit blocks (1-byte blocks) to make the patterns visible. The cipher is the AES S-box itself --- a bijection on single bytes that provides good confusion but (in ECB mode) zero diffusion across blocks.
We'll encrypt structured messages and see how the structure leaks through.
Step 1: ECB Mode Encryption
In ECB mode, we encrypt each block independently:
No chaining, no IV, no interaction between blocks. Each block is a standalone encryption.
Step 2: Visualize the Pattern Leakage
Even though individual byte values are different (the S-box scrambled them), the pattern structure is perfectly preserved. Let's visualize this with a histogram and a structural comparison.
Step 3: Compare with CBC Mode
In CBC (Cipher Block Chaining) mode, each block is XORed with the previous ciphertext block before encryption:
The chaining means identical plaintext blocks produce different ciphertext blocks (unless also happens to be the same, which is astronomically unlikely).
The Fix: Chained Modes of Operation
Never use ECB for multi-block messages. Use a mode that chains blocks together:
| Mode | How it works | Advantage |
|---|---|---|
| CBC | Hides patterns, widely deployed | |
| CTR | Parallelizable, random access | |
| GCM | CTR + GHASH authentication | Encryption + integrity (gold standard) |
All of these ensure that identical plaintext blocks produce different ciphertext blocks.
AES-GCM is the standard choice in TLS 1.3, and we'll explore it in the Connect notebook on AES-GCM authenticated encryption.
Exercises
Exercise 1
Encrypt the string 'HELLO HELLO HELLO HELLO HELLO' in both ECB and CBC mode. How many repeated ciphertext blocks does each mode produce?
Exercise 2
Implement CTR (Counter) mode: . Encrypt the same structured image. Does it hide patterns like CBC?
Exercise 3
In CBC mode, what happens if you reuse the same IV for two different messages that share the same first block? What does the attacker learn from where and ?
Summary
| Property | ECB | CBC / CTR / GCM |
|---|---|---|
| Equal plaintext blocks → equal ciphertext? | Yes (fatal) | No |
| Pattern leakage | Complete | None |
| Block independence | Each block isolated | Blocks chained together |
| Safe for multi-block messages? | No | Yes |
Key takeaways:
ECB mode encrypts blocks independently, so patterns in the plaintext are perfectly preserved in the ciphertext.
An attacker can detect which plaintext blocks are equal without knowing the key.
Chained modes (CBC, CTR, GCM) break this by making each ciphertext block depend on more than just its plaintext block.
This is why ECB should never be used for messages longer than one block.
The underlying block cipher (AES) is perfectly fine --- the weakness is entirely in the mode.
Back to Module 03: Galois Fields and AES