HMAC

Incryptography, anHMAC(sometimes expanded as eitherkeyed-hash message authentication codeorhash-based message authentication code) is a specific type ofmessage authentication code(MAC) involving acryptographic hash functionand a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both thedata integrityand authenticity of a message. An HMAC is a type of keyed hash function that can also be used in a key derivation scheme or a key stretching scheme.
HMAC can provide authentication using ashared secretinstead of usingdigital signatureswithasymmetric cryptography. It trades off the need for a complexpublic key infrastructureby 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.
Any cryptographic hash function, such asSHA-2orSHA-3, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-x, wherexis the hash function used (e.g. HMAC-SHA256 or HMAC-SHA3-512). Thecryptographic strengthof 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.
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 againstlength extension attacks.
An iterative hash function (one that uses theMerkle–Damgård construction) breaks up a message into blocks of a fixed size and iterates over them with acompression function. For example, SHA-256 operates on 512-bit 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 SHA-256 and SHA3-512, 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 byMihir Bellare,Ran Canetti, andHugo Krawczyk,and they also wrote RFC 2104 in 1997.The 1996 paper also defined a nested variant called NMAC (Nested MAC).FIPSPUB 198 generalizes and standardizes the use of HMACs.HMAC is used within theIPsec,SSHandTLSprotocols and forJSON Web Tokens.
This definition is taken from RFC 2104:
where
The followingpseudocodedemonstrates how HMAC may be implemented. The block size is 512 bits (64 bytes) when using one of the following hash functions: SHA-1, MD5, RIPEMD-128.
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 ("length-extension 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.
No known extension attacks have been found against the current HMAC specification which is defined asH(key∥H(key∥message)) because the outer application of the hash function masks the intermediate result of the internal hash. The values ofipadandopadare not critical to the security of the algorithm, but were defined in such a way to have a largeHamming distancefrom 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.
TheKeccakhash function, that was selected byNISTas theSHA-3competition 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 length-extension attacks.
The cryptographic strength of the HMAC depends upon the size of the secret key that is used and the security of the underlying 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.In particular, Mihir Bellare proved that HMAC is apseudo-random function(PRF) under the sole assumption that the compression function is a PRF.Therefore, HMAC-MD5 does not suffer from the same weaknesses that have been found in MD5.
RFC 2104 requires that "keys longer thanBbytes are first hashed usingH" which leads to a confusing pseudo-collision: if the key is longer than the hash block size (e.g. 64 bytes for SHA-1), thenHMAC(k, m)is computed asHMAC(H(k), m). This property is sometimes raised as a possible weakness of HMAC in password-hashing 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.
In 2006,Jongsung Kim,Alex Biryukov,Bart Preneel, andSeokhie Hongshowed how to distinguish HMAC with reduced versions of MD5 and SHA-1 or full versions ofHAVAL,MD4, andSHA-0from arandom functionor HMAC with a random function. Differential distinguishers allow an attacker to devise a forgery attack on HMAC. Furthermore, differential and rectangle distinguishers can lead tosecond-preimage attacks. HMAC with the full version of MD4 can beforgedwith this knowledge. These attacks do not contradict the security proof of HMAC, but provide insight into HMAC based on existing cryptographic hash functions.
In 2009,Xiaoyun Wanget al.presented a distinguishing attack on HMAC-MD5 without using related keys. It can distinguish an instantiation of HMAC with MD5 from an instantiation with a random function with 2queries with probability 0.87.
In 2011 an informational RFC 6151 was published to summarize security considerations inMD5and HMAC-MD5. For HMAC-MD5 the RFC summarizes that – although the security of theMD5hash function itself is severely compromised – the currently known"attacks on HMAC-MD5 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 HMAC-MD5 should not be included".
In May 2011, RFC 6234 was published detailing the abstract theory and source code for SHA-based HMACs.
Here are some HMAC values, assuming 8-bit ASCII for the input and hexadecimal encoding for the output: