Speed

CTR Turns AES Into a Keystream

CTR mode encrypts a sequence of counter blocks and XORs the result with the message, so there is no padding and the ciphertext is as long as the message; the first keystream block is plain AES of the first counter block.

The question

ECB and CBC feed the message through AES. CTR, the counter mode, does not. It feeds AES a sequence of counter blocks and XORs the output with the message. What does that change: how long is the ciphertext, and what exactly is the stream that is XORed?

SP 800-38A section 6.5 defines it: O_j = CIPH_K(T_j) for each counter block T_j, and C_j = P_j xor O_j. The lesson runs real AES-128 through OpenSSL under a test key that is written in the commands.

Work it by hand

Appendix F.5.1 of SP 800-38A publishes the test material key 2b7e1516 28aed2a6 abf71588 09cf4f3c and the first counter block f0f1f2f3 f4f5f6f7 f8f9fafb fcfdfeff. Its first output block, AES of that counter block, starts ec 8c df 73.

The demo message counter mode keeps the message length. is 38 bytes and starts with coun, bytes 63 6f 75 6e. XOR with ec 8c df 73: 63 with ec is 8f, 6f with 8c is e3, 75 with df is aa, 6e with 73 is 1d. The ciphertext must start 8f e3 aa 1d.

The message has two whole blocks and 6 bytes more. The last 6 bytes use the first 6 bytes of a third keystream block, so nothing is padded. The counter block grows by one for each block, as a 128-bit number: the second is f0f1f2f3 f4f5f6f7 f8f9fafb fcfdff00, where the carry runs one byte to the left.

Run it

The command encrypts the 38-byte message in the data box with AES-128 in CTR mode (-aes-128-ctr), with the key and the first counter block given as hex literals (-K, -iv), and writes the result to out.bin. The picture shows the message, the ciphertext and the byte count.

The 38 message bytes in three lines, starting 63 6f 75 6e, and the 38 ciphertext bytes starting 8f e3 aa 1d, with the count 38.

Public test material, generated for this lesson. Never use it for anything real.

# Encrypts in/message.bin with AES-128 in CTR mode, result in out.bin.
# The key and the counter block are public test material.
openssl enc -aes-128-ctr -K 2b7e151628aed2a6abf7158809cf4f3c \
  -iv f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff -in in/message.bin -out out.bin
{
  "answer": "8fe3aa1dec050e909fbd7210caf5c48146585c480f16710e7dd304b69b3553c20f42a40c10a7",
  "file": "out.bin",
  "bytes": 38
}

You can change the message to any whole number of bytes, written as hex pairs, up to 1024 bytes. The ciphertext always has as many bytes as the message; the empty message writes an empty file.

Your turn

The keystream block is the output of AES for a counter block. Run the command below, read what it does, and edit it so that it writes, to out.bin, the 16 bytes that CTR mode XORs with the first 16 message bytes when the file in/counter.bin holds the first counter block. Check runs it on the file shown and on five others.

The 16-byte counter block 00 11 22 33 and so on; out.bin and bytes show no value, because the shown command wrote no file.

Public test material, generated for this lesson. Never use it for anything real.

# Reads the 16-byte file in/counter.bin and writes to out.bin.
# The key is public test material.
openssl enc -aes-128-cbc -nopad -K 2b7e151628aed2a6abf7158809cf4f3c \
  -in in/counter.bin -out out.bin
{
  "message": "iv undefined"
}

Write one openssl command that writes to out.bin the 16-byte keystream block O_1 = CIPH_K(T_1) of AES-128 for the counter block T_1 stored in the 16-byte file in/counter.bin, with the key 2b7e151628aed2a6abf7158809cf4f3c (SP 800-38A section 6.5). The output must be exactly 16 bytes. Check runs your command on the file shown and on five others.

Read the result carefully

The command of Your turn encrypts one block with AES alone, to show what CTR works with. ECB is not how a message is encrypted here. The counter file must be 16 bytes: with a size that is not a multiple of 16 OpenSSL stops with the reason "wrong final block length", and with 32 bytes it writes two blocks. The key is a literal test key; Check changes only the counter.

CTR needs a counter block that is never used twice under one key. What happens when one is reused is in a-reused-nonce-leaks-the-xor. The page does not show how real protocols build counter blocks and does not run a counter past its wrap.

Recap

  • CTR: O_j = CIPH_K(T_j) and C_j = P_j xor O_j (SP 800-38A, section 6.5).
  • There is no padding; the ciphertext has the length of the message.
  • The first keystream block is AES of the first counter block; the second counter block is the first plus one.
  • Counter blocks must never repeat under one key.