Read this lesson as text

HMAC

Cryptography · Axiom Academy

LESSON HMAC: Hash-based Message Authentication Code Understanding how cryptographic hashing and secret keys combine to provide message authentication and integrity 1. Why Plain Hashing Isn't Enough You might think that simply hashing a message with a secret key would work: H(key || message) . However, this approach is vulnerable to length extension attacks. This happens because many hash functions process data in blocks and output their internal state. An attacker can: Take the hash output H(key || message) Append new data and continue hashing Produce a valid hash for the extended message HMAC solves the length extension problem through a clever two-pass construction using inner and outer padding: K = secret key (padded/hashed to block size) H = cryptographic hash function (e.g., SHA-256) opad = outer padding (0x5c repeated) ipad = inner padding (0x36 repeated) HMAC uses specific padding constants that are XORed with the key: They have good bit diffusion (roughly 50% ones and zeros) They differ in exactly half their bits (Hamming distance = 4) XORing with these values creates two distinct key derivatives This ensures the inner and outer hash computations use effectively different keys The padding is repeated to match the block size of the hash function (typically 64 bytes for SHA-256). 4. Security Properties and Proof Sketch HMAC provides strong security guarantees when built on a secure hash function: Unforgeability: Cannot create valid HMAC without knowing the key

This is the written version of the interactive lesson above. See the full Cryptography course.