HMAC Truncation and Output Length Selection
What you'll learn: How to safely shorten HMAC outputs and choose the right tag length for your security needs.
Why Truncate HMAC?
HMAC outputs match their underlying hash function's length—256 bits for HMAC-SHA256, for example. Sometimes you want shorter authentication tags to save bandwidth or storage. The good news: HMAC is designed to be safely truncatable from the left side of the output.
The Security Trade-Off
Think of an HMAC tag like a lock with a certain number of tumblers. A 256-bit HMAC is like a lock with 256 tumblers—virtually impossible to pick. Truncating to 128 bits removes half the tumblers, making brute-force guessing easier, but still astronomically difficult.
The critical formula: an attacker has a 1 in 2^n chance of forging a valid tag, where n is the truncated bit length.
Choosing Appropriate Lengths
Minimum recommendations:
- 96 bits (12 bytes): Acceptable for most applications, gives 2^96 security
- 128 bits (16 bytes): Strong security for high-value data
- 80 bits (10 bytes): Absolute minimum, only for low-risk scenarios
- ≤64 bits: Generally too weak for authentication
Never go below 80 bits. At 64 bits, an attacker with modest resources could potentially forge tags through brute force (2^64 operations are feasible today).
Practical Considerations
The output length doesn't affect HMAC's resistance to key recovery—that depends on the full internal computation. Truncation only impacts forgery resistance. You still get the same protection against length extension attacks and timing attacks that make HMAC superior to plain hashing.
Key Takeaway: HMAC can be safely truncated to shorter lengths, but never below 80 bits; choose 128 bits for strong security or 96 bits as a reasonable bandwidth/security balance.