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.
The stack, where a function keeps its local variables: the bytes stay after the function returns, until a later call overwrites them.
Registers and spills: a register keeps its value until reused, and when a function needs more values than there are registers, the compiler stores some in hidden stack slots called spills. The source code has no name for a spill slot, so it cannot clear one.
The heap:
freedoes not clear a block; the allocator keeps the block in a list or cache of free blocks and hands its old bytes to a latermalloc.realloc, which resizes a block, may copy it elsewhere and free the old block without clearing it.Derived state: the 176-byte key schedule of AES-128 (Advanced Encryption Standard with a 128-bit key) begins with the 16-byte key itself, and an HMAC §5.8 (hash-based message authentication code) context holds the key, or values computed from it that let its holder compute tags without the key.
Output and storage: logs, debug prints, crash dumps, swap, and flash memory §6.6 or disks, which keep old versions after a logical overwrite.
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.
Keep few copies: pass secrets by pointer, and give each buffer a known lifetime.
Clear each copy right after its last use, on every path out of the function, error paths included. Clear heap buffers before
free, and neverreallocthem.Clear derived state, such as key schedules and HMAC contexts, with the library’s own clearing function.
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.Keep secrets out of logs, and lock their pages in memory, with
mlockon Linux, so that they are never written to swap.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: bytes in all. A plain memset(k, 0, 16) at the end of the function clears at most 16 of them, so 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 be a secret, and let be the set of memory and storage locations whose contents would let an observer either recover or perform an operation that needs , such as computing an HMAC tag under . Zeroization of at time is complete when, at every time after , no location in still holds such contents.
The protection this gives rests on three assumptions.
Complete inventory: lists every copy, including copies the compiler makes in registers and spill slots, copies inside libraries, and copies in output channels.
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.
No earlier read: no observer read any location in before time .
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.
Stack frame, the memory a function uses for its local variables: its bytes stay after the function returns, until a later call overwrites them. Clear a local secret with an operation the compiler cannot remove, such as
memset_explicitorexplicit_bzero.Register spill, a value the compiler stored in a hidden stack slot because it ran out of registers: the source code has no name for the slot, so it cannot clear it. Use a compiler option §12.3 that clears used registers on return, overwrite the stack area after the sensitive function returns, or write that function in assembly language.
Log: a log line or debug print that once contained the secret keeps it in a file, on a serial console or in a crash report, possibly for years. No memory clearing removes it, so secrets must never be logged.
Allocator cache, the list of freed heap blocks that the memory allocator hands out again:
freedoes not clear a block, so its old bytes go to the next allocation. Clear a secret buffer before freeing it, and do notreallocit, becausereallocmay move the block and free the old one uncleared.
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].