Definition
Tag truncation is the practice of keeping only the leftmost bits of the tag that a message authentication code §5.7 produces, and sending or storing those bits as the tag. A message authentication code is an algorithm §14.1 that computes, from a secret key and a message, a short value called a tag that only key holders can produce. The receiver recomputes the full tag, keeps the same bits and compares. Truncation saves space, but a single blind guess of a -bit tag succeeds with probability .
The idea
A sensor sends 20-byte readings over a slow radio link and authenticates each one with HMAC-SHA-256 §5.9: HMAC §5.8 (hash-based message authentication code) computed with SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output). The full tag is 32 bytes, more than the reading itself. Keeping the first 8 bytes, 64 bits, shrinks each packet from bytes to bytes.
The cost is guessing. An attacker without the key can always invent a tag and hope it is right. A -bit tag has possible values, and only a holder of the key can tell whether a guess is right. The attacker therefore cannot test guesses on its own computer: each guess is an online attempt that the receiver checks, sees and can count. Truncation trades bytes for a larger chance per guess, and the receiver’s limit on failed checks is what keeps the total chance small.
A combination lock is a fair picture: dials with two positions each give combinations, and every try must be made on the lock itself. The picture stops at counting. A mechanical dial lock cannot count failed tries, but a receiver can, and must.
How it works
Fix the length. The protocol fixes the tag length for each key. NIST SP 800-38B (Special Publication 800-38B), the NIST recommendation that defines CMAC §15.7 (cipher-based message authentication code), a message authentication code built from a block cipher §15.3, requires the tag length to be the same for every use of a given key [316].
Truncate. The sender computes the full tag and keeps its leftmost bits. Leftmost means first: the bits of the first byte from its most significant bit down, then those of the second byte, and so on. RFC 2104 and FIPS 198-1 prescribe the leftmost bits for HMAC [8, 313].
Verify. The receiver recomputes the full tag over the received message, keeps the same leftmost bits, and accepts only if all bits equal the received ones. It compares them with a constant-time comparison §4.33, one whose running time does not depend on where the first difference is.
Count. The receiver counts failed checks for each key and replaces the key before the count reaches a planned maximum.
When is not a multiple of 8, the tag occupies bytes, where is rounded up to a whole number, and the last byte is only partly used. The protocol must state which bits of that byte belong to the tag and what the unused bits must contain. The receiver must compare exactly the tag bits.
The receiver takes from its own configuration, never from the received message. A receiver that compares only as many bytes as arrived accepts a 1-byte tag after at most 256 guesses, and accepts an empty tag at once, because comparing zero bytes always finds them equal.
Worked example
Counting tags. With the possible tags are 00, 01, 10 and 11: values. With there are . Assume the tag behaves like a random value; the assumption is stated precisely below. An attacker who submits 3 different guesses for one message against a 4-bit tag wins if the true tag is one of those 3, which has probability . After 16 different guesses it has tried every value and is certain to win.
A limit on failed checks. Now let , and let the receiver replace the key after failed checks. The attacker’s chance of any success is at most , where is the number of guesses checked: , about , or 1 in 1,048,576. NIST SP 800-107 gives this example for HMAC [310].
No limit. With and no limit, different guesses for one message are certain to include the right tag. At 100 guesses per second that takes seconds, about 11 minutes.
Choosing from a target. NIST SP 800-38B works this example. The key is retired after at most failed checks, and the users accept a risk of , about one in a million, that one forged message is accepted. Write for the power to which 2 must be raised to give . Then bits.
A partial byte. RFC 4231 test case 1 gives an HMAC-SHA-256 tag whose first two bytes are b0 34, binary 10110000 00110100. Its leftmost 12 bits are 1011 0000 0011: all 8 bits of the first byte and the top 4 bits of the second. The low 4 bits of the second byte, 0100, are not part of a 12-bit tag.
Bytes saved. Truncating a 256-bit tag to 128 bits saves bytes per message. RFC 4231 test case 5 publishes such a 128-bit HMAC-SHA-256 tag, a3b6167473100ee06e0c796c2955552b, which is the first 16 bytes of the full tag.
Stated precisely
Assumption (ideal tags). To anyone without the key, the full tag of each message is a uniformly random value, independent of the tags of all other messages, including the tags the attacker has seen. This is the ideal that a pseudorandom function §15.1 approximates: a pseudorandom function is a keyed function whose outputs no feasible computation without the key can tell apart from such random values. Under this assumption the leftmost bits of a tag are uniform over values.
Bound. Let be the number of forged message-and-tag pairs the receiver checks, each for a message whose valid tag the attacker has not seen. Under the assumption, whatever the attacker’s strategy, Two cases make this concrete. If all guesses are different values for one message, the probability is exactly for , because each rejection rules out one value; it reaches 1 at . If the guesses are for different messages, each succeeds independently with probability , and the probability is . That is slightly below by the union bound §15.42 (the probability that at least one of several events happens is at most the sum of their probabilities). For example, and give , against .
Choosing . To keep the chance of accepting a forgery at or below a risk when at most failed checks are allowed per key, choose NIST SP 800-38B states this rule, calling MaxInvalids and Risk. A real message authentication code only approximates ideal tags. For a code that is a pseudorandom function, the bound then holds up to the attacker’s chance of telling the code apart from a function whose outputs are truly random. Unforgeability of the full tag is not enough: a code whose full tag starts with 64 zero bits can still be unforgeable, yet its leftmost 64 bits are a tag anyone can predict.
Limits and common mistakes
For a message authentication code whose tags look like random values to anyone without the key, truncation weakens only resistance to guessing. Truncation does not change which bytes the tag covers, it gives no confidentiality §1.8, and it does not stop a replay attack §1.26, in which a recorded message is sent again with its valid tag.
counts every failed check with the key, on every device and in every session that shares it. SP 800-38B says the limit applies across all implementations that use a given key. A limit per session on a key that lives for years does not bound .
A failure counter that resets when the device restarts lets the attacker restart the device and continue guessing. Keep the count in storage that survives a restart, or bound the attempts per unit of time by rate limiting §9.38 and the lifetime of the key.
Use one tag length per key. If a receiver accepts tags of several lengths for one key, the attacker attacks the shortest.
Standards set different minimums. RFC 2104 recommends that be at least half the hash output length and at least 80 bits, which is 128 bits for HMAC-SHA-256. NIST SP 800-107 allows HMAC tags as short as 32 bits, with a limit on failed checks as in the bound above. NIST SP 800-38B says at least 64 bits gives enough protection against guessing for most uses of CMAC, and allows shorter tags only after a careful risk analysis together with a limit on failed checks.
Truncation is not only a loss. RFC 2104 notes that a truncated tag also shows the attacker fewer bits of the hash result.
The guessing bound says nothing about a leaked or weak key. Whoever holds the key computes every tag, whatever its length.
Questions answered
How does a t-bit tag change online guessing probability after q attempts?
With a tag of bits, each forgery attempt succeeds with probability , so after attempts the probability that at least one forged message was accepted is at most . Here a tag is the short value that a message authentication code computes from a secret key and a message, is the number of tag bits the receiver checks, and is the number of forged message-and-tag pairs the receiver checks for one key. The bound assumes the tags look like uniformly random bits to anyone without the key. It is exact, , when the attacker tries different values, at most , for one message.
Every bit removed from the tag doubles the bound, and the bound reaches certainty at , when an attacker who tries different values for one message has tried them all. For example, with at most failed checks per key gives , about one in a million. With and no limit, success is certain after 65,536 attempts, about 11 minutes at 100 attempts per second. The attempts are online: each must be checked by the receiver, which holds the key. The receiver therefore controls , by counting failed checks across everything that uses the key and replacing the key before a set maximum, and the designer chooses , where is the largest tolerable probability that a forged message passes the check and is the power to which 2 must be raised to give .
Related entries: Message authentication code (MAC) (§5.7), Authentication coverage (§5.41), Constant-time comparison (§4.33), Forgery probability (§5.13), HMAC (§5.8), Rate limiting (§9.38), Pseudorandom function (PRF) (§15.1). Further reading: [8, 167, 310, 313, 316].