Skip to content
Back to search results

HMAC

Introduced by
Hugo Krawczyk
Year
1997
Type
cryptographic authentication mechanism
Family
Message authentication code
Font size

What does it do?

HMAC (Hash-Based Message Authentication Code) is a cryptographic algorithm for authenticating messages using a secret key and a hash function.

Standardized in RFC 2104 (1997), HMAC can be combined with any cryptographic hash function (SHA-256, SHA-3, etc.) to create a message authentication code (MAC). HMAC provides both data integrity and authentication, detecting both accidental corruption and deliberate tampering, and is widely used in web security (TLS/SSL), API authentication, and network protocols.

The intuition behind the method

HMAC works by embedding a secret key into the hash computation in a specific way that prevents attackers from forging authentication codes. Rather than simply concatenating the key with the message and hashing, HMAC uses two nested hashing operations with key-derived padding values (ipad and opad). This double hashing design ensures that even if an attacker can find hash collisions, they cannot forge valid HMACs without knowing the secret key.

History

HMAC was designed by Krawczyk, Bellare, and Canetti in the mid-1990s as a standard, provably secure way to add authentication to any hash function. It was standardized as RFC 2104 in 1997 and quickly became ubiquitous in cryptographic protocols. The design remains secure to the present day and is compatible with the evolution of hash function standards, including the transition from SHA-1 to SHA-256 to SHA-3.

When to use it

HMAC is essential for message authentication in protocols where both parties share a secret key. Use HMAC when you need to verify that a message has not been modified and that it came from the authenticated source. Common applications include API authentication (signing requests with HMAC-SHA256), session management, and in TLS/SSL for additional authentication layers. HMAC is suitable for both high-speed and security-critical applications.

Limitations and considerations

  • Requires secure shared key management; both sender and receiver must possess the same secret key
  • Does not provide non-repudiation; both parties can deny creating or authorizing a message
  • Vulnerable to key recovery if the underlying hash function is broken or misused
  • Timing attacks on HMAC comparison can leak information about the correct code if verification is not constant-time

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Provides both message integrity and authentication in a single operation
  • Simple and efficient to compute, with minimal computational overhead compared to digital signatures
  • Compatible with any underlying hash function, offering flexibility in algorithm choice
  • Theoretically proven secure if the underlying hash function behaves like a random oracle

Applications

  • TLS/SSL/HTTPS for message authentication in secure web connections
  • API authentication (e.g., AWS Signature Version 4) for signing HTTP requests
  • Session management and CSRF token verification in web applications
  • IPsec and VPN authentication protecting network traffic
  • Git commit signing and verification for code integrity
  • Cryptocurrency and blockchain for transaction signing and verification

How it works

How it works

1

If the key is longer than the hash function's block size, hash it. If shorter, pad it with zeros to the block size.

2

XOR the padded key with ipad (a constant pattern) to create the inner key.

3

Concatenate the inner key with the message and compute the hash.

4

XOR the original padded key with opad (a different constant pattern), append the inner hash result, and compute the final hash.

Common pitfalls

  • Comparing HMACs using non-constant-time string comparison, allowing timing attacks to forge valid codes
  • Reusing HMAC keys across different protocols or contexts; derive key-specific keys using KDFs
  • Using weak hash functions in HMAC; always use modern functions like SHA-256 or SHA-3
  • Transmitting HMACs over unencrypted channels; HMAC alone does not provide confidentiality

Frequently asked questions

Why is HMAC better than just concatenating the key and message before hashing?

Simple concatenation (key || message) is vulnerable to length extension attacks. An attacker can compute HMAC(key || message || x) without knowing the key if they know the original HMAC. HMAC's nested design with separate padding prevents this attack.

How do I compare HMACs securely?

Use constant-time comparison functions (like secrets.compare_digest in Python) instead of standard string equality. This prevents timing attacks that allow an attacker to forge valid HMACs by observing how long comparison takes.

Can I use HMAC for key derivation?

HMAC alone is not a strong key derivation function. Use HKDF (HMAC-based Key Derivation Function) or PBKDF2 instead, which apply HMAC multiple times with additional randomness to generate cryptographic keys from passwords or shared secrets.

Is HMAC quantum-resistant?

HMAC's security relies on the underlying hash function. If the hash function is quantum-resistant, then HMAC is also quantum-resistant. SHA-3 and other post-quantum hash functions can be used with HMAC for future-proofing.

What hash function should I use with HMAC?

Use SHA-256 or SHA-3 for new systems. SHA-1 is deprecated for most uses. Avoid MD5. The choice depends on your security requirements and compatibility needs.

Related methods

Sources

  1. Krawczyk, H., Bellare, M., & Crechanko, R. (1997). HMAC: Keyed-Hashing for Message Authentication. RFC 2104.

    Open publication reference
    Copied.
  2. Bellare, 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
    Copied.