How do HMAC-SHA256(key, data) and SHA256(key + data) differ?

810    Asked by AashnaSaito in SQL Server , Asked on Nov 26, 2021

Is there anything different about how secure these two hashing algorithms (HMAC-SHA256 and SHA256) are? Does HMAC "fuse" the data and the key in a special way that's more security-aware?


That is true, HMAC is way more complex than simple concatenation.

  As a simplistic example, if you were to simply concatenate key + data, then "key1"+"data" yields identical results to "key"+"1data", which is suboptimal. HMAC will yield different results for each. There are other flaws with simple concatenation in many cases, as well; see cpast's answer for one.

The specification for HMAC is called RFC2104, which you should read if you have this level of interest.

In summary, to implement HMAC, you should first:

Create "ipad", which is 0x36 repeated BLOCKSIZE times. Create "opad", which is 0x5c repeated BLOCKSIZE times.
Note that BLOCKSIZE is 64 bytes for MD5, HMAC-SHA-1, HMAC-SHA-224, HMAC-SHA-256, and 128 bytes for SHA-384 and SHA-512, per RFC2104 and RFC4868.
Then HMAC is defined as:
HASH(Key XOR opad, HASH(Key XOR ipad, text))
or, in detail from the RFC,
(Pretext: The definition of HMAC requires a cryptographic hash function, which we denote by H, and a secret key K. We assume H to be a cryptographic hash function where data is hashed by iterating a basic compression function on blocks of data. We denote by B the byte-length of such blocks.)

Remarks on HMAC-SHA256

HMAC-SHA256 is a type of keyed hash algorithm that is constructed from the HMAC-SHA256 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, and then applies the hash function a second time. The output hash is 256 bits in length. An HMAC can be used to determine whether a message sent over an insecure channel has been tampered with, provided that the sender and receiver share a secret key. The sender computes the hash value for the original data and sends both the original data and hash value as a single message. The receiver recalculates the hash value on the received message and checks that the computed HMAC matches the transmitted HMAC.

Any change to the data or the hash value results in a mismatch because knowledge of the secret key is required to change the message and reproduce the correct hash value. Therefore, if the original and computed hash values match, the message is authenticated. HMAC-SHA256 accepts keys of any size and produces a hash sequence of 256 bits in length.



Your Answer

Interviews

Parent Categories