Setting
A sensor gateway is a small device that collects readings from sensors and passes them on. In this invented example a publisher, the one party authorized to change a gateway’s policy, sends policy updates to two gateways, A and B, through a phone that only carries the bytes. Each update is a file that the questions call the artifact. Gateway A has the device ID 0x0001, and gateway B has 0x0002.
The publisher and both gateways hold one shared secret key, , the active message-authentication key, 32 bytes long. The publisher protects each artifact with HMAC-SHA-256 §5.9: HMAC §5.8 (hash-based message authentication code) computed with the hash function SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output). The artifact consists of these fields, in this order:
the device ID of the target gateway, 2 bytes;
an operation code §2.8, 1 byte, for example the code for installing a program;
a sequence number, 4 bytes, which the publisher increases by 1 for each artifact;
the length of the program in bytes, 2 bytes;
the program bytes;
the tag, 32 bytes, computed by HMAC-SHA-256 with key over the covered bytes.
In version 1 of the format, the covered bytes are all fields from the operation code to the end of the program: everything except the device ID and the tag itself. The HMAC code in the publisher’s software and in both gateways is correct. A gateway recomputes the tag over the covered bytes and accepts the artifact when the result equals the received tag.
Short answer
HMAC computes a fixed-length tag, 32 bytes with SHA-256, from a secret key and a message by hashing twice: first the key, mixed with one constant, followed by the message; then the key, mixed with a second constant, followed by the first result. A matching tag shows that some holder of the key produced exactly the covered bytes and that nobody has changed them since. It shows nothing about bytes outside the coverage, which key holder made it or whether the artifact is new, and it hides nothing.
Follow-up questions
Give the construction
HMAC (hash-based message authentication code) hashes twice, with the secret key mixed into each hash in a different way: RFC 2104 defines HMAC around a cryptographic hash function §5.1 , a function that maps input of any length to a fixed-length value called the digest. The symbols mean:
is the secret key and the message, the exact bytes to protect.
is the block size of : processes its input in blocks of bytes. is the length of the digest in bytes. For SHA-256 (Secure Hash Algorithm with a 256-bit output), and .
is the key brought to exactly bytes: a key longer than bytes is first replaced by its -byte digest , and zero bytes are then appended until the length is .
, the inner pad, is the byte
0x36repeated times, and , the outer pad, is the byte0x5crepeated times.is bitwise exclusive or (XOR), and is the byte string followed by the byte string .
The computation runs in four steps.
Bring the key to bytes, giving .
Compute the inner digest , which is bytes long.
Compute the tag , again bytes long.
The receiver repeats steps 1 to 3 on the bytes it received and compares its result with the received tag by a constant-time comparison §4.33, one that takes the same time wherever the first differing byte lies, so that its timing does not reveal how many leading bytes of a forged tag were right.
Sizes with SHA-256 and a 100-byte key: the key is longer than 64 bytes, so it is replaced by its 32-byte digest, and zero bytes are appended, giving a 64-byte . For a 300-byte message the inner hash reads bytes, and the outer hash reads bytes. The tag is 32 bytes whatever the length of the message.
RFC 4231 publishes test values. In its test case 2 the key is the 4 ASCII bytes Jefe, so zero bytes are appended to form , and the message is the 28-byte text what do ya want for nothing?. HMAC-SHA-256 gives this 32-byte tag, written in hexadecimal:
5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
Both hashes are needed. The shortcut , called a secret-prefix hash, is not HMAC, and with SHA-256 it can be forged. SHA-256 follows the Merkle–Damgård construction §15.8: it processes its input block by block and outputs its final internal state. Anyone who sees and knows the length of can resume hashing from that state and obtain for any bytes , where is the padding that SHA-256 appended to , all without the key. This forgery is called a length-extension attack §5.11. In HMAC the tag is the output of the outer hash, whose input is always exactly bytes long. Extending the outer hash gives the hash of a longer input, which is not the HMAC tag of any message.
Why not use an ordinary checksum §4.32?
Because anyone can compute a checksum, an attacker who changes the artifact simply computes the checksum of the changed artifact and sends that instead. A checksum, such as CRC-32 (the 32-bit cyclic redundancy check), is a short value computed from data by a public rule that uses no secret. It detects accidental damage, such as bits flipped by radio noise or by a failing cell of flash memory §6.6, because random damage almost never yields data that still matches the original value. It cannot detect a deliberate change, because whoever makes the change also recomputes the value.
In this setting a publisher sends an artifact, a policy update, to a gateway through a phone. If the artifact carried a CRC-32 instead of a tag, the phone could change one byte of the program, compute the CRC-32 of the changed bytes in microseconds and replace the old value, and the gateway’s comparison would succeed. To produce a matching tag computed with HMAC (hash-based message authentication code), the phone would need the secret key , which it does not have. HMAC works like a checksum that only holders of the key can compute.
A plain cryptographic hash such as SHA-256 (Secure Hash Algorithm with a 256-bit output), sent next to the artifact, is no better: it has no key either, so the phone replaces the artifact and its hash together. A hash protects only when the reference value reaches the receiver by a path the attacker cannot change. Examples are a digest stored in the gateway at manufacture, or a digest listed in a manifest §13.4, a file that lists digests, that the publisher has signed. Then second-preimage resistance §5.4, the infeasibility of finding a different input with the same digest as a given input, makes the comparison meaningful, and the protection comes from the separate path.
The general test is to ask who chooses the reference value and who can recompute it. A comparison detects deliberate change only when the reference value is out of the attacker’s reach, or when recomputing it requires a secret that the attacker does not hold.
Which bytes must be covered?
The tag must cover every field whose value changes what the receiver does or how it reads the rest of the message. For a policy update sent from a publisher to a gateway, the covered bytes include:
the device ID of the intended gateway;
the operation code;
the role of the key, when one gateway holds keys for several purposes;
the sequence number, or another value that shows the message is new, and any transaction §8.7 number that links the update to a later confirmation;
the format version, every length field and every flag;
the program bytes themselves.
An uncovered field defeats a correct HMAC (hash-based message authentication code). In this setting a publisher and two gateways, A with device ID 0x0001 and B with 0x0002, all hold one shared key , and version 1 of the update format leaves the device ID outside the covered bytes. The publisher makes an update for A. The phone that carries it changes the device ID from 0x0001 to 0x0002 and delivers the update to B. B recomputes HMAC over the covered bytes, which are byte for byte the bytes the publisher tagged, gets the same tag and accepts a program meant for A. Nobody found a collision and nobody forged a tag: HMAC did exactly its job. The defect is in the protocol, which left a field that changes the meaning of the update outside the coverage.
The repair has two parts. Put the device ID inside the covered bytes, and have each gateway compare that covered ID with its own. After the change, B’s recomputed tag no longer matches, because the bytes the publisher tagged contain 0x0001 where B now reads 0x0002. Giving each gateway its own key would also stop this particular attack, but the device ID belongs in the covered bytes anyway, so that the protection does not depend on how keys happen to be shared.
Two further rules apply. Encode the fields so that two different sets of values never produce the same bytes, for example with fixed-width fields, as here, or with a length before each variable field. And act only on the covered copy of each field, never on a similar value from elsewhere, such as the radio address of the sender. When one key protects several kinds of message, also cover a domain label §5.22, a fixed string that names the kind of message, so that a tag for one kind can never pass as a tag for another.
Does a shared tag identify one individual?
No: a tag made with a shared key shows only that some holder of that key computed it at some time, and the party that presents it may simply be resending it. Checking a tag computed with HMAC (hash-based message authentication code) means computing the tag again, so every party that can verify a tag can also create one. In this setting a publisher and two gateways, A and B, all hold the key . A valid tag could therefore come from the publisher, from A, from B, or from anyone who has extracted from one of them. The phone that delivers an update need not have made it either; it can replay an update it recorded.
Separate keys narrow the set. If the publisher shares one key with A and a different key with B, a valid tag at A comes from the publisher or from A itself. A knows that it did not make the tag, so A can conclude that the publisher did. A cannot prove this to anyone else, because A could have computed the same tag. A message authentication code §5.7 therefore never gives non-repudiation, which is proof, convincing to a third party, of who created a message.
A digital signature §5.14 separates the two abilities. The signer uses a private key to sign, and anyone uses the matching public key to verify; the public key cannot sign. If the gateways hold only the public key, none of them can create a valid signature. Even then, a valid signature identifies a key, not a person. To link it to one individual, an investigator also needs evidence of who held the private key and that nobody else used it.
State the conclusion exactly: “some holder of computed this tag over these bytes”, not “the publisher sent this, now”.
Does the tag hide the artifact?
No: a tag protects the artifact against undetected change, not against reading, and the artifact travels in readable form next to its tag. In this setting a phone carries the artifact, a policy update, from a publisher to a gateway, and the tag is computed with HMAC (hash-based message authentication code) under a key that the phone does not hold. The phone can read the program, copy it, store it and show it to anyone. What the phone cannot do without is change the covered bytes and still produce a matching tag.
The tag itself also reveals something. HMAC is deterministic: the same artifact under the same key always gives the same tag. An observer who sees two equal tags therefore learns that the two artifacts are, with overwhelming probability, identical.
Keeping the program secret from the phone needs encryption under a key that the phone does not hold, applied by the publisher and removed only by the gateway. This is end-to-end security §1.34: protection whose two ends are the publisher and the gateway, not the devices in between. AEAD §5.16 (authenticated encryption with associated data) does both jobs in one algorithm §14.1: it encrypts the program and computes a tag over the encrypted program together with any header fields left readable. Encrypting only the radio link between the phone and the gateway does not hide the program from the phone, because the phone is one end of that link and holds the readable program.
Whether secrecy is needed is a separate decision. A policy program is often not secret, and then a readable program with a tag is the right design.
Related entries: HMAC (§5.8), Digital signature (§5.14), Integrity (§1.9), Checksum versus CRC (§4.32), HMAC-SHA-256 (§5.9), HMAC inner and outer pads (§5.10), Domain separation (§5.22), Constant-time comparison (§4.33), Merkle–Damgård construction (§15.8), Confidentiality (§1.8). Further reading: [7, 8, 167].