← Back to section · Foundations of Cryptography
Why HMAC Is Built the Way It Is: The Reasoning Behind Two Layers and Two Constants
HMAC looks like merely hashing key and message together twice, but every detail — the ipad and opad constants, the inner-then-outer nesting, pre-hashing over-long keys — answers a specific failure mode. This article takes apart the 1996 design that remains the default answer for MACs.
The most natural way to turn a hash into a keyed message authentication code is to stir the key into the message and hash. Before HMAC was standardized in 1996, people tried nearly every concatenation, and nearly every one was broken. HMAC’s final shape — two nested layers and two odd constants — is not an aesthetic choice but a point-by-point response to a string of failures. Understanding those responses is understanding the fundamentals of MAC design.
How the three naive schemes die
The first, the prefix scheme , puts the key at the front. It dies of the length extension described in a companion article: an iterated hash’s final state is its output, so an attacker holding a tag can keep iterating and forge tags for extended messages, with the key never entering the picture. The second, the suffix scheme , does stop length extension, but it bets its security on collision resistance: if an attacker can find a collision between and , the keyed computations may be made to “meet” before the key is ever added, and forgery follows — so when a hash’s collisions fall (the fate of MD5 and SHA-1), schemes like this are buried with it. The third, the sandwich scheme , plugs both ends and looks sound, but analysis showed it still leaks structure and admits no clean security reduction. The common thread: all three try to assemble a keyed primitive out of concatenation, and the position of the seam itself becomes the attack surface.
HMAC’s answer: nesting plus two constants
HMAC’s formula is:
where is the key padded (or first hashed, see below) to the hash’s block length, ipad is the byte 0x36 repeated to block length, and opad is 0x5c repeated likewise. Take it apart and every piece has a job. The inner layer processes the message under a key-derived mask and produces an intermediate hash; the outer layer hashes that intermediate result under key material once more. What the attacker sees is only the outer output, which is neither the inner layer’s internal state nor anything that can be iterated onward — length extension is beheaded here. And the two well-separated constants ipad and opad derive two sufficiently different effective keys from the same key: XORing with two very different constants bends one key into two unrelated ones, locking two doors separately and denying any exploitable correlation between the layers. That is also why the constants are 0x36 and 0x5c specifically — they differ in many bit positions, and they deliver a two-key effect from a single key, sparing all key-management fuss.
One easily missed detail: if the key is longer than the block size, hash it first, then pad. This keeps the key material entering the construction at a controlled length and prevents long keys from creating unexpected structure in the concatenation.
HMAC’s second job: deriving keys
HMAC’s uses have long outgrown “compute a tag for a message”. The skeleton of modern key derivation, HKDF (RFC 5869), has two steps: the extract step uses HMAC to concentrate possibly uneven key material (such as a key exchange’s shared secret) into a short, strong pseudorandom key; the expand step then iterates HMAC to stretch it into as many independent subkeys as needed. TLS 1.3’s entire key schedule is built on HKDF. Another long-lived example is PBKDF2, which uses HMAC as a repeatedly applied “slowing-down” function, iterated thousands of times to derive keys from passwords (modern password storage has moved to memory-hard functions of the Argon2 family — a companion article in this section covers that — but PBKDF2-HMAC remains widely deployed). Both jobs tell the same story: HMAC is in truth a reusable “keyed pseudorandom function building block”, and the MAC is merely its original post.
Two implementation details are often asked about. First, may the tag be truncated? The standard permits using a shortened HMAC output (keeping the first 128 bits, for example), which is common where bandwidth matters; truncation lowers the ceiling on forgery probability, so keep enough bits for your security target, and do not go below 80 bits. Second, verification must compare in constant time: if a byte-by-byte tag comparison returns at the first differing byte, response timing leaks the tag’s prefix, and an attacker can extract the correct tag byte by byte — an implementation error rather than a construction error, yet one genuinely present in plenty of home-grown signature-checking code.
Why HMAC outlives its hash functions
HMAC’s deepest property is its security reduction: Bellare and colleagues proved that as long as the underlying hash’s compression function behaves like a pseudorandom function, HMAC is a secure MAC. Note that this condition does not require collision resistance. Reality supplied a striking verification: after MD5’s collisions were thoroughly broken in 2004, HMAC-MD5’s security as a MAC did not collapse with them (though prudence long ago stopped recommending it for new systems); and after SHA-1 collisions became practical, HMAC-SHA1 was still considered broadly serviceable for authentication. By contrast, every naive concatenation that bet on collision resistance died alongside the collision attacks. This is another case of “the construction decides the fate”: HMAC rests its trust on a weaker, more durable assumption.
A selection question is worth clarifying here: given that the sponge’s KMAC, and BLAKE2’s built-in keyed mode, can also serve as MACs, why does HMAC remain the default? The answer is mainly ecosystem and analytic stock: HMAC’s proof framework, library implementations, hardware support and thirty years of field testing form enormous inertia, and its performance is entirely sufficient for the overwhelming majority of scenarios. New constructions certainly have their place in new protocols (inside the SHA-3 suite, KMAC is the preferred choice), but “newer” is by itself no reason to replace a healthy primitive — the same attitude this site takes toward algorithm migration generally: the decision turns on risk and benefit, not on old versus new.
In today’s engineering reality, HMAC-SHA256 is the default choice for API signing, token authentication and key derivation (HKDF’s extract step is exactly HMAC). It has needed almost no modification in nearly thirty years — rare longevity in cryptography — and the reason for that longevity hides precisely in those two slightly odd-looking constants.
Tags: HMACMessage AuthenticationKeyed Hashing