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 of a one-block message knows the chaining value after , so the attacker can arrange for a second block to re-enter the cipher with the input : XOR into it in advance. The two-block message , meaning followed by , then also has the tag . 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:
: the secret AES key, and : AES encryption of one block under .
, where means the block of 128 zero bits.
and : the two subkeys, one block each.
: the message, any number of bytes, including none.
: the tag, 16 bytes, which may be shortened (tag truncation §5.12).
Step 1, subkeys. Compute . Then is doubled and is 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 elements. It is reversible, so different values of give different subkeys.
Step 2, last block. Split into 16-byte blocks ; the last block may be shorter, and an empty message counts as one empty block, so . If the last block is complete, XOR it with . 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 . Call the result .
Step 3, chaining. Start with equal to the all-zero block. For each block , with running from 1 to in order, replace by . The tag is .
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 on 4-bit blocks. The attacker asks for the tag of the one-block message and receives ; so . The attacker forms the two-block message , whose second block is . CBC-MAC computes:
First block: .
Second block: the cipher input is . In the numbers, the two copies of cancel: . The output is .
The tag of the new message is , which the attacker already holds. Neither the key nor any property of was used, and the same steps work with AES and 16-byte blocks.
CMAC subkeys. RFC 4493 uses the AES-128 key = 2b7e151628aed2a6abf7158809cf4f3c (hexadecimal). Then
=
7df76b0c1ab899b33e42f047b91b546f.
Its first byte 0x7d = 01111101 begins with a 0 bit, so is 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
=
fbeed618357133667c85e08f7236a8de.
begins with a 1 bit, so is shifted left, with 0x87 then combined by XOR into the last byte. The last byte 0xde = 11011110 shifted left is 10111100 = 0xbc, and . So
=
f7ddac306ae266ccf90bc11ee46d513b.
Two tags. The 16-byte message 6bc1bee22e409f96e93d7e117393172a is one complete block, so CMAC computes = 070a16b46b4d4144f79bdd9dd04a287c. The empty message is one incomplete block; padded, it is 0x80 followed by 15 zero bytes, and CMAC computes = bb1d6929e95937287fa37d129b756746. Both tags equal the ones printed in RFC 4493.
The forgery fails against CMAC. With CMAC, the tag of a one-block message is . For the message , the first block is not the last, so CMAC computes with no subkey, and the cipher input for the last block is . The tag equals only if this input equals , that is only if . That is impossible: is a permutation, and because is not the all-zero block (it would be only if were, a chance of about for a random key). Running the forgery with the RFC key and the 16-byte message above gives a tag different from .
Stated precisely
Let be the block length in bits, AES encryption under the key , bitwise XOR, the bit string followed by the bit string , and a string of zero bits. Let be the block shifted left by one bit, the leftmost bit of , and , the block whose last byte is 0x87 and whose other bits are 0. Then
Let be the length of the message in bits and , where is rounded up to a whole number. Split into blocks , all of bits except possibly , and let be the length of in bits. Then A truncated tag is the first bits of .
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 , plus for a -bit tag. The middle term is the birthday bound §5.6: the key must be changed long before about 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 -bit tag is guessed with probability 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 has the tag , the two-block message followed by has the same tag: after the first block the running value is , the second cipher input is , and its encryption is again. The attacker has a valid tag for a new message without knowing the key.
The same splice works for any two messages: if has the tag and the message has the tag , then has the tag . 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].