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 . Here is a hash function that reads -byte blocks, is the message, is the key padded with zero bytes to bytes (hashed first if longer), is bitwise XOR (exclusive or), joins byte strings, and ipad and opad repeat the bytes 0x36 and 0x5c times. HMAC authenticates exactly the bytes . The protocol must still fix the hash function, key, tag length, fields of and their unambiguous encoding, context, freshness and verification rule.
Terms in the question
HMAC: a message authentication code §5.7 (MAC), which is an algorithm §14.1 that computes a short value, the tag, from a secret key and a message, such that only holders of the key can compute the correct tag. HMAC builds a MAC from a cryptographic hash function §5.1. RFC 2104 defines HMAC, and FIPS 198-1 defines the same construction [313].
Cryptographic hash function: a public algorithm that maps input of any length to an output of fixed length, the digest, such that nobody can feasibly find two inputs with the same digest. SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) reads its input in blocks of 64 bytes and returns a digest of 32 bytes.
Construction: the exact recipe that builds HMAC from the hash function.
Protocol: the rules that two parties follow when they exchange messages: the format of each message, the order of the messages, and what each party does when a message arrives.
Specify: state exactly, so that sender and receiver compute the tag over the same bytes and draw the same conclusion from it.
Full answer
Step 1: the construction. Let be the hash function, its block length in bytes and its output length in bytes; for SHA-256, and . Let be the key, its length in bytes, and the message. Write for the byte string followed by the byte string , for bitwise XOR of two strings of equal length, and for zero bytes. The standard names two constants: , the inner pad, is the byte 0x36 repeated times, and , the outer pad, is the byte 0x5c repeated times. Then In words: bring the key to exactly bytes; hash the key combined with ipad, followed by the message, which gives an -byte inner digest; then hash the key combined with opad, followed by that inner digest, which gives the -byte tag. A protocol may keep only the leftmost bits of the tag, which is called tag truncation §5.12.
Step 2: why two nested hashes. The obvious design 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 can continue the computation from that state, and so obtain the digest of followed by the hash function’s padding and any bytes the attacker chooses, without knowing . 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 , 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 , 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 is secret and random and meets the assumptions stated in the HMAC entry, nobody without 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 , 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 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.
The hash function, which fixes and ; for example HMAC-SHA-256 §5.9.
The key: its length (at least 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.
The tag length and the position of the tag in the message. A random guess at a -bit tag is right with probability . RFC 2104 recommends that be at least half the output length in bits and at least 80 bits; for SHA-256 that means at least 128 bits.
The coverage: exactly which fields form . Every field that changes what the receiver does, or how the receiver reads the rest of the message, must be inside : the message type, the format version, every length field, the target and the payload. A field outside can be changed without detection.
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.
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.
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.
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 and , and a 32-byte key in which every byte is 0x0b.
Key. , so is followed by zero bytes.
Pads. Each of the first 32 bytes of is , and each of the last 32 bytes is . In the bytes are and .
Sizes for a 5-byte message. The inner hash reads 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 is more than 64, the padded input is 128 bytes, which is 2 blocks. The outer hash reads bytes, and 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 and 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].