HMAC
1. What Is HMAC? (beginner — hash + key done RIGHT!)
Naive breaks (length-extension!). HMAC nests TWO hashes with padded keys (RFC 2104!): inner ( + ipad + ) then outer ( + opad + inner!). Safe with SHA-256/512!
2. Steps (exam recipe!)
1. key > block? hash it! key < block? pad zeros to block size!
2. inner pad: K XOR 0x36 repeated (ipad!) → hash with message!
3. outer pad: K XOR 0x5c repeated (opad!) → hash inner result!
4. send (message, HMAC)! receiver recomputes + constant-time compares!
3. Why Nested? (exam why!)
Single hash leaks extendable state (attacker appends!). Outer hash hides inner digest (extension dies!). Even MD5-based HMAC survived (structure saves weak hash — though use SHA-256!).
4. Uses (exam map!)
API tokens, cookies, JWT signatures (HMAC-SHA256!), TLS records, GitHub webhooks! Variants: HMAC-SHA256 (standard!), HMAC-SHA512, KMAC (SHA-3 native!).
5. Exam Points
- Formula + ipad/opad steps — 7 marks, very frequent.
- Why not (extension!) — 5 marks, favourite trap.