Section 15.7

Cipher-based MAC (CMAC)

Full name: Cipher-based Message Authentication Code.

Definition

CMAC is a message authentication code §5.7 built from a block cipher §15.3: from a secret key and a message of any length it computes a short tag that only a key holder can produce. Like CBC-MAC (cipher block chaining §15.6 message authentication code), it XORs (bitwise exclusive or) each block into the previous cipher output and encrypts the result. Before encrypting the last block, it XORs in one of two subkeys derived from the key: one if that block is complete, the other if padded. NIST Special Publication 800-38B defines CMAC [316].

The idea

A block cipher can serve as a MAC (message authentication code) by chaining: start with an all-zero block, XOR the first message block into it and encrypt, XOR the second message block into the result and encrypt, and so on; the final cipher output is the tag. This is CBC-MAC: CBC encryption with an all-zero IV (initialization vector), of which only the last ciphertext block is kept. Bellare, Kilian and Rogaway proved in 1994 that CBC-MAC is secure when every message has the same number of blocks [371].

Allow messages of different lengths and CBC-MAC breaks. Its tag is the complete chaining value at the end of a message, and that chaining value is all that a longer message needs in order to continue. An attacker who learns the tag tt of a one-block message xx knows the chaining value after xx, so the attacker can arrange for a second block to re-enter the cipher with the input xx: XOR tt into it in advance. The two-block message x‖(x⊕t)x \mathbin{\Vert} (x \oplus t), meaning xx followed by x⊕tx \oplus t, then also has the tag tt. The attacker made a valid tag for a new message without the key.

CMAC makes the end of the message visible to the computation: only the last block is combined by XOR with a secret subkey before encryption. A tag is therefore never the chaining value that a longer message passes through, and nobody can continue from it. The two subkeys also separate a complete last block from a padded one, so a message and its padded form get different tags.

How it works

RFC 4493 specifies CMAC with AES (Advanced Encryption Standard), whose blocks are 128 bits (16 bytes), as AES-CMAC. The parts:

Step 1, subkeys. Compute LL. Then K1K_1 is LL doubled and K2K_2 is K1K_1 doubled. To double a 128-bit block, shift all its bits one place to the left, dropping the leftmost bit and putting a 0 at the right end; if the dropped bit was 1, also XOR the constant 0x87 into the last byte. In algebraic terms, doubling multiplies by 2 in a finite field §19.1.2 of 21282^{128} elements. It is reversible, so different values of LL give different subkeys.

Step 2, last block. Split MM into 16-byte blocks M1,…,MmM_1, \dots, M_m; the last block may be shorter, and an empty message counts as one empty block, so m=1m = 1. If the last block is complete, XOR it with K1K_1. Otherwise append the byte 0x80 (a 1 bit followed by seven 0 bits) and then zero bytes up to 16 bytes, and XOR the result with K2K_2. Call the result Mm*M_m^{*}.

Step 3, chaining. Start with XX equal to the all-zero block. For each block MiM_i, with ii running from 1 to m−1m - 1 in order, replace XX by EK(X⊕Mi)E_K(X \oplus M_i). The tag is T=EK(X⊕Mm*)T = E_K(X \oplus M_m^{*}).

Step 4, verification. The receiver recomputes the tag from the received message and compares it with the received tag by a constant-time comparison §4.33, which takes the same time wherever the first difference lies, so that the time taken does not reveal how many leading bytes of a guessed tag were right.

Worked example

The forgery against plain CBC-MAC. Take a toy block cipher EE on 4-bit blocks. The attacker asks for the tag of the one-block message x=𝟷𝟶𝟷𝟶x = \mathtt{1010} and receives t=𝟶𝟷𝟷𝟶t = \mathtt{0110}; so E(𝟷𝟶𝟷𝟶)=𝟶𝟷𝟷𝟶E(\mathtt{1010}) = \mathtt{0110}. The attacker forms the two-block message x‖(x⊕t)x \mathbin{\Vert} (x \oplus t), whose second block is 𝟷𝟶𝟷𝟶⊕𝟶𝟷𝟷𝟶=𝟷𝟷𝟶𝟶\mathtt{1010} \oplus \mathtt{0110} = \mathtt{1100}. CBC-MAC computes:

  1. First block: E(𝟶𝟶𝟶𝟶⊕𝟷𝟶𝟷𝟶)=E(𝟷𝟶𝟷𝟶)=𝟶𝟷𝟷𝟶E(\mathtt{0000} \oplus \mathtt{1010}) = E(\mathtt{1010}) = \mathtt{0110}.

  2. Second block: the cipher input is 𝟶𝟷𝟷𝟶⊕𝟷𝟷𝟶𝟶=𝟷𝟶𝟷𝟶\mathtt{0110} \oplus \mathtt{1100} = \mathtt{1010}. In the numbers, the two copies of tt cancel: (x⊕t)⊕t=𝟷𝟷𝟶𝟶⊕𝟶𝟷𝟷𝟶=𝟷𝟶𝟷𝟶=x(x \oplus t) \oplus t = \mathtt{1100} \oplus \mathtt{0110} = \mathtt{1010} = x. The output is E(𝟷𝟶𝟷𝟶)=𝟶𝟷𝟷𝟶E(\mathtt{1010}) = \mathtt{0110}.

The tag of the new message is 𝟶𝟷𝟷𝟶=t\mathtt{0110} = t, which the attacker already holds. Neither the key nor any property of EE was used, and the same steps work with AES and 16-byte blocks.

CMAC subkeys. RFC 4493 uses the AES-128 key KK = 2b7e151628aed2a6abf7158809cf4f3c (hexadecimal). Then

LL = 7df76b0c1ab899b33e42f047b91b546f.

Its first byte 0x7d = 01111101 begins with a 0 bit, so K1K_1 is LL shifted left by one bit with no constant. The first byte becomes 11111011 = 0xfb: 01111101 shifted left gives 11111010, and the leftmost bit of the next byte, 0xf7 = 11110111, moves in at the right. The whole result is

K1K_1 = fbeed618357133667c85e08f7236a8de.

K1K_1 begins with a 1 bit, so K2K_2 is K1K_1 shifted left, with 0x87 then combined by XOR into the last byte. The last byte 0xde = 11011110 shifted left is 10111100 = 0xbc, and 𝟶𝚡𝚋𝚌⊕𝟶𝚡𝟾𝟽=𝟷𝟶𝟷𝟷𝟷𝟷𝟶𝟶⊕𝟷𝟶𝟶𝟶𝟶𝟷𝟷𝟷=𝟶𝟶𝟷𝟷𝟷𝟶𝟷𝟷=𝟶𝚡𝟹𝚋\texttt{0xbc} \oplus \texttt{0x87} = \mathtt{10111100} \oplus \mathtt{10000111} = \mathtt{00111011} = \texttt{0x3b}. So

K2K_2 = f7ddac306ae266ccf90bc11ee46d513b.

Two tags. The 16-byte message 6bc1bee22e409f96e93d7e117393172a is one complete block, so CMAC computes T=EK(M1⊕K1)T = E_K(M_1 \oplus K_1) = 070a16b46b4d4144f79bdd9dd04a287c. The empty message is one incomplete block; padded, it is 0x80 followed by 15 zero bytes, and CMAC computes T=EK(padded block⊕K2)T = E_K(\text{padded block} \oplus K_2) = bb1d6929e95937287fa37d129b756746. Both tags equal the ones printed in RFC 4493.

The forgery fails against CMAC. With CMAC, the tag of a one-block message xx is t=EK(x⊕K1)t = E_K(x \oplus K_1). For the message x‖(x⊕t)x \mathbin{\Vert} (x \oplus t), the first block is not the last, so CMAC computes EK(x)E_K(x) with no subkey, and the cipher input for the last block is EK(x)⊕x⊕t⊕K1E_K(x) \oplus x \oplus t \oplus K_1. The tag equals tt only if this input equals x⊕K1x \oplus K_1, that is only if EK(x)=t=EK(x⊕K1)E_K(x) = t = E_K(x \oplus K_1). That is impossible: EKE_K is a permutation, and x⊕K1≠xx \oplus K_1 \ne x because K1K_1 is not the all-zero block (it would be only if LL were, a chance of about 2−1282^{-128} for a random key). Running the forgery with the RFC key and the 16-byte message above gives a tag different from tt.

Stated precisely

Let n=128n = 128 be the block length in bits, EKE_K AES encryption under the key KK, ⊕\oplus bitwise XOR, a‖ba \mathbin{\Vert} b the bit string aa followed by the bit string bb, and 0j0^{j} a string of jj zero bits. Let X≪1X \ll 1 be the block XX shifted left by one bit, msb(X)\mathrm{msb}(X) the leftmost bit of XX, and R=0120‖𝟷𝟶𝟶𝟶𝟶𝟷𝟷𝟷R = 0^{120} \mathbin{\Vert} \mathtt{10000111}, the block whose last byte is 0x87 and whose other bits are 0. Then L=EK(0n),dbl(X)={X≪1if msb(X)=0,(X≪1)⊕Rif msb(X)=1,K1=dbl(L),K2=dbl(K1).\begin{aligned} L &= E_K(0^{n}), \\ \mathrm{dbl}(X) &= \begin{cases} X \ll 1 & \text{if } \mathrm{msb}(X) = 0, \\ (X \ll 1) \oplus R & \text{if } \mathrm{msb}(X) = 1, \end{cases} \\ K_1 &= \mathrm{dbl}(L), \qquad K_2 = \mathrm{dbl}(K_1). \end{aligned}

Let |M||M| be the length of the message MM in bits and m=max(1,⌈|M|/n⌉)m = \max(1, \lceil |M| / n \rceil), where ⌈y⌉\lceil y \rceil is yy rounded up to a whole number. Split MM into blocks M1,…,MmM_1, \dots, M_m, all of nn bits except possibly MmM_m, and let rr be the length of MmM_m in bits. Then Mm*={Mm⊕K1if r=n,(Mm‖1‖0n−1−r)⊕K2if r<n,X0=0n,Xi=EK(Xi−1⊕Mi)for 1≤i≤m−1,T=EK(Xm−1⊕Mm*).\begin{aligned} M_m^{*} &= \begin{cases} M_m \oplus K_1 & \text{if } r = n, \\ (M_m \mathbin{\Vert} 1 \mathbin{\Vert} 0^{n-1-r}) \oplus K_2 & \text{if } r < n, \end{cases} \\ X_0 &= 0^{n}, \qquad X_i = E_K(X_{i-1} \oplus M_i) \quad \text{for } 1 \le i \le m - 1, \\ T &= E_K(X_{m-1} \oplus M_m^{*}). \end{aligned} A truncated tag is the first tt bits of TT.

Guarantee. If AES is a pseudorandom permutation §15.2, meaning that no feasible computation can tell it apart from a randomly chosen permutation of 128-bit blocks, then CMAC is a pseudorandom function §15.1 on messages of every length: its tags cannot feasibly be told apart from random values. An attacker who has obtained tags for messages of the attacker’s choice then forges a tag for a new message with probability at most the advantage of telling AES apart from a random permutation, plus a term that grows with the square of the total number of blocks processed under the key divided by 21282^{128}, plus 2−t2^{-t} for a tt-bit tag. The middle term is the birthday bound §5.6: the key must be changed long before about 2642^{64} blocks have been processed.

CMAC is the design that Iwata and Kurosawa published in 2003 as OMAC1 (One-Key CBC-MAC 1) [372]. NIST adopted it as CMAC in 2005, and RFC 4493 (2006) specifies AES-CMAC with test values.

Limits and common mistakes

CMAC authenticates; it does not encrypt. The message is readable by anyone unless a separate mechanism encrypts it.

A valid tag does not show that a message is new. A recorded message and its tag are accepted again, a replay attack §1.26, unless the protected bytes include a counter or another freshness value that the receiver checks.

CMAC protects exactly the bytes it is given. A field left outside them can be changed undetected, and if one key tags several kinds of messages, a fixed label naming the kind must be included (domain separation §5.22).

Use the CMAC key for nothing else. The security argument covers a key used only for CMAC, and sharing one key between CBC encryption and CBC-MAC is a classic source of forgeries, because CBC ciphertext blocks and CBC-MAC chaining values are computed the same way.

A tt-bit tag is guessed with probability 2−t2^{-t} on each attempt, so tag truncation trades space for risk. NIST Special Publication 800-38B discusses choosing the tag length together with a limit on the number of failed verifications.

CMAC is not the only repair of CBC-MAC. CBC-MAC is also secure when no message encoding is a prefix of another (prefix-free encoding §15.38), for example when the message length is placed in the first block, as CCM §5.17 (Counter with CBC-MAC) does. Fixing the length of every message is the special case that Bellare, Kilian and Rogaway proved secure.

Questions answered

Why is naïve CBC-MAC unsafe for unrestricted variable-length messages?

Naive CBC-MAC is unsafe when messages may have different lengths because its tag is the complete chaining value at the end of a message, and that value is all a longer message needs to continue the chain. CBC-MAC (cipher block chaining message authentication code) starts from an all-zero block, combines each message block by XOR (exclusive or) into the running value and encrypts the result with a block cipher under the secret key; the last cipher output is the tag. If the one-block message xx has the tag t=EK(x)t = E_K(x), the two-block message xx followed by x⊕tx \oplus t has the same tag: after the first block the running value is tt, the second cipher input is t⊕x⊕t=xt \oplus x \oplus t = x, and its encryption is tt again. The attacker has a valid tag for a new message without knowing the key.

The same splice works for any two messages: if MM has the tag t1t_1 and the message N=N1‖N2‖…N = N_1 \mathbin{\Vert} N_2 \mathbin{\Vert} \dots has the tag t2t_2, then M‖(N1⊕t1)‖N2‖…M \mathbin{\Vert} (N_1 \oplus t_1) \mathbin{\Vert} N_2 \mathbin{\Vert} \dots has the tag t2t_2. CBC-MAC is secure only on message sets in which no message is a prefix of another, such as messages of one fixed length. CMAC removes the restriction by combining a secret subkey by XOR into the last block before its encryption, so that a tag is never a chaining value from which a longer message can continue.

Related entries: Message authentication code (MAC) (§5.7), Block cipher (§15.3), Authentication coverage (§5.41), Cipher block chaining (CBC) (§15.6), Tag truncation (§5.12), Prefix-free encoding (§15.38), AES-CCM (§5.17). Further reading: [47, 48, 316, 371, 372].