HMAC
What is HMAC?
HMACA keyed Message Authentication Code construction built on top of a cryptographic hash function, defined in RFC 2104 and FIPS 198-1.
HMAC (RFC 2104, FIPS 198-1) turns any cryptographic hash H into a keyed authenticator: HMAC(K,m) = H((K' ⊕ opad) ‖ H((K' ⊕ ipad) ‖ m)), where K' is the secret key padded or hashed to the hash's block size, ipad is the byte 0x36 repeated and opad is 0x5c repeated. The two nested passes are what give HMAC its strength: Mihir Bellare's security proof shows HMAC is a secure PRF/MAC as long as the compression function is a PRF, so it does not require full collision resistance — which is why HMAC-MD5 and HMAC-SHA1 stayed practically safe for authentication even after collisions broke those hashes for digital signatures.
The nesting also defeats length-extension attacks that plague naive secret-prefix constructions of the form H(K ‖ m). The 2009 Flickr API signature forgery, disclosed by Thai Duong and Juliano Rizzo, exploited exactly such an MD5 secret-prefix signature, letting an attacker append parameters to a request yet still produce a valid signature; a proper HMAC would have prevented it. Implementations must still compare tags in constant time to avoid timing side channels. HMAC underpins TLS, JWT HS256, AWS Signature V4 and IPsec, and serves as the PRF inside HKDF (RFC 5869), PBKDF2 and TOTP.
flowchart TD K["Secret key K"] --> KP["Pad or hash to block size -> K'"] KP --> I["K' XOR ipad (0x36)"] KP --> O["K' XOR opad (0x5c)"] M["Message m"] --> IH["Inner hash: H(ipad block, m)"] I --> IH IH --> OH["Outer hash: H(opad block, inner hash)"] O --> OH OH --> TAG["HMAC tag"]
● Examples
- 01
JWT tokens with the HS256 algorithm are signed using HMAC-SHA-256.
- 02
AWS Signature Version 4 uses HMAC-SHA-256 to sign API requests.
● Frequently asked questions
What is HMAC?
A keyed Message Authentication Code construction built on top of a cryptographic hash function, defined in RFC 2104 and FIPS 198-1. It belongs to the Cryptography category of cybersecurity.
What does HMAC mean?
A keyed Message Authentication Code construction built on top of a cryptographic hash function, defined in RFC 2104 and FIPS 198-1.
How do you defend against HMAC?
Defences for HMAC typically combine technical controls and operational practices, as detailed in the full definition above.
What are other names for HMAC?
Common alternative names include: Keyed-Hash MAC, RFC 2104 HMAC.