feat(core): zeroize the profile/loc signing copy and the TUI passphrase buffer #47

Merged
sirius merged 1 commit from feat/zeroize-tui-profile into main 2026-09-12 20:31:46 +00:00
Owner

Why

Follow-up to #46 (and stacked on it — see Base below). #46 wrapped every secret its file scope allowed; two same-class copies were out of scope:

  1. src/profile.rs::sign still built the Ed25519 identity secret into a bare [u8; 32] on the stack (try_into → SigningKey::from_bytes), a raw non-wiping copy of the identity secret. pay::sign_bytes / qr::encode had the same fix in #46.
  2. src/tui.rs held the in-progress backup/restore passphrase in a plain String (Screen::Passphrase { first, buf }), so the typed passphrase stayed in a non-wiping heap buffer for the life of the screen.

What changed

profile / loc

  • profile::signing_key_bytes(identity_sk) -> Zeroizing<[u8; 32]> is the single place the raw 32-byte copy is made; sign() uses it. It writes with copy_from_slice into a Zeroizing<[u8; 32]> (rather than Zeroizing::new(<[u8; 32]>::try_from(..)?) as in #46) so no Copy array temporary is left on the stack. Type + drop behaviour are the same as the #46 pattern.
  • src/loc.rs::sign made the same raw copy and was not in this card's list. It now calls the shared helper instead of the third hand-rolled try_into copy. Flagged here because the card is titled "the remaining identity-secret copies" and this was a fourth one; revert that hunk if you want it as its own card.

TUI

  • New PassphraseEntry owns the two buffers as Zeroizing<String> (the hidden buffer + the first-passphrase copy kept across the two-step backup prompt). Screen::Passphrase carries it instead of first: Option<String> / buf: String.
  • PassphraseEntry::close() wipes both buffers (eager zeroize + drop of the first copy) and is called on every exit path: Esc (cancel), Enter (submit — success, mismatch, RPC error) and the mismatch path inside submit_passphrase. Zeroizing's drop is the backstop for the paths that cannot call it (Ctrl-Q, panic).
  • The submitted passphrase is now moved out of the buffer (take_buf / take_first) instead of cloned into two more plain Strings, so no extra non-wiping copy exists on the submit path.

Evidence

  • RED first: tests/zeroize.rs referenced profile::signing_key_bytes → error[E0425]: cannot find function ... in module profile; the src/tui.rs unit tests referenced PassphraseEntry → error[E0433]: cannot find type PassphraseEntry in this scope.
  • GREEN: cargo test --locked → 206 passed / 0 failed / 3 ignored (the 3 ignored are the live-Tor tests). New: 3 in tests/zeroize.rs, 3 unit tests in src/tui.rs.
  • cargo clippy --locked --all-targets -- -D warnings → clean.
  • Tests are behavioural, not comments:
    • profile_signing_key_bytes_are_zeroizing_and_wipe — type-level &Zeroizing<[u8; 32]> binding plus a ManuallyDrop + drop_in_place + read_volatile drop probe asserting the buffer is all-zero after its drop (a plain drop(k) moves the value and zeroes the copy, not the original); profile_signing_key_bytes_rejects_a_short_secret covers 31/33-byte inputs.
    • passphrase_buffers_are_zeroizing — asserts the field types and Zeroizing<String>: ZeroizeOnDrop.
    • closing_the_passphrase_screen_wipes_the_typed_secret — types "hunter2-secret", captures the buffer pointer+capacity, calls the screen-close path, then reads the live allocation back and asserts every byte of the capacity is zero (Vec::zeroize zeroes the whole capacity and keeps the allocation, so this is not a read of freed memory).
    • closing_the_passphrase_screen_drops_the_first_entry_copy — the two-step backup flow's first copy is gone after close.

Still not covered (unchanged from #46's list, plus one new)

  • snow 0.10 has no zeroize support: the Noise ephemeral + derived transport keys inside HandshakeState/TransportState cannot be wiped without upstream work.
  • Allocator copies, swap/hibernation, core dumps; src/file.rs buffers.
  • src/backup.rs::BackupKeys::from_bytes still builds [u8; 32] field values via b[a..b].try_into().unwrap() — the same Copy-temporary shape as the copy fixed here. BackupKeys itself is ZeroizeOnDrop and the input plaintext is Zeroizing, so only the transient array is unwiped. Left alone deliberately: src/backup.rs is rewritten by the still-open #43/#46 chain and carving a hunk out of it here would collide in exactly that function.
  • The type-to-confirm prompts (BackupPrompt, RotatePrompt, WipePrompt, QuitPrompt) stay plain Strings: they hold BACKUP/ROTATE/WIPE/QUIT, not secrets.

Base

Branch is origin/feat/zeroize-secrets (#46) + origin/main merged in + this commit, because the drop probes live in tests/zeroize.rs, which #46 introduces; adding the probes to a file that #46 is still writing would have merged as a conflict. Base is main per the card, so the diff shows #43/#46's commits until those land — then it collapses to f5576e7 alone. Merge #43 → #46 first.

Refs #46. No main push.

## Why Follow-up to #46 (and stacked on it — see *Base* below). #46 wrapped every secret its file scope allowed; two same-class copies were out of scope: 1. `src/profile.rs::sign` still built the Ed25519 identity secret into a bare `[u8; 32]` on the stack (`try_into` → `SigningKey::from_bytes`), a raw non-wiping copy of the identity secret. `pay::sign_bytes` / `qr::encode` had the same fix in #46. 2. `src/tui.rs` held the in-progress backup/restore passphrase in a plain `String` (`Screen::Passphrase { first, buf }`), so the typed passphrase stayed in a non-wiping heap buffer for the life of the screen. ## What changed **profile / loc** - `profile::signing_key_bytes(identity_sk) -> Zeroizing<[u8; 32]>` is the single place the raw 32-byte copy is made; `sign()` uses it. It writes with `copy_from_slice` into a `Zeroizing<[u8; 32]>` (rather than `Zeroizing::new(<[u8; 32]>::try_from(..)?)` as in #46) so no `Copy` array temporary is left on the stack. Type + drop behaviour are the same as the #46 pattern. - `src/loc.rs::sign` made the *same* raw copy and was not in this card's list. It now calls the shared helper instead of the third hand-rolled `try_into` copy. Flagged here because the card is titled "the remaining identity-secret copies" and this was a fourth one; revert that hunk if you want it as its own card. **TUI** - New `PassphraseEntry` owns the two buffers as `Zeroizing<String>` (the hidden buffer + the first-passphrase copy kept across the two-step backup prompt). `Screen::Passphrase` carries it instead of `first: Option<String>` / `buf: String`. - `PassphraseEntry::close()` wipes both buffers (eager zeroize + drop of the first copy) and is called on every exit path: `Esc` (cancel), `Enter` (submit — success, mismatch, RPC error) and the mismatch path inside `submit_passphrase`. `Zeroizing`'s drop is the backstop for the paths that cannot call it (Ctrl-Q, panic). - The submitted passphrase is now *moved* out of the buffer (`take_buf` / `take_first`) instead of cloned into two more plain `String`s, so no extra non-wiping copy exists on the submit path. ## Evidence - RED first: `tests/zeroize.rs` referenced `profile::signing_key_bytes` → `error[E0425]: cannot find function ... in module profile`; the `src/tui.rs` unit tests referenced `PassphraseEntry` → `error[E0433]: cannot find type PassphraseEntry in this scope`. - GREEN: `cargo test --locked` → **206 passed / 0 failed / 3 ignored** (the 3 ignored are the live-Tor tests). New: 3 in `tests/zeroize.rs`, 3 unit tests in `src/tui.rs`. - `cargo clippy --locked --all-targets -- -D warnings` → clean. - Tests are behavioural, not comments: - `profile_signing_key_bytes_are_zeroizing_and_wipe` — type-level `&Zeroizing<[u8; 32]>` binding plus a `ManuallyDrop` + `drop_in_place` + `read_volatile` drop probe asserting the buffer is all-zero after its drop (`a plain drop(k) moves the value and zeroes the copy, not the original`); `profile_signing_key_bytes_rejects_a_short_secret` covers 31/33-byte inputs. - `passphrase_buffers_are_zeroizing` — asserts the field types and `Zeroizing<String>: ZeroizeOnDrop`. - `closing_the_passphrase_screen_wipes_the_typed_secret` — types "hunter2-secret", captures the buffer pointer+capacity, calls the screen-close path, then reads the *live* allocation back and asserts every byte of the capacity is zero (`Vec::zeroize` zeroes the whole capacity and keeps the allocation, so this is not a read of freed memory). - `closing_the_passphrase_screen_drops_the_first_entry_copy` — the two-step backup flow's first copy is gone after close. ## Still not covered (unchanged from #46's list, plus one new) - `snow` 0.10 has no zeroize support: the Noise ephemeral + derived transport keys inside `HandshakeState`/`TransportState` cannot be wiped without upstream work. - Allocator copies, swap/hibernation, core dumps; `src/file.rs` buffers. - `src/backup.rs::BackupKeys::from_bytes` still builds `[u8; 32]` field values via `b[a..b].try_into().unwrap()` — the same `Copy`-temporary shape as the copy fixed here. `BackupKeys` itself is `ZeroizeOnDrop` and the *input* plaintext is `Zeroizing`, so only the transient array is unwiped. Left alone deliberately: `src/backup.rs` is rewritten by the still-open #43/#46 chain and carving a hunk out of it here would collide in exactly that function. - The type-to-confirm prompts (`BackupPrompt`, `RotatePrompt`, `WipePrompt`, `QuitPrompt`) stay plain `String`s: they hold `BACKUP`/`ROTATE`/`WIPE`/`QUIT`, not secrets. ## Base Branch is `origin/feat/zeroize-secrets` (#46) + `origin/main` merged in + this commit, because the drop probes live in `tests/zeroize.rs`, which #46 introduces; adding the probes to a file that #46 is still writing would have merged as a conflict. Base is `main` per the card, so the diff shows #43/#46's commits until those land — then it collapses to `f5576e7` alone. Merge #43 → #46 first. Refs #46. No `main` push.
feat(store): encrypt the whole sqlite store with SQLCipher
All checks were successful
ci / test (pull_request) Successful in 10m31s
3adfc24391
The db held the ed25519 identity secret, friend pubkeys, locators, profiles
and payment rows in the clear; only chat bodies were encrypted. Switch
rusqlite to `bundled-sqlcipher-vendored-openssl` and open every store with
`PRAGMA key = "x'<64 hex>'"` as the first statement after open, using the
existing Argon2id output as a raw key (no second KDF pass).

The per-install KDF salt moves out of the db into `store_salt` (0600) — a key
cannot be derived from a salt that lives inside the file it opens. The
per-message ChaCha20-Poly1305 + row-AAD layer is unchanged: whole-db
encryption does not defend against ciphertext swapped between rows, the AAD
does.

v0.2.1 installs are plaintext. The first open with the right passphrase
upgrades in place (WAL checkpoint, ATTACH ... KEY, sqlcipher_export) and only
swaps the file after the new one opens, passes integrity_check and unwraps
store_meta. A wrong passphrase leaves the plaintext file untouched; a
missing `store_salt` next to an encrypted db refuses to open rather than
guessing.

SQLCipher's internal "hmac check failed" ERROR on a rejected key is
suppressed (cipher_log_level = NONE) so a typo'd passphrase is not an error
dump.

docs/THREAT_MODEL.md: the store is now encrypted at rest; sqlcipher leaves
the "Out of v1" list.
feat(core): zeroize in-memory secret material (Noise/Ed25519/X25519/store key)
All checks were successful
ci / test (pull_request) Successful in 4m7s
f3fca69fbe
Wrap every secret this crate holds in zeroize: the local identity and
prekey secrets (Keys), the backup bundle and its derived Argon2id wrap
key, the store data key and the SQLCipher key material, the identity
secret handed out by Store::self_identity, the raw identity-secret
copies in pay/qr signing, and the wallet RPC password plus the
user:realm:password digest input.
feat(core): zeroize the profile/loc signing copy and the TUI passphrase buffer
All checks were successful
ci / test (pull_request) Successful in 3m53s
f5576e751b
The Ed25519 identity secret was still copied into a bare [u8; 32] on the
stack in two signers (profile::sign, loc::sign), and the TUI kept the
typed backup/restore passphrase in a plain String for the life of the
screen. Both now go through Zeroizing:

- profile::signing_key_bytes() builds the 32-byte copy into a
  Zeroizing<[u8; 32]> (same pattern as pay::sign_bytes / qr::encode,
  but with copy_from_slice so no Copy temporary is left behind);
  loc::sign shares the helper instead of making its own copy.
- Screen::Passphrase now owns a PassphraseEntry whose buffers are
  Zeroizing<String>. Every exit path (Enter, mismatch, Esc) calls
  close(), which wipes both buffers eagerly; a drop — including Ctrl-Q
  and a panic — is the backstop.

Evidence: tests/zeroize.rs gains a drop probe (ManuallyDrop +
drop_in_place + read_volatile) on the Zeroizing signing buffer and a
length-rejection test; src/tui.rs gains unit tests asserting the buffer
types and that close() leaves every byte of the passphrase's heap
capacity zeroed.
sirius force-pushed feat/zeroize-tui-profile from f5576e751b
All checks were successful
ci / test (pull_request) Successful in 3m53s
to e3be8ffdd0
All checks were successful
ci / test (pull_request) Successful in 4m14s
2026-09-12 19:40:27 +00:00
Compare
sirius merged commit a50df7ee43 into main 2026-09-12 20:31:46 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!47
No description provided.