liveBook · Manning

HMAC

As explained in Hacking Cryptography

HMAC is a keyed-hash message authentication construction that produces an integrity and authentication tag from a key and message.

HMAC is a keyed-hash message authentication construction: a secret key and message produce a tag that supports message integrity and sender authentication. Unlike secret-prefix or secret-suffix MACs, HMAC hashes the input twice with separate inner and outer key transformations. section 7.0 section 7.5

Figure 7.12 HMAC(key, message) = Houter(key || Hinner(key || message))
figure

HMAC follows the nested construction HMAC(K, m) = Houter(K || Hinner(K || m)), preventing a digest from exposing a resumable internal hash state. section 7.5

For a block-sized key K′, HMAC computes H((K′ XOR opad) || H((K′ XOR ipad) || m)). A key longer than the hash block size is hashed first; ipad and opad create distinct intermediate keys. section 7.5

The construction addresses two failures of simpler hash-based MACs:

  • Secret-prefix MACs, H(key || message), expose a length-extension attack when built from Merkle–Damgård hashes. section 7.2 section 7.3.2
  • Secret-suffix MACs, H(message || key), can succumb to collision attacks, including the RADIUS weakness involving MD5. section 7.2 section 7.5
  • HMAC resists both attack classes because extending the outer authentication tag does not produce a valid inner digest, while forging collisions requires the secret key. section 7.5

Forging a vulnerable API request

A secret-prefix transaction API authenticated an original amount of 10. By resuming SHA-256 from the published digest and adding padding plus amount=424242, an attacker produced a new authenticated request without the API secret. Replacing the direct hash with HMAC prevents this length-extension path. section 7.3.2

How Hacking Cryptography covers it

How deep it goes
Explains the nested equation, key normalization, ipad/opad transformations, and attacks against simpler MACs.
Examples
Uses a forged bank transaction, Flickr-style API authentication, RADIUS, AWS API signatures, and HMAC-MD5 as concrete cases.
What it assumes
Assumes familiarity with hash functions and message authentication codes while defining their security failures through concrete attacks.
What you can do after
Evaluate MAC designs and choose HMAC instead of direct secret-prefix or secret-suffix hashing for authentication.
How it connects
Connects HMAC with message authentication codes, hash functions, Merkle–Damgård constructions, collision attacks, and length-extension attacks.

Also explained in

Compare how 4 books explain HMAC →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.