What is Hash based Message Authentication Code (HMAC) in Network Security

February 21, 2018 Author: virendra
Print Friendly, PDF & Email

Today the Internet has virtually become the way of doing business as it offers a powerful ubiquitous medium of commerce and enables greater connectivity of disparate groups throughout the world. However this medium has its inherent risks. Loss of privacy, loss of data integrity, identify spoofing and denial of service are some of the major threats in the Internet. 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 fake its origin, is detected. Most commonly such a mechanism is based on a shared key between the parties, and in this setting is usually called a MAC, or Message Authentication Code.

Overview of Hash based Message Authentication Code

Providing a way to check the integrity of information transmitted over or stored in an unreliable medium is a prime necessity in the world of open computing and communications. Mechanisms that provide such integrity checks based on a secret key are usually called message authentication codes (MACs). Typically, message authentication codes are used between two parties that share a secret key in order to authenticate information transmitted between these parties. This standard defines a MAC that uses a cryptographic hash function in conjunction with a secret key. This mechanism is called HMAC. HMAC uses a secret key for the calculation and verification of the MACs. The main goals behind the HMAC construction are:

  • To use available hash functions without modifications; in particular, hash functions that perform well in software, and for which code is freely and widely available,
  • To preserve the original performance of the hash function without incurring a significant degradation,
  • To use and handle keys in a simple way,
  • To have a well-understood cryptographic analysis of the strength of the authentication mechanism based on reasonable assumptions on the underlying hash function, and
  • To allow for easy replaceability of the underlying hash function in the event that faster or more secure hash functions are later available.

The cryptographic strength of the HMAC depends on the underlying strength of the message digest used and how securely the secret key is exchanged. HMAC is widely used by Internet security technologies, such as the TLS and IPSec protocols, to verify the integrity of transmitted data during secure communications. HMAC generates a message digest for each block of transmitted data and uses a random secret symmetric key to encrypt the message digests. The secret key is securely shared between the parties involved in the secure communications. When data is received, the secret key is necessary to decrypt the message digest and perform the data integrity check.

Defining Hash based Message Authentication Code (HMAC)

Message authentication is a procedure that allows communicating parties to verify that received messages are authentic. The two important aspects are verifying that the contents of the message have not been altered and that the source is authentic. The Message Authentication Code (MAC) is a widely used technique for performing message authentication. A variation on the MAC algorithm has emerged as an Internet standard for a wide variety of applications — HMAC, short for “Keyed-Hashing for Message Authentication.

A hash function such as MD5 was not designed for use as a MAC and cannot be used directly for that purpose because it does not rely on a secret key. There have been a number of proposals to incorporate a secret key into an existing hash algorithm. HMAC received the most support. HMAC has been chosen as the mandatory-to-implement MAC for IP Security, and is used in other Internet protocols, such as Transport Layer Security (TLS, soon to replace Secure Sockets Layer) and Secure Electronic Transaction (SET).

The HMAC Algorithm

Figure 1 depicts the overall operation of HMAC. Define the following terms:

HMAC Algorithm

Figure 1: HMAC Algorithm

HMAC Notations

  1. H = embedded hash function (e.g., MD5, SHA-1, RIPEMD-160),
  2. IV = Initial value input to hash function,
  3. M = message input to HMAC (including the padding specified in the embedded hash function),
  4. \( Y_i \)​= ​\( i^th \)​ block of M,
  5. b = number of bits in a block
  6. n = length of hash code produced by embedded hash function
  7. K = secret key recommended length is ≥ b; the key is input to the hash function to produce an n-bit key
  8. \( K^+ \)​=  K padded with zeros on the left so that the result is b bits in length
  9. ipad = 00110110 (36 in hexadecimal) repeated b/8 times
  10. opad = 01011100 (5C in hexadecimal) repeated b/8 times

Then HMAC can be expressed as follows:

\( HMAC (K,M)=H [(K^+⊕opad,H(K^+⨁ipad,M))] \)

Algorithmic Steps:

  1. Append zeros to the left end of K to create a b-bit string ​\( K^+ \)​ (e.g., if K is of length 160 bits and b = 512 then K will be appended with 44 zero bytes 0 x 00).
  2. XOR (bitwise exclusive-OR) ​\( K^+ \)​ with ipad to produce the b-bit block.
  3. Append M to ​\( S_i \)​.
  4. Apply H to the stream generated in step 3.
  5. XOR ​\( K^+ \)​ with opad to produce the b-bit block ​\( S_o \)
  6. Append the hash result from step 4 to ​\( S_o \)
  7. Apply H to the stream generated in step 6 and output the result

Note that the XOR with ipad results in flipping one-half of the bits of K. Similarly, the XOR with opad results in flipping one-half of the bits of K, but a different set of bits. In effect, by passing  and  through the compression function of the hash algorithm, we have pseudo randomly generated two keys from K.

HMAC Example

HMAC is a message authentication code (MAC) and can be used to verify the integrity and authentication of a message. It involves hashing a message with a secret key. As with any MAC, it can be used with standard hash function, such as MD5 or SHA-1, which results in methods such as HMAC-MD5 or HMAC-SHA-1. As with any hashing function, the strength depends on the quality of the hashing function, and the resulting number of code bits. Along with this the number of bits in the secret key is a factor. In this example we wrote sentence in text box “Hello I am Alice, This is my HMAC Example”, with key of welcome. The algorithm return hash MAC for different hash function as illustrate is figure 2.

HMAC Example

Figure 2: HMAC Example


[1] Mihir Bellare Ran Canettiy Hugo Krawczykz, “Message Authentication using Hash Functions -the HMAC Construction”, Appears in RSA Laboratories’ CryptoBytes, Vol. 2, No. 1, Spring 1996

[2] “Section 12.3. HMAC”, available online at: https://flylib.com/books/en/

[3] “The HMAC Algorithm”, available online at: http://www.drdobbs.com/security/the-hmac-algorithm/184410908

[4] Hashed Message Authentication Code Functions, available online at: https://technet.microsoft.com/en-us/library/cc962016.aspx

[5] “HMAC Example”, available online at: https://asecuritysite.com/encryption/hmac

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Insert math as
Additional settings
Formula color
Text color
Type math using LaTeX
Nothing to preview