Section 5.17

AES-CCM

Full name: Advanced Encryption Standard in Counter with Cipher Block Chaining-Message Authentication Code mode.

Definition

AES-CCM is an authenticated encryption §5.16 mode: a method that uses one secret key both to encrypt a message and to compute a tag, a short value with which the receiver detects any change. AES (Advanced Encryption Standard) is the block cipher §15.3 used, a keyed function that maps one 16-byte block to another. CCM stands for Counter with CBC-MAC (cipher block chaining message authentication code): CBC-MAC computes the tag, and counter mode §15.5 encrypts. Every message needs a nonce §5.18, a value never used twice with the same key. RFC 3610 defines CCM.

The idea

A sensor sends readings by radio. Each packet has an 8-byte header that relays must read and a 23-byte reading that must stay private. Encryption alone does not stop changes. In counter mode, the encryption method inside CCM, the encrypted message (the ciphertext) is the original message combined bit by bit with a stream of secret pseudorandom bytes, so flipping one ciphertext bit flips exactly the same message bit, and decryption cannot notice. A message authentication code §5.7 alone detects changes but hides nothing. Combining the two by hand leaves open which key does which job, which bytes the tag covers, and whether to check before decrypting.

CCM settles those questions once. One AES key does both jobs, which RFC 3610 says simplifies implementation. The tag covers an unambiguous encoding of the nonce, both lengths, the header and the message, and the tag is then encrypted. The header goes in as associated data: bytes that the tag covers but that travel unencrypted.

The picture is a chain and a mask. CBC-MAC is the chain: each 16-byte block of input is mixed into a running 16-byte value, which passes through AES again, so the final value depends on every input byte. Counter mode supplies the mask: AES applied to successive counter blocks, each of which contains the nonce, gives a stream of bytes that only a key holder can compute. A new nonce gives a new mask. Reusing a nonce reuses the mask.

How it works

RFC 3610 and NIST Special Publication (SP) 800-38C both define CCM [169, 318]. RFC 3610 names the inputs as follows.

The nonce and the message length share one 16-byte block with a flags byte: 1+(15−L)+L=161 + (15 - L) + L = 16. That is why a longer nonce means a shorter maximum message.

Encryption runs in eight steps.

  1. First block. Build the 16-byte block B0B_0: one flags byte, then the nonce, then ℓ(m)\ell(m) written in LL bytes, most significant byte first. The flags byte is 64⋅Adata+8⋅M′+L′64 \cdot \mathrm{Adata} + 8 \cdot M' + L', where Adata\mathrm{Adata} is 1 if there is associated data and 0 otherwise, M′=(M−2)/2M' = (M - 2)/2 and L′=L−1L' = L - 1.

  2. Associated data blocks. If aa is not empty, write its length first: in 2 bytes when ℓ(a)<216−28=65,280\ell(a) < 2^{16} - 2^{8} = 65{,}280; otherwise, when ℓ(a)<232\ell(a) < 2^{32}, as the bytes 0xFF 0xFE followed by 4 length bytes; and otherwise as 0xFF 0xFF followed by 8 length bytes. Append aa, pad with zero bytes to a multiple of 16 bytes, and cut the result into 16-byte blocks.

  3. Message blocks. Cut mm into 16-byte blocks, padding the last one with zero bytes.

  4. Tag. Run CBC-MAC over all blocks in order. Encrypt B0B_0 with AES. For each following block, combine it with the previous result by bitwise XOR (exclusive or, which sets each bit where the two inputs differ) and encrypt the combination. The first MM bytes of the last result are the tag TT.

  5. Keystream. Build counter blocks A0,A1,A2,…A_0, A_1, A_2, \ldots, each made of a flags byte equal to L′L', the nonce, and the block number ii in LL bytes. Encrypting AiA_i with AES gives the keystream block SiS_i.

  6. Encryption. XOR the message with the first ℓ(m)\ell(m) bytes of S1,S2,…S_1, S_2, \ldots joined end to end. S0S_0 is kept for the tag.

  7. Tag encryption. Compute UU, the XOR of TT with the first MM bytes of S0S_0.

  8. Output. Send the encrypted message followed by UU. The associated data is not part of the output; the receiver must already have it, for example as the packet header.

Decryption. The receiver rebuilds the keystream from the key and the nonce, recovers the message and TT, recomputes CBC-MAC over its own copy of the associated data and the recovered message, and compares the result with TT. RFC 3610 requires that on a mismatch the receiver release nothing except the fact that the check failed: no decrypted bytes and no tag.

Two details of the format have consequences that are easy to miss. First, bits 3 to 5 of the flags byte, counting from bit 0, the least significant bit, hold M′M', which is never zero, in B0B_0, and are zero in every counter block, so B0B_0 never equals a counter block; RFC 3610 relies on this when it uses one key for both jobs. Second, because B0B_0 holds the message length and every step of the chain depends on the step before, the sender must know ℓ(m)\ell(m) before the first AES call, and ℓ(a)\ell(a) before any associated data enters the chain. SP 800-38C states that CCM is meant for data that is fully available before processing starts and is not designed for stream processing.

Worked example

This is packet vector 1 of RFC 3610, recomputed here both from the steps above and with a cryptographic library.

The nonce has 13 bytes, so L=15−13=2L = 15 - 13 = 2, and the longest allowed message is 216−1=65,5352^{16} - 1 = 65{,}535 bytes; 23 bytes fits. There is associated data, M′=(8−2)/2=3M' = (8 - 2)/2 = 3 and L′=2−1=1L' = 2 - 1 = 1, so the flags byte is 64×1+8×3+1=8964 \times 1 + 8 \times 3 + 1 = 89, which is 0x59. The message length 23 is 00 17 in two bytes. So

B0B_0 = 59 00 00 00 03 02 01 00 A0 A1 A2 A3 A4 A5 00 17

The associated data becomes the 2-byte length 00 08 followed by the 8 header bytes, 2+8=102 + 8 = 10 bytes, padded to one block. The 23-byte message fills one block and 7 bytes of a second one, which gets 16−7=916 - 7 = 9 zero bytes of padding. CBC-MAC therefore chains 1+1+2=41 + 1 + 2 = 4 blocks with 4 AES calls, and the first 8 bytes of its result are the tag:

TT = 2D C6 97 E4 11 CA 83 A8

The counter blocks begin with the flags byte L′=1L' = 1, so

A1A_1 = 01 00 00 00 03 02 01 00 A0 A1 A2 A3 A4 A5 00 01

The message needs S1S_1 and S2S_2, and the tag needs S0S_0: 3 more AES calls, 4+3=74 + 3 = 7 in all. S1S_1 begins with the byte 50, so the first message byte 08 becomes 08 XOR 50 = 58. The first 8 bytes of S0S_0 are 3A 2E 46 C8 EC 33 A5 48. The encrypted tag UU therefore begins with 2D XOR 3A = 17, and in full it is 17 E8 D1 2C FD F9 26 E0.

The packet sent is the 8 header bytes in the clear, the 23 encrypted message bytes and the 8-byte encrypted tag, 8+23+8=398 + 23 + 8 = 39 bytes:

00 01 02 03 04 05 06 07 (header)
58 8C 97 9A 61 C6 63 D2 F0 66 D0 C2 C0 F9 89 80
6D 5F 6B 61 DA C3 84 (encrypted message, 23 bytes)
17 E8 D1 2C FD F9 26 E0 (encrypted tag)

This is the output printed in RFC 3610. CCM added 8 bytes, the tag. A protocol that also sends the 13-byte nonce with each packet adds 13 more, 39+13=5239 + 13 = 52 bytes.

Stated precisely

Let nn be the nonce length in bytes, LL the size of the length field in bytes, MM the tag length in bytes, and ℓ(m)\ell(m) and ℓ(a)\ell(a) the lengths in bytes of the message and the associated data. RFC 3610 (section 2) and SP 800-38C (appendix A) allow exactly the parameters that satisfy L∈{2,3,…,8},n=15−L,M∈{4,6,8,10,12,14,16},0≤ℓ(m)<28L,0≤ℓ(a)<264.L \in \{2, 3, \ldots, 8\}, \quad n = 15 - L, \quad M \in \{4, 6, 8, 10, 12, 14, 16\}, \quad 0 \le \ell(m) < 2^{8L}, \quad 0 \le \ell(a) < 2^{64}. So nn lies between 7 and 13. SP 800-38C writes the tag length in bits, Tlen=8M\mathrm{Tlen} = 8M, and requires it to stay the same for every use of one key.

For each key KK the following rules hold.

  1. Nonce uniqueness. Every encryption under KK uses a nonce that no other encryption under KK has used. RFC 3610 says that the set of nonce values used with a key must not contain duplicates; SP 800-38C says that distinct pairs of message and associated data protected during the lifetime of the key must get distinct nonces.

  2. Volume. The total number of AES calls under KK, counting CBC-MAC and encryption together, must not exceed 2612^{61}. Each 16-byte message block costs two AES calls, one in the chain and one for its keystream, so this allows about 260×16=2642^{60} \times 16 = 2^{64} bytes.

  3. One mode per key. SP 800-38C requires that the key be used only for CCM.

  4. Check before release. No decrypted byte leaves the receiver before the tag has been checked.

RFC 3610 cites a security proof of confidentiality §1.8 and authenticity by Jakob Jonsson, “On the Security of CTR + CBC-MAC” (2002) [320]; CTR is the usual short name for counter mode. The design rationale in RFC 3610 rests on one assumption about AES: that it behaves as a pseudorandom permutation §15.2, meaning that nobody without the key can tell it apart from a permutation of 16-byte blocks chosen at random. For forgeries, SP 800-38C states that the probability that a single inauthentic ciphertext passes the check, for a given nonce and associated data, is no greater than 2−Tlen2^{-\mathrm{Tlen}}, and that many attempts raise the total chance. Its sizing rule: if at most MaxErrs\mathrm{MaxErrs} failed checks are allowed before the key is retired, and Risk\mathrm{Risk} is the highest acceptable probability of accepting a forgery, choose Tlen≥log2(MaxErrs/Risk)\mathrm{Tlen} \ge \log_2(\mathrm{MaxErrs} / \mathrm{Risk}). In its example, MaxErrs=210\mathrm{MaxErrs} = 2^{10} and Risk=2−20\mathrm{Risk} = 2^{-20} give Tlen≥30\mathrm{Tlen} \ge 30, so a 32-bit tag is enough.

Limits and common mistakes

A repeated nonce under one key repeats the keystream. The XOR of the two ciphertexts then equals the XOR of the two messages, so an attacker who knows or guesses one message learns the other. RFC 5116, which defines a common interface for authenticated encryption, records this loss of confidentiality for CCM, and RFC 3610 says that reuse destroys the security properties of the mode. The nonce entry explains how to keep nonces unique across restarts.

A short tag is easy to guess. With M=4M = 4, one random guess passes with probability 1 in 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296. RFC 3610 recommends MM of at least 8 for most applications, and SP 800-38C allows tags shorter than 64 bits only after a careful risk analysis and where the protocol limits how often the check may fail.

CCM does not detect a replay attack §1.26, in which an attacker records a valid packet and sends it again: the copy passes the check. The receiver must reject old nonces itself, for example by remembering the highest packet counter it has accepted.

The CBC-MAC inside CCM depends on the CCM formatting. SP 800-38C does not approve CBC-MAC outside CCM, and SP 800-38B says that the basic CBC-MAC has security deficiencies. One of them: without a length block, if tt is the tag of a one-block message m1m_1, then tt is also the tag of the two-block message m1m_1 followed by m1m_1 XOR tt, because the second chain step encrypts tt XOR m1m_1 XOR tt, which is m1m_1 again. CCM’s length field in B0B_0 and its encrypted tag prevent this.

With 128-bit keys, using the same nonce value with many different keys invites precomputation. RFC 3610 describes an attacker who tabulates the keystream block S1S_1, which encrypts the first 16 message bytes, for one chosen nonce under 2642^{64} random keys, then compares it with messages sent under that nonce whose first 16 bytes it knows; after about 2642^{64} such messages it can expect a match, which reveals that message’s key for a total of about 2642^{64} steps instead of 21282^{128}. It suggests a 256-bit key, or a few random bytes in the nonce.

Only the AES block size is the same in every use of CCM: 16 bytes. The key is 16, 24 or 32 bytes, the nonce 7 to 13 bytes and the tag 4 to 16 bytes, and a protocol usually fixes each of them; RFC 5116, for example, defines AEAD_AES_128_CCM with a 16-byte key, a 12-byte nonce, a 16-byte tag and L=3L = 3.

Questions answered

What nonce-uniqueness and message-length constraints must an implementation obey?

An AES-CCM implementation must never use the same nonce twice with the same key, and every message must be shorter than 28L2^{8L} bytes, where LL, from 2 to 8, is the size in bytes of the length field and the nonce is 15−L15 - L bytes long. AES-CCM is the Advanced Encryption Standard block cipher used in Counter with CBC-MAC (cipher block chaining message authentication code) mode, which encrypts a message and computes a tag that detects changes; a nonce is a value that may be used only once per key. In detail:

A protocol that uses CCM usually fixes these values, and its own limits apply on top.

Related entries: Authenticated encryption (AEAD) (§5.16), Nonce (§5.18), Tag truncation (§5.12), Message authentication code (MAC) (§5.7), Forgery probability (§5.13), Block cipher (§15.3), Counter mode (CTR) (§15.5), Cipher block chaining (CBC) (§15.6), Replay attack (§1.26). Further reading: [47, 67, 74, 318, 320].