Section 5.37

Secret zeroization

Definition

Secret zeroization is the deliberate overwriting of a secret, such as a key, a password or a value computed from them, with zeros or another fixed pattern as soon as the program no longer needs it, so that a later read of that memory finds nothing useful. It must reach every place that holds the secret: the original buffer, every copy, and every derived value that lets its holder do what the secret allows. Cryptographic module standards, such as FIPS 140-3, use the word for destroying keys [338].

The idea

A secret left in memory after its last use waits for any later bug or attacker that can read memory. In 2014 the Heartbleed bug [339] in the OpenSSL cryptographic library let a remote attacker read up to 64 KiB of a server’s memory per request, and researchers recovered servers’ private keys this way. Crash dumps and swap files, to which an operating system copies memory, carry secrets to disk. A cold boot attack [340] uses the fact that DRAM (dynamic random-access memory), a computer’s main memory, keeps most of its contents for seconds to minutes after power is cut, and longer when cooled, so an attacker who holds the machine can restart it, or move its memory to another machine, and read what was there. Erasing a secret as soon as its work is done shortens the time in which any of these routes can find it.

Shredding a letter does not destroy its photocopies. Zeroization is only as good as its list of copies, and two things make that list hard to write: programs copy secrets silently, and a compiler may delete the final overwrite because no later instruction reads the zeroed bytes.

How it works

Zeroization has two parts: finding every copy, and overwriting each one in a way that really takes place.

Where copies remain.

Overwriting so that it happens. A call memset(key, 0, 16) just before a buffer is freed or goes out of scope is a dead store: nothing reads those bytes again. The C standard lets a compiler remove any operation whose absence the program cannot observe, and compilers do remove such stores. Use an operation the compiler must keep: memset_explicit, standard C since the 2023 revision [309]; explicit_bzero in glibc, the common C library on Linux; SecureZeroMemory on Windows; a cryptographic library’s own function, such as sodium_memzero in libsodium; or a loop that writes through a pointer declared with the volatile qualifier §4.34, which forbids the compiler to remove or merge those writes.

Order of work.

  1. Keep few copies: pass secrets by pointer, and give each buffer a known lifetime.

  2. Clear each copy right after its last use, on every path out of the function, error paths included. Clear heap buffers before free, and never realloc them.

  3. Clear derived state, such as key schedules and HMAC contexts, with the library’s own clearing function.

  4. For registers and spill slots, use the toolchain §12.2: some compilers offer the option -fzero-call-used-regs, which clears the registers a function used when it returns, and a helper that zeroes a large local array §14.5, called after the sensitive function returns, overwrites the stack area that function used.

  5. Keep secrets out of logs, and lock their pages in memory, with mlock on Linux, so that they are never written to swap.

  6. Check the result: stop the program in a debugger after the clearing point and search memory for the secret’s bytes.

Persistent storage. Programming flash memory can only change bits from 1 to 0, and returning them to 1 needs an erase of a whole page. File systems and flash controllers therefore write new data elsewhere and leave the old copy until its page is erased, and wear leveling, which spreads writes over all pages, moves data where the program cannot see it. NIST Special Publication 800-88, the United States guidance on media sanitization, names three levels, clear, purge and destroy, and warns that overwriting cannot reach every old copy on such flash storage. It also describes cryptographic erase: when the stored data is encrypted, destroying the key makes the data unreadable, which reduces the problem to zeroizing one key.

Worked example

A function receives a 16-byte AES-128 key in a local array k and expands it into a 176-byte key schedule inside a context structure ctx. The bytes that hold key material are the 16 bytes of k and the 176 bytes of the schedule, whose first 16 bytes are a copy of the key and whose other 160 bytes are computed from it: 16+176=19216 + 176 = 192 bytes in all. A plain memset(k, 0, 16) at the end of the function clears at most 16 of them, so 192−16=176192 - 16 = 176 bytes still give the key to anyone who reads them. Worse, nothing reads k after that memset, so the compiler may delete it and leave all 192 bytes.

The repair clears k with memset_explicit(k, 0, 16) and ctx with the library’s context-clearing function, which together cover all 192 bytes that the source code can name. Registers and spill slots are outside that count and need the toolchain measures above. The extra copies matter in practice: in the cold boot research published in 2008, the redundancy of AES key schedules let the researchers correct bits that had decayed in memory and recover complete keys.

Stated precisely

Let SS be a secret, and let R(S)R(S) be the set of memory and storage locations whose contents would let an observer either recover SS or perform an operation that needs SS, such as computing an HMAC tag under SS. Zeroization of SS at time tt is complete when, at every time after tt, no location in R(S)R(S) still holds such contents.

The protection this gives rests on three assumptions.

  1. Complete inventory: R(S)R(S) lists every copy, including copies the compiler makes in registers and spill slots, copies inside libraries, and copies in output channels.

  2. Effective writes: the compiled program really executes each clearing write, and the write changes the physical storage that held the copy, rather than a new location while the old one survives, as on flash storage.

  3. No earlier read: no observer read any location in R(S)R(S) before time tt.

The third assumption is outside the program’s control. Zeroization limits future exposure; it cannot recall a copy that has already left.

Limits and common mistakes

Zeroization is not revocation. If an attacker copied a key before it was cleared, clearing the device’s copy does nothing to the attacker’s copy; the key must be revoked and replaced, as the key revocation §5.32 entry describes.

A memset that appears in the source code may be missing from the program. A link-time optimization §12.10 can see into a zeroing function written in another file and remove its writes, unless the function is one the compiler is forbidden to optimize away.

Forward secrecy §5.33 depends on zeroization. It promises that a later theft of long-term keys does not expose earlier sessions, which assumes that the short-lived keys of those sessions were erased. A copy kept in a debug log breaks the promise.

A cleared key buffer is not an unusable one. A context whose key was zeroed still works, now with an all-zero key that everyone knows. Mark the context as unusable when it is cleared, so that every later use fails.

Zeroization after use does not protect a secret while it is in use; an attacker who reads memory at that moment finds it.

Questions answered

Does a copy remain in a stack frame, register spill, log, or allocator cache?

Often yes: clearing the original buffer leaves each of these copies in place unless the program clears it separately, and source code cannot reach some of them at all.

For a real program, list every location that held the secret or a value derived from it, clear each one with an operation the compiler must keep, and confirm by searching memory for the secret’s bytes after the clearing point.

Related entries: Forward secrecy (§5.33), Root key and root seed (§5.25), Compiler optimization (§3.39), Key revocation (§5.32), Volatile qualifier (§4.34), Static, stack, and heap storage (§4.11). Further reading: [150, 309, 338, 339, 340].