RFC 2104: HMAC: Authenticating messages with hash functions

Summary

In February 1997, Hugo Krawczyk, Mihir Bellare and Ran Canetti described HMAC in RFC 2104, a scheme that turns an ordinary hash function and a secret key into a message authentication code. The construction hashes the message twice with two differently derived keys and can be combined with any iterated hash function. HMAC is found today in TLS, IPsec, SSH, JWT and one-time passwords and proved surprisingly robust against weaknesses in the underlying hash functions.

Hash twice, two keys

RFC 2104 defines HMAC as H(K XOR opad, H(K XOR ipad, message)). The key is brought to the block length of the hash function, 64 bytes for MD5 and SHA-1, and combined once with the byte 0x36 (ipad) and once with 0x5C (opad). The inner hash processes the message, the outer hash the inner hash.

The text names its goals explicitly: use existing hash functions unmodified, preserve their speed, handle keys simply, offer a comprehensible security analysis and be able to replace the hash function if needed. The analysis itself is in a publication by the same authors from 1996.

Why HMAC outlived MD5

A simple hash over key and message is vulnerable, for example to length extension attacks. HMAC prevents this through the double nesting. Its security therefore depends less on the collision resistance of the hash function than on other properties.

When MD5 had long been broken for signatures, RFC 6151 concluded in March 2011 that the known attacks on HMAC-MD5 do not indicate a practical vulnerability when used as a message authentication code. For new protocols, experts nevertheless recommend HMAC with SHA-256 or stronger.

Ideas

  • HMAC turns a hash function and a key into a MAC.
  • Two nested hashes protect against length extension attacks.
  • ipad and opad derive two different keys from one.
  • The hash function can be replaced without changing the construction.
  • The key should be at least as long as the hash output.
  • A plain hash proves no authenticity.

Insights

  • A well-analysed construction can outlast the building blocks it is made of.
  • Cryptography is safest when existing, vetted algorithms are combined correctly instead of inventing new ones.

Facts

  • RFC 2104 appeared in February 1997 with the status Informational.
  • ipad is the byte 0x36, opad the byte 0x5C, each repeated to block length.
  • For MD5 and SHA-1 the block length is 64 bytes.
  • RFC 6151 from March 2011 sees no practical attack on HMAC-MD5 as a MAC.

References

Remarks

  • Time-based one-time passwords in authenticator apps rely on HMAC: TOTP according to RFC 6238 computes an HMAC over the current time.

Recommendations

  • Use HMAC through a maintained cryptographic library; never re-implement it yourself.
  • Choose HMAC-SHA-256 or stronger for new applications.
  • Always compare MACs in constant time to avoid timing attacks.

Read the RFC at the RFC Editor

Search the Web Archive

Found a mistake or have something to add? Get in touch.