amnesia.rs

crates/veilvoice-crypto/src/amnesia.rs

veilvoice-crypto · 472 lines · read the source here · or on GitHub

Amnesic secret storage: page-locked, zeroized, and never printed.

No unsafe, even here

Locking pages out of the swap file is a raw syscall, VirtualLock on Windows and mlock on Unix, and it is the one place a project like this usually has to reach for unsafe. It does not here: the region crate exposes a safe, cross-platform wrapper. VeilVoice therefore contains no unsafe code at all, and every crate keeps #![forbid(unsafe_code)].

What locking does and does not buy

Locking keeps key material out of the page file, so a secret cannot be recovered later by reading swap off the disk. It does not protect against an attacker who can already read this process's memory, and it does not survive hibernation, which writes RAM to disk wholesale. Locking can also fail outright, because unprivileged Linux users get a small RLIMIT_MEMLOCK budget, so it is best-effort hardening and never a precondition. Secret::is_locked reports what actually happened, so the UI can tell the user the truth rather than imply a guarantee that was not obtained.

Zeroization, by contrast, always happens.

In a browser, nothing is locked at all

Compiled to wasm32-unknown-unknown, this type still allocates, still wipes on drop, still compares in constant time and still refuses to print itself. It does not lock anything, because there is nothing there to lock: a WebAssembly module has a linear memory the host owns, there is no mlock and no page file of its own, and what the browser's process does with that memory is the browser's business. region is therefore not compiled for that target at all, and Secret::is_locked answers false, which is the same answer a Linux machine with no RLIMIT_MEMLOCK budget gives and has always given.

That is a real reduction and it is written down rather than left to a cfg. Roadmap item 177 compiles the window to WebAssembly so the site can run the real interface, and that build is a demonstration: it holds no recording anybody cares about and no passphrase worth keeping out of a page file. A browser is not somewhere to open a vault, and the reason it is not is this paragraph. docs/WHITEPAPER.md says the same thing where a reader will find it.

Why each secret owns whole pages

Locking has page granularity, not byte granularity. If two secrets share a 4 KiB page, both lock it, and the first one dropped unlocks the page out from under the second, which is still live and now swappable.

Each Secret therefore over-allocates and locks a page-aligned, page-sized span lying entirely within its own allocation. No other allocation can occupy those bytes, so none can be inside those pages: lock and unlock are exact, and locking a secret never drags unrelated data into physical memory alongside it.

Why the lock is not held by an RAII guard

region::lock hands back a guard that unlocks on drop, but its destructor panics if unlocking fails, and unlocking can fail for reasons that are nobody's fault: Windows does not reference-count VirtualLock and may drop pages from a process working set on its own, after which VirtualUnlock reports ERROR_NOT_LOCKED. A type whose entire job is holding key material must not abort the process while being dropped. The lock is released explicitly instead, and a failure to unlock is ignored: it leaves pages pinned, which is harmless, rather than unwinding out of a destructor.

In plain words

A place to hold a passphrase or a key while it is being used, which tries hard to forget it afterwards.

It asks the operating system not to write that memory out to disk, wipes it as soon as it is finished with, and refuses to print itself. That last one matters more than it sounds: secrets most often escape not by being stolen but by appearing in an error message or a log that somebody later sends on.

Comparisons take the same amount of time whether or not they match, so nothing is given away by how long an answer took.

WHAT THIS FILE CONTAINS

472 lines defining 17 functions (9 public), 1 type and 0 constants. Everything below is read out of the source, so it cannot disagree with the code.

The types it owns.

  • struct Secret line 91 · A byte buffer holding key material.

What happens when it runs. These are the ways in: public, and nothing else in this file calls them, so they are what an outside caller reaches first.

  • Secret::new line 175 · Wrap bytes, taking ownership and wiping the caller's copy.
    reaches zeroed, lock_pages, page_size
  • Secret::random line 217 · Fill len bytes from the operating-system CSPRNG.
    reaches zeroed, lock_pages, page_size
  • Secret::is_locked line 228 · Whether the pages were successfully locked out of swap.
  • Secret::len line 233 · Length in bytes.
  • Secret::is_empty line 238 · Whether the secret is empty.
  • Secret::expose_mut line 249 · Borrow mutably, for filling in place.
  • Secret::wipe line 257 · Wipe the contents now, before the value goes out of scope.

WHAT CALLS WHAT

page_size line 112 page_size line 121 lock_pages line 147 unlock_pages line 161 Secret::new line 175 Secret::zeroed line 183 Secret::random line 217 Secret::is_locked line 228 Secret::len line 233 Secret::is_empty line 238 Secret::expose line 244 Secret::expose_mut line 249 Secret::wipe line 257 Secret::drop line 263 Secret::clone line 274 Secret::eq line 284 Secret::fmt line 293 entry: a way in: public, and nothing in this file calls it api: public, and also used inside this file helper: private to this file dashed: a call that goes back up, or across a wrapped rank The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one.

The functions this file defines, and the calls between them. An edge means the callee's name appears, called, inside the caller's body. This is a syntactic reading, not a type-resolved one.

The same graph as Mermaid source
%%{init: {"theme":"base","themeVariables":{"background":"#1a1b26","primaryColor":"#1f2335","primaryTextColor":"#c0caf5","primaryBorderColor":"#7aa2f7","secondaryColor":"#16161e","tertiaryColor":"#16161e","lineColor":"#737aa2","textColor":"#c0caf5","mainBkg":"#1f2335","nodeBorder":"#7aa2f7","clusterBkg":"#16161e","clusterBorder":"#2f3549","fontFamily":"ui-monospace, SFMono-Regular, Consolas, monospace","fontSize":"14px"}}}%%
flowchart TD
    n_page_size["page_size<br/>line 112"]
    n_page_size["page_size<br/>line 121"]
    n_lock_pages["lock_pages<br/>line 147"]
    n_unlock_pages["unlock_pages<br/>line 161"]
    n_new(["Secret::new<br/>line 175"])
    n_zeroed["Secret::zeroed<br/>line 183"]
    n_random(["Secret::random<br/>line 217"])
    n_is_locked(["Secret::is_locked<br/>line 228"])
    n_len(["Secret::len<br/>line 233"])
    n_is_empty(["Secret::is_empty<br/>line 238"])
    n_expose["Secret::expose<br/>line 244"]
    n_expose_mut(["Secret::expose_mut<br/>line 249"])
    n_wipe(["Secret::wipe<br/>line 257"])
    n_drop["Secret::drop<br/>line 263"]
    n_clone["Secret::clone<br/>line 274"]
    n_eq["Secret::eq<br/>line 284"]
    n_fmt["Secret::fmt<br/>line 293"]
    n_clone --> n_expose
    n_clone --> n_zeroed
    n_drop --> n_unlock_pages
    n_eq --> n_expose
    n_new --> n_zeroed
    n_random --> n_zeroed
    n_zeroed --> n_lock_pages
    n_zeroed --> n_page_size
    click n_page_size href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L112" "open the source"
    click n_page_size href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L121" "open the source"
    click n_lock_pages href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L147" "open the source"
    click n_unlock_pages href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L161" "open the source"
    click n_new href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L175" "open the source"
    click n_zeroed href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L183" "open the source"
    click n_random href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L217" "open the source"
    click n_is_locked href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L228" "open the source"
    click n_len href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L233" "open the source"
    click n_is_empty href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L238" "open the source"
    click n_expose href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L244" "open the source"
    click n_expose_mut href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L249" "open the source"
    click n_wipe href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L257" "open the source"
    click n_drop href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L263" "open the source"
    click n_clone href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L274" "open the source"
    click n_eq href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L284" "open the source"
    click n_fmt href "https://github.com/tilas01/veilvoice/blob/main/crates/veilvoice-crypto/src/amnesia.rs#L293" "open the source"
    classDef entry fill:#1f2335,stroke:#7aa2f7,color:#c0caf5
    class n_new,n_random,n_is_locked,n_len,n_is_empty,n_expose_mut,n_wipe entry
    classDef api fill:#1f2335,stroke:#7dcfff,color:#c0caf5
    class n_zeroed,n_expose api
    classDef helper fill:#1f2335,stroke:#bb9af7,color:#c0caf5
    class n_page_size,n_page_size,n_lock_pages,n_unlock_pages,n_drop,n_clone,n_eq,n_fmt helper
    subgraph chart_key ["Key"]
        direction LR
        key_entry["entry: a way in: public, and nothing in this file calls it"]
        key_api["api: public, and also used inside this file"]
        key_helper["helper: private to this file"]
    end
    class key_entry entry
    class key_api api
    class key_helper helper

This site loads no third-party script, so it cannot run Mermaid; the diagram above is the same nodes and edges drawn by the generator instead. GitHub renders the source below directly.

ITEMS

ItemLineDocumentation
Secret pub struct91A byte buffer holding key material.
page_size fn112The size of a page of memory, for aligning the span a Secret locks.
page_size fn121The browser's answer, which is a number rather than a question asked of the host: see the note on the other half of this pair for why 4 KiB and not WebAssembly's own 64 KiB page.
lock_pages fn147Lock span bytes at at out of swap, answering whether it happened.
unlock_pages fn161Release a lock taken by lock_pages.
Secret::new pub fn175Wrap bytes, taking ownership and wiping the caller's copy.
Secret::zeroed pub fn183Allocate len zero bytes, ready to be filled in place.
Secret::random pub fn217Fill len bytes from the operating-system CSPRNG.
Secret::is_locked pub fn228Whether the pages were successfully locked out of swap.
Secret::len pub fn233Length in bytes.
Secret::is_empty pub fn238Whether the secret is empty.
Secret::expose pub fn244Borrow the raw bytes.
Secret::expose_mut pub fn249Borrow mutably, for filling in place.
Secret::wipe pub fn257Wipe the contents now, before the value goes out of scope.
Secret::drop fn263
Secret::clone fn274
Secret::eq fn284
Secret::fmt fn293