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 checks requests with its active key , of generation number , where the generation number counts key replacements. It is switching to a pending key , which becomes generation 8. It also has a recovery key §5.31 , which authorizes only recovery actions, and a floor: the lowest generation it will accept, held in a monotonic counter §8.28, a stored number that can only increase. The counter changes in one indivisible step. The floor is 7.
The gateway stores key records in flash memory §6.6, which keeps its content without power. This tree assumes a simple storage model. A record is written once, and a record whose write completed stays readable. A write cut short by power loss leaves a record that is either complete or detectably incomplete, as a model assumption. Each record ends with a checksum §4.32, a short value computed over the record, which is written last. A checksum detects many corruptions. Establishing that the device detects every interrupted write allowed by this model requires the storage and record-format guarantees as well. Flash already holds , the record of , and , a commit record §8.21 naming . A commit record is a small record whose valid presence declares a change complete and in force.
The key change §5.29 runs in five steps:
write , a record that holds for generation 8;
write , a commit record naming ;
raise the floor to 8;
switch the key in RAM to ;
send the publisher an acknowledgment.
At boot, the gateway considers only complete commit records at or above the trusted floor that name complete key records. It uses the newest eligible record and raises the floor to that generation if needed. If none qualifies, it enters recovery.
Short answer
Power can fail at any step, but if the new key’s record is complete before a single commit record makes it authoritative, every restart finds one of two states: generation 7 still active, perhaps with a pending candidate, or generation 8 fully active. Boot decides between them by the commit record alone. A wrong order, such as raising the floor before the new key is stored, can leave a claim of generation 8 with no usable key, which forces a choice between rollback and lockout.
Follow-up questions
Enumerate crash cuts
A crash cut is the point in a sequence of writes at which power fails; what boot finds in storage at that point decides what happens next. Here a sensor gateway replaces its active key , generation 7, by a new key , generation 8. It writes , a record that holds ; then , a commit record naming ; then it raises its floor, the lowest generation it accepts, to 8; then it switches the key in RAM and sends an acknowledgment. The storage model says an interrupted write leaves either a complete record or one that boot recognizes as incomplete. Flash still holds the generation-7 records. The cuts are:
Before starts. Flash holds only the generation-7 records. Boot uses .
During . Boot ignores an incomplete . Even a complete is only a pending candidate until a commit record names it. Either way, boot uses .
After , before . is complete, but nothing commits it. Boot uses and finds a pending candidate, not an active generation 8.
During . If is incomplete, boot uses . If the complete record reached flash just before power failed, boot uses and raises the floor to 8. The write routine may never have returned to its caller.
After , before or during the floor raise. is complete and names the complete . Boot uses and, if the counter still holds 7, raises the floor to 8 itself. This holds although RAM never switched before the failure.
After the floor raise. Boot uses , whether the failure came before or after the switch in RAM and the acknowledgment.
The completed write of is the commit point. Cuts 1 through 3 leave generation 7 active. Cut 4 can leave either generation active, depending on the durable record. Cuts 5 and 6 leave generation 8 active. A publisher that received no acknowledgment cannot tell which outcome occurred. It keeps both and and asks the gateway for its current generation, or repeats the activation request, which the gateway answers without acting twice. Boot’s raise of the floor gives the same result however often it runs, so a second power failure during it is harmless.
The list is complete only for the storage model assumed. A real device needs further cases: garbage collection §8.16, which copies live records elsewhere to reclaim space and could be interrupted mid-copy; damage to a record that was once complete; and a flash cell that wears out.
Why is write order security relevant?
Write order decides which states boot can find after a power failure, and some of those states force a choice between two security failures. Here a sensor gateway replaces its active key , generation 7, by , generation 8. It keeps a floor, the lowest generation it will accept, in a monotonic counter, a stored number that can only increase. is the flash record that holds , and is a commit record naming : a small record whose valid presence declares the change in force.
Take a wrong order: raise the floor to 8 first, then write , then . Power fails after the floor reaches 8 but before is complete. Boot finds a floor of 8 and a usable key only for generation 7. It has two choices, and both fail:
Use . Since , this breaks rollback protection §8.26, the rule that the gateway never returns below its floor. If was replaced because it leaked, the thief regains full authority.
Accept nothing. The gateway rejects every request, and its owner is locked out until a recovery action with the recovery key, or until someone services the device by hand.
Writing before is complete creates a commit record that names a missing key, and a boot rule that trusts then faces the same dilemma. Erasing , the record of , before and are complete leaves no usable key at all.
The safe order follows one rule:
make every prerequisite durable, here ;
write the single commit record, ;
perform what depends on the commit: raise the floor, switch RAM, acknowledge;
clean up the old state last: erase and every other copy of .
This order keeps one invariant, a condition that is true in every state boot can see: whenever the floor is , or the newest complete commit record names generation , a complete key record of generation exists. Storage drivers may buffer or reorder writes, so each step must wait until the previous write is confirmed in flash. Choosing whichever old key happens to survive is not a repair: it restores service by ignoring the floor.
Are two copies sufficient?
No: two copies protect against losing one copy, but they do not tell the gateway which copy has authority. Here a sensor gateway keeps the record of its active key, the key that authenticates its owner’s requests, in flash memory. A generation number counts key replacements. The floor, held in a monotonic counter that can only increase, is the lowest generation the gateway accepts. A checksum is a short value computed over a record that reveals accidental damage.
Two copies can be arranged in two ways, and each arrangement fails differently:
Two slots, old and new. Slot A holds generation 7 and slot B holds generation 8, and both checksums are correct. Both copies are intact, yet they disagree. A rule must choose: the highest generation that is complete, committed and at least the floor. If slot B is damaged and the floor is 8, slot A’s generation 7 is intact and still not eligible, since . No eligible key remains, and the gateway must enter recovery instead of using slot A.
Two identical copies. A write cut short by power loss damages at most the copy being written, so writing the copies one after the other keeps one copy intact. Both copies are lost, though, if they share an erase block, the smallest unit that flash can erase, because erasing one erases both. And a software bug that writes a wrong value writes it into both.
Two copies are enough only together with four more things:
a fault model §1.30, the written list of failures the design must survive, stating that one failure damages at most one copy; that holds only if the copies sit in different erase blocks and are never written at the same time;
a selection rule;
a repair rule that copies the good record over the damaged one only if the good one is eligible;
a floor stored apart from both copies, so that damage to a copy cannot lower it.
What if all history is erased?
If all history is erased and the gateway is then set up again from the same secrets, an old recorded request can become valid again. History here means everything the gateway stored about its past: its key records, its generation number, its floor (the lowest generation it accepts) and the counter value of the last request it accepted. Every request carries a counter value that rises by 1 from one request to the next, and the gateway rejects any request whose counter value is not higher than the last one accepted. That rule defends against a replay attack §1.26, the resending of a recorded genuine message. A complete erase, such as erasing the whole chip through its debug port (the connector used to program the chip) or a factory reset, removes all of this history.
Example. The gateway derives its first active key from a root secret and its device identifier (ID) with a key derivation function §5.20 (KDF), an algorithm §14.1 that computes keys from a secret and labels. It starts its count of accepted requests at 0. An attacker records request number 1, “install policy P”, tagged with that key. Months later, the gateway is erased and set up again with the same , the same ID and a counter at 0. It derives the same first key, expects request number 1, and accepts the recorded message. The attacker has reinstalled the old policy P.
After an erase, the new setup must therefore start a genuinely new lifecycle:
new key material from a fresh random source; or
an epoch number, a count of complete erasures kept in memory that the erase cannot clear, such as one-time programmable fuses, bits that can be set once and never cleared. The epoch is mixed into every derived key and covered by every tag.
A “new” context computed from unchanged inputs, such as the same root and the same ID, reproduces the same keys and protects nothing. Ordinary power failure and complete erasure are different events. The first keeps history, and the gateway must recover it. The second is a new setup, and its procedure must state which former authority survives it, for example whether the recovery key and the root remain valid.
Can every lost state be repaired automatically?
No: when two different histories leave the same evidence on the device, no procedure that runs on the device alone can respond correctly to both. Here a sensor gateway checks its owner’s requests with a key, and revoking a key means storing a rule that the gateway no longer accepts it. Take two histories:
: the key was never revoked, and the gateway still holds as its active key.
: was revoked because it leaked. The gateway wrote the revocation, the replacement key and the raised floor as new records after the ones it already had. Later exactly those new records were lost, for example through damage to the area of storage that held them.
What remains in is the storage of , bit for bit. A deterministic program, one that always gives the same output for the same input, must then make the same decision in both. If it accepts , it is wrong in , because the thief of regains control. If it rejects , it is wrong in , because the legitimate owner is locked out. Choosing at random does not escape this. If the program accepts with probability , it is wrong in with probability and wrong in with probability , and the two cannot both be 0.
Repair therefore needs evidence from outside the lost state:
an authenticated statement of the current generation from a party the gateway trusts and the loss did not reach, such as the publisher using the recovery key;
a record the loss could not reach, such as a floor kept in a monotonic counter, a stored number that can only increase, in a separate chip;
a defined manual procedure, such as an operator installing keys over a wired connection.
Until such evidence arrives, the safe choice is to refuse: reject every request that depends on the uncertain fact, and accept only the recovery path.
Related entries: Crash consistency (§8.4), Torn write (§8.19), Write ordering (§8.20), Commit record and selector (§8.21), Rollback versus anti-rollback (§8.26), Monotonic counter (§8.28), Lifecycle reset and replay across complete reinitialization (§19.3.7), Recovery and anti-rollback can conflict (§17.11).