Message authentication codes and HMAC
Why a plain hash cannot authenticate a sender, what a MAC adds, and HMAC's exact construction, structure and the reasoning behind why HMAC-MD5 is still safe to use.
- Explain why hashing alone, and message encryption alone, both fail to provide message authentication.
- State the two requirements a MAC function must satisfy.
- Reproduce the HMAC formula and the seven-step algorithm that computes it, including the ipad and opad constants.
- Explain why HMAC-MD5 remains safe to use even though MD5 itself is broken.
18 min read
Intuition
A bare hash fails to authenticate a sender because computing one needs no secret: an attacker can substitute a message and recompute a matching hash just as easily as the real sender could. Fixing this means tying the hash to something the attacker does not have. A Message Authentication Code (MAC) does exactly that, by mixing a shared secret key into the computation. It is sometimes called a keyed hash function for this reason.
Mechanism
Message authentication needs a function that generates an authenticator, a value used to authenticate a message, which a higher-level protocol then checks. Three classes of function can generate one:
Hash function alone. Insufficient on its own, an unprotected hash is exactly the person-in-the-middle weakness from the previous topic.
Symmetric message encryption. Encrypting with a shared key lets the receiver infer the message came from someone who knows the key, but an attacker can still flip bits of the ciphertext without knowing that key. If the plaintext happens to be an arbitrary bit sequence, the receiver has no way to tell the resulting decrypted garbage from a genuine message.
Public-key encryption. Plain public-key encryption gives confidentiality but not authentication: anyone can encrypt a forged message with the recipient’s public key and substitute it undetected. Encrypting with the sender’s private key instead gives authentication (only the sender could have produced it) but drops confidentiality, since anyone with the sender’s public key can decrypt it. Combining both, sign with the sender’s private key, then encrypt with the receiver’s public key, gives both properties, at the cost of running the public-key algorithm four times per message instead of two.
Mechanism
The MAC itself. A fixed-size tag, generated from the message and a secret key shared by both parties, and appended to the message. Also called a cryptographic checksum.
Formula
MAC
- the input message
- the secret key, shared between sender and receiver
- the MAC function
Sent alongside the original message, unlike encryption, which replaces it.
Mechanism
The receiver recomputes the MAC on the received message with the shared key and compares it to the one that was sent. Assuming the key is known only to sender and receiver, a match assures the receiver of three things: the message was not altered (an attacker without the key cannot produce a matching MAC for a modified message), the message’s origin (only a key holder could have generated a valid MAC), and, if a sequence number is included, that it was not tampered with either.
A MAC by itself provides none of this alongside confidentiality. Confidentiality is layered on separately, by encrypting either before or after the MAC computation.
Mechanism
A function used as a MAC must satisfy two requirements. First, it must have the resistance properties of a cryptographic hash function. Second, given a message and its MAC , it must be computationally infeasible to predict a correct MAC for a different message , even when other pairs are known. A cryptographic hash function satisfies the first requirement on its own; mixing in a secret satisfies the second, provided the resistance properties still hold once the secret is added.
Mechanism
HMAC is the most widely used way to build a MAC from a hash function: published as RFC 2104, mandatory for IPsec, used in SSL, and standardised by NIST as FIPS 198.
Formula
HMAC
- any cryptographic hash function, e.g. MD5, SHA-1, SHA-2
- the shared secret key
- the input message
- publicly known constant bit strings, required for HMAC's security proof to hold
ipad = 0x36 repeated for the block length; opad = 0x5c repeated for the block length.
Mechanism
In the notation the lecture uses for the structure: is the number of bits in one block, the number of blocks in , is padded with zeros on the left to reach bits, is the -th block of for , and is the length of the hash code produces.
Worked example
AnswerThe HMAC algorithm runs H twice: once over (K+ xor ipad) concatenated with M, once over (K+ xor opad) concatenated with that result.
- Append zeros to the left of to build a -bit string . For example, if is 160 bits and , this needs zero bits, equivalently 44 bytes, appended.
- XOR with to produce the -bit block .
- Append to .
- Apply to the stream from step 3.
- XOR with to produce the -bit block .
- Append the hash result from step 4 to .
- Apply to the stream from step 6 and output the result. This is .
Pitfall
The notes describe the padding in the worked example above as “44 zeroes” for a 160-bit key against a 512-bit block. Read literally as bits, that is wrong: , nowhere near the required 512 bits. The figure only works out if read as 44 bytes, which is bits, exactly the gap between 160 and 512. When the notes’ padding counts look inconsistent with the stated block size, check whether bits or bytes are meant before assuming an error.
Mechanism
Security of HMAC. The security of any hash-based MAC depends on the strength of the underlying hash. What makes HMAC specifically appealing is that its designers proved an exact relationship between the strength of the embedded hash function and the strength of HMAC built on top of it.
Worked example
AnswerYes, HMAC-MD5 is still safe to use, because attacking HMAC needs online observation under an unknown key, not offline computation.
- MD5 has a 128-bit output. By the birthday bound, roughly attempts give a 50% chance of a collision, feasible with today’s technology.
- Attacking MD5 directly can be done entirely offline: the attacker knows the algorithm and the default IV, so they can compute the hash of any message they choose without needing to observe anything.
- Attacking HMAC is different. The attacker does not know the secret key , so they cannot generate pairs offline. They must instead observe a sequence of messages generated by HMAC under the same unknown key.
- For a 128-bit hash, this requires observing about blocks, or bits, generated under one unchanging key.
- On a 1-Gbps link, observing that much traffic under a single unchanged key would take roughly 150,000 years, making the attack infeasible with today’s technology.
- Answer: yes, HMAC-MD5 remains safe to use. If raw speed matters and a choice is available, the slides note HMAC-MD5 can be preferred over HMAC-SHA-1 purely for that reason, not because MD5’s own weakness threatens HMAC.
Exam detail
Keep the two attack models distinct: attacking a hash function directly is an offline search over any input the attacker chooses, since the algorithm and its IV are public. Attacking a MAC built from that same hash function is an online search that requires observing real traffic under a secret, unknown key. That difference in threat model, not any special strength added by HMAC’s construction, is why “MD5 is broken” does not automatically mean “HMAC-MD5 is broken.”
Recall
Why can a broken hash function like MD5 still be used safely inside HMAC, when it cannot be trusted on its own?
Because the two are attacked differently. MD5 alone can be attacked entirely offline, since anyone can compute its hash for any chosen message. HMAC adds a secret key the attacker does not know, forcing them to observe real message-and-tag pairs generated under that key instead of computing their own, which for MD5’s 128-bit output pushes the practical cost up to roughly 150,000 years of continuous observation on a 1-Gbps link.
Source
Week 6 Notes PDF