13

I understand how for hash functions which are vulnerable to length extension attacks (such as SHA1 and SHA2) it is safer to use a HMAC construction.

What I don't understand is, how or why is $\operatorname{HMAC\_SHA256}_\mathrm{key}(\mathrm{message})$ safer (in terms of resistant against certain attacks) than $\operatorname{SHA256}(\mathrm{key}_1 \mathbin\| \mathrm{message} \mathbin\| \mathrm{key}_2)$, assuming that all key strings are sufficient in length and entropy?

forest
  • 15,626
  • 2
  • 49
  • 103
RocketNuts
  • 1,397
  • 1
  • 13
  • 24

2 Answers2

14

The construction you are proposing is called the "envelope" or "sandwich" MAC, it predates HMAC, and it is in fact secure—provided the key and message are appropriately padded. That is,

$$ \text{SHA256}(k \parallel m \parallel 1 \parallel 0^{b - 1 - (|m| \bmod b)} \parallel k) $$

is secure, as long as $k$ is the underlying hash function's block length $b$ ($b = 512$ in the case of SHA256), or is padded to that effect. Note that the padding after the message is crucial: without making sure the second key is in its own block the security of the envelope MAC is lower than that of HMAC. This was proved by Yasuda, and later Koblitz and Menezes (§6) argue that there is no sound theoretical reason to prefer HMAC over (correctly padded) envelope-MAC, beyond the former's more established nature.

Samuel Neves
  • 12,960
  • 46
  • 54
3

Some research brought up this paper On the Security of Two MAC Algorithms (Preneel and Oorschot, 1995).

The authors state that it's possible to significantly reduce the claimed security so that the security is about the same as collision resistance instead of preimage resistance. The details can be read there and in the references.

Artjom B.
  • 2,085
  • 1
  • 23
  • 53
SEJPM
  • 46,697
  • 9
  • 103
  • 214