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.
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 .
List the effects of every operation. For each step of the programming procedure, find its erase set , every address the step can erase, and its write set , 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.
Require that the sets do not meet. No step may erase or write an address in , 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 too.
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.
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.
Challenge and response. A verifier that holds the key, such as the maker’s server, sends a fresh random nonce §5.18, a number used only once. The device returns the HMAC §5.8 (hash-based message authentication code) of a fixed purpose label followed by the nonce, computed with its key. The verifier computes the same value with its own copy and compares. A match shows that the device still holds the same key. Because the nonce is new, an answer recorded before the update cannot be replayed.
Key check value. Before and after the update, the device computes the HMAC of a fixed label under its key and reports the result, or only its first bytes; equal values show that the key did not change. Payment systems use the same idea with a block cipher §15.3, an encryption algorithm §14.1 for fixed-size blocks, by encrypting a block of zero bytes. A key check value is stable, so it acts as a fingerprint: it reveals nothing practical about a random key, but anyone who sees it can recognize the same key later.
Records that are not secret, such as the version high-water mark or a counter, are read back directly and compared with their values before the update.
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 covers the addresses to . The 16-byte device key sits at the start of the last page, page 255, which begins at , written 0xFF000 in hexadecimal. So is the 16 addresses 0xFF000 to 0xFF00F.
The new firmware image is 172,800 bytes long and starts at address 0. It spans 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 , which is 0x2B000. That set does not reach 0xFF000: the key survives.
Three ways to lose the key anyway:
A chip-erase option. Its erase set is every page and UICR, which contains .
An image file that also holds data for page 255, for example default settings that the build placed in the last page. Erasing the pages the image covers now includes page 255.
A layout in which the key shares a page with code. If the key sat at
0x2A800, which is 174,080 and lies in page 42 (172,032 to 176,127), the update would erase it, although the image’s last byte is at 172,799 and no byte of the image is written there.
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 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 to .
Stated precisely
Let be the set of addresses that hold protected state, and let the procedure consist of steps . Let and be the sets of addresses that step 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 where is set union, is set intersection and is the empty set. With page erase, is the union of all pages that contain an address the step writes, not only those addresses.
The check after the update: let be the device’s key after the update, the verifier’s copy, a fresh nonce and a fixed label. Write for the byte string followed by the byte string . The device returns , and the verifier accepts when .
The check rests on three assumptions.
HMAC behaves as a pseudorandom function §15.1, a keyed function whose outputs look random to anyone without the key. Then, if , the two values agree only with negligible probability, about for a full 32-byte tag such as HMAC with SHA-256 produces.
The firmware that answers is the authentic firmware. Firmware that holds the key can answer correctly wherever it keeps the key.
The nonce is new, so an answer recorded earlier cannot be replayed.
The check shows that the device can still compute with . It does not show that 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 .
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].