feat(core): zeroize in-memory secret material (Noise/Ed25519/X25519/store key)
All checks were successful
ci / test (pull_request) Successful in 4m7s

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.
This commit is contained in:
Sirius DevOps 2026-09-12 13:36:21 -04:00
commit f3fca69fbe
No known key found for this signature in database
12 changed files with 297 additions and 71 deletions

View file

@ -72,9 +72,9 @@ fn restore_overwrites_self_keys_friends_stay() {
)
.unwrap();
let me = store.self_identity().unwrap();
assert_eq!(me.identity_sk, incoming.identity_sk);
assert_eq!(me.identity_sk[..], incoming.identity_sk[..]);
assert_eq!(me.identity_pk, incoming.identity_pk);
assert_eq!(me.prekey_sk, incoming.prekey_sk);
assert_eq!(me.prekey_sk[..], incoming.prekey_sk[..]);
assert_eq!(me.prekey_pk, incoming.prekey_pk);
assert_eq!(me.onion, old.onion, "onion is locator, not restored");
assert_eq!(store.friend_count().unwrap(), 1);

View file

@ -67,7 +67,7 @@ fn first_run_creates_0700_dirs_and_self_row() {
let me = store.self_identity().expect("self");
assert_eq!(me.identity_pk.len(), 32);
assert_eq!(me.identity_sk.len(), 32);
assert_ne!(me.identity_pk, me.identity_sk);
assert_ne!(me.identity_pk, me.identity_sk.to_vec());
assert_eq!(me.onion, "");
assert_eq!(store.friend_count().unwrap(), 0);
@ -152,7 +152,7 @@ fn self_has_x25519_prekey_and_set_onion() {
let me = store.self_identity().unwrap();
assert_eq!(me.prekey_pk.len(), 32);
assert_eq!(me.prekey_sk.len(), 32);
assert_ne!(me.prekey_pk, me.prekey_sk);
assert_ne!(me.prekey_pk, me.prekey_sk.to_vec());
store.set_onion("abc.onion").unwrap();
assert_eq!(store.self_identity().unwrap().onion, "abc.onion");
}
@ -277,7 +277,7 @@ fn whole_store_file_is_encrypted_at_rest() {
(b"reply-at-rest-xyz".to_vec(), "inbound chat body"),
(hex(&pk(7)).into_bytes(), "friend pubkey hex"),
(b"friend.onion".to_vec(), "friend locator"),
(me.identity_sk.clone(), "identity secret key"),
(me.identity_sk.to_vec(), "identity secret key"),
] {
assert!(
!store_contains(dir.path(), &needle),
@ -394,7 +394,7 @@ fn env_passphrase_skips_prompt() {
unsafe {
std::env::remove_var("ONIONWIRE_STORE_PASSPHRASE");
}
assert_eq!(got.unwrap(), "from-env");
assert_eq!(got.unwrap().as_str(), "from-env");
assert!(!prompted);
}
@ -428,7 +428,7 @@ fn missing_env_reads_secret_and_strips_newline() {
Ok("secret-from-tty\n".into())
})
.unwrap();
assert_eq!(got, "secret-from-tty");
assert_eq!(got.as_str(), "secret-from-tty");
}
#[test]

118
tests/zeroize.rs Normal file
View file

@ -0,0 +1,118 @@
//! Wipe in-memory secrets on drop — keys, not just the file at rest.
//!
//! Not covered here (see the PR body): allocator copies, swap/hibernation,
//! core dumps, OS `String` interning, `src/file.rs` + TUI buffers, and the
//! Noise handshake/transport state that lives inside `snow`.
use onionwire::backup::BackupKeys;
use onionwire::session::Keys;
use onionwire::wallet::Wallet;
use onionwire::Store;
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
/// Type-level: the wrapper is applied to the type, not just mentioned in a
/// comment. Removing the derive breaks the build here.
fn assert_zeroize_on_drop<T: ZeroizeOnDrop>() {}
#[test]
fn session_and_backup_key_types_are_zeroize_on_drop() {
assert_zeroize_on_drop::<Keys>();
assert_zeroize_on_drop::<BackupKeys>();
}
/// Behavioural: the derived impl must wipe the secret halves and leave the
/// public halves alone. A `Zeroize` that only touched the publics would still
/// satisfy a bare drop assertion and leak anyway.
#[test]
fn session_keys_zeroize_wipes_secrets_and_keeps_publics() {
let mut k = Keys::generate();
let (id_pk, prekey_pk) = (k.identity_pk, k.prekey_pk);
assert_ne!(k.identity_sk, [0u8; 32]);
assert_ne!(k.prekey_sk, [0u8; 32]);
k.zeroize();
assert_eq!(k.identity_sk, [0u8; 32], "identity secret not wiped");
assert_eq!(k.prekey_sk, [0u8; 32], "prekey secret not wiped");
assert_eq!(k.identity_pk, id_pk, "identity public key was wiped");
assert_eq!(k.prekey_pk, prekey_pk, "prekey public key was wiped");
}
/// Drops `v` in place and reads its secret field back, so a drop test cannot
/// pass on a copy that the caller still holds.
///
/// SAFETY: only for `ManuallyDrop<Keys>` locals — the value is dropped exactly
/// once, at its own address, and its storage is still alive when it is read.
unsafe fn drop_in_place_and_peek_identity_sk(v: &mut Keys) -> [u8; 32] {
let p = std::ptr::addr_of!(v.identity_sk);
// SAFETY: the caller guarantees `v` is a `ManuallyDrop<Keys>` dropped once.
unsafe {
std::ptr::drop_in_place(v as *mut Keys);
std::ptr::read_volatile(p)
}
}
/// Drop behaviour, not a comment: after the value drops, the local secret key
/// material that feeds the Noise IK handshake is gone from its memory.
#[test]
fn dropped_session_keys_leave_no_secret_behind() {
let mut k = std::mem::ManuallyDrop::new(Keys::generate());
// SAFETY: `k` is a live local; the pointer targets its own field.
assert_ne!(unsafe { *std::ptr::addr_of!(k.identity_sk) }, [0u8; 32]);
let after = unsafe { drop_in_place_and_peek_identity_sk(&mut k) };
assert_eq!(after, [0u8; 32], "identity secret survived drop");
}
/// `Keys` is `Clone`, so a cloned copy is itself live secret material. It must
/// wipe on *its* drop, and dropping it must not wipe the original it came from.
#[test]
fn cloned_session_keys_wipe_their_own_copy_only() {
let original = std::mem::ManuallyDrop::new(Keys::generate());
let mut copy = std::mem::ManuallyDrop::new((*original).clone());
let after = unsafe { drop_in_place_and_peek_identity_sk(&mut copy) };
assert_eq!(after, [0u8; 32], "cloned secret survived its drop");
// SAFETY: `original` is a live local; the pointer targets its own field.
assert_ne!(
unsafe { *std::ptr::addr_of!(original.identity_sk) },
[0u8; 32],
"dropping the clone wiped the original's key"
);
}
/// The identity secret handed out by the store is a `Zeroizing`, not a bare
/// `Vec<u8>` that outlives every caller.
#[test]
fn self_identity_secrets_are_zeroizing_and_wipe() {
let dir = tempfile::tempdir().expect("tempdir");
let store = Store::open_at_with_passphrase(dir.path(), "zeroize-pass").expect("open");
let mut me = store.self_identity().expect("self");
let _: &Zeroizing<Vec<u8>> = &me.identity_sk;
let _: &Zeroizing<Vec<u8>> = &me.prekey_sk;
assert_eq!(me.identity_sk.len(), 32);
assert!(me.identity_sk.iter().any(|b| *b != 0));
me.identity_sk.zeroize();
me.prekey_sk.zeroize();
assert!(me.identity_sk.iter().all(|b| *b == 0));
assert!(me.prekey_sk.iter().all(|b| *b == 0));
}
/// The wallet RPC password is a secret, so it is `Zeroizing` and its `Debug`
/// redacts it instead of printing it.
#[test]
fn wallet_endpoint_debug_redacts_the_rpc_password() {
let w = Wallet::from_url("http://monero:[email protected]:18083/json_rpc").expect("url");
let dbg = format!("{w:?}");
assert!(
!dbg.contains("hunter2"),
"wallet RPC password leaked into Debug: {dbg}"
);
assert!(dbg.contains("<redacted>"), "{dbg}");
}