Section 17.8

HMAC construction and unambiguous messages

Question

Write the general HMAC §5.8 construction and explain what a protocol still must specify.

Short answer

HMAC (hash-based message authentication code) computes the tag HMAC(K,m)=H((K0⊕opad)‖H((K0⊕ipad)‖m))\mathrm{HMAC}(K, m) = H\bigl((K_0 \oplus \mathrm{opad}) \mathbin{\Vert} H((K_0 \oplus \mathrm{ipad}) \mathbin{\Vert} m)\bigr). Here HH is a hash function that reads BB-byte blocks, mm is the message, K0K_0 is the key KK padded with zero bytes to BB bytes (hashed first if longer), ⊕\oplus is bitwise XOR (exclusive or), ‖\Vert joins byte strings, and ipad and opad repeat the bytes 0x36 and 0x5c BB times. HMAC authenticates exactly the bytes mm. The protocol must still fix the hash function, key, tag length, fields of mm and their unambiguous encoding, context, freshness and verification rule.

Terms in the question

Full answer

Step 1: the construction. Let HH be the hash function, BB its block length in bytes and LL its output length in bytes; for SHA-256, B=64B = 64 and L=32L = 32. Let KK be the key, |K||K| its length in bytes, and mm the message. Write a‖ba \mathbin{\Vert} b for the byte string aa followed by the byte string bb, ⊕\oplus for bitwise XOR of two strings of equal length, and 0n0^{n} for nn zero bytes. The standard names two constants: ipad\mathrm{ipad}, the inner pad, is the byte 0x36 repeated BB times, and opad\mathrm{opad}, the outer pad, is the byte 0x5c repeated BB times. Then K0={Kif |K|=B,H(K)‖0B−Lif |K|>B,K‖0B−|K|if |K|<B,K_0 = \begin{cases} K & \text{if } |K| = B, \\ H(K) \mathbin{\Vert} 0^{B-L} & \text{if } |K| > B, \\ K \mathbin{\Vert} 0^{B-|K|} & \text{if } |K| < B, \end{cases} HMAC(K,m)=H((K0⊕opad)‖H((K0⊕ipad)‖m)).\mathrm{HMAC}(K, m) = H\bigl((K_0 \oplus \mathrm{opad}) \mathbin{\Vert} H((K_0 \oplus \mathrm{ipad}) \mathbin{\Vert} m)\bigr). In words: bring the key to exactly BB bytes; hash the key combined with ipad, followed by the message, which gives an LL-byte inner digest; then hash the key combined with opad, followed by that inner digest, which gives the LL-byte tag. A protocol may keep only the leftmost tt bits of the tag, which is called tag truncation §5.12.

Step 2: why two nested hashes. The obvious design H(K‖m)H(K \mathbin{\Vert} m) fails with SHA-256 and other hash functions built by the Merkle–Damgård construction §15.8. Such a function processes its input block by block and outputs its final internal state as the digest. An attacker who sees the digest of K‖mK \mathbin{\Vert} m can continue the computation from that state, and so obtain the digest of K‖mK \mathbin{\Vert} m followed by the hash function’s padding and any bytes the attacker chooses, without knowing KK. This is a length-extension attack §5.11. In HMAC the inner digest is hashed once more under a second form of the key, so the tag never reveals the internal state in which the inner hash ended. The two pads differ in 4 bits of every byte, because 𝟶𝚡𝟹𝟼⊕𝟶𝚡𝟻𝚌=𝟶𝚡𝟼𝚊\texttt{0x36} \oplus \texttt{0x5c} = \texttt{0x6a}, which is 01101010 in binary; so the inner and the outer hash start from different forms of the key.

Step 3: verification. The receiver recomputes the tag over the bytes it received, with its own copy of KK, and compares the result with the received tag by a constant-time comparison §4.33: a comparison whose running time does not depend on where the first differing byte is, so that timing does not reveal how much of a guessed tag is right. The receiver acts on the message only if the two tags are equal.

Step 4: what HMAC guarantees, and what it does not. If KK is secret and random and HH meets the assumptions stated in the HMAC entry, nobody without KK can compute a valid tag for a message whose tag they have not seen, except by a lucky guess, even after seeing the tags of many messages of their choice. That is the only promise. HMAC does not hide mm, which travels in the clear; confidentiality §1.8 needs encryption, or authenticated encryption §5.16, which provides both protections at once. HMAC does not detect a repeat: the same key and message always give the same tag, so a recorded message can be sent again with its tag, which is a replay attack §1.26. HMAC does not say which key holder computed the tag. And HMAC never reads mm as fields; it sees one string of bytes.

Step 5: what the protocol must specify. Each item below closes a gap that HMAC leaves open.

  1. The hash function, which fixes BB and LL; for example HMAC-SHA-256 §5.9.

  2. The key: its length (at least LL bytes, chosen at random), how it is created and delivered, which parties hold it, and when it is replaced. The key serves one purpose only, a rule called key separation §5.23: a key that tags commands is not also used for encryption or for another kind of message.

  3. The tag length tt and the position of the tag in the message. A random guess at a tt-bit tag is right with probability 2−t2^{-t}. RFC 2104 recommends that tt be at least half the output length in bits and at least 80 bits; for SHA-256 that means at least 128 bits.

  4. The coverage: exactly which fields form mm. Every field that changes what the receiver does, or how the receiver reads the rest of the message, must be inside mm: the message type, the format version, every length field, the target and the payload. A field outside mm can be changed without detection.

  5. The encoding: a fixed order of fields, a fixed byte order §4.21 for numbers, and fixed widths or length prefixes §4.30, so that two different lists of field values never produce the same bytes.

  6. The context: the intended receiver, the direction or role of the sender, the session, and a domain label §5.22, which is a fixed string that names the purpose of the tag. A tag made for one context then cannot be accepted in another.

  7. Freshness: a counter that must exceed the last value the receiver accepted, or a nonce §5.18, a value used only once, that the receiver chose as a challenge. The receiver keeps the state it needs to reject a repeated message.

  8. The verification rule: recompute over the received bytes exactly as received, compare in constant time, act on no part of a message whose tag fails, and use exactly the bytes that were verified.

Worked example

Take HMAC-SHA-256, so B=64B = 64 and L=32L = 32, and a 32-byte key KK in which every byte is 0x0b.

  1. Key. |K|=32<64|K| = 32 < 64, so K0K_0 is KK followed by 64−32=3264 - 32 = 32 zero bytes.

  2. Pads. Each of the first 32 bytes of K0⊕ipadK_0 \oplus \mathrm{ipad} is 𝟶𝚡𝟶𝚋⊕𝟶𝚡𝟹𝟼=𝟶𝚡𝟹𝚍\texttt{0x0b} \oplus \texttt{0x36} = \texttt{0x3d}, and each of the last 32 bytes is 𝟶𝚡𝟶𝟶⊕𝟶𝚡𝟹𝟼=𝟶𝚡𝟹𝟼\texttt{0x00} \oplus \texttt{0x36} = \texttt{0x36}. In K0⊕opadK_0 \oplus \mathrm{opad} the bytes are 𝟶𝚡𝟶𝚋⊕𝟶𝚡𝟻𝚌=𝟶𝚡𝟻𝟽\texttt{0x0b} \oplus \texttt{0x5c} = \texttt{0x57} and 𝟶𝚡𝟶𝟶⊕𝟶𝚡𝟻𝚌=𝟶𝚡𝟻𝚌\texttt{0x00} \oplus \texttt{0x5c} = \texttt{0x5c}.

  3. Sizes for a 5-byte message. The inner hash reads 64+5=6964 + 5 = 69 bytes. SHA-256 appends the byte 0x80, then zero bytes, then the input length in bits as an 8-byte number, up to a multiple of 64 bytes. Because 69+1+8=7869 + 1 + 8 = 78 is more than 64, the padded input is 128 bytes, which is 2 blocks. The outer hash reads 64+32=9664 + 32 = 96 bytes, and 96+1+8=10596 + 1 + 8 = 105 also pads to 128 bytes, 2 blocks. One tag therefore costs 4 runs of the compression function, the step of the hash function that processes one block, and the tag is 32 bytes long.

Ambiguous encoding. A message carries two text fields, and the sender joins them directly, with no lengths. The pair (a, bc) becomes the ASCII bytes 61 62 63, and so does the pair (ab, c). HMAC sees identical bytes, so both pairs get the identical tag, whose first 8 bytes (of 32) are cc2b6e43b092526c. The receiver cannot tell from the bytes which pair the sender meant. If the receiver’s rule for splitting the bytes differs from the sender’s, the receiver acts on a pair that the sender never tagged, and the tag still verifies. HMAC worked correctly; the protocol failed to give the bytes one meaning.

Unambiguous encoding. Put one byte that holds the field’s length in front of each field. The pair (a, bc) becomes 01 61 02 62 63, and the pair (ab, c) becomes 02 61 62 01 63. The byte strings differ, so the tags differ: their first 8 bytes are c597d459826415fe and 9ec9ae5f247af2fe. Each byte string splits into two fields in exactly one way: the first byte gives the length of the first field, and the byte after that field gives the length of the second. Length-prefixed fields are one kind of prefix-free encoding §15.38.

Follow-up questions and limits

Can part of the work be done once per key? Yes. The strings K0⊕ipadK_0 \oplus \mathrm{ipad} and K0⊕opadK_0 \oplus \mathrm{opad} are each exactly one block long and depend only on the key, so an implementation can store the internal state of the hash function after each of them and start every tag from those two states. RFC 2104 describes this. In the example above it saves 2 of the 4 runs of the compression function per tag. The stored states need the same protection as the key, because anyone who holds them can compute tags.

Does a valid tag show which party sent the message? No. Every holder of the key can compute every tag. To tell senders apart, give each sender its own key. For a proof that a third party can check, use a digital signature §5.14.

This answer does not prove HMAC secure. The HMAC entry states the security goal and the assumptions on the hash function that the proof needs.

Fullest treatment: HMAC (§5.8).

Related entries: Prefix-free encoding (§15.38), Authentication coverage (§5.41), Tag truncation (§5.12), Replay attack (§1.26), HMAC inner and outer pads (§5.10), Length-extension attack (§5.11), Constant-time comparison (§4.33), Domain separation (§5.22). Further reading: [8, 46, 313].