Section 20.18

Tree 18: “What happens if power fails during a key change?”

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 KaK_a, of generation number g=7g = 7, where the generation number counts key replacements. It is switching to a pending key KpK_p, which becomes generation 8. It also has a recovery key §5.31 KrK_r, 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 C7C_7, the record of KaK_a, and A7A_7, a commit record §8.21 naming C7C_7. 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:

  1. write C8C_8, a record that holds KpK_p for generation 8;

  2. write A8A_8, a commit record naming C8C_8;

  3. raise the floor to 8;

  4. switch the key in RAM to KpK_p;

  5. 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 KaK_a, generation 7, by a new key KpK_p, generation 8. It writes C8C_8, a record that holds KpK_p; then A8A_8, a commit record naming C8C_8; 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:

  1. Before C8C_8 starts. Flash holds only the generation-7 records. Boot uses KaK_a.

  2. During C8C_8. Boot ignores an incomplete C8C_8. Even a complete C8C_8 is only a pending candidate until a commit record names it. Either way, boot uses KaK_a.

  3. After C8C_8, before A8A_8. C8C_8 is complete, but nothing commits it. Boot uses KaK_a and finds a pending candidate, not an active generation 8.

  4. During A8A_8. If A8A_8 is incomplete, boot uses KaK_a. If the complete record reached flash just before power failed, boot uses KpK_p and raises the floor to 8. The write routine may never have returned to its caller.

  5. After A8A_8, before or during the floor raise. A8A_8 is complete and names the complete C8C_8. Boot uses KpK_p and, if the counter still holds 7, raises the floor to 8 itself. This holds although RAM never switched before the failure.

  6. After the floor raise. Boot uses KpK_p, whether the failure came before or after the switch in RAM and the acknowledgment.

The completed write of A8A_8 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 KaK_a and KpK_p 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 KaK_a, generation 7, by KpK_p, generation 8. It keeps a floor, the lowest generation it will accept, in a monotonic counter, a stored number that can only increase. C8C_8 is the flash record that holds KpK_p, and A8A_8 is a commit record naming C8C_8: a small record whose valid presence declares the change in force.

Take a wrong order: raise the floor to 8 first, then write C8C_8, then A8A_8. Power fails after the floor reaches 8 but before C8C_8 is complete. Boot finds a floor of 8 and a usable key only for generation 7. It has two choices, and both fail:

Writing A8A_8 before C8C_8 is complete creates a commit record that names a missing key, and a boot rule that trusts A8A_8 then faces the same dilemma. Erasing C7C_7, the record of KaK_a, before C8C_8 and A8A_8 are complete leaves no usable key at all.

The safe order follows one rule:

  1. make every prerequisite durable, here C8C_8;

  2. write the single commit record, A8A_8;

  3. perform what depends on the commit: raise the floor, switch RAM, acknowledge;

  4. clean up the old state last: erase C7C_7 and every other copy of KaK_a.

This order keeps one invariant, a condition that is true in every state boot can see: whenever the floor is nn, or the newest complete commit record names generation nn, a complete key record of generation nn 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 copies are enough only together with four more things:

  1. 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;

  2. a selection rule;

  3. a repair rule that copies the good record over the damaged one only if the good one is eligible;

  4. 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 RR 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 RR, 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:

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 RR 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:

What remains in H2H_2 is the storage of H1H_1, 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 KaK_a, it is wrong in H2H_2, because the thief of KaK_a regains control. If it rejects KaK_a, it is wrong in H1H_1, because the legitimate owner is locked out. Choosing at random does not escape this. If the program accepts with probability qq, it is wrong in H2H_2 with probability qq and wrong in H1H_1 with probability 1−q1 - q, and the two cannot both be 0.

Repair therefore needs evidence from outside the lost state:

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