Jump to content

HMAC: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Rescuing 2 sources, flagging 0 as dead, and archiving 17 sources. (Peachy 2.0 (alpha 8))
Line 137: Line 137:


==Design principles==
==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 ("[[Length extension attack|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.<ref name=BCK96>{{Cite web |title=Keying Hash Functions for Message Authentication |first1=Mihir |last1=Bellare |authorlink1=Mihir Bellare |first2=Ran |last2=Canetti |first3=Hugo |last3=Krawczyk |url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.134.8430 |year=1996 |postscript=.}}</ref><ref>{{Cite web |title=MDx-MAC and Building Fast MACs from Hash Functions |year=1995 |first1=Bart |last1=Preneel |authorlink1=Bart Preneel |first2=Paul C. |last2=van Oorschot |authorlink2=Paul van Oorschot |url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.3855 |accessdate=28 August 2009 |postscript=.}}{{dead link|date=September 2015}}</ref><ref>{{Cite web |title=On the Security of Two MAC Algorithms |year=1995 |first1=Bart |last1=Preneel |authorlink1=Bart Preneel |first2=Paul C. |last2=van Oorschot |authorlink2=Paul van Oorschot |url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.42.8908 |accessdate=28 August 2009 |postscript=.}}{{dead link|date=September 2015}}</ref>
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|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.<ref name=BCK96>{{Cite web |title=Keying Hash Functions for Message Authentication |first1=Mihir |last1=Bellare |authorlink1=Mihir Bellare |first2=Ran |last2=Canetti |first3=Hugo |last3=Krawczyk |url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.134.8430 |year=1996 |postscript=.}}</ref><ref>{{Cite web|title=MDx-MAC and Building Fast MACs from Hash Functions |year=1995 |first1=Bart |last1=Preneel |authorlink1=Bart Preneel |first2=Paul C. |last2=van Oorschot |authorlink2=Paul van Oorschot |url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.3855 |accessdate=28 August 2009 |postscript=. |deadurl=yes |archiveurl=https://web.archive.org/20100604234324/http://citeseerx.ist.psu.edu:80/viewdoc/summary?doi=10.1.1.34.3855 |archivedate=4 June 2010 }}</ref><ref>{{Cite web|title=On the Security of Two MAC Algorithms |year=1995 |first1=Bart |last1=Preneel |authorlink1=Bart Preneel |first2=Paul C. |last2=van Oorschot |authorlink2=Paul van Oorschot |url=http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.42.8908 |accessdate=28 August 2009 |postscript=. |deadurl=yes |archiveurl=https://web.archive.org/20090223134840/http://citeseerx.ist.psu.edu:80/viewdoc/summary?doi=10.1.1.42.8908 |archivedate=23 February 2009 }}</ref>


No known extensions 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.{{citation needed|date=June 2015}}
No known extensions 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.{{citation needed|date=June 2015}}

Revision as of 00:30, 16 October 2015

SHA-1 HMAC Generation.

In cryptography, a keyed-hash message authentication code (HMAC) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authentication of a message. Any 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-SHA1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, 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 or SHA-1, respectively), although it can be truncated if desired.

The definition and analysis of the HMAC construction was first published in 1996 by Mihir Bellare, Ran Canetti, and Hugo Krawczyk,[1] who also wrote RFC 2104. This paper also defined a variant called NMAC that is rarely, if ever, used. FIPS PUB 198 generalizes and standardizes the use of HMACs. HMAC-SHA1 and HMAC-MD5 are used within the IPsec and TLS protocols.


Definition (from RFC 2104)

where

H is a cryptographic hash function,
K is a secret key padded to the right with extra zeroes to the input block size of the hash function, or the hash of the original key if it is longer than that block size,
m is the message to be authenticated,
|| denotes concatenation,
⊕ denotes exclusive or (XOR),
opad is the outer padding (0x5c5c5c…5c5c, one-block-long hexadecimal constant),
and ipad is the inner padding (0x363636…3636, one-block-long hexadecimal constant).

Implementation

The following pseudocode demonstrates how HMAC may be implemented. Blocksize is 64 (bytes) when using one of the following hash functions: SHA-1, MD5, RIPEMD-128/160.[2]

function hmac (key, message)
    if (length(key) > blocksize) then
        key = hash(key) // keys longer than blocksize are shortened
    end if
    if (length(key) < blocksize) then
        key = key ∥ [0x00 * (blocksize - length(key))] // keys shorter than blocksize are zero-padded (where  is concatenation)
    end if
   
    o_key_pad = [0x5c * blocksize] ⊕ key // Where blocksize is that of the underlying hash function
    i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR)
   
    return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) // Where  is concatenation
end function

The following is a Python implementation of HMAC-MD5:

#!/usr/bin/env python

from hashlib import md5

trans_5C = bytearray((x ^ 0x5c) for x in range(256))
trans_36 = bytearray((x ^ 0x36) for x in range(256))
blocksize = md5().block_size # 64

def hmac_md5(key, msg):
    if len(key) > blocksize:
        key = md5(key).digest()
    key = key + bytearray(blocksize - len(key))
    o_key_pad = key.translate(trans_5C)
    i_key_pad = key.translate(trans_36)
    return md5(o_key_pad + md5(i_key_pad + msg).digest())

if __name__ == "__main__":
    # This is one example from the appendix of RFC 2104    
    h = hmac_md5(b"Jefe", b"what do ya want for nothing?")
    print(h.hexdigest()) # 750c783e6ab0b503eaa86e310a5db738

Python includes an hmac module,[3] so the function above can be replaced by a shorter version.

import hmac
from hashlib import md5

def hmac_md5(key, msg):
    return hmac.HMAC(key, msg, md5)

The following is a PHP implementation of HMAC-SHA1:

function sha1_hmac($key,$data,$blockSize=64,$opad=0x5c,$ipad=0x36) {

    // Keys longer than blocksize are shortened
    if (strlen($key) > $blockSize) {
        $key = sha1($key,true);	
    }
    
    // Keys shorter than blocksize are right, zero-padded (concatenated)
    $key       = str_pad($key,$blockSize,chr(0x00),STR_PAD_RIGHT);	
    $o_key_pad = $i_key_pad = '';
    
    for($i = 0;$i < $blockSize;$i++) {
        $o_key_pad .= chr(ord(substr($key,$i,1)) ^ $opad);
        $i_key_pad .= chr(ord(substr($key,$i,1)) ^ $ipad);
    }
    
    return sha1($o_key_pad.sha1($i_key_pad.$data,true),true);
}

$hmac = sha1_hmac('key','The quick brown fox jumps over the lazy dog');

PHP includes an hmac function,[4] so the example above can be replaced by a shorter version.

<?php
hash_hmac('sha1', 'The quick brown fox jumps over the lazy dog', 'key');

The following is an implementation using Qt's QCryptographicHash

QByteArray hmacSha256(QByteArray key, const QByteArray& message) {
	const int blocksize = 64;
	if (key.length() > blocksize)
		QCryptographicHash::hash(key, QCryptographicHash::Sha256);

	while (key.length() < blocksize)
		key.append('\0');

	QByteArray o_key_pad('\x5c', blocksize);
	o_key_pad.fill('\x5c', blocksize);

	QByteArray i_key_pad;
	i_key_pad.fill('\x36', blocksize);

	for (int i=0; i<blocksize; i++) {
		o_key_pad[i] = o_key_pad[i] ^ key[i];
		i_key_pad[i] = i_key_pad[i] ^ key[i];
	}

	return QCryptographicHash::hash(o_key_pad 
		+ QCryptographicHash::hash(i_key_pad + message, QCryptographicHash::Sha256), QCryptographicHash::Sha256);
}

The following is an example using Node.js's built in crypto module using HMAC-SHA1:

var crypto = require('crypto'),
    hmac = crypto.createHmac('sha1', 'Shared Secret');
 
hmac.update(message);
 
var hmacHash = hmac.digest('hex');

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(keymessage). 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(messagekey), 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(keymessagekey) is better, but various security papers have suggested vulnerabilities with this approach, even when two different keys are used.[1][5][6]

No known extensions attacks have been found against the current HMAC specification which is defined as H(keyH(keymessage)) 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.[citation needed]

The Keccak hash function, that was selected by NIST as the SHA-3 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 length-extension-attacks.[7]

Security

The cryptographic strength of the HMAC depends upon the size of the secret key that is 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.[8][9][10] Therefore, HMAC-MD5 does not suffer from the same weaknesses that have been found in MD5.

In 2006, Jongsung Kim, Alex Biryukov, Bart Preneel, and Seokhie Hong showed how to distinguish HMAC with reduced versions of MD5 and SHA-1 or full versions of HAVAL, MD4, and SHA-0 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 second-preimage 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.[11]

In 2009, Xiaoyun Wang et 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 2^97 queries with probability 0.87.[12]

In 2011 an informational RFC 6151[13] was approved to update the security considerations in MD5 and HMAC-MD5. For HMAC-MD5 the RFC summarizes that - although the security of the MD5 hash 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."

In improperly-secured systems a timing attack can be performed to find out a HMAC digit by digit.[14]

Examples of HMAC (MD5, SHA1, SHA256)

Here are some empty HMAC values:

HMAC_MD5("", "")    = 0x74e6f7298a9c2d168935f58c001bad88
HMAC_SHA1("", "")   = 0xfbdb1d1b18aa6c08324b7d64b71fb76370690e1d
HMAC_SHA256("", "") = 0xb613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad

Here are some non-empty HMAC values, assuming 8-bit ASCII or UTF-8 encoding:

HMAC_MD5("key", "The quick brown fox jumps over the lazy dog")    = 0x80070713463e7749b90c2dc24911e275
HMAC_SHA1("key", "The quick brown fox jumps over the lazy dog")   = 0xde7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
HMAC_SHA256("key", "The quick brown fox jumps over the lazy dog") = 0xf7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8

--

References

  1. ^ a b Bellare, Mihir; Canetti, Ran; Krawczyk, Hugo (1996). "Keying Hash Functions for Message Authentication".{{cite web}}: CS1 maint: postscript (link)
  2. ^ RFC 2104, section 2, "Definition of HMAC", page 3.
  3. ^ hmac — Keyed-Hashing for Message Authentication, Python Software Foundation, retrieved 7 May 2014
  4. ^ PHP: hash_hmac - Manual, The PHP Group, retrieved 13 September 2015
  5. ^ Preneel, Bart; van Oorschot, Paul C. (1995). "MDx-MAC and Building Fast MACs from Hash Functions". Archived from the original on 4 June 2010. Retrieved 28 August 2009. {{cite web}}: Unknown parameter |deadurl= ignored (|url-status= suggested) (help)CS1 maint: postscript (link)
  6. ^ Preneel, Bart; van Oorschot, Paul C. (1995). "On the Security of Two MAC Algorithms". Archived from the original on 23 February 2009. Retrieved 28 August 2009. {{cite web}}: Unknown parameter |deadurl= ignored (|url-status= suggested) (help)CS1 maint: postscript (link)
  7. ^ Keccak team. "Strengths of Keccak - Design and security". Retrieved 30 January 2013. Unlike SHA-1 and SHA-2, Keccak does not have the length-extension weakness, hence does not need the HMAC nested construction. Instead, MAC computation can be performed by simply prepending the message with the key.
  8. ^ Bruce Schneier (August 2005). "SHA-1 Broken". Retrieved 9 January 2009. although it doesn't affect applications such as HMAC where collisions aren't important
  9. ^ IETF (February 1997). "RFC 2104". 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.
  10. ^ Bellare, Mihir (June 2006). "New Proofs for NMAC and HMAC: Security without Collision-Resistance". In Dwork, Cynthia (ed.). Advances in Cryptology – Crypto 2006 Proceedings. Lecture Notes in Computer Science 4117. Springer-Verlag. Retrieved 25 May 2010. 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 resistance-to-attack that HMAC has shown even when implemented with hash functions whose (weak) collision resistance is compromised. {{cite conference}}: Unknown parameter |booktitle= ignored (|book-title= suggested) (help)
  11. ^ Jongsung, Kim; Biryukov, Alex; Preneel, Bart; Hong, Seokhie (2006). "On the Security of HMAC and NMAC Based on HAVAL, MD4, MD5, SHA-0 and SHA-1" (PDF). {{cite journal}}: Cite journal requires |journal= (help)CS1 maint: postscript (link)
  12. ^ Wang, Xiaoyun; Yu, Hongbo; Wang, Wei; Zhang, Haina; Zhan, Tao (2009). "Cryptanalysis on HMAC/NMAC-MD5 and MD5-MAC" (PDF). Retrieved 15 June 2015. {{cite journal}}: Cite journal requires |journal= (help)CS1 maint: postscript (link)
  13. ^ "RFC 6151 – Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms". Internet Engineering Task Force. March 2011. Retrieved 15 June 2015.
  14. ^ Briefly mentioned at the end of this session Sebastian Schinzel:Time is on my Side - Exploiting Timing Side Channel Vulnerabilities on the Web 28th Chaos Communication Congress, 2011.
Notes
  • Mihir Bellare, Ran Canetti and Hugo Krawczyk, Keying Hash Functions for Message Authentication, CRYPTO 1996, pp1–15 (PS or PDF).
  • Mihir Bellare, Ran Canetti and Hugo Krawczyk, Message authentication using hash functions: The HMAC construction, CryptoBytes 2(1), Spring 1996 (PS or PDF).

External links