Section 15.5

Counter mode (CTR)

Full name: Counter mode; CTR is an abbreviation of counter.

Definition

Counter mode (CTR) is a mode of operation, a rule for applying a block cipher §15.3 to messages of any length; a block cipher encrypts one fixed-size block under a secret key. CTR encrypts a sequence of distinct counter blocks to produce a keystream, pseudorandom bits as long as the message, and combines keystream and message by bitwise XOR (exclusive or). Decryption repeats these steps on the ciphertext. Each counter block may be used at most once per key. NIST Special Publication (SP) 800-38A specifies CTR.

The idea

A block cipher encrypts 16 bytes at a time, but a sender has a 50-byte message. Encrypting each block separately reveals which blocks are equal, and chaining modes need padding and process the blocks one after another. CTR starts instead from the one-time pad: XOR the message with a random pad of the same length, use the pad only once, and the ciphertext reveals nothing except the length. A true one-time pad must be as long as all the data ever sent. CTR makes the pad from a short key. The block cipher, keyed with the secret key, encrypts a sequence of different blocks T1,T2,T3,…T_1, T_2, T_3, \dots, the counter blocks, and its outputs laid end to end form the pad, here called the keystream. A good block cipher behaves like a pseudorandom function §15.1 on distinct inputs, so the keystream looks random to anyone without the key.

The picture also shows the rule that must never be broken. A pad used twice is no longer a one-time pad, and a counter block encrypted twice under one key produces the same keystream bytes twice. Almost every practical failure of CTR is a repeated counter block, or a change to the ciphertext that nothing detected.

CTR has practical strengths. It needs no padding, because the last block uses only as many keystream bytes as it needs. Blocks can be processed in parallel and in any order. The keystream can be computed before the message is known. Decryption uses the block cipher in the encryption direction only.

How it works

Inputs and output.

Steps.

  1. Form mm counter blocks T1,…,TmT_1, \dots, T_m that differ from each other and from every counter block used before under KK. A common layout puts a nonce §5.18, a value used for only one message under this key, in the left part of the block and a block counter in the right part, increased by one for each block. GCM (Galois/Counter Mode), for example, with its usual 96-bit nonce, uses the remaining 32 bits of the 128-bit block as the block counter. The standard incrementing function of SP 800-38A adds 1 to the rightmost bb bits of the block, wrapping from 2b−12^b - 1 to 0.

  2. Encrypt each counter block: Oi=EK(Ti)O_i = E_K(T_i). The blocks O1,O2,…O_1, O_2, \dots form the keystream.

  3. XOR: Ci=Pi⊕OiC_i = P_i \oplus O_i. For a last block of uu bits, use only the first uu bits of OmO_m and discard the rest.

  4. Send the ciphertext together with whatever the receiver needs to rebuild T1T_1, usually the nonce.

  5. The receiver rebuilds T1,…,TmT_1, \dots, T_m, computes the same OiO_i, and recovers Pi=Ci⊕OiP_i = C_i \oplus O_i.

The receiver also encrypts the counter blocks; it never runs the block cipher’s decryption. XOR undoes itself: applying XOR with the same keystream twice gives back the original bits. For this reason CTR works with any pseudorandom function, not only with a block cipher.

Worked example

One AES block. Appendix F.5.1 of SP 800-38A gives this example, in hexadecimal, for the key KK, the first counter block T1T_1, the keystream block O1=EK(T1)O_1 = E_K(T_1) and the first plaintext block P1P_1:

KK = 2b7e151628aed2a6abf7158809cf4f3c
T1T_1 = f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff
O1O_1 = ec8cdf7398607cb0f2d21675ea9ea1e4
P1P_1 = 6bc1bee22e409f96e93d7e117393172a

The ciphertext block is C1=P1⊕O1C_1 = P_1 \oplus O_1, computed byte by byte. The first byte is 𝟼𝚋⊕𝚎𝚌\texttt{6b} \oplus \texttt{ec}, which in binary is 𝟶𝟷𝟷𝟶𝟷𝟶𝟷𝟷⊕𝟷𝟷𝟷𝟶𝟷𝟷𝟶𝟶=𝟷𝟶𝟶𝟶𝟶𝟷𝟷𝟷\texttt{01101011} \oplus \texttt{11101100} = \texttt{10000111}, that is 87; the second is 𝚌𝟷⊕𝟾𝚌=𝟺𝚍\texttt{c1} \oplus \texttt{8c} = \texttt{4d}. The whole block gives

C1C_1 = 874d6191b620e3261bef6864990db6ce

The next counter block is T2=T1+1T_2 = T_1 + 1:

T2T_2 = f0f1f2f3f4f5f6f7f8f9fafbfcfdff00

The last byte ff wraps to 00, and the carry raises fe to ff.

A message that is not a whole number of blocks. A 50-byte message needs ⌈50/16⌉=4\lceil 50 / 16 \rceil = 4 counter blocks, where ⌈x⌉\lceil x \rceil is xx rounded up to a whole number: three full blocks of 16 bytes, which is 48 bytes, and a last block of 50−48=250 - 48 = 2 bytes. The four keystream blocks supply 4×16=644 \times 16 = 64 bytes; 50 are used and 64−50=1464 - 50 = 14 are discarded. The ciphertext is 50 bytes.

Keystream reuse. Use 4-bit values to see the algebra. Let the keystream be S=𝟷𝟶𝟷𝟶S = \texttt{1010}. The plaintext P=𝟶𝟷𝟷𝟶P = \texttt{0110} encrypts to C=P⊕S=𝟷𝟷𝟶𝟶C = P \oplus S = \texttt{1100}. By mistake, the same SS also encrypts Q=𝟶𝟶𝟷𝟷Q = \texttt{0011}, giving D=Q⊕S=𝟷𝟶𝟶𝟷D = Q \oplus S = \texttt{1001}. An eavesdropper who sees CC and DD computes C⊕D=𝟷𝟷𝟶𝟶⊕𝟷𝟶𝟶𝟷=𝟶𝟷𝟶𝟷C \oplus D = \texttt{1100} \oplus \texttt{1001} = \texttt{0101}. That equals P⊕Q=𝟶𝟷𝟷𝟶⊕𝟶𝟶𝟷𝟷=𝟶𝟷𝟶𝟷P \oplus Q = \texttt{0110} \oplus \texttt{0011} = \texttt{0101}: the keystream has dropped out. If the eavesdropper also knows PP, for example because it is a fixed message header, then S=P⊕C=𝟶𝟷𝟷𝟶⊕𝟷𝟷𝟶𝟶=𝟷𝟶𝟷𝟶S = P \oplus C = \texttt{0110} \oplus \texttt{1100} = \texttt{1010}, and Q=D⊕S=𝟷𝟶𝟶𝟷⊕𝟷𝟶𝟷𝟶=𝟶𝟶𝟷𝟷Q = D \oplus S = \texttt{1001} \oplus \texttt{1010} = \texttt{0011} is recovered.

Changing a ciphertext without the key. An attacker who knows that C=𝟷𝟷𝟶𝟶C = \texttt{1100} carries P=𝟶𝟷𝟷𝟶P = \texttt{0110} wants the receiver to read 1001 instead. The attacker XORs CC with 𝟶𝟷𝟷𝟶⊕𝟷𝟶𝟶𝟷=𝟷𝟷𝟷𝟷\texttt{0110} \oplus \texttt{1001} = \texttt{1111} and sends C′=𝟷𝟷𝟶𝟶⊕𝟷𝟷𝟷𝟷=𝟶𝟶𝟷𝟷C' = \texttt{1100} \oplus \texttt{1111} = \texttt{0011}. The receiver computes C′⊕S=𝟶𝟶𝟷𝟷⊕𝟷𝟶𝟷𝟶=𝟷𝟶𝟶𝟷C' \oplus S = \texttt{0011} \oplus \texttt{1010} = \texttt{1001}, the attacker’s chosen value, and has no way to notice.

Overlapping counter ranges. Suppose a sender picks a whole initial counter block for each message and increments the whole block. Message A starts at counter value 1000 and takes 10 blocks, so it uses 1000 to 1009. Message B starts at 1005 and takes 8 blocks, so it uses 1005 to 1012. The values 1005 to 1009, which is 1009−1005+1=51009 - 1005 + 1 = 5 counter blocks, are used twice, so 5 blocks of keystream are reused, although no two messages share an initial counter block.

Stated precisely

Let EKE_K be the block cipher under key KK, with block length nn bits. Let the plaintext be P1,…,PmP_1, \dots, P_m, where P1,…,Pm−1P_1, \dots, P_{m-1} have nn bits and PmP_m has uu bits with 1≤u≤n1 \le u \le n, and let T1,…,TmT_1, \dots, T_m be the counter blocks. Write firstu(O)\mathrm{first}_u(O) for the leftmost uu bits of a block OO. Then Oi=EK(Ti)(1≤i≤m),Ci=Pi⊕Oi(1≤i<m),Cm=Pm⊕firstu(Om).\begin{aligned} O_i &= E_K(T_i) \quad (1 \le i \le m), \\ C_i &= P_i \oplus O_i \quad (1 \le i < m), \\ C_m &= P_m \oplus \mathrm{first}_u(O_m). \end{aligned} Decryption computes the same OiO_i and Pi=Ci⊕OiP_i = C_i \oplus O_i, using firstu(Om)\mathrm{first}_u(O_m) for the last block.

The uniqueness requirement of SP 800-38A: across all messages encrypted under one key, for the whole time the key is in use, no counter block may occur twice.

What reuse reveals. If plaintexts PP and QQ of equal length are encrypted with the same keystream SS, then C=P⊕SC = P \oplus S and D=Q⊕SD = Q \oplus S, and C⊕D=(P⊕S)⊕(Q⊕S)=(P⊕Q)⊕(S⊕S)=P⊕Q,C \oplus D = (P \oplus S) \oplus (Q \oplus S) = (P \oplus Q) \oplus (S \oplus S) = P \oplus Q, because XOR may be regrouped and reordered freely and S⊕SS \oplus S is all zeros.

What uniqueness buys. Let σ\sigma be the total number of blocks encrypted under one key. If every counter block is distinct, then any adversary who obtains the encryptions of messages of its choice tells those ciphertexts apart from random strings of the same lengths with advantage at most ε+σ(σ−1)/2n+1\varepsilon + \sigma(\sigma - 1)/2^{n+1}. Here the advantage is the difference between the probabilities that the adversary answers “real” in the two cases, ε\varepsilon is the block cipher’s advantage bound as a pseudorandom permutation §15.2 for σ\sigma queries, and σ(σ−1)/2n+1\sigma(\sigma-1)/2^{n+1} is the cost of treating a permutation as a random function (the switching lemma). The reason: a truly random function gives independent uniform outputs on distinct inputs, so the keystream would be a one-time pad. For AES with σ=232\sigma = 2^{32} blocks, the second term is below 264/2129=2−652^{64}/2^{129} = 2^{-65}.

Capacity. A bb-bit block counter covers at most 2b2^b blocks per nonce before it wraps back to its starting value. With b=32b = 32 and 16-byte blocks that is 232×16=2362^{32} \times 16 = 2^{36} bytes, which is 64 GiB, per message. If qq nonces of ℓ\ell bits are chosen at random, the probability that two of them coincide is at most q(q−1)/2ℓ+1q(q-1)/2^{\ell+1} (the birthday bound §5.6). For ℓ=96\ell = 96 and q=232q = 2^{32} messages this is below 264/297=2−332^{64}/2^{97} = 2^{-33}.

Limits and common mistakes

CTR gives no integrity §1.9. Flipping a ciphertext bit flips the same plaintext bit, as the worked example shows, and the receiver cannot notice. Use authenticated encryption §5.16, which adds a tag that the receiver checks, such as GCM or CCM §5.17 (counter with cipher block chaining message authentication code), or add a message authentication code §5.7 computed over the ciphertext.

Uniqueness is a property of counter blocks, not of nonces alone. Counter blocks repeat when a nonce is reused, when the counter ranges of two messages overlap, when counter wraparound §6.21 happens inside a long message, when a device restarts its message counter after a reboot or after a backup is restored while the key stays the same, and when two senders share a key and choose their nonces independently. The remedies are a monotonic counter §8.28 (one that never goes backwards) kept in persistent state §8.1 (storage that survives a restart), a separate key or nonce prefix for each sender, or random nonces long enough for the birthday bound.

CTR is not misuse-resistant, meaning that it offers no fallback protection once its nonce rule is broken: one repeated counter block is enough to reveal the XOR of two plaintext blocks. Modes such as SIV §19.1.11 (synthetic initialization vector) limit the damage of a repeated nonce to revealing that two messages are equal.

In GCM, reusing a nonce also reveals the key of the authentication §1.6 function, after which the attacker can forge tags (Joux, 2006 [368]).

The nonce need not be secret or unpredictable; it must only not repeat. CBC §15.6 (cipher block chaining), by contrast, needs an unpredictable initialization vector.

The ciphertext has exactly the length of the plaintext, so CTR reveals the message length.

Questions answered

Why does reusing the same keystream reveal the XOR of two plaintexts?

Reusing the same keystream reveals the XOR of the two plaintexts because each ciphertext is the XOR of its plaintext and that keystream, and in the XOR of the two ciphertexts the keystream drops out. XOR (bitwise exclusive or) gives 1 where two bits differ and 0 where they are equal, so the XOR of any value with itself is all zeros, and a chain of XORs may be regrouped and reordered freely. With keystream SS, plaintexts PP and QQ, and ciphertexts C=P⊕SC = P \oplus S and D=Q⊕SD = Q \oplus S, it follows that C⊕D=P⊕Q⊕S⊕S=P⊕QC \oplus D = P \oplus Q \oplus S \oplus S = P \oplus Q. An eavesdropper computes C⊕DC \oplus D without knowing the key or the keystream.

For example, with S=𝟷𝟶𝟷𝟶S = \texttt{1010}, P=𝟶𝟷𝟷𝟶P = \texttt{0110} and Q=𝟶𝟶𝟷𝟷Q = \texttt{0011}: C=𝟷𝟷𝟶𝟶C = \texttt{1100}, D=𝟷𝟶𝟶𝟷D = \texttt{1001}, and C⊕D=𝟶𝟷𝟶𝟷=P⊕QC \oplus D = \texttt{0101} = P \oplus Q. If the eavesdropper knows or guesses PP, then S=P⊕CS = P \oplus C and Q=D⊕SQ = D \oplus S follow at once; even without that, the redundancy of text or of protocol headers usually lets both plaintexts be recovered from their XOR. In counter mode (CTR) the keystream is the block-cipher encryption of the counter blocks, so the same keystream appears whenever a counter block is used twice under one key. Every counter block must therefore be unique for the whole lifetime of the key.

Related entries: Nonce (§5.18), Authenticated encryption (AEAD) (§5.16), Key separation (§5.23), Block cipher (§15.3), Pseudorandom function (PRF) (§15.1), Counter wraparound (§6.21), Synthetic initialization vector (SIV) and misuse resistance (§19.1.11), Bit operations: truth tables, masks, shifts, and rotations (§4.50). Further reading: [47, 137, 368].