Full name: Hash-based message authentication code (HMAC) computed with the Secure Hash Algorithm SHA-256.
Definition
HMAC-SHA-256 is a message authentication code §5.7: an algorithm §14.1 that computes a short value, called a tag, from a secret key and a message. HMAC §5.8 (hash-based message authentication code) builds such an algorithm from a cryptographic hash function §5.1, which turns any data into a fixed-length value. SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) is the hash function used, so the tag is 256 bits (32 bytes) long. Only a holder of the key can compute the correct tag, so a receiver with the key detects any change to the message.
Where it fits
The HMAC entry explains the construction for any hash function. Choosing SHA-256, which the NIST standard FIPS 180-4 defines, fixes two numbers. SHA-256 reads its input in blocks of 64 bytes, and its output is 32 bytes. In HMAC-SHA-256, therefore, a key longer than 64 bytes is first replaced by its 32-byte SHA-256 hash, a key shorter than 64 bytes (such a hash included) is extended to 64 bytes with zero bytes at its end, and the tag is 32 bytes. RFC 2104, which defines HMAC, recommends a key of at least the hash output length, here 32 bytes.
Tag truncation §5.12 shortens a tag to its first bits to save space. RFC 2104 writes the shortened form as the name followed by , so HMAC-SHA-256-128 means the first 128 bits of the HMAC-SHA-256 tag. A shorter tag is easier to guess: one random guess is right with probability . RFC 2104 recommends that be at least half the hash output length and at least 80 bits, which for SHA-256 means at least bits.
Naming the algorithm says nothing about what is protected. HMAC-SHA-256 protects exactly the bytes it is given as the message. It does not know which device the message is for, which operation the message requests, or whether the message has been sent before.
Questions answered
Which fields and domain labels §5.22 must be included in the authenticated message?
The authenticated message must include every field that changes what the receiver will do, or how the receiver will read the rest of the message. The authenticated message, also called the authentication coverage §5.41, is the exact sequence of bytes from which HMAC-SHA-256 computes its tag, a short value that only a holder of the secret key can compute. In practice it includes:
the identity of the intended receiver, so that a message for one device is not accepted by another;
the operation requested, so that a tag made for one operation cannot be reused for a different one;
the version of the message format, which decides how the other fields are read;
every length field, and the payload itself;
a freshness value such as a counter, which the receiver compares with the last value it accepted, so that a recorded message cannot be sent again later and accepted;
a domain label, meaning a fixed string that names the purpose of this tag, whenever the same key protects more than one kind of message; this use of a label is called domain separation.
The fields must be encoded so that two different sets of field values can never produce the same bytes, for example by giving every field a fixed width or an explicit length. The exact list comes from the rules of the protocol, not from the name HMAC-SHA-256. Any field left outside the authenticated message is a field an attacker can change without being detected.
Related entries: Authentication coverage (§5.41), Domain separation (§5.22), SHA-256 (§5.2). Further reading: [7, 8].