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 many gateways through a phone that only carries the bytes. Each message carries a tag computed with HMAC §5.8 (hash-based message authentication code): a short value computed from a secret key and the message, which only holders of the key can compute. Each gateway uses several keys, each for one job:
, the active message-authentication key, with which the publisher tags policy updates;
, a pending replacement for during a key change §5.29, which becomes active only when the change completes;
, the recovery credential, which authorizes a new if is lost or stolen; was generated at random for each gateway at manufacture and is not derived from anything else;
, a confirmation key, with which the gateway tags its reply “installed” to the publisher.
The generation number counts key changes: it increases by 1 each time a new becomes active, and it is 8 now. The publisher computes , and from one secret root key §5.25 with a key derivation function §5.20 (KDF), an algorithm §14.1 that turns one secret into many keys. The input to the KDF names the key’s role, the gateway’s device ID and . The owner asks for three separations: different roles get different keys, different gateways get different keys, and different generations get different keys.
Short answer
Derive a separate key for each role, each gateway and each generation, so that a tag made for one purpose never verifies in another and the leak of one derived key exposes only its own context. The derivation input must encode role, device and generation unambiguously, and the gateway must still check which key each message may use and which generation is current. Separation spreads one secret over many independent-looking keys but adds no secret: anyone who knows the root key computes every derived key.
Follow-up questions
First answer
Separate keys keep each key’s tags, and the damage done when a key leaks, inside one context, which one shared key cannot do. In this setting a publisher sends policy updates to sensor gateways, each update carrying a tag computed with HMAC (a keyed function whose result only key holders can compute), and each gateway holds several keys: for policy updates, for its own confirmations, as a pending replacement for and for recovery. Three separations matter.
By role. Suppose the gateway tagged its confirmations with instead of , and a confirmation contained 16 bytes chosen by whoever asked for it. If the bytes of some confirmation could also be read as a policy update, an attacker would choose those 16 bytes so that the gateway itself computes a valid policy tag for a message of the attacker’s design. With separate from , the tag of a confirmation never verifies as a policy tag, whatever its bytes.
By device. Gateway 1 and gateway 2 get different keys, so an attacker who extracts the keys of gateway 1 learns nothing that works on gateway 2.
By generation. When the generation number , the count of key changes, rises from 8 to 9, the new key is unrelated to the old one, so a tag made with the old key fails under the new one.
The usual way to get all three from one root key is a key derivation function (KDF), an algorithm that turns one secret into many keys, given a label for each context. With HKDF §5.21 (HMAC-based Extract-and-Expand Key Derivation Function, defined in RFC 5869), the publisher runs the Extract step once on and then the Expand step once per key, each time with a label that encodes the role, the device ID and , for example the role policy, the device 0x0001 and . Giving each purpose its own label, so that outputs for different purposes differ, is domain separation §5.22. One key per purpose, its result here, is key separation §5.23.
Derivation does not finish the job. The gateway must enforce each role: it verifies policy updates only with and recovery requests only with . Derivation also decides nothing about who may obtain a key: a key service that derives any role’s key for anyone who asks separates nothing. Whether a request for a key is allowed is a question of authorization §1.7, checked separately.
Show an encoding pitfall
Joining fields without marking where each one ends lets two different contexts produce the same input bytes, and a key derivation function given the same bytes returns the same key. Take a derivation input made of a role name followed directly by a device name. The role a with the device bc gives abc; the role ab with the device c also gives abc. In ASCII both are the three bytes 61 62 63, written in hexadecimal, so both contexts get one and the same key. Nothing reports an error: domain separation, the practice of giving each purpose its own label, has failed silently. Realistic names fail the same way: the role policy with the device 12 and the role policy1 with the device 2 both give policy12.
The repair is an encoding that marks boundaries. With length-prefix framing §4.30, each field is preceded by one byte that holds its length in bytes:
role
a, devicebc:01 61 02 62 63;role
ab, devicec:02 61 62 01 63.
The first bytes, 01 and 02, differ, so a reader splits the two inputs at different places, and the two inputs are different byte strings. The property needed is a prefix-free encoding §15.38: no valid encoding of one list of fields is a starting part of the encoding of a different list, so a reader always knows where each field ends. Fixed-width fields also have this property. A separator character, such as a comma, works only if no field can contain it, or if every occurrence inside a field is escaped.
A label keeps two meanings apart only if the encoding keeps the labels apart.
Is every HMAC derivation HKDF?
No: HKDF is one specific construction, defined in RFC 5869, and a key derivation that merely uses HMAC (hash-based message authentication code) is a different construction with its own requirements. HKDF (HMAC-based Extract-and-Expand Key Derivation Function) has two steps. Let the input keying material (IKM) be the secret to start from, the salt §5.19 an optional random value that need not be secret, info a label that names the purpose, and the number of output bytes wanted. Write for HMAC with key and message , for the single byte with value , and for the byte string followed by the byte string .
Extract computes the pseudorandom key (PRK): .
Expand computes blocks and for , and outputs the first bytes of .
Other HMAC-based derivations look similar and differ in detail. One is a single call with a root key . Another is the counter-mode §15.5 derivation of NIST Special Publication (SP) 800-108, in which the HMAC message for output block is the counter , a label, a zero byte, a context and the requested output length [306].
Three differences matter.
Input requirement. The single call has no Extract step, so it is a sound derivation only if is already a uniformly random secret key, with every value equally likely, and HMAC behaves as a pseudorandom function §15.1, a keyed function whose outputs look random to anyone without the key. A Diffie–Hellman §19.1.4 shared secret, which has mathematical structure, needs Extract first.
Output length. One HMAC call with SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) gives exactly 32 bytes. HKDF with SHA-256 gives up to bytes, because the block number is one byte.
Exact bytes. equals HKDF’s Expand step run with as the PRK and only when the label is the info string followed by the byte
0x01, because then the message is exactly that of . Without that byte the two give different keys, so a design that says HKDF but computes the single call produces keys that no HKDF implementation reproduces.
So name the construction exactly, and make the security argument for the construction actually used.
Does a generation-bound tag revoke old keys by itself?
No: binding the generation number into a tag stops an attacker from changing the stated generation, but an old key is revoked only when the receiver refuses messages from older generations and stops using the old key. In this setting a gateway authenticates policy updates with its active message-authentication key and keeps a generation number , which counts key changes and is now 8. A tag is generation-bound when the tagged bytes include , or when the key itself was derived with in its label. Call the key of generation 7 and the current key .
Suppose has leaked, and the attacker sends a request that states and carries a valid tag under . If the gateway chooses the verification key according to the generation the message states, and still holds , the tag verifies and the request is accepted. The binding did its job, since the stated 7 is genuine, and the old key is still in force. Revocation happens only when the gateway compares the stated generation with its own: , so it rejects. If the attacker changes the stated 7 to 8, the gateway verifies with over bytes that now contain 8; the tag was computed with , so it does not match.
The gateway therefore needs five rules.
Keep the current generation in persistent storage and only ever increase it, as a monotonic counter §8.28.
Choose the verification key from the stored generation, never from the number a message states.
Reject every message whose generation differs from the stored one.
Erase the old key once the change is complete, so that a later bug or extraction cannot bring it back; this erasure is called secret zeroization §5.37.
Raise the stored generation in the same atomic step, one that happens completely or not at all, that makes the pending key the active key. A power failure during the change then leaves the gateway either entirely on generation 8 with or entirely on generation 9 with the new key, never on a mixture.
Writing 8 into a record is not revocation. The check of every message against the protected, stored value is.
Does domain separation restore a leaked root?
No: if the root key leaks, every key derived from it under any public label is exposed, including keys under labels invented after the leak. Domain separation gives each purpose its own label, so that the keys derived for different purposes differ; the labels are not secret. In this setting a publisher derives each sensor gateway’s keys from one secret root key with a key derivation function (KDF), an algorithm that turns one secret into many keys, using labels that name the key’s role, the gateway’s device ID and the generation number , a public count of key changes.
Suppose leaks, and the publisher responds by changing the role label from policy to policy-v2 and raising . The attacker reads the new label in the protocol description or in a firmware image, knows the device IDs and , and computes each new key exactly as the publisher does, with one KDF call per key. The new labels changed which keys the honest parties use, not who can compute them.
Recovery needs secret material that the attacker does not have, delivered over a path that the attacker can neither read nor forge.
Fresh material. A new root , generated by a CSPRNG §5.27 (cryptographically secure pseudorandom number generator) seeded from a physical noise source, and never derived from .
A surviving path. Each gateway adopts new keys only through a credential that the attacker lacks, such as its recovery credential , which was generated at random for that gateway and not derived from , or through provisioning by hand. A new key sent encrypted or tagged under any key derived from protects nothing, because the attacker derives that key too.
Regaining security after a compromise by bringing in fresh secrets is called post-compromise security §5.34. Domain separation stops a key made for one purpose from being used for another; it creates no secrecy, so it cannot provide post-compromise security.
Related entries: Key derivation function (KDF) (§5.20), HKDF (§5.21), Domain separation (§5.22), Key separation (§5.23), Prefix-free encoding (§15.38), Length-prefix framing (§4.30), Key rotation (§5.29), Key revocation (§5.32), Post-compromise security (§5.34), Monotonic counter (§8.28). Further reading: [9, 306].