HMAC
In cryptography, an HMAC (sometimes expanded as either keyedhash message authentication code or hashbased message authentication code) is a specific type of message authentication code (MAC) involving a cryptographic hash function and a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and authenticity of a message.
HMAC can provide authentication using a shared secret instead of using digital signatures with asymmetric cryptography. It trades off the need for a complex public key infrastructure by delegating the key exchange to the communicating parties, who are responsible for establishing and using a trusted channel to agree on the key prior to communication.
Details
Any cryptographic hash function, such as SHA2 or SHA3, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMACX, where X is the hash function used (e.g. HMACSHA256 or HMACSHA3512). The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, and the size and quality of the key.^{[1]}
HMAC uses two passes of hash computation. Before either pass, the secret key is used to derive two keys – inner and outer. Next, the first pass of the hash algorithm produces an internal hash derived from the message and the inner key. The second pass produces the final HMAC code derived from the inner hash result and the outer key. Thus the algorithm provides better immunity against length extension attacks.
An iterative hash function (one that uses the MerkleDamgård construction) breaks up a message into blocks of a fixed size and iterates over them with a compression function. For example, SHA256 operates on 512bit blocks. The size of the output of HMAC is the same as that of the underlying hash function (e.g., 256 and 512 bits in the case of SHA256 and SHA3512, respectively), although it can be truncated if desired.
HMAC does not encrypt the message. Instead, the message (encrypted or not) must be sent alongside the HMAC hash. Parties with the secret key will hash the message again themselves, and if it is authentic, the received and computed hashes will match.
The definition and analysis of the HMAC construction was first published in 1996 in a paper by Mihir Bellare, Ran Canetti, and Hugo Krawczyk,^{[1]}^{[2]} and they also wrote RFC 2104 in 1997.^{[3]} The 1996 paper also defined a nested variant called NMAC. FIPS PUB 198 generalizes and standardizes the use of HMACs.^{[4]} HMAC is used within the IPsec,^{[2]} SSH and TLS protocols and for JSON Web Tokens.
Definition
This definition is taken from RFC 2104:
 [math]\displaystyle{ \begin{align} \operatorname{HMAC}(K, m) &= \operatorname{H}\Bigl(\bigl(K' \oplus opad\bigr) \parallel \operatorname{H} \bigl(\left(K' \oplus ipad\right) \parallel m\bigr)\Bigr) \\ K' &= \begin{cases} \operatorname{H}\left(K\right) & K\text{ is larger than block size} \\ K & \text{otherwise} \end{cases} \end{align} }[/math]
where
 H is a cryptographic hash function.
 m is the message to be authenticated.
 K is the secret key.
 K' is a blocksized key derived from the secret key, K; either by padding to the right with 0s up to the block size, or by hashing down to less than or equal to the block size first and then padding to the right with zeros.
 ‖ denotes concatenation.
 ⊕ denotes bitwise exclusive or (XOR).
 opad is the blocksized outer padding, consisting of repeated bytes valued 0x5c.
 ipad is the blocksized inner padding, consisting of repeated bytes valued 0x36.^{[3]}
Hash function H

b , bytes

L , bytes


MD5  64  16 
SHA1  64  20 
SHA224  64  28 
SHA256  64  32 
SHA512/224  128  28 
SHA512/256  128  32 
SHA384  128  48 
SHA512  128  64^{[5]} 
SHA3224  144  28 
SHA3256  136  32 
SHA3384  104  48 
SHA3512  72  64^{[6]} 
out = H( in ) ^{[3]}

Implementation
The following pseudocode demonstrates how HMAC may be implemented. The block size is 512 bits (64 bytes) when using one of the following hash functions: SHA1, MD5, RIPEMD128.^{[3]}
function hmac is input: key: Bytes // Array of bytes message: Bytes // Array of bytes to be hashed hash: Function // The hash function to use (e.g. SHA1) blockSize: Integer // The block size of the hash function (e.g. 64 bytes for SHA1) outputSize: Integer // The output size of the hash function (e.g. 20 bytes for SHA1) // Compute the block sized key block_sized_key = computeBlockSizedKey(key, hash, blockSize) o_key_pad ← block_sized_key xor [0x5c blockSize] // Outer padded key i_key_pad ← block_sized_key xor [0x36 blockSize] // Inner padded key return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) function computeBlockSizedKey is input: key: Bytes // Array of bytes hash: Function // The hash function to use (e.g. SHA1) blockSize: Integer // The block size of the hash function (e.g. 64 bytes for SHA1) // Keys longer than blockSize are shortened by hashing them if (length(key) > blockSize) then key = hash(key) // Keys shorter than blockSize are padded to blockSize by padding with zeros on the right if (length(key) < blockSize) then return Pad(key, blockSize) // Pad key with zeros to make it blockSize bytes long return key
Design principles
The design of the HMAC specification was motivated by the existence of attacks on more trivial mechanisms for combining a key with a hash function. For example, one might assume the same security that HMAC provides could be achieved with MAC = H(key ∥ message). However, this method suffers from a serious flaw: with most hash functions, it is easy to append data to the message without knowing the key and obtain another valid MAC ("lengthextension attack"). The alternative, appending the key using MAC = H(message ∥ key), suffers from the problem that an attacker who can find a collision in the (unkeyed) hash function has a collision in the MAC (as two messages m1 and m2 yielding the same hash will provide the same start condition to the hash function before the appended key is hashed, hence the final hash will be the same). Using MAC = H(key ∥ message ∥ key) is better, but various security papers have suggested vulnerabilities with this approach, even when two different keys are used.^{[1]}^{[7]}^{[8]}
No known extension attacks have been found against the current HMAC specification which is defined as H(key ∥ H(key ∥ message)) because the outer application of the hash function masks the intermediate result of the internal hash. The values of ipad and opad are not critical to the security of the algorithm, but were defined in such a way to have a large Hamming distance from each other and so the inner and outer keys will have fewer bits in common. The security reduction of HMAC does require them to be different in at least one bit.
The Keccak hash function, that was selected by NIST as the SHA3 competition winner, doesn't need this nested approach and can be used to generate a MAC by simply prepending the key to the message, as it is not susceptible to lengthextension attacks.^{[9]}
Security
The cryptographic strength of the HMAC depends upon the size of the secret key that is used and the security of the underying hash function used. It has been proven that the security of an HMAC construction is directly related to security properties of the hash function used. The most common attack against HMACs is brute force to uncover the secret key. HMACs are substantially less affected by collisions than their underlying hashing algorithms alone.^{[2]}^{[10]}^{[11]} In particular, Mihir Bellare proved that HMAC is a PRF under the sole assumption that the compression function is a PRF.^{[12]} Therefore, HMACMD5 does not suffer from the same weaknesses that have been found in MD5.^{[13]}
RFC 2104 requires that "keys longer than B bytes are first hashed using H" which leads to a confusing pseudocollision: if the key is longer than the hash block size (e.g. 64 bytes for SHA1), then HMAC(k, m)
is computed as HMAC(H(k), m).
This property is sometimes raised as a possible weakness of HMAC in passwordhashing scenarios: it has been demonstrated that it's possible to find a long ASCII string and a random value whose hash will be also an ASCII string, and both values will produce the same HMAC output.^{[14]}^{[15]}^{[16]}
In 2006, Jongsung Kim, Alex Biryukov, Bart Preneel, and Seokhie Hong showed how to distinguish HMAC with reduced versions of MD5 and SHA1 or full versions of HAVAL, MD4, and SHA0 from a random function or HMAC with a random function. Differential distinguishers allow an attacker to devise a forgery attack on HMAC. Furthermore, differential and rectangle distinguishers can lead to secondpreimage attacks. HMAC with the full version of MD4 can be forged with this knowledge. These attacks do not contradict the security proof of HMAC, but provide insight into HMAC based on existing cryptographic hash functions.^{[17]}
In 2009, Xiaoyun Wang et al. presented a distinguishing attack on HMACMD5 without using related keys. It can distinguish an instantiation of HMAC with MD5 from an instantiation with a random function with 2^{97} queries with probability 0.87.^{[18]}
In 2011 an informational RFC 6151 was published to summarize security considerations in MD5 and HMACMD5. For HMACMD5 the RFC summarizes that – although the security of the MD5 hash function itself is severely compromised – the currently known "attacks on HMACMD5 do not seem to indicate a practical vulnerability when used as a message authentication code", but it also adds that "for a new protocol design, a ciphersuite with HMACMD5 should not be included".^{[13]}
In May 2011, RFC 6234 was published detailing the abstract theory and source code for SHAbased HMACs.^{[19]}
Examples
Here are some HMAC values, assuming 8bit ASCII encoding:
HMAC_MD5("key", "The quick brown fox jumps over the lazy dog") = 80070713463e7749b90c2dc24911e275 HMAC_SHA1("key", "The quick brown fox jumps over the lazy dog") = de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9 HMAC_SHA256("key", "The quick brown fox jumps over the lazy dog") = f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8 HMAC_SHA256("The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog", "message") = 5597b93a2843078cbb0c920ae41dfe20f1685e10c67e423c11ab91adfc319d12
References
 ↑ ^{1.0} ^{1.1} ^{1.2} Bellare, Mihir; Canetti, Ran; Krawczyk, Hugo (1996). Keying Hash Functions for Message Authentication. pp. 1–15.
 ↑ ^{2.0} ^{2.1} ^{2.2} Bellare, Mihir; Canetti, Ran; Krawczyk, Hugo (Spring 1996). "Message Authentication using Hash Functions— The HMAC Construction". CryptoBytes 2 (1). https://cseweb.ucsd.edu/~mihir/papers/hmaccb.pdf.
 ↑ ^{3.0} ^{3.1} ^{3.2} ^{3.3} HMAC: KeyedHashing for Message Authentication, sec. 2, doi:10.17487/RFC2104, RFC 2104, https://tools.ietf.org/html/rfc2104
 ↑ "FIPS 1981: The KeyedHash Message Authentication Code (HMAC)". Federal Information Processing Standards. July 16, 2008. https://csrc.nist.gov/publications/detail/fips/198/1/final.
 ↑ "FIPS 1802 with Change Notice 1". https://csrc.nist.gov/publications/fips/fips1802/fips1802withchangenotice.pdf.
 ↑ Dworkin, Morris (August 4, 2015). "SHA3 Standard: PermutationBased Hash and ExtendableOutput Functions". Federal Information Processing Standards. https://www.nist.gov/publications/sha3standardpermutationbasedhashandextendableoutputfunctions.
 ↑ Preneel, Bart; van Oorschot, Paul C. (1995). MDxMAC and Building Fast MACs from Hash Functions.
 ↑ Preneel, Bart; van Oorschot, Paul C. (1995). On the Security of Two MAC Algorithms.
 ↑ Keccak team. "Keccak Team – Design and security". https://keccak.team/keccak_strengths.html. "Unlike SHA1 and SHA2, Keccak does not have the lengthextension weakness, hence does not need the HMAC nested construction. Instead, MAC computation can be performed by simply prepending the message with the key."
 ↑ Bruce Schneier (August 2005). "SHA1 Broken". http://www.schneier.com/blog/archives/2005/02/sha1_broken.html. "although it doesn't affect applications such as HMAC where collisions aren't important"
 ↑ IETF (February 1997), HMAC: KeyedHashing for Message Authentication, sec. 6, doi:10.17487/RFC2104, RFC 2104, https://tools.ietf.org/html/rfc2104, retrieved 3 December 2009, "The strongest attack known against HMAC is based on the frequency of collisions for the hash function H ("birthday attack") [PV,BCK2], and is totally impractical for minimally reasonable hash functions."
 ↑ Bellare, Mihir. "New Proofs for NMAC and HMAC: Security without CollisionResistance". https://eprint.iacr.org/2006/043.pdf. Retrieved 20211215. "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 since no known attacks compromise the pseudorandomness of the compression function, and it also helps explain the resistancetoattack that HMAC has shown even when implemented with hash functions whose (weak) collision resistance is compromised."
 ↑ ^{13.0} ^{13.1} "RFC 6151 – Updated Security Considerations for the MD5 MessageDigest and the HMACMD5 Algorithms". Internet Engineering Task Force. March 2011. https://tools.ietf.org/html/rfc6151.
 ↑ "PBKDF2+HMAC hash collisions explained · Mathias Bynens". https://mathiasbynens.be/notes/pbkdf2hmac.
 ↑ "Aaron Toponce : Breaking HMAC" (in enUS). https://pthree.org/2016/07/29/breakinghmac/.
 ↑ "RFC 2104 Errata Held for Document Update · Erdem Memisyazici". https://www.rfceditor.org/errata/eid4809.
 ↑ Jongsung, Kim; Biryukov, Alex; Preneel, Bart; Hong, Seokhie (2006). On the Security of HMAC and NMAC Based on HAVAL, MD4, MD5, SHA0 and SHA1. http://eprint.iacr.org/2006/187.pdf.
 ↑ Wang, Xiaoyun; Yu, Hongbo; Wang, Wei; Zhang, Haina; Zhan, Tao (2009). Cryptanalysis on HMAC/NMACMD5 and MD5MAC. https://www.iacr.org/archive/eurocrypt2009/54790122/54790122.pdf. Retrieved 15 June 2015.
 ↑ Eastlake 3rd, D.; Hansen, T. (May 2011) (in en). US Secure Hash Algorithms (SHA and SHAbased HMAC and HKDF). doi:10.17487/RFC6234. ISSN 20701721. https://www.rfceditor.org/rfc/rfc6234.
External links
 RFC2104
 Online HMAC Generator / Tester Tool
 FIPS PUB 1981, The KeyedHash Message Authentication Code (HMAC)
 C HMAC implementation
 Python HMAC implementation
 Java implementation
 Rust HMAC implementation
Original source: https://en.wikipedia.org/wiki/HMAC.
Read more 