Definition
A message authentication code (MAC) is a short value, called a tag, that an algorithm §14.1 of the same name computes from a secret key and a message. The sender attaches the tag to the message. The receiver holds the same key, recomputes the tag and accepts the message only if the tags are equal. Nobody without the key can feasibly produce a valid tag for a new or altered message, so a valid tag shows that a key holder produced exactly this message. A MAC does not hide the message.
The idea
A door controller receives the command unlock door 7 over a radio link on which anyone nearby can transmit. The controller must tell a command from its owner apart from a command that an outsider invented, and from a genuine command that was altered on the way. Attaching a checksum §4.32 or a plain hash of the command, a short value that a public function computes from the command alone, does not help. The outsider computes such a value as easily as the owner does.
The idea of a MAC is to put a secret into the computation. The owner and the controller share a secret key. The tag depends on the key and on every bit of the message, and the function is built so that seeing any number of messages with their tags does not help anyone to produce the tag of one more message.
A wax seal pressed with a signet ring is a fair picture: the receiver recognizes the seal and trusts the letter. The picture stops being accurate in two ways. A wax seal can be lifted off one letter and put on another, but a tag fits only the exact message it was computed for. And here both sides own the same ring, so the receiver could have sealed the letter too. A tag cannot show a third party which key holder made it.
How it works
A MAC has these parts.
Key: a secret value known only to the parties that may create and check messages. It must be unpredictable, for example 128 or 256 random bits from a CSPRNG §5.27 (cryptographically secure pseudorandom number generator).
Message: the exact bytes to protect.
Tag: the output, of a fixed length such as 128 or 256 bits.
Tagging algorithm: computes the tag from the key and the message.
Verification algorithm: takes the key, a message and a tag, and answers accept or reject.
In use the steps are as follows.
The parties obtain the shared key in advance, by a method outside the MAC itself.
The sender computes the tag of the message and sends the message together with the tag.
The receiver computes the tag of the message as received, using the same key.
The receiver compares the computed tag with the received tag, over all bits, using a constant-time comparison §4.33. A comparison that stops at the first differing byte reveals, through its running time, how many leading bytes of a guessed tag were right.
If the tags are equal, the receiver accepts the message. Otherwise the receiver discards the message and acts on none of its content.
Standard MAC algorithms are built from other components. HMAC §5.8 (hash-based message authentication code) is built from a cryptographic hash function §5.1, a public function that turns any data into a short fixed-length value called a digest. CMAC §15.7 (cipher-based MAC) is built from a block cipher §15.3, an algorithm that encrypts one block of fixed size under a key. Authenticated encryption §5.16 algorithms, which encrypt a message and protect it against change in one operation, produce a tag as part of their output. In HMAC and CMAC the verification algorithm recomputes the tag and compares, so each message has exactly one valid tag under a given key.
Worked example
Computing and checking a tag. RFC 4231 publishes test values for HMAC used with the hash function SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) [167]. In its test case 2 the key is the four ASCII characters Jefe and the message is the 28 characters what do ya want for nothing?. The tag has 32 bytes:
5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
A key of 4 bytes is acceptable only in a test. A real key must be long and random.
Suppose the message is altered on the way so that the final ? becomes !, a change of 4 bits. The receiver computes the tag of what arrived:
b3e375524094b7a3fd1c0bacdd4c1f327843ee972e67164831d35b68718cd2b2
This value differs from the received tag in 135 of 256 bits, so the receiver rejects the message. To succeed, the attacker would have to replace the tag as well, and computing the right tag requires the key.
Guessing. Suppose a system shortens its tags to 32 bits, a practice called tag truncation §5.12. A guessed tag for a new message is right with probability , about one in 4.3 billion. If the receiver tolerates failed verifications before the key is replaced, the attacker has 4096 guesses, and the chance that one of them is accepted is at most , about one in a million. With 64-bit tags and tolerated failures the chance is at most . NIST Special Publication 800-107 Revision 1 (2012) gives these same two figures [310].
Stated precisely
A MAC consists of a tagging algorithm and a verification algorithm , where is the key, a message and a tag. Correctness: always answers accept.
Security is defined by a contest called the chosen-message attack.
A key is chosen at random and kept from the attacker.
The attacker submits messages of its own choice, one after another, and receives the genuine tag of each. Each choice may depend on the tags already received. These requests are called tagging queries.
The attacker presents a pair .
The attacker wins if answers accept and is not one of the messages . Such a pair is called a forgery.
The MAC is secure if no attacker with a feasible amount of computation wins, except with a probability so small that it can be ignored. This property is called existential unforgeability under chosen-message attack, abbreviated EUF-CMA. Existential means that the forged message may be any message at all, even a meaningless one. A stricter property, strong existential unforgeability under chosen-message attack (SUF-CMA), changes step 4: the attacker also wins with a message that it did submit, provided that is a valid tag different from every tag it received for . For a MAC with exactly one valid tag for each message the two properties are the same.
The forgery probability §5.13, the chance that a pair presented by the attacker is accepted, can never be zero, because a tag can be guessed. Let be the tag length in bits. Every message has at least one valid tag among the possible values, so a random guess is accepted with probability at least . An ideal MAC is one whose tags for different messages are independent, uniformly random values of bits. For an ideal MAC, guessing is the best attack, and if the attacker presents at most pairs for verification under one key, each with a message that was never tagged, the probability that any of them is accepted is at most
A real MAC adds a second term. Give an attacker access to one of two things without saying which: the tagging algorithm of the real MAC under a random key, or an ideal MAC as just described. Let the attacker ask for tags of messages of its choice and then say which of the two it was given. Let be the largest amount by which the probability of the answer “real” can differ between the two cases, over all attackers that use about as much computation and as many requests as the forger. A keyed function for which is too small to matter is called a pseudorandom function §15.1. For a MAC that verifies by recomputing the tag, the probability that any of presented pairs is accepted is then at most . The term cannot be smaller than the chance of finding the key: an attacker who tests candidate keys against a few known messages and tags finds a random key of bits with probability about , and the right key makes every forgery possible. A long tag therefore does not rescue a key that can be guessed.
Limits and common mistakes
A MAC provides integrity §1.9, meaning that a change to the message is detected, and authentication §1.6 of the origin, meaning that the message came from a key holder. It does not provide confidentiality §1.8: the message is readable unless it is also encrypted.
Every holder of the key can create valid tags. A valid tag shows that some key holder produced the message, not which one. The receiver cannot use the tag to prove to a third party that the sender wrote the message, because the receiver could have computed the tag. Where that proof is needed, use a digital signature §5.14, in which the key that signs is different from the key that verifies.
A MAC does not stop a replay attack §1.26. A recorded message with its tag verifies again later, because it is genuine, and the definition of forgery leaves this case out on purpose. To reject replays, put a counter or a fresh random challenge inside the authenticated message and check it.
The tag protects exactly the bytes that were given to the tagging algorithm. A field left outside, such as a header, a length or the name of the intended receiver, can be changed without detection. Deciding which bytes go in is the subject of authentication coverage §5.41.
Authentic does not mean permitted or safe. A valid tag says where the message came from. Whether the sender may request this action, and whether the content is well formed, are separate checks that come after verification.
A hash function without a key is not a MAC. Neither is the digest of the key followed by the message when the hash function is SHA-256 or another function that outputs its whole final working state as the digest: the length-extension attack §5.11 lets an outsider who has seen one such digest compute the correct value for a longer message. HMAC avoids this.
Use one key for one purpose. If the same key authenticates two kinds of message that can be confused, a tag made for one kind can be accepted for the other. Key separation §5.23, a different key for each purpose, and domain separation §5.22, a purpose label inside the authenticated bytes, address this.
Questions answered
What forgery model permits the attacker to request tags for chosen messages?
The chosen-message attack model. A MAC (message authentication code) computes a short value, called a tag, from a secret key and a message, and a forgery is a valid tag produced without the key. In this model the attacker may ask for the genuine tag of any message it chooses, repeatedly. In the standard form, the adaptive chosen-message attack, each request may depend on the tags already received. The attacker must then produce a valid tag for a message it never asked about. A MAC that resists this attack is called existentially unforgeable under chosen-message attack, abbreviated EUF-CMA.
The model is deliberately generous to the attacker. A weaker model, the known-message attack, lets the attacker only observe messages that the legitimate parties chose. Real attackers often do better than that. They can cause a system to authenticate data that they supplied, for example by sending input that the system later stores or forwards with a tag. A MAC that is secure against chosen messages is secure in every weaker setting as well.
Two things lie outside the model. Sending again a message and tag that were genuinely produced is not a forgery, so a protocol must stop replays itself, with a counter or a fresh random challenge inside the authenticated message. And the model assumes that the key stays secret: an attacker who learns the key can tag anything. A stricter variant, strong unforgeability, also counts a different valid tag for an already tagged message as a forgery.
Related entries: HMAC (§5.8), Tag truncation (§5.12), Authentication coverage (§5.41), Replay attack (§1.26), Forgery probability (§5.13), Digital signature (§5.14), Pseudorandom function (PRF) (§15.1), Cipher-based MAC (CMAC) (§15.7), Constant-time comparison (§4.33). Further reading: [8, 66, 167, 310].