Section 15.3

Block cipher

Definition

A block cipher is a pair of keyed algorithms §14.1, encryption and decryption, that work on blocks: bit strings of one fixed length nn. Encryption turns a secret key and an nn-bit plaintext block into an nn-bit ciphertext block; decryption with the same key returns the plaintext block. For each key, encryption is therefore a permutation, a one-to-one rearrangement of all nn-bit blocks. AES (Advanced Encryption Standard), with n=128n = 128 and keys of 128, 192 or 256 bits, is the standard example. A mode of operation extends a block cipher to messages.

The idea

A block cipher solves a narrow problem. It transforms one short block so that, without the key, the result looks unrelated to the input, and with the key, the transformation can be undone exactly. Cryptographers study this small building block intensely and model a good block cipher as a pseudorandom permutation §15.2: under a random secret key, it cannot feasibly be told apart from a rearrangement of all nn-bit blocks chosen uniformly at random.

For one key, a block cipher acts as a codebook: a table that pairs each of the 2n2^n possible plaintext blocks with its ciphertext block. The table is far too large to write down, since for n=128n = 128 it has 21282^{128} rows, so the cipher computes any row from the key. The picture shows the weakness of using the codebook directly: the same plaintext block always looks up to the same ciphertext block.

Real data is not one block. A sender has a 20-byte sensor reading, a 32-byte command or a 1-MiB file. Turning one-block encryption into a secure message format raises questions that the block cipher does not answer: how to split a message and fill a last block that is too short, how to make equal blocks and equal messages encrypt differently, how to let the receiver detect a changed or rearranged ciphertext, and how to reject an old message sent again. A mode of operation, a rule for applying the block cipher to many blocks, answers the first two questions. Authentication §1.6 and replay protection need further mechanisms.

How it works

The interface.

Inside. Almost every block cipher repeats a simple transformation, called a round, many times. A key schedule expands the key into one round key per round. Two designs dominate. A substitution-permutation network replaces small pieces of the block through fixed tables and then mixes the pieces, and every one of its steps is invertible; AES works this way, as the AES round structure §15.4 entry describes. A Feistel network splits the block into two halves and in each round combines a keyed function of one half into the other half, which is invertible whatever that function is; DES (Data Encryption Standard), the United States standard block cipher of 1977 [364], works this way.

From block to message. NIST Special Publication (SP) 800-38A defines five modes of operation for confidentiality §1.8. Three of them show the main ideas. Here P1,P2,…P_1, P_2, \dots are the plaintext blocks, C1,C2,…C_1, C_2, \dots the ciphertext blocks, and ⊕\oplus is bitwise XOR (exclusive or), which gives 1 in each bit position where the two inputs differ.

  1. ECB (electronic codebook) encrypts each block separately: Ci=EK(Pi)C_i = E_K(P_i). Equal plaintext blocks give equal ciphertext blocks.

  2. CBC §15.6 (cipher block chaining) XORs each plaintext block with the previous ciphertext block before encrypting it: Ci=EK(Pi⊕Ci−1)C_i = E_K(P_i \oplus C_{i-1}). The first block uses an initialization vector C0C_0, a block that is sent with the message and must be unpredictable to an attacker. The message must be padded to a whole number of blocks.

  3. CTR §15.5 (counter mode) encrypts a sequence of distinct counter blocks and XORs the outputs onto the message. It needs no padding, and decryption uses only EKE_K.

None of these modes lets the receiver detect a change to the ciphertext. Authenticated encryption §5.16 closes that gap: it adds a tag, a short value computed with the key that the receiver checks before accepting the plaintext. GCM (Galois/Counter Mode) and CCM §5.17 (counter with cipher block chaining message authentication code) are the common schemes built on a block cipher. Replay protection, such as a message counter that the receiver checks, comes from the protocol on top.

Worked example

ECB shows equal blocks. FIPS 197, the AES standard, gives this example for AES with a 128-bit key KK: the plaintext block PP encrypts to the ciphertext block CC, all written in hexadecimal.

KK = 000102030405060708090a0b0c0d0e0f
PP = 00112233445566778899aabbccddeeff
CC = 69c4e0d86a7b0430d8cdb78070b4c55a

Now encrypt a 32-byte message that consists of PP twice. AES blocks are 16 bytes, so the message is 32/16=232 / 16 = 2 blocks. ECB encrypts each block separately, so the ciphertext is CC twice: C‖CC \mathbin{\Vert} C, where ‖\Vert joins two blocks end to end. An observer without the key sees two equal ciphertext blocks and learns that the two plaintext blocks are equal. In a table of records or a bitmap image, such equalities reveal the structure of the plaintext.

A short last block. A 20-byte message needs ⌈20/16⌉=2\lceil 20 / 16 \rceil = 2 blocks, where ⌈x⌉\lceil x \rceil means xx rounded up to a whole number: one full block of 16 bytes and 20−16=420 - 16 = 4 further bytes. ECB and CBC can encrypt only whole blocks, so the second block must be filled. The padding rule of PKCS #7 (Public-Key Cryptography §5.15 Standards number 7) appends 16−4=1216 - 4 = 12 bytes, each with the value 12 (0x0c); the receiver reads the last byte and removes that many bytes. A message that already fills whole blocks gets a full extra block of 16 bytes with the value 16, so that the rule is never ambiguous. The ciphertext is 32 bytes. CTR instead uses the first 20 bytes of a 32-byte keystream and produces 20 bytes of ciphertext.

Correct is not secure. Take a toy block cipher with 4-bit blocks and 4-bit keys: EK(P)=P⊕KE_K(P) = P \oplus K. With K=𝟷𝟶𝟷𝟶K = \texttt{1010} and P=𝟶𝟷𝟷𝟶P = \texttt{0110}, the ciphertext is C=𝟶𝟷𝟷𝟶⊕𝟷𝟶𝟷𝟶=𝟷𝟷𝟶𝟶C = \texttt{0110} \oplus \texttt{1010} = \texttt{1100}. Decryption XORs the key again: 𝟷𝟷𝟶𝟶⊕𝟷𝟶𝟷𝟶=𝟶𝟷𝟷𝟶\texttt{1100} \oplus \texttt{1010} = \texttt{0110}, the original block. The toy cipher is correct. One known pair of plaintext and ciphertext breaks it: P⊕C=𝟶𝟷𝟷𝟶⊕𝟷𝟷𝟶𝟶=𝟷𝟶𝟷𝟶=KP \oplus C = \texttt{0110} \oplus \texttt{1100} = \texttt{1010} = K.

Stated precisely

A block cipher with key length kk and block length nn is a pair of deterministic algorithms E,D:{0,1}k×{0,1}n→{0,1}nE, D\colon \{0,1\}^k \times \{0,1\}^n \to \{0,1\}^n such that D(K,E(K,P))=Pfor every key K and every block P.D(K, E(K, P)) = P \quad \text{for every key } K \text{ and every block } P. This correctness condition forces E(K,⋅)E(K, \cdot) to be one-to-one, hence a permutation of the 2n2^n blocks, for every key KK.

Security is a separate claim, stated as pseudorandom-permutation security: when KK is chosen uniformly at random and kept secret, no adversary that runs for at most tt steps and asks for at most qq encryptions can tell E(K,⋅)E(K, \cdot) apart from a uniformly random permutation with advantage above ε\varepsilon. The advantage is the difference between the probabilities that the adversary answers “real” in the two cases, and tt, qq and ε\varepsilon are stated together. Nobody proves this for a real block cipher. For AES it is a conjecture supported by more than two decades of public analysis, and the security proofs of modes of operation assume it.

Encryption of messages has a different and stronger contract. The usual one is indistinguishability under chosen-plaintext attack. An attacker who may obtain encryptions of messages of the attacker’s choice picks two messages M0M_0 and M1M_1 of equal length. A hidden fair coin selects one of them, and the attacker receives its encryption. The attacker must not be able to guess which message was encrypted with probability above 1/2+ε1/2 + \varepsilon, for a small stated ε\varepsilon. No deterministic scheme, one in which the same key and message always give the same ciphertext, meets this contract: the attacker obtains the encryption of M0M_0 beforehand and compares. ECB is deterministic and fails. A secure mode adds a random initialization vector or a nonce §5.18, a value never used twice with one key.

Ciphertext integrity §1.9, meaning that the receiver rejects every ciphertext that the key holder did not produce, is a third contract. Neither the block cipher nor ECB, CBC or CTR provides it.

Limits and common mistakes

A longer key does not give a larger block. AES-256 still encrypts 16-byte blocks. The block length, not the key length, sets how much data one key may protect before the birthday bound §5.6 makes repeated blocks likely: about 2n/22^{n/2} blocks, which is 2642^{64} blocks for AES and 2322^{32} blocks for 64-bit block ciphers such as Triple DES.

Do not invent modes or use them outside their rules. ECB reveals equal blocks. CBC without a message authentication code §5.7 falls to padding oracle attacks, in which the receiver’s different reactions to correct and incorrect padding let an attacker decrypt a message byte by byte (Vaudenay, 2002 [365]). CTR with a repeated counter block reveals the XOR of two plaintexts.

An attacker can reorder, delete or repeat ECB ciphertext blocks, and the receiver decrypts the result without noticing. In CBC and CTR, flipping chosen ciphertext bits flips predictable plaintext bits.

A block cipher is neither a hash function nor a message authentication code, although both can be built from one. CMAC §15.7 (cipher-based message authentication code) is a message authentication code built from a block cipher.

Correct mathematics can still leak the key through a side channel §1.29. AES implemented with lookup tables indexed by secret data leaks key bits through cache timing (Bernstein, 2005 [336]). Use processor instructions for AES or a constant-time implementation §5.36, one whose timing and memory accesses do not depend on secret data.

The security model assumes independent, uniformly random keys. A key taken directly from a password, or keys with a known relation to one another, fall outside it.

Questions answered

Why is a block cipher alone not an arbitrary-length secure-message format?

A block cipher alone is not a secure message format because it encrypts exactly one block of a fixed length, nn bits (128 bits for AES, the Advanced Encryption Standard), and encrypts it deterministically, whereas a message format must handle any length, hide repetition and let the receiver reject altered or replayed messages. Applying the cipher to each block separately, the electronic codebook (ECB) mode, shows the gaps. A message whose length is not a multiple of nn needs padding or a different construction. Equal plaintext blocks give equal ciphertext blocks, so patterns in the plaintext show through. An attacker can reorder, delete or repeat blocks without detection. Nothing stops a recorded message from being accepted again.

A secure format therefore adds three things: a mode of operation that uses a nonce (a value never repeated under one key) or a random initialization vector; a tag that the receiver checks, which covers the ciphertext and its header and is usually provided by one authenticated encryption scheme; and a replay rule, such as a message counter that the receiver checks.

Related entries: Pseudorandom permutation (PRP) (§15.2), AES round structure (§15.4), Authenticated encryption (AEAD) (§5.16), Counter mode (CTR) (§15.5), Cipher block chaining (CBC) (§15.6), Cipher-based MAC (CMAC) (§15.7). Further reading: [47, 66, 137, 336, 364, 365].