- Introduced by
- Hugo Krawczyk
- Year
- 1997
- Type
- cryptographic authentication mechanism
- Family
- Message authentication code
What does it do?
HMAC (Hash-Based Message Authentication Code) is a message authentication code built from a cryptographic hash function and a secret key.
It is specified in RFC 2104 (1997) and can be instantiated with any suitable hash function, such as SHA-256 or SHA-3. A valid HMAC tag shows that a message was not altered and was produced by a party holding the shared key, which is why it appears in protocols such as TLS/SSL.
The intuition behind the method
HMAC does not simply hash the key together with the message. It runs two nested hash computations, and the padded key is mixed into each one with a different fixed pattern: ipad for the inner hash and opad for the outer hash. The outer hash takes the inner hash output as its input, so the final tag is never a plain hash of key and message. Under the assumption that the underlying compression function behaves as a pseudorandom function, this nesting makes forging a tag without the key infeasible; collision resistance alone is not the basis of the guarantee.
History
HMAC was introduced in the mid-1990s by Krawczyk, Bellare and Canetti as a way to build a message authentication code from any hash function with a provable security argument. The 1996 paper by Bellare, Canetti and Krawczyk on keying hash functions for message authentication sets out that analysis, and the construction was standardized as RFC 2104 in 1997. No practical attack against the HMAC construction itself is established. The construction is defined in terms of a generic hash function and its block size, so the same structure applies to SHA-256 and SHA-3 as well as to SHA-1; the block size is taken from the chosen hash.
When to use it
Choose HMAC when two parties already share a secret key and need to confirm that a message is unaltered and came from a holder of that key, for example when signing API requests or verifying tokens in a web session. It needs only hash operations, so it suits high-volume use. Do not use HMAC when a third party must be able to verify who created a message, because anyone holding the key could have produced the tag; a digital signature is the appropriate tool then. HMAC does not hide message content, so it must be combined with encryption where confidentiality is required. Do not use HMAC directly to derive keys from passwords; a dedicated key derivation function such as PBKDF2 is designed for that purpose.
Limitations and considerations
- Both parties hold the same key, so either can produce a valid tag; the tag cannot prove to a third party which party created the message (no non-repudiation).
- Security depends entirely on key secrecy and on how keys are distributed; anyone who obtains the key can forge tags for any message.
- The message is not encrypted. HMAC establishes authenticity and integrity, not confidentiality.
- Verification recomputes the tag over the received message and compares it with the received tag; a comparison that leaks timing information can reveal the correct tag to an attacker. Verification should reject malformed or wrongly sized tags before comparison, and compare the full tag in constant time, rejecting on any mismatch.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Provides integrity and authentication with a shared key using only hash operations and no public-key arithmetic, which is why it is common for signing high volumes of requests.
- The nested construction means the tag is not a plain hash of key and message, so it avoids the length extension weakness of hash(key || message) constructions in iterated hash functions such as SHA-256.
- Can be instantiated with different hash functions, so the hash can be replaced when it is deprecated without changing the construction.
- The security argument reduces to the pseudorandom function property of the compression function, as analysed in the 1996 paper by Bellare, Canetti and Krawczyk; the guarantee is conditional on that assumption.
Applications
- TLS/SSL/HTTPS for message authentication in secure web connections
- API request signing, for example AWS Signature Version 4 for signing HTTP requests
- Session management and CSRF token verification in web applications
- IPsec and VPN authentication of network traffic
How it works
How it works
If the key is longer than the hash function's block size, hash it first; if it is shorter, pad it with zeros up to the block size. The result is a key of exactly one block length.
XOR the block-sized key with ipad, a fixed constant pattern, to obtain the inner key.
Hash the concatenation of the inner key and the message. The output of this step is the inner hash.
XOR the block-sized key with opad, a different constant pattern, then hash the concatenation of this outer key and the inner hash. The output of this final hash is the HMAC tag.
Common pitfalls
- Comparing tags with ordinary string equality rather than a constant-time comparison, which can allow timing attacks to forge valid codes.
- Using one key for several purposes. Keys for different protocols or contexts should be separated, for example by deriving context-specific keys with a key derivation function such as HKDF.
- Truncating the tag without analysis. A shorter tag leaves an attacker a larger space of tags to guess, so any truncation should follow an explicit security requirement of the protocol.
- Relying on a weak hash such as MD5, or on SHA-1 in new designs. HMAC's security argument depends on the properties of the underlying hash, so SHA-256 or SHA-3 are preferred for new systems.
Frequently asked questions
Why is HMAC better than just concatenating the key and message before hashing?
Hashing the key concatenated with the message, hash(key || message), is vulnerable to length extension in iterated hash functions such as SHA-256. Given a valid tag, an attacker can compute a valid tag for an extended message without knowing the key. HMAC's outer hash over a key-XORed input prevents this, because the attacker's extension is never fed to the function that produces the final tag.
How do I compare HMACs securely?
Use a constant-time comparison function, such as secrets.compare_digest in Python, instead of standard string equality. This prevents timing attacks in which an attacker infers the correct tag by observing how long comparisons take.
Can I use HMAC for key derivation?
HMAC alone is not a password-hashing scheme or a general key derivation function. HKDF (HMAC-based Key Derivation Function) applies HMAC in a defined process to derive keys from a shared secret. PBKDF2 applies HMAC repeatedly with a salt and an iteration count and is intended for deriving keys from passwords.
Is HMAC quantum-resistant?
HMAC's security rests on the underlying hash function, so the hash choice and the lengths of keys and tags determine its margin. Generic quantum search can reduce the effective security of symmetric primitives, so key and tag lengths should be chosen with that reduction in mind. No attack that breaks HMAC itself is established. Quantum resistance therefore depends mainly on the hash function and parameter choices rather than on the construction alone.
What hash function should I use with HMAC?
Use SHA-256 or SHA-3 for new systems. MD5 should be avoided, and SHA-1 is not recommended for new designs. The choice depends on your security requirements and compatibility needs.
Related methods
Sources
Krawczyk, H., Bellare, M., & Crechanko, R. (1997). HMAC: Keyed-Hashing for Message Authentication. RFC 2104.
Open publication referenceBellare, M., Canetti, R., & Krawczyk, H. (1996). Keying hash functions for message authentication. In Advances in Cryptology - CRYPTO 1996, LNCS 1109, pp. 1-15.
Open DOI reference