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 , 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.
A secret key for a block cipher with -bit blocks; for AES (the Advanced Encryption Standard), bits, which is 16 bytes.
A plaintext of any length, split into -bit blocks ; the last block may be shorter.
An initial counter block, which the receiver must be able to rebuild: it is sent with the ciphertext or derived from a message number that both sides know.
Output: the ciphertext , exactly as long as .
Steps.
Form counter blocks that differ from each other and from every counter block used before under . 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 bits of the block, wrapping from to 0.
Encrypt each counter block: . The blocks form the keystream.
XOR: . For a last block of bits, use only the first bits of and discard the rest.
Send the ciphertext together with whatever the receiver needs to rebuild , usually the nonce.
The receiver rebuilds , computes the same , and recovers .
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 , the first counter block , the keystream block and the first plaintext block :
=
2b7e151628aed2a6abf7158809cf4f3c
=f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff
=ec8cdf7398607cb0f2d21675ea9ea1e4
=6bc1bee22e409f96e93d7e117393172a
The ciphertext block is , computed byte by byte. The first byte is , which in binary is , that is 87; the second is . The whole block gives
=
874d6191b620e3261bef6864990db6ce
The next counter block is :
=
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 counter blocks, where is rounded up to a whole number: three full blocks of 16 bytes, which is 48 bytes, and a last block of bytes. The four keystream blocks supply bytes; 50 are used and are discarded. The ciphertext is 50 bytes.
Keystream reuse. Use 4-bit values to see the algebra. Let the keystream be . The plaintext encrypts to . By mistake, the same also encrypts , giving . An eavesdropper who sees and computes . That equals : the keystream has dropped out. If the eavesdropper also knows , for example because it is a fixed message header, then , and is recovered.
Changing a ciphertext without the key. An attacker who knows that carries wants the receiver to read 1001 instead. The attacker XORs with and sends . The receiver computes , 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 counter blocks, are used twice, so 5 blocks of keystream are reused, although no two messages share an initial counter block.
Stated precisely
Let be the block cipher under key , with block length bits. Let the plaintext be , where have bits and has bits with , and let be the counter blocks. Write for the leftmost bits of a block . Then Decryption computes the same and , using 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 and of equal length are encrypted with the same keystream , then and , and because XOR may be regrouped and reordered freely and is all zeros.
What uniqueness buys. Let 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 . Here the advantage is the difference between the probabilities that the adversary answers “real” in the two cases, is the block cipher’s advantage bound as a pseudorandom permutation §15.2 for queries, and 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 blocks, the second term is below .
Capacity. A -bit block counter covers at most blocks per nonce before it wraps back to its starting value. With and 16-byte blocks that is bytes, which is 64 GiB, per message. If nonces of bits are chosen at random, the probability that two of them coincide is at most (the birthday bound §5.6). For and messages this is below .
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 , plaintexts and , and ciphertexts and , it follows that . An eavesdropper computes without knowing the key or the keystream.
For example, with , and : , , and . If the eavesdropper knows or guesses , then and 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].