MAC and HMAC
A tag you cannot compute without the key
A MAC (message authentication code) is a short tag computed from a message and a secret key. A receiver who knows the same key computes the tag again and compares it with the one received: a match means the message was sent by someone with the key and nobody changed it on the way. A plain hash does not do that — SHA-256 has no key, so whoever changes the message recomputes its hash too. HMAC (RFC 2104) builds a MAC from a hash function in two passes: the inner one hashes the key mixed with the constant ipad together with the message, the outer one the key mixed with the constant opad together with the inner result. A MAC does not encrypt; the message stays in the clear. The key is shared, so the receiver can compute the tag too: a MAC does not prove which of the two parties wrote the message — that is the job of a digital signature.
HMAC-SHA-256 live
-1002 B too long. Shorten the text — the toy works on small data.
Computing the tag…
Appending and a stopwatch
A home-made MAC of the form SHA-256(key ‖ message) looks reasonable, but SHA-256 reads data in 64-byte blocks and the digest is simply its state after the last block. Whoever knows the digest and the data length resumes hashing from that state and adds more blocks: they get a valid tag for the message with padding and an appended part, without knowing the key. The key length only has to be guessed — the server says which try passed. MD5, SHA-1 and SHA-512 are built the same way. In HMAC the tag is the output of a second, keyed outer pass, so there is no state to resume. Resuming SHA-256 from a given state needs an implementation of its own — the toy uses a toy one, checked against NIST vectors and compared with WebCrypto; the server uses the real SHA-256.
Appending data to a signed request
A home-made MAC: the server appends the request to a secret key and computes SHA-256 (the real one, from WebCrypto). It executes the request if the tag matches.
Type 1 to 64 B.
The server signs the request…
Lengths tried: 0 of 64.
The second trap is in verification. A comparison that stops at the first differing byte answers faster the earlier the tag is wrong. The attacker sends 256 versions of the first byte, checks which one answers slowest and moves on to the next: instead of 2²⁵⁶ possible tags they try 256 candidates per byte, each as many times as the noise requires. A browser cannot time one byte comparison — its clock is too coarse for that — so the toy below is a model with visible parameters, not a measurement.
A stopwatch on the tag comparison
The browser clock is too coarse to time one byte comparison, so response times come from a model: overhead + byte cost × compared bytes + normally distributed noise. One time unit is the cost of comparing one byte. The tag is a real HMAC-SHA-256 from WebCrypto.
- Fixed response overhead
- 100 u.
- Comparing one byte
- 1 u.
- Noise (standard deviation σ)
- 2 u.
The server is starting…
Where MACs guard data
- Flickr API (2009)Thai Duong and Juliano Rizzo showed that Flickr’s request signature — MD5 of the shared secret followed by the sorted parameters — could be extended: an attacker appended data to a signed request and computed a valid signature without the secret. As the long-term fix they recommended HMAC.Source (Flickr API (2009))
- Xbox 360 boot loaderThe boot loader checked a 16-byte HMAC with memcmp, byte by byte. By timing the comparison, the value was guessed byte by byte — at most 16 × 256 = 4096 tries according to Free60 — and the console was downgraded to a vulnerable kernel.Source (Xbox 360 boot loader)
- Keyczar (2009)Nate Lawson reported that Google’s crypto library compared HMAC tags with
==in Python andArrays.equalsin Java, both of which stop at the first difference. That allowed forging HMAC-SHA1 signatures, the library’s default algorithm.Source (Keyczar (2009)) - GitHub webhooksGitHub signs every webhook delivery with HMAC-SHA256 under a secret you set (the
X-Hub-Signature-256header), and its documentation warns: never a plain==, always a constant-time comparison such ascrypto.timingSafeEqual.Source (GitHub webhooks) - SHA-3 (Keccak)According to its designers, Keccak, unlike SHA-1 and SHA-2, has no length-extension weakness and so does not need the nested HMAC construction — a MAC can be computed by simply prepending the key to the message.Source (SHA-3 (Keccak))
Platform HMAC, constant-time comparison
Authenticate messages with HMAC-SHA-256 from your platform’s library, with a random key, and verify the tag
with a function that compares in constant time by itself: in the browser crypto.subtle.verify (the
WebCrypto specification requires a constant-time
comparison there), in Node crypto.timingSafeEqual, in .NET the FixedTimeEquals method of CryptographicOperations. A hash
without the length-extension weakness, such as BLAKE2 with its built-in key
(RFC 7693), also gives a MAC, but HMAC-SHA-256 is
everywhere. When you also need confidentiality, use an AEAD (the AES-GCM station)
instead of combining a cipher and a MAC yourself. HOTP and TOTP one-time codes are HMAC over a counter — the
One-time codes station.
const key = await crypto.subtle.generateKey(
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign', 'verify'],
);
const tag = await crypto.subtle.sign('HMAC', key, data);
// verification: the platform compares in constant time
const ok = await crypto.subtle.verify('HMAC', key, tag, data);import { createHmac, timingSafeEqual } from 'node:crypto';
const expected = createHmac('sha256', secret).update(body).digest();
// timingSafeEqual throws when the lengths differ
const ok = received.length === expected.length &&
timingSafeEqual(received, expected);Do not build a MAC from a bare hash: SHA-256(key ‖ message) falls to length extension, and SHA-256(message)
alone only protects against accidental damage. Do not compare tags with ==, ===, memcmp or
Arrays.equals — they stop at the first difference. Do not truncate the tag more than you must: with n bits
of tag every random try hits with a chance of 1 in 2ⁿ.
// ✕ home-made MAC: data can be appended without the key
const tag = sha256(secret + query);
// ✕ the comparison stops at the first difference
if (receivedTag === tag) execute(query);