Also known as MAC. A keyed function that produces a short tag for a message , proving the message came from a holder of the key and was not altered.
The sender transmits . The receiver recomputes and accepts iff it equals .
History
Early MACs came from banking, where interbank transfers needed to be authenticated.
- 1981
ANSI X9.9 defines a DES based MAC for wholesale banking. - 1985
FIPS 113 standardizes CBC-MAC. - 1996
Bellare, Canetti and Krawczyk propose HMAC. - 1997
HMAC is published as RFC 2104. - 2000
Bellare, Kilian and Rogaway prove CBC-MAC secure for fixed-length messages. - 2002
HMAC is standardized in FIPS 198. - 2005
CMAC is standardized in NIST SP 800-38B.
Properties
- Integrity
Any change to changes the tag. - Authenticity
Only a holder of the shared key can produce a valid tag. - No non-repudiation
Both parties hold , so a tag cannot prove which of them created it.
A hash function alone gives no authenticity, since an attacker can alter and recompute the public digest.
Forgery Security
A MAC is secure iff it is existentially unforgeable under a chosen-message attack.
The attacker may request tags for any messages of their choice. The attacker wins by producing a valid tag for any message not yet queried, even a meaningless one.
The tag length bounds security. An -bit tag lets a random guess succeed with probability .
Verification Requirements
MAC verification confirms authenticity and integrity. It does not confirm that a message is new.
- Constant time comparison
Comparing tags byte by byte and stopping at the first mismatch leaks the matching prefix length through timing, letting an attacker forge a tag one byte at a time. - Replay
An attacker can capture a valid pair and send it again. The receiver accepts it because both and are unchanged. Include a sequence number or timestamp in . The receiver rejects a repeated sequence number or an expired timestamp.
Block-Cipher MACs
CBC-MAC
Encrypts the message with a block cipher in CBC mode using a zero IV. The tag is the final ciphertext block.
It is secure only when all messages have the same fixed length. For variable lengths, an attacker forges as follows.
- Obtain and for single-block messages.
- The message has tag .
The forged message has 2 blocks. CBC-MAC processes the first block before processing the second block .
Fixes:
- Length prefix
Prepend the message length as the first block. - CMAC Also known as OMAC. XOR a key derived subkey into the last block before the final encryption.
- ECBC-MAC
Encrypt the final tag again under a second key.
Hash-Based MACs
Hash functions are faster than block ciphers in software and were freely exportable, so MACs built from them were attractive. Turning a keyless hash into a keyed one needed care.
- Secret prefix
breaks under length extension on Merkle–Damgård hashes. Knowing gives a valid tag for . - Secret suffix
resists length extension. A collision still gives , so the MAC is only as strong as the collision resistance of . - Envelope
was an early patch. Its security was not proved, which led to HMAC.
HMAC
Also known as Hash-based MAC. Nests 2 hash calls under 2 keys derived from .
-
key padded with zeros to the hash block size, or hashed first if longer than a block
- the byte
0x36repeated to the block size - the byte
0x5crepeated to the block size
The outer hash hides the inner digest, which defeats length extension.
Bellare, Canetti and Krawczyk proved HMAC secure if the compression function of is a pseudorandom function. Collision resistance of is not required. HMAC-MD5 and HMAC-SHA-1 therefore stayed unbroken after collisions were found in MD5 and SHA-1, though new designs use HMAC-SHA-256.
HMAC is used in TLS, IPsec, JWT and as the core of HKDF in key derivation functions.
Universal Hash MACs
Faster than HMAC for small messages. Compresses the message using a secret key, then a block cipher or stream cipher masks the result. This is the Carter–Wegman construction.
Examples:
- Poly1305 by Daniel Bernstein, 2005
- GMAC, the authentication part of AES-GCM, 2004
Encryption and Authentication
Encryption alone does not detect tampering. See malleability.
- Encrypt-then-MAC
Encrypt to , then send . This is the secure composition. The receiver rejects tampered ciphertext before decrypting. - MAC-then-encrypt
Used in older TLS. Padding errors during decryption leaked information, enabling padding oracle attacks. - Encrypt-and-MAC
Used in SSH. The tag of the plaintext can leak information about the plaintext.
The encryption key and the MAC key must be different.