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. is the active key: the key whose tags the gateway accepts today. is the pending key: a replacement for that the publisher sends but the gateway does not use yet. 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 is lost. Every active key has a generation number , a count that rises by 1 at each replacement. belongs to , and is to become .
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 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 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:
Working memory (RAM). The variable that holds the key; copies that functions which handled the key left on the stack, the memory where functions keep their temporary values; processor registers; and the internal state of the tag computation. For HMAC (hash-based message authentication code), that state can hold two 32-byte values computed from the key that are enough to compute tags without the key itself.
Persistent storage. The current key record in flash memory, storage that keeps its content without power; older versions of the record that were replaced but not yet erased; and the pending replacement key.
Outside the device. The publisher’s copy and its backups; a copy compiled into the firmware image, the program file loaded onto the device, if the key was ever placed there; logs; and crash dumps, which are copies of memory saved after a fault.
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 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 without ever seeing . 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 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 .
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 before leaves the publisher’s system, so that only the gateway can decrypt it. The key for that encryption comes from one of two sources:
a key-encryption key (KEK), a key that the publisher and the gateway already share and use for nothing but carrying other keys;
a fresh key agreement, such as elliptic-curve Diffie–Hellman §19.1.5 (ECDH), in which each side combines its own private value with the other side’s public value, and both arrive at the same secret, which a relay that sees only the public values cannot compute. The publisher must obtain the gateway’s public value from a source the phone cannot alter, such as a record made when the gateway was manufactured, and the gateway must check a tag on the publisher’s public value, computed with a key the gateway already trusts. Otherwise the phone can substitute public values of its own and read everything.
The choice of key decides what a later leak exposes. If travels encrypted under the old active key , or under a key computed from , then anyone who later learns and recorded the transfer also learns . A key change §5.29 meant to shut out a suspected thief of then shuts out nobody. More generally, whoever later learns any key that can decrypt the recorded transfer also learns . Encryption that begins too late does not help either: if the phone receives in readable form and only then encrypts it for the gateway, the phone has already seen .
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 for a sensor gateway. 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.
A dedicated KEK. The publisher and the gateway share it, nobody else holds it, and it serves no other purpose, so no other operation can be made to compute or reveal values with it.
An authenticated algorithm. NIST Special Publication (SP) 800-38F specifies two such algorithms built on AES (the Advanced Encryption Standard block cipher §15.3): AES Key Wrap (KW) and AES Key Wrap with Padding (KWP). RFC 3394 and RFC 5649 publish the same two algorithms [307, 308]. Authenticated encryption with associated data §5.16 (AEAD), encryption that detects changes and can also protect extra data that stays unencrypted, is a common alternative. Encryption that only hides data is not enough: a changed wrapped key would unwrap into a wrong key, which the gateway might install.
An admitted length. KW accepts only input whose length is a multiple of 8 bytes and at least 16 bytes, and its output is 8 bytes longer than its input. KWP accepts any length of at least 1 byte: it pads the input to a multiple of 8 bytes and then adds 8 bytes.
Binding. KW and KWP take only the key to be wrapped and the KEK; they have no input for other data. A wrapped key therefore carries no statement of which device, role or generation it is for. Suppose those fields travel beside the wrapped key without protection. An attacker who recorded the wrapped key of generation 6, wrapped under the same KEK, can send it again labeled as generation 8; it unwraps correctly, and the gateway returns to a key the attacker may have learned since. The device identifier (ID), the role, the generation and the allowed use must be authenticated together with the key: placed inside the wrapped input in a fixed layout, carried as the associated data of an AEAD algorithm, or covered by the tag of a message authentication code (MAC) computed over the whole message.
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: is 32 bytes. Adding a 4-byte device ID, a 1-byte role, a 2-byte generation and a 1-byte use code gives bytes. Since , KW accepts this input and outputs 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 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: , 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:
Integrity: the record is complete, and its checksum or tag verifies.
Identity: the record belongs to this device and to the role of active key.
Commitment: a commit record §8.21, a small record whose valid presence declares a change complete and in force, names this key.
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 , generation 7. It receives the instruction to switch to the pending key , 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 , so the gateway accepts again. The publisher believed the reply, discarded and now tags its requests with , which the gateway rejects. The publisher is locked out until a recovery action.
A durable key change acknowledges only after the commit point:
write the record that holds , and wait until the storage confirms that the record is programmed into flash;
write the commit record that names generation 8, and wait again;
switch the key in RAM;
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 as well as , 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].