Section 5.10

HMAC inner and outer pads

Full name: Inner pad and outer pad of the hash-based message authentication code (HMAC), named ipad and opad.

Definition

The HMAC inner and outer pads are two constant public byte strings that HMAC §5.8 (hash-based message authentication code) combines with its key to make two different key blocks, one per hash run. From a secret key and a message, HMAC computes a tag that reveals to a key holder, with overwhelming probability, any change to the message. Each pad fills one input block of the hash function, 64 bytes for SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output). The inner pad, ipad, repeats the byte 0x36; the outer pad, opad, repeats 0x5c.

Where it fits

Main entry: HMAC (§5.8).

The HMAC entry describes the whole construction. RFC 2104, which defines HMAC, names the pads ipad and opad, with i for inner and o for outer. Let BB be the block length of the hash function and LL its output length, both in bytes. For SHA-256, B=64B = 64 and L=32L = 32; for SHA-512 (Secure Hash Algorithm §14.1 with a 512-bit output), B=128B = 128. HMAC first turns the key KK into a key K0K_0 of exactly BB bytes. The inner hash input then starts with the block K0⊕ipadK_0 \oplus \mathrm{ipad}, and the outer hash input with the block K0⊕opadK_0 \oplus \mathrm{opad}, where ⊕\oplus is bitwise XOR (exclusive or): each output bit is 1 exactly when the two input bits differ.

A key byte 0x0b, binary 00001011, becomes 0x3d with the inner pad byte 00110110 and 0x57 with the outer pad byte 01011100. A zero byte of K0K_0 becomes 0x36 or 0x5c. Whatever the key, the two blocks differ by ipad⊕opad\mathrm{ipad} \oplus \mathrm{opad}, the byte 0x6a (binary 01101010) repeated: in 4 of every 8 bits, 256 of the 512 bits of a 64-byte block. Bellare, Canetti and Krawczyk, who designed HMAC, chose the two values for a simple representation and a large Hamming distance, the number of bit positions in which two strings differ, so that the two internal states that the hash function reaches after these two blocks, which serve as the inner and outer keys, behave as independent keys.

The pads are public constants and add no secrecy; all the secrecy of both blocks comes from K0K_0. Because each block depends only on the key, an implementation may hash the two blocks once per key and store the two resulting internal states of the hash function (RFC 2104, section 4). Those two states are enough to compute the tag of any message, so they must be protected like the key.

Limits and common mistakes

Do not confuse the block length BB with the output length LL. For SHA-256, a 40-byte key is longer than L=32L = 32 but shorter than B=64B = 64, so it is not hashed: it receives 64−40=2464 - 40 = 24 zero bytes.

Because a short key is filled with zero bytes, a key and the same key with zero bytes appended give identical tags, as long as the longer of the two is at most BB bytes. The 3-byte key abc and the 4-byte key abc followed by one zero byte are interchangeable. An application should fix one key length.

A key longer than BB bytes and its LL-byte hash are interchangeable as well, because HMAC replaces the first by the second.

Questions answered

What happens when the input key is longer than the hash block size?

HMAC (hash-based message authentication code) first hashes the key and uses the hash output in place of the key. FIPS 198-1 states the rule [313]: when the key is longer than the block length BB of the hash function, compute the LL-byte hash of the key, where LL is the hash output length, and append B−LB - L zero bytes. The resulting BB-byte value is then combined with the inner and outer pads, the bytes 0x36 and 0x5c repeated BB times, like any other key. For SHA-256 (Secure Hash Algorithm with a 256-bit output), B=64B = 64 and L=32L = 32, so a 100-byte key becomes its 32-byte SHA-256 digest followed by 64−32=3264 - 32 = 32 zero bytes. A key of exactly 64 bytes is used unchanged, and a shorter key receives zero bytes up to 64.

Test case 6 of RFC 4231 shows the rule: its 131-byte key, every byte 0xaa, gives for its 54-byte test message an HMAC-SHA-256 §5.9 tag beginning 60e43159, and using the 32-byte SHA-256 digest of that key as the key gives the same tag. The long key and its digest are therefore interchangeable, and a key longer than the block gives no more strength than its 8L8L-bit digest, 256 bits for SHA-256. NIST SP 800-107 (Special Publication 800-107) says it is not sensible to generate an HMAC key longer than the block [310].

Related entries: Entropy and min-entropy (§5.26), HMAC (§5.8), HMAC-SHA-256 (§5.9), Bit operations: truth tables, masks, shifts, and rotations (§4.50). Further reading: [8, 46, 167, 310, 313].