โ† Wiki Index / Computation & Algorithms / HMAC
๐Ÿ’ป Computation & Algorithms

HMAC

HMAC is a keyed-hash message authentication code algorithm standardized in RFC 2104 that combines a cryptographic hash function with a secret key through a two-pass nested padding construction to verify data integrity and authenticity.

HMAC (Keyed-Hash Message Authentication Code) is a standardized cryptographic message authentication algorithm in computation and algorithms that calculates a cryptographic tag over a message using an underlying cryptographic hash function in combination with a shared secret key. Developed in 1996 by Mihir Bellare, Ran Canetti, and Hugo Krawczyk, HMAC was officially published in RFC 2104 and standardized by NIST as FIPS PUB 198-1, establishing the primary mechanism for verifying both the data integrity and authenticity of transmissions across IPsec, Transport Layer Security (TLS), JSON Web Tokens (JWT), and SSH.

While naive combinations of keys and hash functionsโ€”such as prefix hashing H(K || M) or suffix hashing H(M || K)โ€”exhibit severe structural vulnerabilities under length-extension attacks or collision attacks, HMAC's two-pass nested architecture provides provable pseudorandomness and existential unforgeability under standard cryptographic assumptions.

Mathematical Construction and Padding Bytes

Let H be an underlying iterated hash function operating on data blocks of B bytes (e.g., B = 64 bytes for SHA-256; B = 128 bytes for SHA-512) with an output digest length of L bytes. Let K be a secret key and M be an arbitrary-length message.

The HMAC algorithm executes a two-pass nested hashing operation parameterized by two distinct constant byte values:

  • Inner Pad (ipad): The byte 0x36 repeated B times (forming a B-byte block of 0x363636...36).
  • Outer Pad (opad): The byte 0x5C repeated B times (forming a B-byte block of 0x5C5C5C...5C).

The HMAC output is defined by the mathematical expression:

HMAC(K, M) = H((K0 โŠ• opad) || H((K0 โŠ• ipad) || M))

Key Preprocessing Protocol

Before executing the nested hashing passes, the input key K is normalized to an exact length of B bytes, yielding the preprocessed key K0:

  1. Key Shortening: If the key length exceeds the block size (LK > B), the key is hashed: K0 = H(K) || 0Bโˆ’L (padded with trailing zeros to reach B bytes).
  2. Key Padding: If the key length is shorter than the block size (LK < B), K is padded with trailing zero bytes: K0 = K || 0Bโˆ’LK.
  3. If the key is already exactly B bytes in length, K0 = K.

Two-Pass Execution Sequence

Once K0 is constructed, the evaluation proceeds in two distinct stages:

  • Inner Hash Pass: The preprocessed key is XORed with the inner pad (K0 โŠ• ipad) to produce a B-byte block. The message M is appended to this block, and the inner hash is computed:

hin = H((K0 โŠ• ipad) || M)

  • Outer Hash Pass: The preprocessed key is XORed with the outer pad (K0 โŠ• opad) to produce a second B-byte block. The L-byte digest hin from the inner pass is appended to this block, and the outer hash is computed:

Tag = H((K0 โŠ• opad) || hin)

The resulting L-byte string is the authenticated HMAC tag.

Provable Security and Length-Extension Defense

The design of HMAC was motivated by the mathematical failures of simple key-hash concatenations:

Vulnerability of Secret-Prefix Hashing

If a system computes Tag = H(K || M) using a Merkleโ€“Damgรฅrd hash function (such as SHA-256), it is vulnerable to a length-extension attack. Because the hash function's output represents its internal state after processing K || M, an adversary who knows M and the length of K can initialize a new hashing instance with Tag as the state and continue hashing an appended string M', producing a valid tag H(K || M || padding || M') without ever learning K.

HMAC completely neutralizes this attack through its outer hash pass: because the output of the inner hash hin is passed through the outer compression step H((K0 โŠ• opad) || hin), the internal state that processed M is hidden behind an independent keyed one-way permutation.

Security Reductions

Bellare, Canetti, and Krawczyk proved that HMAC is a Pseudorandom Function (PRF) and provides Existential Unforgeability under Chosen-Message Attacks (EUF-CMA). The security proof relies on two mild assumptions:

  1. The underlying compression function f is a secure PRF when keyed with the initialization vector or chaining variable.
  2. The compression function is weakly collision-resistant.

Significantly, the security of HMAC does not rely on the collision resistance of the hash function H. Even when the underlying hash function suffers collision vulnerabilities (as occurred with MD5 and SHA-1), HMAC-MD5 and HMAC-SHA1 remained resilient against practical forgery attacks, demonstrating the robustness of the nested architecture.

Applications in Key Derivation and Protocols

Beyond message authentication, HMAC serves as the foundation for modern pseudorandom key derivation:

  • HKDF (RFC 5869): The HMAC-based Extract-and-Expand Key Derivation Function breaks key generation into an Extract phase (distilling uniform entropy from raw input secrets using HMAC) and an Expand phase (generating cryptographically strong subkeys of arbitrary length). HKDF forms the cryptographic backbone of TLS 1.3, QUIC, and the Signal protocol.
  • PBKDF2 (RFC 8018): Password-Based Key Derivation Function 2 applies HMAC iteratively (typically tens of thousands of rounds) over a password and cryptographic salt to slow down brute-force and dictionary attacks.
  • API Authentication and HOTP: HMAC is widely implemented in REST API request signing (such as AWS Signature Version 4) and time-based one-time password generation (TOTP / RFC 6238).

Comparison of Message Authentication Primitives

PrimitiveUnderlying StructureKey SizeTag SizeDominant Hardware OptimizationPrimary Vulnerability
HMAC-SHA256Nested Hash FunctionVariable (32 bytes ideal)32 bytesCPU SHA extensionsSide-channel timing in tag comparisons
KMAC128Keccak Sponge (SHA-3)VariableConfigurableSIMD Keccak operationsNone currently known
AES-CMACBlock Cipher (AES)16 / 32 bytes16 bytesAES-NI instruction setBlock cipher collision bound (264)
Poly1305Universal Hash over GF(2130โˆ’5)32 bytes16 bytesFast 64-bit integer arithmeticCatastrophic failure if key is reused

References

  1. Bellare, M., Canetti, R., & Krawczyk, H. (1996). Keying hash functions for message authentication. Advances in Cryptology โ€” CRYPTO '96, 1-15.
  2. Krawczyk, H., Bellare, M., & Canetti, R. (1997). HMAC: Keyed-Hashing for Message Authentication. Internet Engineering Task Force RFC 2104.
  3. National Institute of Standards and Technology (US). (2008). The Keyed-Hash Message Authentication Code (HMAC). Federal Information Processing Standards Publication 198-1.
  4. Krawczyk, H., & Eronen, P. (2010). HMAC-based Extract-and-Expand Key Derivation Function (HKDF). Internet Engineering Task Force RFC 5869.
๐Ÿงฌ Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
๐Ÿ’ก Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision