Section 20.15

Tree 15: “Why derive separate keys and bind context?”

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 generation number gg counts key changes: it increases by 1 each time a new KaK_a becomes active, and it is 8 now. The publisher computes KaK_a, KpK_p and KcK_c from one secret root key §5.25 RR 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 gg. 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: KaK_a for policy updates, KcK_c for its own confirmations, KpK_p as a pending replacement for KaK_a and KrK_r for recovery. Three separations matter.

The usual way to get all three from one root key RR 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 RR and then the Expand step once per key, each time with a label that encodes the role, the device ID and gg, for example the role policy, the device 0x0001 and g=8g = 8. 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 KaK_a and recovery requests only with KrK_r. 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:

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 LL the number of output bytes wanted. Write HMAC(k,m)\mathrm{HMAC}(k, m) for HMAC with key kk and message mm, byte(i)\mathrm{byte}(i) for the single byte with value ii, and a‖ba \mathbin{\Vert} b for the byte string aa followed by the byte string bb.

  1. Extract computes the pseudorandom key (PRK): PRK=HMAC(salt,IKM)\mathrm{PRK} = \mathrm{HMAC}(\mathrm{salt}, \mathrm{IKM}).

  2. Expand computes blocks T1=HMAC(PRK,info‖byte(1))T_1 = \mathrm{HMAC}(\mathrm{PRK}, \mathrm{info} \mathbin{\Vert} \mathrm{byte}(1)) and Ti=HMAC(PRK,Ti−1‖info‖byte(i))T_i = \mathrm{HMAC}(\mathrm{PRK}, T_{i-1} \mathbin{\Vert} \mathrm{info} \mathbin{\Vert} \mathrm{byte}(i)) for i≥2i \ge 2, and outputs the first LL bytes of T1‖T2‖…T_1 \mathbin{\Vert} T_2 \mathbin{\Vert} \dots.

Other HMAC-based derivations look similar and differ in detail. One is a single call K=HMAC(R,label)K = \mathrm{HMAC}(R, \mathrm{label}) with a root key RR. Another is the counter-mode §15.5 derivation of NIST Special Publication (SP) 800-108, in which the HMAC message for output block ii is the counter ii, a label, a zero byte, a context and the requested output length [306].

Three differences matter.

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 KaK_a and keeps a generation number gg, which counts key changes and is now 8. A tag is generation-bound when the tagged bytes include gg, or when the key itself was derived with gg in its label. Call the key of generation 7 K7K_7 and the current key K8K_8.

Suppose K7K_7 has leaked, and the attacker sends a request that states g=7g = 7 and carries a valid tag under K7K_7. If the gateway chooses the verification key according to the generation the message states, and still holds K7K_7, 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: 7<87 < 8, so it rejects. If the attacker changes the stated 7 to 8, the gateway verifies with K8K_8 over bytes that now contain 8; the tag was computed with K7K_7, so it does not match.

The gateway therefore needs five rules.

  1. Keep the current generation in persistent storage and only ever increase it, as a monotonic counter §8.28.

  2. Choose the verification key from the stored generation, never from the number a message states.

  3. Reject every message whose generation differs from the stored one.

  4. 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.

  5. Raise the stored generation in the same atomic step, one that happens completely or not at all, that makes the pending key KpK_p the active key. A power failure during the change then leaves the gateway either entirely on generation 8 with K8K_8 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 RR 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 gg, a public count of key changes.

Suppose RR leaks, and the publisher responds by changing the role label from policy to policy-v2 and raising gg. The attacker reads the new label in the protocol description or in a firmware image, knows the device IDs and gg, 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.

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].