Hash-Based Message Authentication Code
HMAC solves Length Extension Attacks & Collision Attacks by using two passes of Hashing with two different keys derived from the main secret key.
- H: The underlying hash function (e.g., SHA-256).
- k: The secret key.
- ipad (Inner Pad): The byte
0x36repeated (creates the “Inner Key”). - opad (Outer Pad): The byte
0x5Crepeated (creates the “Outer Key”).
This structure works because:
- Defeats Length Extension:
- The “internal state” output by the inner hash
H(inner_key∣∣m)is not exposed, because the outer hash immediately processes it. - An attacker cannot extend the message because they cannot pass the result through the outer hash (which requires the outer key).
- The “internal state” output by the inner hash
- Defeats Collisions:
- The nested structure masks the direct collision relationships found in simple suffix constructions.
Relevant Note(s): Message Authentication Code