# HMAC

A keyed-hash message authentication code, or HMAC, is a type of message authentication code (MAC) calculated using a cryptographic hash function in combination with a secret key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authenticity of a message. Any iterative cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA-1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function and on the size and quality of the key.

An iterative hash function breaks up a message into blocks of a fixed size and iterates over them with a compression function. For example, MD5 and SHA-1 operate on 512-bit blocks. The size of the output of HMAC is the same as that of the underlying hash function (128 or 160 bits in the case of MD5 and SHA-1), although it can be truncated if desired.

HMAC is defined as

${\displaystyle HMAC_{K}(m)=h{\Bigg (}(K\oplus opad)\;||\;h{\Big (}(K\oplus ipad)\;||\;m{\Big )}{\Bigg )},}$

where h is an iterated hash function, K is a secret key padded with extra zeros to the block size of the hash function and m is the message to be authenticated. "||" denotes concatenation, and "${\displaystyle \oplus }$" denotes exclusive or. The two constants ipad and opad, each one block long, are defined as ipad = 0x363636...3636 and opad = 0x5c5c5c...5c5c. That is, if block size of the hash function is 512, ipad and opad are 64 repetitions of the (hexadecimal) bytes 0x36 and 0x5c respectively.

The construction and analysis of HMACs was first published in 1996 by Mihir Bellare, Ran Canetti, and Hugo Krawczyk, who also wrote RFC 2104. FIPS PUB 198 generalizes and standardizes the use of HMACs. HMAC-SHA-1 and HMAC-MD5 are used within the IPsec and TLS protocols.

## Typical usage

A pizza restaurant that suffers from attackers that place bogus orders may insist that all its customers provide a secret key. Then, on any order consisting of that customer's secret key and a number unique to that transaction, the customer must supply an HMAC hexdigest of that order along with the order.