Section 16.7

Credential-preserving programming

Definition

Credential-preserving programming is a procedure for writing new firmware or settings into a device’s non-volatile memory that leaves the device’s credentials intact, together with the state tied to them: its secret keys, its identity data and its freshness records, such as version high-water marks §8.27 and counters. It has two parts. The first chooses operations whose erase and write regions provably avoid every protected region, even if power fails midway. The second checks afterwards, without revealing any secret byte, that the protected state is unchanged.

The idea

A maker ships devices that each hold a key installed in the factory, and later needs to fix a bug in the firmware. If the update erases the key, every device must be provisioned again, which may be impossible in the field, and a careless re-provisioning can also reset the records that make old messages unacceptable. So the update must replace the code and keep the key. A programming tool’s report that the new image was written and verified does not show this: the tool checks the bytes it wrote, not the bytes it should have left alone.

The idea has two halves. First, know which memory every operation can change, in the units the hardware erases, and keep the protected regions out of all of it. Flash memory §6.6 is erased in whole pages, so a key that shares a page with code dies with the code. Second, check the result without opening the safe: ask the device to prove that it still holds the same key by computing with it, instead of reading the key out. Renovating a room around a safe bolted to the wall is a fair picture. The builders must know which walls they will knock down, and afterwards the owner tests the safe by opening it with the known combination, not by drilling it to look inside. The picture stops at one point: in a device, software performs the test, so the test is only as trustworthy as the software that answers it.

How it works

The procedure has five steps.

  1. List the protected state. Write down the address range of every protected item: the credential, the identity data, each freshness record such as a version high-water mark (the highest firmware version accepted so far) or a monotonic counter §8.28 (a counter that can only increase), and settings in UICR (User Information Configuration Registers) §16.1 on Nordic chips that keep them there. Call the set of these addresses CC.

  2. List the effects of every operation. For each step of the programming procedure, find its erase set EE, every address the step can erase, and its write set WW, every address it can write. Erase sets come in the hardware’s units: a whole flash page, the whole UICR, or the whole chip. Programming tools’ erase options differ in exactly this. An option that erases only the pages a new image covers has a small erase set; a chip-erase option has the whole chip as its erase set.

  3. Require that the sets do not meet. No step may erase or write an address in CC, unless the step is an authorized replacement of that item. Include what happens when power fails partway: an update that copies images through a scratch page must keep the scratch page outside CC too.

  4. Choose the route. On a chip whose readout protection §16.5 locks the debugger out, the debugger can usually get in only by erasing everything. A credential-preserving update of such a chip must therefore go through firmware already on the device, such as a boot loader §6.30 that accepts only updates signed by the maker.

  5. Verify without exposing the credential, as described next.

Reading the credential back with a debugger would expose it, and on a protected chip the debugger cannot read it anyway. Instead the device computes with the credential and reports only the result.

A CRC §4.32 (cyclic redundancy check), a common checksum, must not serve as the check value of a secret. Its output is a linear function of the input bits, up to a fixed constant, so it reveals part of the secret, or all of it when the secret is 4 bytes long.

Worked example

A chip has 1 MiB of flash in 256 pages of 4096 bytes; page pp covers the addresses 4096p4096p to 4096p+40954096p + 4095. The 16-byte device key sits at the start of the last page, page 255, which begins at 255×4096=1,044,480255 \times 4096 = 1{,}044{,}480, written 0xFF000 in hexadecimal. So CC is the 16 addresses 0xFF000 to 0xFF00F.

The new firmware image is 172,800 bytes long and starts at address 0. It spans 172,800/4096=42.1875172{,}800 / 4096 = 42.1875 pages, so it touches pages 0 to 42, which is 43 pages. Erasing the pages the image covers has the erase set 0x00000 to 0x2AFFF, because 43×4096=176,12843 \times 4096 = 176{,}128, which is 0x2B000. That set does not reach 0xFF000: the key survives.

Three ways to lose the key anyway:

Now the check. Let the key be the 16 bytes 0x00, 0x01, and so on up to 0x0f, a test value that no real device should use, and let the label be the 9 ASCII bytes of the text key check. The first 8 bytes of the HMAC computed with the hash function SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) are 1513e364e5928a36 before the update. After the update the device reports the same 8 bytes, so the key is unchanged. Had the update erased the key to 16 bytes of 0xff, the device would report 2ea23a2af8ff97f8 instead.

By contrast, take a 4-byte secret 0xDEADBEEF. Its CRC-32, the common 32-bit CRC, is 0x7C9CA35A. On 4-byte inputs CRC-32 is one-to-one: each of the 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296 inputs has its own output. Publishing the CRC-32 therefore publishes the secret, and linear algebra recovers it directly. For a 16-byte key, the CRC-32 cuts the candidates from 21282^{128} to 2128/232=2962^{128} / 2^{32} = 2^{96}.

Stated precisely

Let CC be the set of addresses that hold protected state, and let the procedure consist of steps s1,…,sks_1, \dots, s_k. Let E(si)E(s_i) and W(si)W(s_i) be the sets of addresses that step sis_i can erase and write, counted in the hardware’s erase units and including every outcome of a power failure during the step. The preservation premise is (E(si)∪W(si))∩C=⌀for every i,\bigl(E(s_i) \cup W(s_i)\bigr) \cap C = \varnothing \quad \text{for every } i, where ∪\cup is set union, ∩\cap is set intersection and ⌀\varnothing is the empty set. With page erase, E(si)E(s_i) is the union of all pages that contain an address the step writes, not only those addresses.

The check after the update: let K′K' be the device’s key after the update, KK the verifier’s copy, NN a fresh nonce and ℓ\ell a fixed label. Write a‖ba \mathbin{\Vert} b for the byte string aa followed by the byte string bb. The device returns t=HMAC(K′,ℓ‖N)t = \mathrm{HMAC}(K', \ell \mathbin{\Vert} N), and the verifier accepts when t=HMAC(K,ℓ‖N)t = \mathrm{HMAC}(K, \ell \mathbin{\Vert} N).

The check rests on three assumptions.

  1. HMAC behaves as a pseudorandom function §15.1, a keyed function whose outputs look random to anyone without the key. Then, if K′≠KK' \ne K, the two values agree only with negligible probability, about 2−2562^{-256} for a full 32-byte tag such as HMAC with SHA-256 produces.

  2. The firmware that answers is the authentic firmware. Firmware that holds the key can answer correctly wherever it keeps the key.

  3. The nonce is new, so an answer recorded earlier cannot be replayed.

The check shows that the device can still compute with KK. It does not show that KK sits at its old address, or that no other copy exists.

Limits and common mistakes

Preserving the credential also hands it to the new firmware: whatever code is installed can use the key. A credential-preserving route must restrict which firmware can use that key. For example, the bootloader can require a signature from an authorized signer before starting a replacement image. If the key survives and arbitrary replacement code can run with access to it, that code can use the key. This is why the debug route into a locked chip is destructive by design.

A programming tool’s success report covers the bytes it wrote and verified, not the bytes it was meant to leave alone. Reading back the new code says nothing about CC.

A preserved credential is useful only if the new firmware reads its storage format. A change of format needs a planned migration that is itself safe against power failure.

An equality check reveals equality. A fixed key check value lets anyone who sees it recognize the key later, so show it only to parties that need it.

The premise must hold for every state that an interrupted procedure can leave, not only for the completed procedure; the entry on provisioning power-failure safety §16.8 treats those states.

Keeping a credential, replacing it with an authorized new one, and starting a new device lifecycle are three different outcomes. A procedure must state which one it promises.

Questions answered

How is preservation established without exposing the credential bytes?

Preservation is established in two parts, neither of which reads the credential out. The first part is analysis: list every address that holds protected state (the key, identity data, version high-water marks, counters, protected settings) and show that no step of the programming procedure can erase or write any of them. Count erases in whole pages, the unit flash memory erases, and include every state a power failure can leave. The second part is a check on the device: after the update, the device computes with its key and reports only the result. A verifier that holds the key sends a fresh nonce and compares the device’s HMAC (hash-based message authentication code) of a fixed label and the nonce with its own. Alternatively, the device reports a key check value, the HMAC of a fixed label, before and after the update, and the two must be equal. Records that are not secret, such as the version high-water mark, are read back and compared directly.

Two shortcuts do not establish preservation. A programming tool’s report that the new image verified checks only the bytes that were written. And a CRC (cyclic redundancy check) of the key reveals part or all of the key, so it must not serve as the check value.

Related entries: NVMC (Non-Volatile Memory Controller) (§16.3), ERASEALL and destructive debug recovery (§16.6), Key provisioning (§5.28), Crash consistency (§8.4), Version high-water mark (§8.27), HMAC (§5.8), Checksum versus CRC (§4.32), Provisioning power-failure safety (§16.8), Secure boot (§6.31). Further reading: [56].