feat(core): zeroize in-memory secret material (Noise/Ed25519/X25519/store key) #46

Merged
sirius merged 2 commits from feat/zeroize-secrets into main 2026-09-12 19:06:58 +00:00
Owner

Summary

Every secret this crate keeps in memory is now wiped instead of merely being encrypted at rest. zeroize (already in Cargo.lock at 1.9.0, transitive through the dalek stack) becomes a direct dependency with the derive feature; no version bump was needed or made.

Wrapped: the local identity/prekey secrets handed to the Noise IK handshake, the Ed25519 identity secret wherever a raw copy is made, the X25519 prekeys, the store data key + the SQLCipher key material, the Argon2id-passphrase-derived wrap key, the backup bundle plaintext, the identity secret the store hands out, and the wallet RPC password / digest input.

Stacked on #43 (feat/sqlcipher-store, still open). This branch is based on origin/feat/sqlcipher-store, not on main, because #43 rewrites the same key-handling region of src/store.rs + src/backup.rs. It is a single commit on top of #43. Until #43 lands, the diff here will also show #43's commit; once #43 is on main the diff collapses to f3fca69 alone. Merge #43 first.

Types that hold secret bytes

Type / value Where Now ZeroizeOnDrop? Clone? Clone handling
session::Keys (identity_sk, prekey_sk) src/session.rs yes — #[derive(Zeroize, ZeroizeOnDrop)]; public halves #[zeroize(skip)] yes (derived) the copy is itself ZeroizeOnDrop, so it wipes on its own drop; test cloned_session_keys_wipe_their_own_copy_only proves the copy wipes and the original survives
session::Session::transport (snow::TransportState) src/session.rs no — see "Not covered" no n/a
handshake scratch buf (each of handshake_pinned / handshake_initiator / handshake_responder) src/session.rs yes — Zeroizing<Vec<u8>> no n/a
raw [u8; 32] identity-secret copies in pay::sign_bytes, qr::encode src/pay.rs, src/qr.rs yes — Zeroizing<[u8; 32]> no n/a
backup::BackupKeys (identity_sk, prekey_sk) src/backup.rs yes yes (derived) same as Keys; Debug is now hand-written and prints <redacted> for both secret halves (was #[derive(Debug)], i.e. it printed the bytes)
backup::kdf output (Argon2id wrap key) src/backup.rs yes — returns Zeroizing<[u8; 32]> (the raw [u8; 32] temp is gone) no n/a
BackupKeys::to_bytes plaintext bundle src/backup.rs yes — returns Zeroizing<[u8; 128]> no n/a
decrypted backup plaintext in backup::open src/backup.rs yes — Zeroizing<Vec<u8>> before from_bytes no n/a
Store::msg_key (store data key) src/store.rs yes — field is Zeroizing<[u8; 32]> no (Store is not Clone) n/a
Store::unlock_messages: unwrapped data key, fresh data key src/store.rs yes — Zeroizing immediately after the AEAD unwrap no n/a
Store::pragma_key string, the ATTACH … KEY string src/store.rs yes — Zeroizing<String> (both carried the db key in hex) no n/a
Store::passphrase_from_env, resolve_store_passphrase return value src/store.rs yes — Zeroizing<String> no n/a
SelfIdentity::identity_sk / prekey_sk src/store.rs yes — Zeroizing<Vec<u8>> no n/a
gen_prekey() secret half, ensure_self identity bytes src/store.rs yes no n/a
Node::start passphrase src/node.rs yes — Zeroizing<String> no n/a
wallet::Endpoint::pass src/wallet.rs yes — Zeroizing<String> yes (derived) copy wipes on its own drop; Debug was already redacted, now covered by a test
wallet ONIONWIRE_WALLET_RPC URL + …_LOGIN (may carry user:pass@) src/wallet.rs yes no n/a
wallet user:realm:password digest input src/wallet.rs yes — Zeroizing<String> no n/a

Also checked: session::sign_proof, node::write_backup / restore_backup / qr_payload hold no secret beyond the values above. ed25519_dalek::SigningKey and x25519_dalek::StaticSecret already wipe themselves on drop (both crates ship zeroize in their default features and this crate does not disable defaults), so those copies needed no change.

Not covered (explicit)

  • snow's internal Noise state. The IK ephemeral key and the derived transport keys live inside snow::HandshakeState / snow::TransportState. snow 0.10 exposes no zeroize support, so Session::transport is the one live secret-bearing object this change cannot wipe. Fixing it means upstream snow support or a different Noise implementation — out of scope here.
  • Allocator copies. Vec/String wiping zeroes the buffer in place; any reallocation or allocator-internal copy of that buffer is not reached.
  • Swap / hibernation. Paged-out memory is not wiped; a suspended-to-disk host keeps copies.
  • Core dumps / crash dumps contain whatever was in memory at the time.
  • OS-level String interning / environment copies. The passphrase read from ONIONWIRE_STORE_PASSPHRASE is wiped in our buffer, but the process environment itself is owned by the OS and is not touched (Rust's env::var gives us a copy, which is what we wipe).
  • src/file.rs buffers, the TUI passphrase-entry buffer (src/tui.rs) and src/profile.rs were out of scope per the card (the first two are owned by open PRs / explicitly excluded); the TUI's in-progress passphrase String still lives until the screen is torn down, and profile::sign still makes a raw [u8; 32] identity-secret copy.
  • No unsafe is used in the crate to defeat Zeroize; the only unsafe added is in tests/zeroize.rs, to read a dropped value back so the drop test cannot pass on a moved copy.

Verification (real output)

$ cargo test --locked
TOTAL passed=195 failed=0 ignored=3      # 3 ignored = live-Tor tests

$ cargo clippy --locked --all-targets -- -D warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s

$ cargo test --test zeroize
running 6 tests
test session_and_backup_key_types_are_zeroize_on_drop ... ok
test session_keys_zeroize_wipes_secrets_and_keeps_publics ... ok
test dropped_session_keys_leave_no_secret_behind ... ok
test cloned_session_keys_wipe_their_own_copy_only ... ok
test self_identity_secrets_are_zeroizing_and_wipe ... ok
test wallet_endpoint_debug_redacts_the_rpc_password ... ok
test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

New tests: tests/zeroize.rs (6), src/backup.rs unit tests (3: derived wrap key is Zeroizing, plaintext bundle wipes, Debug redacts), src/store.rs unit test (store data key is a Zeroizing wrapper and wipes). The type-level assertions are compile-time evidence: deleting a derive breaks the build there.

Also verified written first and failing before the change (RED): use zeroize::{…} was an unresolved-crate error at that point, and dropped_session_keys_leave_no_secret_behind failed with the real secret [13, 119, 16, 221, …] still present before the wrapper landed.

Files

Cargo.toml, Cargo.lock (+1 line: the zeroize edge on the onionwire package), src/session.rs, src/backup.rs, src/store.rs, src/pay.rs, src/qr.rs, src/wallet.rs, src/node.rs, tests/zeroize.rs (new), tests/backup.rs, tests/store.rs. No forbidden path touched (src/tui.rs, tests/tui_spec.rs, docs/TUI.md, README.md, crates/onionwire-sdk/**, android/** all untouched), so there is no ## README (deferred) section.

README (deferred)

No README.md wording is needed for this change — it is an internal hardening pass with no user-visible behaviour change.

## Summary Every secret this crate keeps in memory is now wiped instead of merely being encrypted at rest. `zeroize` (already in `Cargo.lock` at 1.9.0, transitive through the dalek stack) becomes a direct dependency with the `derive` feature; no version bump was needed or made. Wrapped: the local identity/prekey secrets handed to the Noise IK handshake, the Ed25519 identity secret wherever a raw copy is made, the X25519 prekeys, the store data key + the SQLCipher key material, the Argon2id-passphrase-derived wrap key, the backup bundle plaintext, the identity secret the store hands out, and the wallet RPC password / digest input. > **Stacked on #43 (`feat/sqlcipher-store`, still open).** This branch is based on `origin/feat/sqlcipher-store`, not on `main`, because #43 rewrites the same key-handling region of `src/store.rs` + `src/backup.rs`. It is a single commit on top of #43. Until #43 lands, the diff here will also show #43's commit; once #43 is on `main` the diff collapses to `f3fca69` alone. **Merge #43 first.** ## Types that hold secret bytes | Type / value | Where | Now `ZeroizeOnDrop`? | `Clone`? | Clone handling | |---|---|---|---|---| | `session::Keys` (`identity_sk`, `prekey_sk`) | `src/session.rs` | yes — `#[derive(Zeroize, ZeroizeOnDrop)]`; public halves `#[zeroize(skip)]` | yes (derived) | the copy is itself `ZeroizeOnDrop`, so it wipes on its own drop; test `cloned_session_keys_wipe_their_own_copy_only` proves the copy wipes and the original survives | | `session::Session::transport` (`snow::TransportState`) | `src/session.rs` | **no** — see "Not covered" | no | n/a | | handshake scratch `buf` (each of `handshake_pinned` / `handshake_initiator` / `handshake_responder`) | `src/session.rs` | yes — `Zeroizing<Vec<u8>>` | no | n/a | | raw `[u8; 32]` identity-secret copies in `pay::sign_bytes`, `qr::encode` | `src/pay.rs`, `src/qr.rs` | yes — `Zeroizing<[u8; 32]>` | no | n/a | | `backup::BackupKeys` (`identity_sk`, `prekey_sk`) | `src/backup.rs` | yes | yes (derived) | same as `Keys`; `Debug` is now hand-written and prints `<redacted>` for both secret halves (was `#[derive(Debug)]`, i.e. it printed the bytes) | | `backup::kdf` output (Argon2id wrap key) | `src/backup.rs` | yes — returns `Zeroizing<[u8; 32]>` (the raw `[u8; 32]` temp is gone) | no | n/a | | `BackupKeys::to_bytes` plaintext bundle | `src/backup.rs` | yes — returns `Zeroizing<[u8; 128]>` | no | n/a | | decrypted backup plaintext in `backup::open` | `src/backup.rs` | yes — `Zeroizing<Vec<u8>>` before `from_bytes` | no | n/a | | `Store::msg_key` (store data key) | `src/store.rs` | yes — field is `Zeroizing<[u8; 32]>` | no (`Store` is not `Clone`) | n/a | | `Store::unlock_messages`: unwrapped data key, fresh data key | `src/store.rs` | yes — `Zeroizing` immediately after the AEAD unwrap | no | n/a | | `Store::pragma_key` string, the `ATTACH … KEY` string | `src/store.rs` | yes — `Zeroizing<String>` (both carried the db key in hex) | no | n/a | | `Store::passphrase_from_env`, `resolve_store_passphrase` return value | `src/store.rs` | yes — `Zeroizing<String>` | no | n/a | | `SelfIdentity::identity_sk` / `prekey_sk` | `src/store.rs` | yes — `Zeroizing<Vec<u8>>` | no | n/a | | `gen_prekey()` secret half, `ensure_self` identity bytes | `src/store.rs` | yes | no | n/a | | `Node::start` passphrase | `src/node.rs` | yes — `Zeroizing<String>` | no | n/a | | `wallet::Endpoint::pass` | `src/wallet.rs` | yes — `Zeroizing<String>` | yes (derived) | copy wipes on its own drop; `Debug` was already redacted, now covered by a test | | wallet `ONIONWIRE_WALLET_RPC` URL + `…_LOGIN` (may carry `user:pass@`) | `src/wallet.rs` | yes | no | n/a | | wallet `user:realm:password` digest input | `src/wallet.rs` | yes — `Zeroizing<String>` | no | n/a | Also checked: `session::sign_proof`, `node::write_backup` / `restore_backup` / `qr_payload` hold no secret beyond the values above. `ed25519_dalek::SigningKey` and `x25519_dalek::StaticSecret` already wipe themselves on drop (both crates ship `zeroize` in their **default** features and this crate does not disable defaults), so those copies needed no change. ## Not covered (explicit) - **`snow`'s internal Noise state.** The IK ephemeral key and the derived transport keys live inside `snow::HandshakeState` / `snow::TransportState`. `snow` 0.10 exposes no `zeroize` support, so `Session::transport` is the one live secret-bearing object this change cannot wipe. Fixing it means upstream `snow` support or a different Noise implementation — out of scope here. - **Allocator copies.** `Vec`/`String` wiping zeroes the buffer in place; any reallocation or allocator-internal copy of that buffer is not reached. - **Swap / hibernation.** Paged-out memory is not wiped; a suspended-to-disk host keeps copies. - **Core dumps / crash dumps** contain whatever was in memory at the time. - **OS-level `String` interning / environment copies.** The passphrase read from `ONIONWIRE_STORE_PASSPHRASE` is wiped in our buffer, but the process environment itself is owned by the OS and is not touched (Rust's `env::var` gives us a copy, which is what we wipe). - **`src/file.rs` buffers, the TUI passphrase-entry buffer** (`src/tui.rs`) and `src/profile.rs` were out of scope per the card (the first two are owned by open PRs / explicitly excluded); the TUI's in-progress passphrase `String` still lives until the screen is torn down, and `profile::sign` still makes a raw `[u8; 32]` identity-secret copy. - No `unsafe` is used in the crate to defeat `Zeroize`; the only `unsafe` added is in `tests/zeroize.rs`, to read a dropped value back so the drop test cannot pass on a moved copy. ## Verification (real output) ``` $ cargo test --locked TOTAL passed=195 failed=0 ignored=3 # 3 ignored = live-Tor tests $ cargo clippy --locked --all-targets -- -D warnings Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s $ cargo test --test zeroize running 6 tests test session_and_backup_key_types_are_zeroize_on_drop ... ok test session_keys_zeroize_wipes_secrets_and_keeps_publics ... ok test dropped_session_keys_leave_no_secret_behind ... ok test cloned_session_keys_wipe_their_own_copy_only ... ok test self_identity_secrets_are_zeroizing_and_wipe ... ok test wallet_endpoint_debug_redacts_the_rpc_password ... ok test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ``` New tests: `tests/zeroize.rs` (6), `src/backup.rs` unit tests (3: derived wrap key is `Zeroizing`, plaintext bundle wipes, `Debug` redacts), `src/store.rs` unit test (store data key is a `Zeroizing` wrapper and wipes). The type-level assertions are compile-time evidence: deleting a derive breaks the build there. Also verified written first and failing before the change (RED): `use zeroize::{…}` was an unresolved-crate error at that point, and `dropped_session_keys_leave_no_secret_behind` failed with the real secret `[13, 119, 16, 221, …]` still present before the wrapper landed. ## Files `Cargo.toml`, `Cargo.lock` (+1 line: the `zeroize` edge on the `onionwire` package), `src/session.rs`, `src/backup.rs`, `src/store.rs`, `src/pay.rs`, `src/qr.rs`, `src/wallet.rs`, `src/node.rs`, `tests/zeroize.rs` (new), `tests/backup.rs`, `tests/store.rs`. No forbidden path touched (`src/tui.rs`, `tests/tui_spec.rs`, `docs/TUI.md`, `README.md`, `crates/onionwire-sdk/**`, `android/**` all untouched), so there is no `## README (deferred)` section. ## README (deferred) No `README.md` wording is needed for this change — it is an internal hardening pass with no user-visible behaviour change.
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.
sirius force-pushed feat/zeroize-secrets from f3fca69fbe
All checks were successful
ci / test (pull_request) Successful in 4m7s
to fc7378596d
All checks were successful
ci / test (pull_request) Successful in 3m50s
2026-09-12 17:53:38 +00:00
Compare
sirius left a comment

Merge order 7 of 7: #38 -> #42 -> #41 -> #44 -> #43 -> #45 -> #46

Last of the core three, deliberately: zeroizing after #45 means the sweep covers the secret handling #45 adds, instead of needing a second pass. Needs one rebase after #41/#43/#45 — conflict in src/wallet.rs (from #41) plus docs/THREAT_MODEL.md, verified by simulation

Note: Forgejo refuses a formal approval from this token ("approve your own pull is not allowed" — the PRs were pushed by account sirius, which is also the token's user). This review records the sign-off and the merge position; a human approval click is still needed. Rebased onto current main before this review, diff verified byte-identical.

**Merge order 7 of 7:** #38 -> #42 -> #41 -> #44 -> #43 -> #45 -> #46 Last of the core three, deliberately: zeroizing after #45 means the sweep covers the secret handling #45 adds, instead of needing a second pass. Needs one rebase after #41/#43/#45 — conflict in `src/wallet.rs` (from #41) plus `docs/THREAT_MODEL.md`, verified by simulation Note: Forgejo refuses a formal approval from this token ("approve your own pull is not allowed" — the PRs were pushed by account `sirius`, which is also the token's user). This review records the sign-off and the merge position; a human approval click is still needed. Rebased onto current `main` before this review, diff verified byte-identical.
Merge branch 'main' into feat/zeroize-secrets
All checks were successful
ci / test (pull_request) Successful in 6m33s
8eb0820df0
sirius merged commit c39259b919 into main 2026-09-12 19:06:58 +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!46
No description provided.