Fact-checked by Grok 2 weeks ago

HMAC

HMAC (Hash-based Message Authentication Code) is a cryptographic mechanism for verifying the integrity and authenticity of a message, utilizing a secret shared key in conjunction with a cryptographic hash function to produce a fixed-length tag that accompanies the message. It ensures that any alteration to the message during transmission would result in a mismatched tag, thereby detecting tampering or forgery by unauthorized parties. Developed as a secure and efficient alternative to earlier keyed hash constructions, HMAC is widely adopted in protocols such as TLS, IPsec, and SSH for protecting data in transit. The construction of HMAC involves nesting two applications of the underlying , with the XORed against fixed constants to create inner and outer hashes. Specifically, for a H with block size B, HMAC(K, m) = H((K* ⊕ opad) ∥ H((K* ⊕ ipad) ∥ m)), where K* is the padded or hashed to B bytes if necessary, opad is the byte 0x5C repeated B times, and ipad is the byte 0x36 repeated B times. This design leverages the of the in a way that provides provable security under standard assumptions about the 's properties, such as or . HMAC was first proposed in 1996 by Mihir Bellare, Ran Canetti, and Hugo Krawczyk in their paper "Keying Hash Functions for Message Authentication," which introduced both NMAC (a nested MAC) and HMAC as practical, efficient schemes compatible with existing hash functions like and without requiring modifications. The mechanism was standardized in RFC 2104 in February 1997 by the (IETF), specifying its use with iterative hash functions for internet protocols. Subsequently, the National Institute of Standards and Technology (NIST) formalized HMAC in Federal Information Processing Standard (FIPS) 198 in 2002, emphasizing its dependence on the underlying hash function's strength and recommending secure hashes like SHA-256 for contemporary applications. Security analyses have confirmed HMAC's robustness; for instance, its pseudorandom (PRF) properties hold as long as the hash's compression behaves as a PRF, even without full , making it resilient against known attacks on specific hashes when paired with secure ones. Today, HMAC remains a cornerstone of cryptographic practice, integral to standards like for API and used in various cryptographic protocols including those based on CMS () for message , with ongoing recommendations to migrate to longer-output variants like HMAC-SHA-512 amid breaks in legacy hash functions such as SHA-1.

Introduction

Overview

HMAC, or Hash-based Message Authentication Code, is a cryptographic construction that combines a with a secret key to generate a (MAC). This mechanism produces a fixed-size output, known as a , which serves as a for the message, allowing the recipient to verify its authenticity and integrity. By incorporating the secret key, HMAC ensures that only parties possessing the key can generate valid tags, thereby preventing unauthorized alterations or forgeries. The primary purpose of HMAC is to provide both —confirming that the message has not been tampered with—and —ensuring it originated from a legitimate source—through the use of a key between sender and receiver. This makes HMAC particularly useful in secure communication protocols, such as those in TLS/SSL, , and various authentications, where verifying the genuineness of transmitted data is essential. Unlike unkeyed functions like or SHA-256, which only provide integrity checks without , HMAC leverages these existing hash primitives to add key-based without requiring entirely new algorithms. In contrast to other MAC constructions like CMAC, which often rely on block ciphers, HMAC's design capitalizes on the widespread availability and efficiency of functions, making it versatile and computationally lightweight for software implementations. It was first standardized by the (IETF) in RFC 2104 in 1997, with an update in RFC 6234 in 2011 to include additional functions and clarifications. This has cemented HMAC's role as a foundational tool in modern , adopted in numerous standards and libraries worldwide.

Historical Development

HMAC was invented in 1996 by cryptographers Mihir Bellare, Ran Canetti, and Hugo Krawczyk to address vulnerabilities in earlier methods for keying cryptographic functions for . These prior constructions, such as those proposed for in drafts, lacked rigorous security proofs and were susceptible to attacks like birthday paradoxes or length-extension exploits when keys were simply prepended or appended to messages. The designers aimed to create a practical, efficient scheme that treated the as a , providing provable security under standard assumptions about the hash's compression function while minimizing performance overhead compared to block ciphers. The core ideas were first detailed in the seminal 1996 paper "Keying Hash Functions for Message Authentication," presented at CRYPTO '96, which introduced the nested MAC (NMAC) construction and its practical instantiation, HMAC, compatible with popular hashes like and SHA-1. This was followed by a more accessible exposition in the Spring 1996 issue of RSA Laboratories' CryptoBytes. Formal standardization came swiftly with IETF 2104 in February 1997, which specified HMAC's algorithm and recommended its use over insecure variants, establishing it as a key mechanism for protocols requiring message integrity. Subsequent updates refined HMAC's implementation for evolving hash functions. In 2002, NIST published FIPS 198, formalizing HMAC as a federal standard for keyed-hash message authentication. In May 2011, RFC 6234 provided detailed guidance and reference code for applying HMAC with the family (SHA-224, SHA-256, SHA-384, SHA-512), addressing the need for stronger amid growing concerns over SHA-1's . Similarly, NIST's Special Publication 800-107 (Revision 1, August 2012) incorporated HMAC into federal guidelines for approved hash-based applications, emphasizing its role in achieving desired strengths for digital signatures and key derivation. HMAC saw rapid adoption in core protocols starting in the late 1990s. It was integrated into TLS 1.0 via 2246 (January 1999) for authenticating messages and records using HMAC- or HMAC-SHA-1. In , 2404 (November 1998) defined HMAC-SHA-1-96 for authentication in and , enabling widespread deployment in VPNs and secure communications by the early 2000s. As cryptographic standards evolved, -based HMAC faced deprecation in modern contexts; for instance, (published March 2019, with validations accelerating post-2022) restricts MD5 to legacy modes only, prohibiting its use in new approved implementations to mitigate collision vulnerabilities. As of June 2025, NIST proposed withdrawing 198-1, with HMAC guidance moving to draft SP 800-224 (June 2024).

Background Concepts

Cryptographic Hash Functions

A is a mathematical that maps of an arbitrary size to a fixed-size value, typically represented as a number, known as the value or digest. This mapping is deterministic, meaning that the same input always produces the same output, and it is designed to be one-way, ensuring that it is computationally infeasible to reverse the process and recover the original input from the hash. The output length is fixed regardless of input size, which enables efficient storage and comparison for applications like data verification. The core properties of a include preimage resistance, second preimage resistance, and . Preimage resistance ensures that, given a , it is computationally infeasible to find any input that produces that exact output. Second preimage resistance guarantees that, for a given input and its corresponding , it is infeasible to find a different input that yields the same . provides the strongest protection by making it computationally infeasible to discover any two distinct inputs that produce the same output. These properties collectively prevent attacks that could undermine the function's reliability in contexts. Additional key characteristics enhance the robustness of cryptographic hash functions. The describes how a minimal change in the input, such as flipping a single bit, results in a dramatically different output, typically altering about half of the bits in the to ensure diffusion of changes. Many widely used hash functions, including those in the SHA family, employ the Merkle-Damgård construction, which processes input data through padding to a multiple of the block size followed by iterative compression using a . This structure allows handling of variable-length messages while preserving security properties derived from the underlying compression function. Common examples illustrate the evolution and current status of these functions. , producing a 128-bit output, was once popular but is now considered insecure for new applications due to practical collision attacks demonstrated in 2004. , with a 160-bit output, has been deprecated by NIST since 2011 for most uses, including digital signatures, following theoretical and practical weaknesses in its . In contrast, SHA-256, generating a 256-bit output, remains widely adopted and secure for contemporary applications, providing a security level of 128 bits against collisions. While cryptographic hash functions excel at ensuring data integrity by detecting unauthorized modifications, they do not inherently provide authentication, as anyone can compute the hash without verifying the message's origin; keying mechanisms are required to address this limitation.

Message Authentication Codes

A message authentication code (MAC) is a family of cryptographic functions parameterized by a symmetric secret key, which takes a message as input and produces a fixed-length authentication tag (or simply "tag") that verifies the message's integrity and the sender's authenticity when recomputed by a party possessing the same key. This tag ensures that the message has not been altered in transit and originates from a legitimate source, providing data origin authentication without relying on public-key infrastructure. MACs are essential in protocols requiring symmetric-key confidentiality and integrity, such as secure communication channels. The of a is defined by its resistance to existential under chosen-message attacks (EUF-CMA), where an adversary, given to tags for messages of its choice, cannot produce a valid for a new message with non-negligible probability. This property makes it computationally infeasible to forge a without knowledge of the , even after observing multiple valid message-tag pairs. A secure MAC must also resist other attacks, such as recovery or universal , ensuring overall robustness in adversarial settings. MACs can be constructed using either block ciphers or cryptographic hash functions, each with distinct trade-offs in , assumptions, and implementation complexity. Block-cipher-based MACs, such as and PMAC, encrypt the message in a chained mode to generate the tag, offering provable under standard assumptions for the underlying like , but requiring a block primitive and potentially higher computational overhead for long messages. In contrast, hash-based MACs like HMAC nest a keyed to produce the tag, leveraging widely available and mature hash designs (e.g., SHA-256) without needing a separate block , which simplifies deployment in resource-constrained environments and often yields better for variable-length messages. However, the security of hash-based MACs depends heavily on the hash function's properties, whereas block-cipher-based ones inherit from the cipher's . Naive approaches to keying hash functions for MACs, such as simple concatenation like hash(key || message) or hash(message || key), are insecure due to vulnerabilities like length-extension attacks, where an adversary can append arbitrary data to a known message and compute a valid tag without the key by exploiting the hash's internal state. These prefix- or suffix-keyed constructions fail to provide EUF-CMA security because they allow attackers to forge tags for extended messages using only the original tag and message length, motivating more sophisticated designs like HMAC that inner and outer pad the key to mitigate such flaws.

Construction and Computation

Formal Definition

The Hash-based Message Authentication Code (HMAC) is formally defined as \text{HMAC}(K, m) = H\left( (K \oplus \text{opad}) \parallel H\left( (K \oplus \text{ipad}) \parallel m \right) \right), where H denotes the underlying , K is the secret key, m is the to be authenticated, \oplus represents bitwise XOR, \parallel denotes , \text{ipad} is the inner padding consisting of the byte $0x36 repeated B times, and \text{opad} is the outer padding consisting of the byte $0x5C repeated B times, with B being the block size of H. The key K is processed to fit the block size B: if its length is less than B, it is padded on the right with zeros to reach length B; if longer than B, it is first hashed using H to produce a B-byte intermediate key, which is then used in place of K. This key derivation ensures compatibility with the hash function's input requirements while maintaining security properties. The output of HMAC is a fixed-length authentication tag of the same size as the digest produced by H, providing a compact value; for instance, HMAC-SHA256 yields a 256-bit (32-byte) tag. The block size B varies by : it is 64 bytes (512 bits) for and SHA-256, and 128 bytes (1024 bits) for SHA-384 and SHA-512.

Step-by-Step Algorithm

The computation of HMAC begins with preprocessing the secret key K to ensure it matches the block size B of the underlying H. If the length of K is exactly B bytes, it is used directly as the derived key K_0. If shorter than B, K is padded on the right with zeros to reach B bytes, forming K_0. If longer than B, H is first applied to K to produce an L-byte hash output (where L is the digest length of H), which is then padded on the right with zeros to B bytes, yielding K_0. Next, the inner hash is computed as follows: Form the inner padded key by XORing each byte of K_0 with the inner padding value ipad (the byte $0x36 repeated B times), denoted as s_{\text{ipad}} = K_0 \oplus \text{ipad}. Append the message m to s_{\text{ipad}}, resulting in the input block s_{\text{ipad}} \parallel m. The H is then applied to this input, incorporating the standard rules of H (such as length-appended bit in or SHA-family algorithms) to process the concatenated data into complete blocks before finalizing the digest. This produces the intermediate result s_1 = H((K_0 \oplus \text{ipad}) \parallel m), which is an L-byte value. The outer hash follows similarly: XOR each byte of K_0 with the outer padding value opad (the byte $0x5C repeated B times), forming s_{\text{opad}} = K_0 \oplus \text{opad}. Append the s_1 from the inner hash to s_{\text{opad}}, creating the outer input s_{\text{opad}} \parallel s_1. Apply H to this input, again using the hash function's standard (e.g., appending the bit length of the input to ensure secure processing of any non-block-aligned ). The resulting L-byte digest is the HMAC output: H((K_0 \oplus \text{opad}) \parallel s_1). All operations are performed at the byte level, with XOR applied bitwise to corresponding bytes. Optionally, the HMAC output may be truncated to a shorter length t bits (where t < L \times 8) by taking the leftmost t bits, such as for truncated to 128 bits, to improve efficiency in bandwidth-constrained environments; however, this reduces the security margin and requires t to be sufficiently large to maintain resistance to attacks, as detailed in subsequent security analyses.

Design Principles

Rationale for Structure

The structure of HMAC, which employs nested applications of a cryptographic hash function H with the secret key K, was designed to address key vulnerabilities inherent in simpler message authentication code (MAC) constructions, such as directly hashing the key concatenated with the message H(K ∥ m). In particular, this nested approach—computing H(K ⊕ opad, H(K ⊕ ipad, m)) where opad and ipad are fixed padding constants—avoids length-extension attacks that plague prepend-only key placements, as an attacker cannot exploit the hash function's padding mechanism to forge valid MACs for extended messages without knowledge of K. By applying the hash twice, the inner computation deeply integrates the key into the message's compression process, while the outer hash adds an additional layer of protection, ensuring security even when H follows the common in hash functions like MD5 and SHA-1. The use of fixed inner (ipad) and outer (opad) paddings, selected for their differing hexadecimal values (0x36 and 0x5C, respectively), creates two distinct key schedules that promote diffusion across the hash computations without depending on the randomness or entropy of the key itself. This choice ensures that the effective keys for the inner and outer hashes differ substantially in their bit patterns, enhancing the overall mixing properties and resistance to certain analytical attacks. A primary motivation for HMAC's design was compatibility with existing hash function implementations, allowing it to leverage mature, optimized code for functions like or without requiring internal modifications to their algorithms. This reuse preserves the performance characteristics of the underlying hash while enabling straightforward adoption in cryptographic protocols and software, as the construction treats H as a black-box primitive.

Parameter Selection

The selection of parameters in HMAC is critical to ensuring its security as a message authentication code, primarily involving the choice of an underlying cryptographic hash function and the secret key. The hash function should be secure, with strong collision resistance to minimize forgery risks, as approved by NIST for use in HMAC constructions. Deprecated functions like MD5, which suffered practical collisions in 2004, and SHA-1, broken by a full collision attack in 2017, must be avoided due to their vulnerability to forgery risks in HMAC. Recommended options include SHA-256 or stronger variants from the SHA-2 family (e.g., SHA-384, SHA-512), which offer at least 128 bits of security against collisions, or SHA-3 family functions (e.g., SHA3-256, SHA3-512) for enhanced resistance, particularly in anticipation of quantum threats where Grover's algorithm could halve classical security levels. Note that shorter outputs like SHA-224 may face deprecation after 2030 for new applications. The secret key for HMAC should be generated randomly using a secure random number generator compliant with NIST standards, such as those in SP 800-90A, to ensure unpredictability. Its length must be at least the output size of the hash function (e.g., 256 bits for SHA-256) to match the security strength, though NIST specifies a minimum of 128 bits overall; keys longer than the hash block size are hashed down to fit, but using keys up to the block size is preferred for optimal resistance to key recovery. Key rotation practices are essential for long-term security, involving periodic replacement (e.g., annually or after a fixed number of authentications) to limit exposure if a key is compromised, while maintaining backward compatibility through dual-key verification during transitions. The block size B of the hash function influences HMAC's efficiency and security margin; for instance, SHA-256 uses B = 512 bits, while SHA-512 employs B = 1024 bits, providing a larger inner padding space that enhances resistance to certain extension attacks but increases computational overhead. Larger block sizes are advisable for high-security applications despite the performance trade-off. When truncating the HMAC output for bandwidth constraints, NIST requires at least 32 bits, with fixed lengths per key lifecycle, but truncations under 64 bits necessitate a formal risk assessment to ensure sufficient forgery resistance (e.g., retaining at least half the full output length for 128-bit security).

Security Analysis

Theoretical Foundations

The security of HMAC as a message authentication code (MAC) is established in cryptographic models where the underlying hash function H serves as a pseudorandom function (PRF) or possesses weak collision resistance. Specifically, if H is weakly collision-resistant—meaning it is computationally infeasible for an adversary to find two distinct inputs with the same output under a fixed initialization vector—then HMAC inherits this property to provide existential unforgeability against adaptive chosen-message attacks. This model ensures that an adversary, after querying the MAC oracle on up to q messages, cannot produce a valid tag for a new message with non-negligible probability. A foundational proof by Bellare, Canetti, and Krawczyk in 1996 demonstrates that the existential unforgeability of reduces directly to the collision resistance of H. Under an adaptive chosen-message attack, the probability of successful forgery is at most approximately \frac{q^2}{2^n}, where q is the number of oracle queries and n is the bit length of the hash output; this bound arises from the birthday paradox applied in the reduction, where breaking implies finding a collision in H keyed by a secret value. The proof employs a black-box reduction, treating the iterated hash function as an oracle without access to its internals, which simplifies practical verification. This analysis was formalized in the same work and later specified in , which standardizes and references the security guarantees. Subsequent extensions refined these foundations by relaxing assumptions. In 2006, Bellare provided new proofs showing that HMAC is a secure PRF if the underlying compression function of H is a PRF, eliminating the need for collision resistance and extending security up to roughly $2^{n/2} queries before birthday attacks become feasible. Post-2010 analyses addressed multi-user scenarios, where multiple parties share the construction; Backendal et al. (2023) established tight multi-user security bounds for HMAC as a dual-PRF, confirming robustness even when messages can serve as keys in protocols like TLS 1.3. Additionally, despite practical breaks like collisions in MD5, HMAC-MD5 retains security for authentication purposes, as its guarantees rely on the weaker PRF property of the hash rather than full collision resistance, per updated considerations in RFC 6151.

Vulnerabilities and Mitigations

HMAC inherits certain weaknesses from the underlying hash function, but its keyed construction often mitigates collision-based attacks that affect the hash in isolation. For instance, despite the practical collision attack on demonstrated in the 2017 , HMAC-SHA1 remains secure as a message authentication code because forging an HMAC requires knowledge of the secret key, and the collision does not extend to the keyed domain. Similarly, HMAC-MD5 is vulnerable to forgery only if the secret key is exposed or revealed post-authentication, as MD5's collision vulnerabilities do not directly compromise the MAC when the key remains confidential. Side-channel attacks pose significant risks to HMAC implementations, particularly in hardware or software environments where physical or timing information can leak key material. Timing attacks can exploit variable execution times during hash computations or tag comparisons, allowing attackers to infer key bits through repeated measurements. Power analysis attacks, such as differential power analysis (DPA), target the intermediate computations in HMAC-SHA-2 by analyzing power consumption traces to recover the key after as few as a few thousand traces in the Hamming weight model. Mitigations include implementing constant-time algorithms to eliminate timing variations and using blinding techniques, where inputs are randomized with ephemeral values before processing and derandomized afterward. Hardware accelerations, such as , enable efficient constant-time SHA computations, reducing exposure in software implementations. Key management flaws can undermine HMAC's security, especially with weak or predictable keys. Using an all-zero key or keys shorter than the hash function's block size results in zero-padding, which reduces effective entropy and makes brute-force attacks more feasible, as the padded key effectively lowers the security margin. In multi-user scenarios, where multiple parties share or derive keys from a common source, the forgery probability increases significantly; for example, HMAC with 80-bit keys provides only about 40 bits of security per user in such settings due to amplified collision risks across queries. Recent advisories highlight HMAC's resilience to major breaks but emphasize preparations for quantum threats and long-term agility. Grover's algorithm reduces the effective key strength of symmetric primitives like HMAC by a factor of two, halving brute-force complexity from $2^n to $2^{n/2}; thus, NIST and other bodies recommend at least 256-bit keys for HMAC to maintain 128-bit post-quantum security. No structural breaks have been found in HMAC, but protocols should incorporate hash agility— the ability to swap underlying hash functions (e.g., from SHA-1 to SHA-3)—to adapt to emerging weaknesses without redesign. NIST approves SHA-2 and SHA-3 families for HMAC use, prioritizing these over deprecated hashes like MD5 or SHA-1.

Practical Usage

Implementation Considerations

Implementing HMAC securely requires reliance on established cryptographic libraries to avoid the risks associated with custom implementations, which can introduce subtle vulnerabilities due to implementation errors. Recommended libraries include , which provides a robust HMAC interface compliant with and supports various hash functions like , and , a Java-based library offering HMAC implementations that adhere to the same standard for cross-platform use. NIST guidelines emphasize using approved implementations to ensure compliance with security requirements for hash-based applications. Developers should never attempt to roll their own HMAC code, as even minor deviations from the specification can compromise security. In terms of performance, computing an HMAC typically incurs approximately twice the computational cost of a single hash operation, as it involves two invocations of the underlying hash function with key-derived padding. For large messages, this overhead is often negligible due to the efficiency of modern hash algorithms, but it can become significant in high-throughput scenarios. Optimizations such as incremental or streaming updates allow processing data in chunks without recomputing the entire message, enabling efficient handling of continuous data streams like network packets. Hardware acceleration further mitigates costs; for instance, Intel's SHA Extensions, introduced in 2013 and available in processors starting from Goldmont architecture, provide instructions that speed up SHA-1 and SHA-256 computations, thereby benefiting HMAC operations based on these hashes. Best practices for HMAC implementation focus on mitigating side-channel and operational risks. Implementations must use constant-time operations for comparisons and computations to prevent timing attacks that could leak information about the key or message. After use, keys should be securely zeroized—overwritten with zeros or random data—to prevent recovery from memory dumps. Input validation is essential, including checks for appropriate key lengths (e.g., at least the hash output size for full security) and message integrity to avoid processing malformed data that could lead to bypasses. These measures align with NIST recommendations for secure key management and hash usage in approved modes. Recent trends highlight HMAC's role in modern authentication frameworks, such as the hmac-secret extension in WebAuthn's CTAP2 protocol, introduced in 2019, which enables authenticators to derive symmetric keys for additional operations like pseudorandom function generation during credential assertions. In authenticated encryption contexts, HMAC serves as a reliable MAC component in encrypt-then-MAC schemes, often as a fallback for AEAD modes like AES-GCM, where GCM's integrated authentication may fail due to nonce reuse or tag truncation, providing a more robust integrity check without the brittleness of GCM's GHASH.

Applications in Protocols

HMAC plays a central role in the Transport Layer Security (TLS) protocol for ensuring message integrity and authenticity. In TLS versions up to 1.2, HMAC is directly used as the message authentication code (MAC) in the record layer for many cipher suites, such as TLS_RSA_WITH_AES_128_CBC_SHA, where it verifies the integrity of encrypted data. In TLS 1.3, defined in RFC 8446 (2018), HMAC underpins the HKDF construct for key derivation during the handshake process, enabling secure session establishment; additionally, HMAC-based cipher suites like those in RFC 9150 provide authentication and integrity without confidentiality for specialized use cases. In the Internet Protocol Security (IPsec) suite, HMAC ensures data origin authentication and integrity for network communications. The Authentication Header (AH) protocol exclusively employs HMAC to protect the entire IP packet, including headers, against tampering. Similarly, the Encapsulating Security Payload (ESP) mode, as specified in RFC 4303 (2005), optionally integrates HMAC for authentication alongside optional encryption, supporting algorithms like HMAC-SHA-256 for robust packet protection in VPNs and secure tunnels. HMAC's versatility extends to several other protocols and systems. In JSON Web Tokens (JWTs), RFC 7518 (2015) defines HMAC-based algorithms such as HS256 (HMAC-SHA-256) and HS512 for signing and verifying token integrity, enabling secure stateless authentication in web applications. Kerberos version 5 incorporates HMAC in its checksum and encryption types for ticket protection, including AES-128-CTS-HMAC-SHA1-96 and AES-256-CTS-HMAC-SHA1-96 as specified in RFC 8009 (2017), ensuring mutual authentication in enterprise environments. Amazon Web Services (AWS) uses HMAC-SHA256 in its Signature Version 4 process to authenticate API requests, where clients sign canonical requests with their secret access key to prevent unauthorized access. In blockchain contexts, Ethereum leverages HMAC-SHA512 within BIP-32 for hierarchical deterministic key derivation, generating child keys from a master seed to manage wallet addresses securely. Key standards formalize HMAC's adoption across protocols. FIPS 198-1 (2008), issued by the National Institute of Standards and Technology (NIST), standardizes HMAC for federal government use with approved hash functions like SHA-1 and SHA-256, emphasizing its role in message authentication. In June 2025, NIST proposed the withdrawal of FIPS 198-1, transitioning the specification to NIST SP 800-224 (2025), which updates HMAC to approve SHA-3 family hashes and disallows SHA-1. RFC 8446 outlines deprecation paths for legacy HMAC constructions using weak hashes like MD5 or SHA-1, mandating transitions to stronger variants such as HMAC-SHA-384 to maintain security in evolving protocols. Recent developments highlight HMAC's adaptability in emerging domains. In post-quantum (PQ) cryptography, HMAC's symmetric design renders it resistant to quantum attacks, allowing its integration into hybrid schemes during NIST's PQ standardization rounds (2022–2024); for instance, HKDF-HMAC pairs with PQ key encapsulation mechanisms like ML-KEM for secure key derivation in protocols transitioning to quantum-safe systems. NIST SP 800-224 (2025) updates HMAC specifications to incorporate SHA-3, enhancing its suitability for PQ hybrids by supporting larger block sizes and truncation options. For Internet of Things (IoT) applications, HMAC secures the Constrained Application Protocol (CoAP) via Datagram TLS (DTLS), where it provides MAC functionality in DTLS 1.2 cipher suites for resource-constrained devices; RFC 8323 (2018) extends CoAP over TLS/DTLS transports, enabling HMAC-based authentication in low-power networks like smart grids.

Examples

Computational Illustration

To illustrate the computation of an HMAC, consider a simple example using HMAC-MD5, noting that MD5 is cryptographically insecure and should not be used for new applications; secure alternatives like HMAC-SHA-256 are recommended in practice. We use the standard test case from RFC 2202, with a secret key K consisting of 16 bytes each set to 0x0b and the message M = "Hi There" (8 bytes in ASCII encoding). The block size B for MD5 is 64 bytes. Since the key length (16 bytes) is less than B, pad K with zeros to reach 64 bytes: the padded key is 0x0b repeated 16 times followed by 0x00 repeated 48 times. Next, compute the inner padded key by XORing the padded key with the inner padding constant ipad (the byte 0x36 repeated 64 times). The first 16 bytes become 0x0b XOR 0x36 = 0x3d (repeated 16 times), and the remaining 48 bytes are 0x00 XOR 0x36 = 0x36 (repeated 48 times). Concatenate this 64-byte inner padded key with the message M to form the inner input block (72 bytes total). The inner hash is then the MD5 hash of this inner input block, yielding a 16-byte digest value. For the outer hash, XOR the padded key with the outer padding constant opad (the byte 0x5c repeated 64 times): the first 16 bytes become 0x0b XOR 0x5c = 0x57 (repeated 16 times), and the remaining 48 bytes are 0x00 XOR 0x5c = 0x5c (repeated 48 times). Concatenate this 64-byte outer padded key with the 16-byte inner hash to form the outer input block (80 bytes total). The final HMAC tag is the MD5 hash of this outer input block, which equals 0x9294727a3638bb1c13f48ef8158bfc9d. To verify the tag on the receiving end, the recipient uses the shared secret key to recompute the HMAC on the received message following the same steps and compares the resulting tag with the provided one; a match confirms the message's authenticity and integrity. Such computations are typically performed using established cryptographic libraries (e.g., OpenSSL or Python's hmac module) rather than manual calculation, and always with secure underlying hash functions in production systems.

Code Snippet

The HMAC construction is typically implemented via the following language-agnostic pseudocode, which outlines the key preparation, padding with inner (ipad) and outer (opad) constants, XOR operations, and nested invocations as defined in the NIST standard.
function HMAC(key, message, H):
    B = H.block_size  // e.g., 64 bytes for SHA-256
    L = H.output_length  // e.g., 32 bytes for SHA-256
    
    // Step 1: Prepare key K to length B
    if length(key) > B:
        K = H(key)
    else if length(key) < B:
        K = key || zeros(B - length(key))  // Append zeros
    else:
        K = key
    
    // Step 2: Compute inner padded block
    ipad = repeat(0x36, B)
    inner = XOR(K, ipad) || message
    
    // Step 3: Compute inner [hash](/page/Hash)
    inner_hash = H(inner)
    
    // Step 4: Compute outer padded block
    opad = repeat(0x5C, B)
    outer = XOR(K, opad) || inner_hash
    
    // Step 5: Compute outer [hash](/page/Hash) (the HMAC)
    hmac_result = H(outer)
    
    return hmac_result  // Truncate to desired length t ≤ L if needed
This follows the procedural steps for any Merkle-Damgård H, ensuring the key is adjusted to the block size before XORing with the fixed pads. In , the hmac module from the implements this efficiently, integrating with hashlib for the underlying . The following example computes HMAC-SHA256 for a simple key and message, producing a representation of the digest.
python
import hmac
import hashlib

key = b'key'
message = b'message'
hmac_obj = hmac.new([key](/page/Key), message, hashlib.sha256)
digest = hmac_obj.digest()  # Binary digest
hex_digest = hmac_obj.hexdigest()  # Hex string
This code handles key preparation internally per RFC 2104, automatically hashing long keys and padding short ones. Implementations in other languages offer similar built-in support. In , the javax.crypto.Mac class provides HMAC via Mac.getInstance("HmacSHA256").doFinal(message) after initialization with the key. In C++, the library exposes HMAC through functions like HMAC_Init_ex, HMAC_Update, and HMAC_Final for incremental processing. For secure coding, validate that inputs are bytes-like objects to prevent type errors, and use constant-time comparison functions like Python's hmac.compare_digest for verifying digests against timing attacks. Additionally, select strong hash functions such as SHA-256 or for new applications, avoiding deprecated weak hashes like and , which NIST has disallowed for security-critical uses since 2011 and 2013, respectively.

References

  1. [1]
    RFC 2104 - HMAC: Keyed-Hashing for Message Authentication
    HMAC is a mechanism for message authentication using cryptographic hash functions, using a secret key for calculation and verification.
  2. [2]
    FIPS 198, The Keyed-Hash Message Authentication Code (HMAC)
    Jul 29, 2008 · HMAC is a mechanism for message authentication using cryptographic hash functions, with a shared secret key. Its strength depends on the hash ...Missing: definition | Show results with:definition
  3. [3]
    [PDF] Keying Hash Functions for Message Authentication - UCSD CSE
    In this paper we present two (related) new schemes, NMAC (the Nested construction) and HMAC. (the Hash based mac). They can utilize any cryptographic hash ...
  4. [4]
    New Proofs for NMAC and HMAC: Security Without Collision ...
    Feb 6, 2006 · This paper proves that HMAC is a PRF under the sole assumption that the compression function is a PRF. This recovers a proof based guarantee.
  5. [5]
    [PDF] Message Authentication using Hash Functions— The HMAC ...
    Two parties communicating across an insecure channel need a method by which any attempt to modify the information sent by one to the other, or.
  6. [6]
  7. [7]
    Cryptographic hash function - Glossary | CSRC
    The function is expected to have the following three properties: 1. Collision resistance (see Collision resistance), 2. Preimage resistance (see Preimage ...
  8. [8]
    Recommendation for Applications Using Approved Hash Algorithms
    This document provides security guidelines for achieving the required or desired security strengths when using cryptographic applications that employ the ...
  9. [9]
    Hash Functions | CSRC - NIST Computer Security Resource Center
    Jan 4, 2017 · Second preimage resistance: It is computationally infeasible to find a second input that has the same hash value as any other specified input.NIST Policy · News & Updates · Events · SHA-3 Standardization
  10. [10]
    Hash Functions | CSRC - NIST Computer Security Resource Center
    Federal agencies may use SHA-1 for the following applications: verifying old digital signatures and time stamps, generating and verifying hash-based message ...
  11. [11]
    Message Authentication Code (MAC) algorithm - Glossary | CSRC
    Definitions: A family of cryptographic functions that is parameterized by a symmetric key. Each of the functions can act on input data (called a “ ...
  12. [12]
    [PDF] Constructing VIL-MACs from FIL-MACs - Computer Science
    The security of a MAC is measured via its resistance to existential forgery under chosen-message attack, following [5], which in turn is a concrete security.
  13. [13]
    RFC 2104: HMAC: Keyed-Hashing for Message Authentication
    ### Summary of HMAC Definition from RFC 2104 (Sections 2 and 3)
  14. [14]
    [PDF] fips pub 180-4 - federal information processing standards publication
    Aug 4, 2015 · ... size of the message block - m bits - depends on the algorithm. a) For SHA-1, SHA-224 and SHA-256, each message block has 512 bits, which are.
  15. [15]
    [PDF] FIPS 198-1, The Keyed-Hash Message Authentication Code (HMAC)
    Jul 1, 2008 · Table 1 illustrates the step by step process in the HMAC algorithm, which is depicted in. Figure 1. Table 1: The HMAC Algorithm. STEPS. STEP-BY ...
  16. [16]
  17. [17]
    [PDF] New Proofs for NMAC and HMAC: Security without Collision ...
    HMAC was proved in [4] to be a PRF assuming that (1) the underlying compression function is a PRF, and (2) the iterated hash function is weakly collision- ...Missing: rationale | Show results with:rationale
  18. [18]
    When Messages are Keys: Is HMAC a dual-PRF?
    Jun 7, 2023 · When Messages are Keys: Is HMAC a dual-PRF? Matilda Backendal , ETH Zurich. Mihir Bellare , University of California, San Diego. Felix Günther ...
  19. [19]
    RFC 6194 - Security Considerations for the SHA-0 and SHA-1 ...
    ... attack on 48 out of 80 rounds with complexity of 2^159. 3.3. HMAC-SHA-1 As of today, there is no indication that attacks on SHA-1 can be extended to HMAC-SHA-1.
  20. [20]
    RFC 6151 - Updated Security Considerations for the MD5 Message ...
    Sep 23, 2025 · This document updates the security considerations for the MD5 message digest algorithm. It also updates the security considerations for HMAC-MD5.
  21. [21]
    Guidelines for Mitigating Timing Side Channels Against ... - Intel
    Jun 29, 2022 · Learn how cryptographic implementations use constant time principles to help protect secret data from traditional side channel attacks.
  22. [22]
    [PDF] Differential Power Analysis of HMAC SHA-2 in the Hamming Weight ...
    We have presented in this paper a side channel attack on HMAC SHA-256 in the Hamming weight model, which requires no assumption on the implementation.
  23. [23]
    [PDF] Intel® SHA Extensions
    This paper provides an introduction to the family of new instructions that support performance acceleration of the Secure Hash Algorithm (SHA) on. Intel®.
  24. [24]
    [PDF] Generic Related-key Attacks for HMAC - Cryptology ePrint Archive
    Abstract. In this article we describe new generic distinguishing and forgery attacks in the related-key scenario (using only a single related-key) for the ...
  25. [25]
    [PDF] ANOTHER LOOK AT HMAC 1. Introduction In the first two sections ...
    Apr 25, 2013 · In [2] Bellare, Canetti, and Krawczyk gave a security proof for the Nested Message. Authentication Code, which they then extended to an ...<|control11|><|separator|>
  26. [26]
    Post-Quantum Cryptography | CSRC
    To protect against the threat of quantum computers, should we double the key length for AES now? (added 11/18/18). Grover's algorithm allows a quantum ...Missing: 8323 HMAC
  27. [27]
    [PDF] Keyed-Hash Message Authentication Code (HMAC)
    Jun 26, 2024 · 1996. Bellare, Canetti, and Krawczyk [8] proposed the HMAC construction. 641. 1997. RFC 2104 [9] specified the HMAC construction. 642. 2000.
  28. [28]
    RFC 8446 - The Transport Layer Security (TLS) Protocol Version 1.3
    The constructions in TLS 1.2 and TLS 1.3 are different, although they are both based on HMAC. While there is no known way in which the same PSK might ...
  29. [29]
    RFC 9150: TLS 1.3 Authentication and Integrity-Only Cipher Suites
    This document defines the use of cipher suites for TLS 1.3 based on Hashed Message Authentication Code (HMAC). Using these cipher suites provides server and ...Table of Contents · Record Payload Protection for... · Security and Privacy...
  30. [30]
    RFC 4303 - IP Encapsulating Security Payload (ESP)
    This document describes an updated version of the Encapsulating Security Payload (ESP) protocol, which is designed to provide a mix of security services in IPv ...
  31. [31]
    RFC 4868: Using HMAC-SHA-256, HMAC-SHA-384, and HMAC ...
    This specification describes the use of Hashed Message Authentication Mode (HMAC) in conjunction with the SHA-256, SHA-384, and SHA-512 algorithms in IPsec.
  32. [32]
    RFC 7518 - JSON Web Algorithms (JWA) - IETF Datatracker
    This specification registers cryptographic algorithms and identifiers to be used with the JSON Web Signature (JWS), JSON Web Encryption (JWE), and JSON Web Key ...
  33. [33]
    RFC 8009 - AES Encryption with HMAC-SHA2 for Kerberos 5
    This document defines two encryption types and two corresponding checksum types for Kerberos 5 using AES with 128-bit or 256-bit keys.<|control11|><|separator|>
  34. [34]
    FIPS 198-1, The Keyed-Hash Message Authentication Code (HMAC)
    HMAC is a keyed-hash message authentication code, a mechanism for message authentication using cryptographic hash functions with a shared secret key.
  35. [35]
    Post-Quantum Cryptography | CSRC
    HQC was selected for standardization on March 11, 2025. NIST IR 8545, Status Report on the Fourth Round of the NIST Post-Quantum Cryptography Standardization ...Workshops and Timeline · Presentations · Email List (PQC Forum) · Post-QuantumMissing: HMAC | Show results with:HMAC
  36. [36]
    SP 800-224, Keyed-Hash Message Authentication Code (HMAC)
    Jun 28, 2024 · The publication also specifies a set of requirements for using HMAC for message authentication, including a list of NIST-approved cryptographic ...
  37. [37]
    RFC 8323 - CoAP (Constrained Application Protocol) over TCP, TLS ...
    This document outlines the changes required to use CoAP over TCP, TLS, and WebSockets transports. It also formally updates RFC 7641 for use with these ...
  38. [38]
    RFC 2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
    ... Hi There" data_len = 8 digest = 0x9294727a3638bb1c13f48ef8158bfc9d test_case ... secret key */ int lk, /* length of the key in bytes */ char* d ...Missing: example | Show results with:example
  39. [39]
    hmac — Keyed-Hashing for Message Authentication — Python 3.14 ...
    The hmac module implements the HMAC algorithm, which is keyed-hashing for message authentication, as described by RFC 2104.
  40. [40]
    Mac (Java SE 17 & JDK 17) - Oracle Help Center
    A MAC mechanism that is based on cryptographic hash functions is referred to as HMAC. HMAC can be used with any cryptographic hash function, e.g., SHA256 or ...