In cryptography, a cipher block chaining message authentication code (CBC-MAC) is a technique for constructing a message authentication code (MAC) from a block cipher. The message is encrypted with some block cipher algorithm in cipher block chaining (CBC) mode to create a chain of blocks such that each block depends on the proper encryption of the previous block. This interdependence ensures that a change to any of the plaintext bits will cause the final encrypted block to change in a way that cannot be predicted or counteracted without knowing the key to the block cipher. To calculate the CBC-MAC of message, one encrypts in CBC mode with zero initialization vector and keeps the last block. The following figure sketches the computation of the CBC-MAC of a message comprising blocks
m1\|m2\| … \|mx
The CBC-MAC construct is used as part of the CCM mode utilized in IEEE 802.11i and NIST SP 800-97 (as CCMP, the CCM encryption protocol for WPA2), IPsec,[1] and TLS 1.2,[2] as well as Bluetooth Low Energy (as of Bluetooth 4.0, see NIST SP 800-121 Rev2).[3] It is available for TLS 1.3, but not enabled by default in OpenSSL.[4]
CBC-MAC is also used as a "conditioning component" (a.k.a. randomness extractor, a method to generate bitstrings with full entropy) in NIST SP 800-90B.
FIPS PUB 113 Computer Data Authentication is a (now obsolete) U.S. government standard that specified the CBC-MAC algorithm using DES as the block cipher.
The CBC-MAC algorithm is also included into ANSI X9.9, ANSI X9.19, ISO 8731-1, and ISO/IEC 9797-1 MAC (Algorithm 1).
If the block cipher used is secure (meaning that it is a pseudorandom permutation), then CBC-MAC is secure for fixed-length messages.[5] However, by itself, it is not secure for variable-length messages. Thus, any single key must only be used for messages of a fixed and known length. This is because an attacker who knows the correct authentication tag (i.e. CBC-MAC) pairs for two messages
(m,t)
(m',t')
m''
t'
m'
m'
m''=m\|[(m1' ⊕ t)\|m2'\|...\|mx']
m''
E | |
KMAC |
(m1' ⊕ t)
E | |
KMAC |
(m1' ⊕ t ⊕ t)=
E | |
KMAC |
(m1')
m''
t'
This problem cannot be solved by adding a message-size block to the end. There are three main ways of modifying CBC-MAC so that it is secure for variable length messages: 1) Input-length key separation; 2) Length-prepending; 3) Encrypt last block. In such a case, it may also be recommended to use a different mode of operation, for example, CMAC or HMAC to protect the integrity of variable-length messages.
One solution is to include the length of the message in the first block;[6] in fact CBC-MAC has been proven secure as long as no two messages that are prefixes of each other are ever used and prepending the length is a special case of this.[7] This can be problematic if the message length may not be known when processing begins.
Encrypt-last-block CBC-MAC (ECBC-MAC)[8] is defined as .[9] Compared to the other discussed methods of extending CBC-MAC to variable-length messages, encrypt-last-block has the advantage of not needing to know the length of the message until the end of the computation.
As with many cryptographic schemes, naïve use of ciphers and other protocols may lead to attacks being possible, reducing the effectiveness of the cryptographic protection (or even rendering it useless). We present attacks which are possible due to using the CBC-MAC incorrectly.[10]
One common mistake is to reuse the same key for CBC encryption and CBC-MAC. Although a reuse of a key for different purposes is a bad practice in general, in this particular case the mistake leads to a spectacular attack:
Suppose Alice has sent to Bob the cipher text blocks
C=C1\|C2\|...\|Cn
C1,...,Cn-1
Cn
When Bob receives the message, he will first decrypt the message by reversing the encryption process which Alice applied, using the cipher text blocks
C=C1\|C2\| … \|Cn
C'=C1'\|...\|Cn-1'\|Cn
Bob first decrypts the message received using the shared secret key to obtain corresponding plain text. Note that all plain text produced will be different from that which Alice originally sent, because Eve has modified all but the last cipher text block. In particular, the final plain text,
Pn'
Pn
Cn
Cn-1'\not=Cn-1
Pn'
Cn
Pn'=Cn-1' ⊕
-1 | |
E | |
K |
(Cn)
It follows that Bob will now compute the authentication tag using CBC-MAC over all the values of plain text which he decoded. The tag for the new message,
t'
t'=EK(Pn' ⊕ EK(Pn-1' ⊕ EK(... ⊕ EK(P1'))))
Notice that this expression is equal to
t'=EK(Pn' ⊕ Cn-1')
which is exactly
Cn
t'=EK(Cn-1' ⊕
-1 | |
E | |
K |
(Cn) ⊕ Cn-1')=EK(E
-1 | |
K |
(Cn))=Cn
and it follows that
t'=Cn=t
Therefore, Eve was able to modify the cipher text in transit (without necessarily knowing what plain text it corresponds to) such that an entirely different message,
P'
P'
P\not=P'
If, instead, we use different keys for the encryption and authentication stages, say
K1
K2
Ci'
Pi'
K2
Pi'
K2
MACi\not=Ci'
This example also shows that a CBC-MAC cannot be used as a collision-resistant one-way function: given a key it is trivial to create a different message which "hashes" to the same tag.
When encrypting data using a block cipher in cipher block chaining (or another) mode, it is common to introduce an initialization vector to the first stage of the encryption process. It is typically required that this vector be chosen randomly (a nonce) and that it is not repeated for any given secret key under which the block cipher operates. This provides semantic security, by means of ensuring the same plain text is not encrypted to the same cipher text, allowing an attacker to infer a relationship exists.
When computing a message authentication code, such as by CBC-MAC, the use of an initialization vector is a possible attack vector.
In the operation of a ciphertext block chaining cipher, the first block of plain text is mixed with the initialization vector using an exclusive OR (
P1 ⊕ IV
However, when performing encryption and decryption, we are required to send the initialization vector in plain text - typically as the block immediately preceding the first block of cipher text - such that the first block of plain text can be decrypted and recovered successfully. If computing a MAC, we will also need to transmit the initialization vector to the other party in plain text so that they can verify the tag on the message matches the value they have computed.
If we allow the initialization vector to be selected arbitrarily, it follows that the first block of plain text can potentially be modified (transmitting a different message) while producing the same message tag.
Consider a message
M1=P1|P2|...
IV1
EK(IV1 ⊕ P1)
(M1,T1)
Now produce the message
M2=P1'|P2|...
P1'
IV1'
EK(P1' ⊕ IV1')
M1
If the freedom to select an initialization vector is removed and all implementations of CBC-MAC fix themselves on a particular initialization vector (often the vector of zeroes, but in theory, it could be anything provided all implementations agree), this attack cannot proceed.
To sum up, if the attacker is able to set the IV that will be used for MAC verification, he can perform arbitrary modification of the first data block without invalidating the MAC.
Sometimes IV is used as a counter to prevent message replay attacks.However, if the attacker can predict what IV will be used for MAC verification,he or she can replay previously observed message by modifying the first data block to compensate for the change in the IV that will be used for the verification.For example, if the attacker has observed message
M1=P1|P2|...
IV1
IV2
M1'=(P1 ⊕ IV1 ⊕ IV2)|P2|...
IV2
The simplest countermeasure is to encrypt the IV before using it (i.e., prepending IV to the data). Alternatively MAC in CFB mode can be used, because in CFB mode the IV is encrypted before it is XORed with the data.
Another solution (in case protection against message replay attacks is not required) is to always use a zero vector IV.[11] Note that the above formula for
M1'
M1'=(P1 ⊕ 0 ⊕ 0)|P2|...=P1|P2|...=M1
M1
M1'