Sub topic 14
Authenticating messages with HMAC
This chapter assumes you know what SHA-256 does and why it is used for integrity checks. You should also be comfortable with bytes in Python.
Why a plain hash is not enough
A SHA-256 digest is a fingerprint that anyone can compute. Suppose you send a message and its digest together. An attacker can change the message and recompute the digest, and the receiver will see a match. A plain hash detects accidents. It does not stop someone who is changing the data on purpose.
To stop that, you need a secret. A message authentication code (MAC) is a short tag computed from a message and a secret key. Only people who hold the key can produce a valid tag. HMAC (hash-based message authentication code) is the standard MAC built from a hash function such as SHA-256.
Why not just hash key plus message
The obvious design is SHA-256(key + message). SHA-256 processes its input block by block and exposes its internal state as the digest. Given the digest of key + message, an attacker can continue from that state and compute a valid digest for key + message + extra, without knowing the key. This is a length extension attack.
HMAC avoids it by hashing twice, with the key mixed in at both steps:
HMAC(K, m) = H((K xor opad) || H((K xor ipad) || m))
Here ipad and opad are fixed padding constants. The outer hash hides the inner digest, so there is no state to extend.
HMAC in Python
import hashlib
import hmac
import os
key = os.urandom(32)
message = b"amount=100&to=alice"
tag = hmac.new(key, message, hashlib.sha256).digest()
def verify(key: bytes, message: bytes, tag: bytes) -> bool:
expected = hmac.new(key, message, hashlib.sha256).digest()
return hmac.compare_digest(tag, expected)
print(verify(key, message, tag))
print(verify(key, b"amount=900&to=mallory", tag))The first call prints True. The second prints False, because changing any byte of the message gives a completely different tag. This is the avalanche effect from earlier chapters at work.
Common errors
- Comparing tags with
==. Ordinary comparison stops at the first differing byte. An attacker can measure the response time and learn the correct tag one byte at a time. Usehmac.compare_digest, which takes the same time however many bytes match. - Weak or reused keys. Generate keys with a secure random source, such as
os.urandom. Use at least 32 bytes for SHA-256, and use a separate key for each purpose. - Treating HMAC as encryption. The message is still readable. HMAC proves who made it and that it was not changed. It does not hide it.
- Ignoring replay. A valid message and tag can be sent again later. Put a timestamp, counter, or nonce (a number used once) inside the message so it is covered by the tag.
- Truncating the tag too far. Cutting the tag to a few bytes makes guessing feasible. Keep at least 16 bytes.
Exercise
A teammate writes tag = hashlib.sha256(key + message).digest() and checks it with tag == received_tag. Name two problems and give the fix for each.
Show answer
First, SHA-256(key + message) is open to length extension, so an attacker can forge a tag for an extended message. Use hmac.new(key, message, hashlib.sha256) instead. Second, == leaks timing information about how many leading bytes match. Use hmac.compare_digest.