Section 20.16

Tree 16: “How do keys survive storage, boot, and transport safely?”

Setting

A sensor gateway is a small device that collects readings from sensors and passes them on. In this invented example, the gateway also runs a small decision program, and a publisher, the one party authorized to change the gateway’s policy, sends it new programs and management requests. Everything travels through a phone that relays the bytes over a Bluetooth radio link. Each request carries a tag computed with HMAC §5.8 (hash-based message authentication code), a message authentication code §5.7 (MAC): a function that computes a short value, the tag, from a secret key and a message, so that someone without the key should have only a negligible chance of creating a valid tag for a new request.

The gateway holds three secret keys of 32 bytes each. KaK_a is the active key: the key whose tags the gateway accepts today. KpK_p is the pending key: a replacement for KaK_a that the publisher sends but the gateway does not use yet. KrK_r is the recovery key §5.31: a separate key, whose copy at the publisher is kept offline, that authorizes only recovery actions, such as installing a new active key after KaK_a is lost. Every active key has a generation number gg, a count that rises by 1 at each replacement. KaK_a belongs to g=7g = 7, and KpK_p is to become g=8g = 8.

The gateway keeps its keys in flash memory §6.6, storage that keeps its content without power, and copies the key in use into RAM. It also keeps a floor: the lowest generation it will ever accept again, held in a monotonic counter §8.28, a stored number that can only increase. The questions follow KpK_p from the publisher, through the phone, into flash, and through a later boot after a power failure.

Short answer

Each stage needs its own protection, because a key’s bytes can pass a stage intact while a different requirement fails. At rest, every copy is listed and either protected or erased. In transit, the key is encrypted end to end, so the phone relays bytes it cannot read. Wrapping must detect tampering and bind the key to its device, role and generation. At boot, the gateway selects the committed key whose generation meets the floor, not the first readable one. A change is acknowledged only once it is durable.

Follow-up questions

At rest

A key at rest is safe only when every stored copy of it is either protected against the attackers the design names or gone. At rest means stored, not moving. Here the key is the active key KaK_a of a sensor gateway: a 32-byte secret from which the gateway computes the tag, a short check value, that every genuine request from its owner, the publisher, must carry. The first step is an inventory, a list of every place a copy exists and how long it stays there:

For each copy, name the attacker and the protection that stops that attacker. A remote attacker who can only send radio messages never reads flash. An attacker who holds the device can read flash through its debug port, the connector used to program and inspect the chip, unless readout protection §16.5, a chip setting that blocks that access, is enabled. Encrypting the stored key under a second key helps only if the second key is better protected, because the problem moves to the second key. A secure element §5.38, a separate chip that stores keys and computes with them but never releases them, keeps the bytes from being copied.

Custody of a key has two sides: who can read its bytes, and who can make the key be used. A secure element that never releases KaK_a still computes a tag for every request it receives. If an attacker takes over the gateway’s software, and that software may ask for a tag over any message, the attacker uses the authority of KaK_a without ever seeing KaK_a. The inventory therefore also lists every interface that can use the key, and who may call it. Organizations often keep such an inventory as a register of secrets: for each key, where its copies are, who has custody of it, and when it is due for replacement.

In transit

A key in transit is safe only when it is encrypted end to end: by the original sender, for the final receiver alone, under a key that no relay holds. Here the publisher sends a replacement key KpK_p to a sensor gateway through a phone, which forwards the bytes over a Bluetooth link. Bluetooth can encrypt that link, but link encryption protects the bytes only between the two ends of the link, the phone and the gateway, against a listener on the radio channel. The phone itself decrypts what it receives. A key that reaches the phone without further protection is therefore readable on the phone, and a phone that runs malicious software, or that was never meant to hold keys, learns KpK_p.

End-to-end security §1.34 is protection from the original sender to the final receiver, whatever relays carry the data between them. The publisher encrypts KpK_p before KpK_p leaves the publisher’s system, so that only the gateway can decrypt it. The key for that encryption comes from one of two sources:

The choice of key decides what a later leak exposes. If KpK_p travels encrypted under the old active key KaK_a, or under a key computed from KaK_a, then anyone who later learns KaK_a and recorded the transfer also learns KpK_p. A key change §5.29 meant to shut out a suspected thief of KaK_a then shuts out nobody. More generally, whoever later learns any key that can decrypt the recorded transfer also learns KpK_p. Encryption that begins too late does not help either: if the phone receives KpK_p in readable form and only then encrypts it for the gateway, the phone has already seen KpK_p.

What does wrapping need?

Wrapping needs four things: a key-encryption key that only the sender and the receiver hold, an algorithm §14.1 that detects any change, an input length that the algorithm accepts, and a binding of the wrapped key to its device, its role (what the key is for, here the active key), its generation and its allowed use. Key wrapping §19.1.12 is the encryption of one key under another key, the key-encryption key (KEK), by an algorithm that also protects integrity §1.9: unwrapping returns exactly the original key or reports failure. Here a publisher wraps a replacement key KpK_p for a sensor gateway. KpK_p is to become the gateway’s active key, the key that authenticates the publisher’s requests, for generation 8, where the generation number counts key replacements.

Successful unwrapping proves only that the bytes came unchanged from a holder of the KEK. Before the gateway stores the key as a candidate, it checks the bound fields against its own state: the device ID is its own, the generation is exactly one more than the active generation, and no other candidate is pending.

A worked layout: KpK_p is 32 bytes. Adding a 4-byte device ID, a 1-byte role, a 2-byte generation and a 1-byte use code gives 32+4+1+2+1=4032 + 4 + 1 + 2 + 1 = 40 bytes. Since 40=5×840 = 5 \times 8, KW accepts this input and outputs 40+8=4840 + 8 = 48 bytes. With a 2-byte use code instead, the input is 41 bytes, which is not a multiple of 8, so KW refuses it. KWP pads the 41 bytes to 48 and outputs 48+8=5648 + 8 = 56 bytes.

At boot

At boot, the gateway must choose the key that currently has authority, which need not be the first key it can read. Here a sensor gateway checks its owner’s requests with an active key, and each active key has a generation number, a count that rises by 1 at each replacement. The gateway also stores a floor, the lowest generation it will ever accept again, in a monotonic counter, a stored number that can only increase.

Suppose the floor is 8. The record of the generation-7 key is intact. The record of the generation-8 key fails its checksum §4.32, a short value computed over the record that reveals accidental damage. Starting with generation 7 is wrong: 7<87 < 8, so generation 7 has been replaced, and the floor exists to stop exactly this return. The replacement may have happened because the generation-7 key leaked. The correct outcome is a recovery state that accepts only actions authorized by the separate recovery key, not a quiet fallback to generation 7.

A failed lookup is not proof of absence. If the generation-8 record cannot be read, the gateway has not learned that no generation-8 key exists. Treating “not found” as “never existed” turns damaged storage into a rollback, a return to an older key.

Boot selects a key only on positive evidence §19.3.5, and it needs all four kinds:

  1. Integrity: the record is complete, and its checksum or tag verifies.

  2. Identity: the record belongs to this device and to the role of active key.

  3. Commitment: a commit record §8.21, a small record whose valid presence declares a change complete and in force, names this key.

  4. Eligibility: its generation is at least the floor.

A checksum shows only that the record is undamaged. Anyone who can write the storage can also compute a checksum, so a checksum is no evidence of authority. If the threat model §1.2 includes an attacker who can write flash directly, the records need a tag under a key that such an attacker cannot read, such as a device key held in hardware. This check at start-up is boot validation §8.30. It complements the arrival authentication that checked the key when it was delivered.

What does durable mean?

Durable means that once the gateway reports a state change as done, that state survives every failure its fault model §1.30 admits, such as loss of power at any instant. The fault model is the written list of failures the design must survive. The guarantee starts at the commit point, the single write after which the new state is in force, and it covers only the failures on that list.

A wrong order shows the difference. A sensor gateway checks requests with its active key KaK_a, generation 7. It receives the instruction to switch to the pending key KpK_p, generation 8. It switches the key in RAM, replies “rotation done”, and plans to update flash memory afterwards. Power fails before the flash write. After the restart, flash still names KaK_a, so the gateway accepts KaK_a again. The publisher believed the reply, discarded KaK_a and now tags its requests with KpK_p, which the gateway rejects. The publisher is locked out until a recovery action.

A durable key change acknowledges only after the commit point:

  1. write the record that holds KpK_p, and wait until the storage confirms that the record is programmed into flash;

  2. write the commit record that names generation 8, and wait again;

  3. switch the key in RAM;

  4. reply.

A write call that returns once the data is buffered, meaning copied into memory to be written later, has not made the data durable. Only a write that flash has confirmed, or a flush that forces buffered data out and waits for it, has.

The reply should state exactly what it promises: “generation 8 committed” is a durable promise, and “request received” is not. Until the publisher receives the durable promise, it keeps KaK_a as well as KpK_p, so that it can talk to the gateway in either state. Durability §8.2 is relative to the fault model. Power loss and reset are normally on the list; a worn-out flash cell or a deliberate erase of the whole chip is not, unless the list says so.

Related entries: Authenticated encryption (AEAD) (§5.16), Secure element (§5.38), Authentication coverage (§5.41), Durability (§8.2), Commit record and selector (§8.21), Key wrapping versus ordinary encryption (§19.1.12), End-to-end security (§1.34), Rollback versus anti-rollback (§8.26), Boot validation versus arrival authentication (§8.30), Secret zeroization (§5.37). Further reading: [149, 307, 308].