Tool 2 of 8

Hash-based Message Authentication Code (HMAC)

Anyone can compute a hash, including an attacker. HMAC adds a shared secret key to the hashing, so only someone who holds the key can produce a valid tag.

Your challengeDiscover why Bob is fooled when Alice only sends a hash, and why he is no longer fooled when she sends an HMAC tag.
Visualise

Why a hash is not enough, and how a key fixes it

Genuine SHA-256 and HMAC Simulated network and attacker
Alice
sender
Message
Pay €100 to Bob
Network Simulated
Mallory
attacker on the network
Nothing sent yet
Bob
receiver (the bank)
Experiment

Tag lab

Genuine HMAC-SHA-256

An HMAC is one box with two inputs: the message and the secret key. The plain hash from Tool 1 has only one input. That key is the whole difference.

SHA-256no key
→
Plain hash: anyone can compute it
HMACkey 1
→
Tag with key 1
HMACkey 2
→
Tag with key 2 (differences from key 1 highlighted)

Keys

Key 1 (256 random bits)
Key 2 (256 random bits)

Type in the message and watch both tags change completely, like a hash. With a different key, the same message gets a totally different tag.

Experiment

Attack lab: can Mallory fool Bob?

Genuine tags and verification Simulated network and attacker
Alice
sender
Shared secret key random
Network Simulated
Message in transit
Tag in transit
(nothing sent)
Mallory can…

You can also edit the message in transit yourself.

Bob
receiver
Bob's copy of the key

What happened

Explain

Key ideas

MAC and tag

A Message Authentication Code (MAC) is a short value, the tag, computed from a message and a secret key. HMAC is the standard way to build a MAC from a hash function (RFC 2104, FIPS 198-1). Here we use HMAC-SHA-256.

One shared key

Sender and receiver hold the same secret key. That raises the same question as symmetric encryption: how do they share it safely? (Tools 4 to 6.)

Verifying

The receiver recomputes the tag from the received message and their own copy of the key, and compares. The tag is never "decrypted" or reversed.

What it gives

  • Integrity: the message was not changed.
  • Data-origin authentication: it was produced by someone holding the key.

What it does not give

  • Confidentiality: the message travels in clear text.
  • Non-repudiation: both sides hold the key, so either could have made the tag.
  • Replay protection: a valid message and tag can be recorded and sent again unchanged.

Not a network MAC address

In cryptography, MAC means Message Authentication Code. It has nothing to do with the MAC address of a network card.

Why not just compute "hash of key followed by message"? Home-made combinations like this have known weaknesses (for example length-extension attacks). HMAC is an established construction designed to avoid them. Never invent your own.

Hash, HMAC, signature, authenticated encryption: what is the difference?

Plain hashHMACDigital signatureAES-GCM
KeyNoneOne shared secret keyKey pair: private to sign, public to verifyOne shared secret key
Who can create itAnyoneAnyone with the shared keyOnly the private-key holderAnyone with the shared key
Who can verify itAnyoneAnyone with the shared keyAnyone with the public keyAnyone with the shared key
Integrity⚠ Only with a trusted hash✔ Yes✔ Yes✔ Yes (built-in tag)
Origin authentication✖ No✔ Yes✔ Yes✔ Yes
Confidentiality✖ No✖ No✖ No✔ Yes
Non-repudiation✖ No✖ No✔ Yes✖ No

Common misconceptions

"HMAC encrypts the message."
The message stays readable. HMAC only adds a tag.
"The tag can be reversed to get the message."
Like a hash, a tag is one-way. Bob recomputes it, he never reverses it.
"A valid tag proves which person sent the message."
It proves someone with the key sent it. Alice and Bob both have the key.
"A hash next to a download protects me even if the server is hacked."
An attacker who controls the server replaces both. A tag needs a key the attacker does not have.
"A valid tag proves the message is new."
A recorded message can be replayed. Protocols add counters or timestamps for that.
"Encryption alone guarantees integrity."
Not always. That is why modern encryption such as AES-GCM includes a tag (Tool 3).
Apply

Where you meet HMAC

Webhooks and APIs

Services such as payment providers or GitHub send a request with an HMAC of its content. The receiving server recomputes it with the shared secret and rejects anything that does not match.

Session tokens

JSON Web Tokens signed with HS256 are HMAC-SHA-256: the server can detect if a user edits their own token (for example to become "admin").

Authenticator apps

The 6-digit codes in apps like Google or Microsoft Authenticator (TOTP) are an HMAC of the current time with a secret shared when you scanned the QR code.

Secure protocols

IPsec VPNs and older TLS versions protect each packet with an HMAC. TLS 1.3 uses HMAC-based functions to derive its keys.

Challenges

1. Spot the forgery

Bob received three messages. He shares the key below with Alice. Only one message is genuine. Verify each one.

Shared key
2. Who could have sent this?

Alice and Bob share an HMAC key. Bob shows a judge a message "Alice owes Bob €5 000" with a valid tag. Can the judge conclude that Alice wrote it? Which tool would solve this?

Show answer
No. Bob also holds the key, so he could have computed the tag himself. HMAC cannot provide non-repudiation. A digital signature (Tool 7) can, because only Alice holds her private key.
3. Which property?

For each mechanism, which properties does it give: hash, HMAC, digital signature, AES-GCM? Fill in the table on paper, then compare with the table in the Explain section.

Continue the journey