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. is the active key, the key whose tags the gateway accepts today; it belongs to generation number , where the generation number counts key replacements. is the pending key, the replacement the publisher wants to make active as generation 8. 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. The gateway also keeps a floor, the lowest generation it will accept, in a monotonic counter §8.28, a stored number that can only increase.
The publisher replaces by in three steps, which this tree keeps apart. Staging: the publisher sends , encrypted so that the phone cannot read it, as candidate number 101 for generation 8, and the gateway checks it and stores it without using it. Activation: the publisher confirms candidate 101, and in one write that survives loss of power the gateway makes the active key. Revocation: the gateway stops accepting for new requests. Every request carries a request identifier, a number the publisher chooses so that a second copy of the same request can be recognized. The phone may deliver a request twice, or late. The protocol is invented for teaching; no standard defines these commands.
Short answer
The gateway changes keys in separate, checked steps. It stages the new key , checking and durably storing it while the old key still works. It activates in one durable write, after the publisher proves that it holds the same . It then revokes : a stored rule, kept across restarts and protected against rollback, says that authorizes no new requests. Each authenticated request names the device, generation, candidate and operation. A durable record of the request and its result prevents a duplicate effect. A cancellation §9.29 for candidate 101 leaves candidate 102 in place.
Follow-up questions
Why stage before activation?
Staging comes first so that every step that can fail runs while the old key still works. To stage a key is to deliver, check and store it without using it. To activate it is to make it the key whose tags the gateway accepts. Here a sensor gateway accepts its publisher’s requests only with a tag of a message authentication code (MAC) computed with the active key , and the publisher is replacing by a new key .
If the gateway switched to the moment arrived, any failure after the switch could split the two sides. A damaged transfer gives the gateway a different from the publisher’s; a lost reply leaves the publisher unsure which key is in force. Either way, the publisher’s next request may carry a tag the gateway rejects, and no accepted request remains with which to repair the split. Staging lets the gateway do four things first:
Validate. Decrypt , check its integrity §1.9 and length, and check that it is meant for this gateway and for generation 8, one more than the active generation 7.
Store durably. Write the candidate to flash memory §6.6, which keeps its content without power, and confirm that the write completed.
Prove possession. The gateway sends a challenge: a nonce §5.18, a random value used only once, such as 16 fresh random bytes. The publisher returns a tag computed with over the purpose of proving possession, the challenge, the gateway’s identifier (ID), generation 8 and candidate number 101. A correct tag shows that the publisher holds the same that the gateway stored. This step is a proof of possession §5.30. It does not show that anything has been committed.
Prepare. Reserve the memory and build the internal state that using needs, so that no step after the commit can fail for lack of resources.
Activation is then a single durable write, the commit, after which every restart uses . The reply can still be lost after the commit. The gateway can report the durable result again without repeating the change.
Who may authorize each operation?
Each operation needs a written rule that names the key allowed to authorize it, the states in which it is allowed, and the fields its tag must cover. A tag of a message authentication code (MAC) shows only that its maker held a particular key. Authentication §1.6 answers “which key made this tag?”; authorization §1.7 answers “may the holder of that key make this change now?”. Here a sensor gateway changes keys: is its active key, of generation 7; is the candidate key for generation 8, staged as candidate number 101; and is the recovery key. One workable rule is:
Stage candidate 101. A tag under , the authority in force, over the device identifier (ID), generation 8, candidate number 101 and the encrypted . Allowed only while generation 7 is active and no other candidate is pending.
Activate candidate 101. A tag under over an explicit activation command, the request ID, a fresh challenge from the gateway, the device ID, generation 8 and candidate number 101. A reply to the earlier proof-of-possession challenge cannot authorize activation. authorized the candidate when it was staged; this tag proves possession of exactly that candidate and states the publisher’s intent to switch.
Cancel candidate 101. A tag under , allowed only while candidate 101 is pending.
Revoke for new requests. No separate request: the durable write that makes active also raises the floor, the lowest generation the gateway accepts, to 8.
Recover. A tag under may install a new active key, but only in a recovery state: for example after boot finds no usable active key, or after someone presses a button on the gateway. may not authorize ordinary requests or policy changes.
The tag must cover every field the rule checks. The set of bytes a tag covers is its authentication coverage §5.41. A correct tag under that names candidate 102, or another gateway, fails the rule for candidate 101. If the candidate number lay outside the tag, anyone relaying the message could change it without detection.
Does rotation equal revocation?
No: rotation gives the gateway a new key, revocation takes authority away from the old key, and only revocation shuts out someone who stole the old key. Key rotation §5.29 replaces the active key of a sensor gateway, the key whose tags authorize its owner’s requests, by a new key for future use. still exists afterwards: in the publisher’s records, in backups, and in an attacker’s hands if it leaked. If the gateway keeps accepting , for example to stay compatible with old tools, every holder of keeps full authority.
Key revocation §5.32 is an enforced rule that a key no longer authorizes a named set of uses. It has three requirements:
Enforced by every verifier. Every device that accepted must reject it, including a device that was switched off when the revocation was sent.
Persistent. The rule is stored in memory that keeps its content without power, so that a restart does not forget it.
Proof against rollback. Replaying the old request that installed , or restoring an old copy of the storage, must not bring back. A floor held in a monotonic counter, a stored number that can only increase, enforces this: with the floor at 8, a key of generation 7 is rejected, because .
Revocation can be partial, and then both halves must be named. The gateway may refuse for new requests and still keep to verify old log entries tagged with it. With a message authentication code (MAC), the key that verifies a tag also creates tags, so whoever keeps for verification can also forge entries that look old. A digital signature §5.14 separates the two powers: its public key verifies signatures but cannot make them.
How does a retry work?
A retry works when the gateway recognizes a repeated request and returns the result it recorded the first time, without acting again. Example: a sensor gateway receives request 41 from its publisher, “activate candidate 101 as generation 8”, which means: make the staged key the active key. The gateway commits the change, but its reply is lost. After 5 seconds without a reply, the publisher sends request 41 again, with identical bytes. If the gateway executed it a second time, it would find no pending candidate and report failure. The publisher would conclude that the key change failed and keep using the old key , which the gateway now rejects.
The repair is to make the request idempotent §8.31: applying it twice has the same effect as applying it once. In the same durable write that activates generation 8, the gateway records the request identifier 41, a hash of the request’s bytes and the result “generation 8 active”. The hash is a short fingerprint of the bytes, computed by a cryptographic hash function §5.1. When request 41 arrives again with the same hash, the gateway returns the recorded result and changes nothing. The gateway looks the identifier up before it checks that the request answers its current challenge, because the second copy repeats the first copy’s challenge, which the gateway no longer expects. Four rules keep this safe:
Record with the effect. If the result were written after the effect, a power failure between the two writes would leave the effect without its record, and the retry would act again.
Same identifier, different bytes, is a conflict. The gateway rejects such a request and never treats it as the same one.
A retry earns no new effect. If generation 9 is active by the time an old copy of request 41 arrives, the gateway returns the recorded result or “superseded”. It never activates generation 8 again.
A transport acknowledgment is not a result. The radio link’s report that bytes arrived says nothing about whether the gateway’s state changed.
Delivery that repeats until a reply arrives, combined with recognition of repeats, gives an exactly-once effect §8.32: the change happens once, however many copies arrive. Returning a recorded result changes nothing, so an attacker who replays request 41 gains only the reply. The table of recorded results has a fixed size. For an identifier that has dropped out of the table, the gateway compares the request with its state: if generation 8 or a later one is active, it answers “already done or superseded” and does not act.
What must a cancellation do?
A cancellation must name exactly what it cancels, take effect only while that thing can still be canceled, and durably record what it did. Here a sensor gateway is replacing its active key , generation 7, and a candidate is a staged replacement key, stored but not yet used, identified by a candidate number.
A late message shows why the name matters. The publisher stages candidate 101 for generation 8 and then cancels it. The phone, unsure whether the cancellation arrived, also sends a second copy, which is delayed. The gateway cancels 101, and the publisher then stages a new candidate 102 for generation 8. Now the delayed copy arrives. If it says only “cancel the pending candidate”, it erases candidate 102, and the publisher wrongly believes that 102 is waiting. If it says “cancel candidate 101 for generation 8 on gateway 5”, the gateway sees that 101 is no longer pending, changes nothing, and repeats its recorded answer, “candidate 101 canceled”.
A correct cancellation does four things:
Names its target. The device identifier (ID), the role of the key (here, active key), the generation and the candidate number, all covered by the tag of a message authentication code (MAC) made with the key allowed to cancel, here .
Checks the state. Only a pending candidate can be canceled. Once candidate 101 is active, the floor, the lowest generation the gateway accepts, stands at 8, is revoked, and the publisher may already use the new key. A cancellation cannot quietly undo that history, so the gateway refuses it. Going back needs a compensating transition: a new forward change, such as a key change to generation 9, authorized and recorded like any other.
Becomes durable before the reply. The gateway marks the candidate canceled in flash memory, which keeps its content without power, before it replies, and then erases every copy of the candidate key, so that the candidate cannot reappear after a restart.
Records its outcome. A repeated copy of the same cancellation then gets the same answer and causes no second effect.
Related entries: Key rotation (§5.29), Proof of possession (§5.30), Key revocation (§5.32), Key confirmation versus application confirmation (§5.42), Exactly-once effect (§8.32), Staging versus commit versus activation (§8.22), Idempotency (§8.31), Prepare before irreversible commit and allocation-free fallback (§19.3.8), Tree 20: “When can a key be retired or destroyed?” (§20.20). Further reading: [147].