Files
arcanum/zeroize-probe/tests/zeroize.rs
mnovak 002371445b test: the zeroize probe also watches the expanded ML-DSA key (QA S-04)
The probe looked only for its 32-byte canary, i.e. the seeds; the key
material that actually signs is derived from them and never contains it,
so an unwiped copy of the expanded key went unseen (QA's mutation Q15).

The probe allocator now also watches up to four further 32-byte patterns,
held in atomics (dealloc must not allocate or lock). The test computes the
canary key's FIPS 204 expanded encoding with ml-dsa itself and watches K
(held raw inside ml-dsa's ExpandedSigningKey) and a 32-byte window of the
encoded s1; controls show an unwiped copy is seen and a Zeroizing one is
not; then building, signing, binding and dropping a canary SigningKey must
free no block holding either.

Q15 re-checked: a SigningKey field holding an unwiped Vec of
ml.to_expanded() fails the probe at the new assertion (reverted). The
ml-dsa rnd / rho'' / y stack residue is out of the probe's reach (stack,
not heap) and is recorded as a gap.
2026-11-26 03:48:11 +01:00

146 lines
6.8 KiB
Rust

//! QA A-29: heap memory that held Arcanum secret bytes is wiped before it is freed.
//!
//! One test function, run sequentially, because the probe is process-global. The controls come
//! first and prove the probe is not blind: an unwiped `Vec` and a `Zeroizing<Vec>` that grows
//! (the reallocation case) are both caught; a `Zeroizing<Vec>` that does not grow is not.
//!
//! Run: `cargo test --manifest-path zeroize-probe/Cargo.toml -- --test-threads=1`
use arcanum::exchange;
use arcanum::seal;
use arcanum::sign::{self, AppContext, SigningKey};
use arcanum::xwing::SecretKey;
use arcanum_zeroize_probe::{CANARY, arm, disarm, watch};
use chacha20::ChaCha20Rng;
use rand_core::SeedableRng;
use zeroize::{Zeroize, Zeroizing};
fn rng(tag: u8) -> ChaCha20Rng {
ChaCha20Rng::from_seed([tag; 32])
}
#[test]
fn secrets_are_wiped_before_their_heap_memory_is_freed() {
// --- controls: the probe sees what it should ---
arm();
drop(std::hint::black_box(CANARY.to_vec()));
assert!(disarm(), "control: an unwiped Vec holding the canary must be seen");
arm();
drop(std::hint::black_box(Zeroizing::new(CANARY.to_vec())));
assert!(!disarm(), "control: a Zeroizing<Vec> that never grows must not be seen");
arm();
let mut grows = Zeroizing::new(CANARY.to_vec());
grows.extend_from_slice(&[0u8; 4096]); // reallocates: the old block is freed unwiped
drop(std::hint::black_box(grows));
assert!(disarm(), "control: the reallocation case (A-29's typical bug) must be seen");
// --- Arcanum ---
// The seed (= the stored secret key) inside a heap-allocated SecretKey.
arm();
drop(std::hint::black_box(Box::new(SecretKey::from_bytes(&CANARY).unwrap())));
assert!(!disarm(), "a boxed SecretKey was freed with its seed still in memory");
// The exported seed.
arm();
let sk = SecretKey::from_bytes(&CANARY).unwrap();
drop(std::hint::black_box(Box::new(sk.to_seed())));
assert!(!disarm(), "a boxed to_seed() copy was freed unwiped");
// seal: the plaintext is copied into the output buffer and encrypted in place.
let recipient = SecretKey::generate(&mut rng(1));
arm();
let sealed = seal::seal(recipient.public_key(), &CANARY, b"aad", &mut rng(2)).unwrap();
let sealed_copy = sealed.clone();
drop(std::hint::black_box(sealed));
assert!(!disarm(), "seal freed a buffer that held the plaintext");
// open: the returned plaintext, dropped by the caller.
arm();
let opened = seal::open(&recipient, &sealed_copy, b"aad").unwrap();
assert_eq!(opened.as_slice(), CANARY);
drop(std::hint::black_box(opened));
assert!(!disarm(), "an opened plaintext was freed unwiped");
// open with the wrong aad: the failure path frees its buffer.
arm();
assert!(seal::open(&recipient, &sealed_copy, b"wrong").is_err());
assert!(!disarm(), "a failed open freed a buffer that held plaintext");
// A plaintext large enough to be split across many blocks by nobody but us.
let big: Vec<u8> = CANARY.iter().copied().cycle().take(70_000).collect();
arm();
let sealed_big = seal::seal(recipient.public_key(), &big, b"", &mut rng(3)).unwrap();
let opened_big = seal::open(&recipient, &sealed_big, b"").unwrap();
assert_eq!(opened_big.as_slice(), big.as_slice());
drop(opened_big);
drop(sealed_big);
assert!(!disarm(), "a 70 kB seal/open freed plaintext unwiped");
drop(Zeroizing::new(big));
// A whole exchange on the heap, with the canary as context.
let a = SecretKey::from_bytes(&[7; 32]).unwrap();
let b = SecretKey::from_bytes(&[8; 32]).unwrap();
arm();
let init = Box::new(exchange::initiate(&a, b.public_key(), &CANARY, &mut rng(4)).unwrap());
let (ct, rs) = exchange::respond(&b, a.public_key(), init.ciphertext(), &CANARY, &mut rng(5)).unwrap();
let is = Box::new(init.finish(&ct).unwrap());
let psk = Box::new(is.wireguard_psk(1));
drop((psk, is, rs));
assert!(!disarm(), "an exchange freed its context unwiped");
// --- v0.2.0: signatures ---
// Both seeds are the canary. Building the key runs `ml-dsa`'s `SigningKey::from_seed`, which
// keeps a copy of the ML-DSA seed in a heap box (`alloc`) and wipes it on drop (`zeroize`).
let seeds: Zeroizing<Vec<u8>> = Zeroizing::new([CANARY, CANARY].concat());
arm();
drop(std::hint::black_box(Box::new(SigningKey::from_bytes(&seeds).unwrap())));
assert!(!disarm(), "a boxed signing key (or ml-dsa's seed box) was freed with a seed in memory");
// The exported seeds.
let sk = SigningKey::from_bytes(&seeds).unwrap();
arm();
drop(std::hint::black_box(Box::new(sk.to_bytes())));
assert!(!disarm(), "a boxed to_bytes() copy was freed unwiped");
// Signing, an app message and a key binding, with the canary key; then the key is dropped.
let ctx = AppContext::new(b"probe").unwrap();
let kem = SecretKey::from_bytes(&[9; 32]).unwrap();
arm();
let sk = Box::new(SigningKey::from_bytes(&seeds).unwrap());
let sig = sk.sign(b"message", &ctx, &mut rng(6)).unwrap();
assert_eq!(sk.verifying_key().verify(b"message", &ctx, &sig), Ok(()));
let bound = sign::bind_kem_key(&sk, b"id", kem.public_key(), (0, 1), &mut rng(7)).unwrap();
assert_eq!(sign::verify_binding(sk.verifying_key(), b"id", kem.public_key(), (0, 1), 0, &bound), Ok(()));
drop((sig, bound, sk));
assert!(!disarm(), "signing freed a buffer that held a seed");
// --- QA S-04: the expanded ML-DSA key, which never contains the canary ---
// FIPS 204 skEncode of the canary key: rho (0..32), K (32..64), tr (64..128), s1 (128..768), ...
// Watch K (held raw in ml-dsa's ExpandedSigningKey) and a window of the encoded s1 (in any
// encoded copy of the key, the shape QA's mutation Q15 leaked).
#[allow(deprecated)]
let mut expanded = ml_dsa::ExpandedSigningKey::<ml_dsa::MlDsa65>::from_seed(&CANARY.into()).to_expanded();
let k: [u8; 32] = expanded[32..64].try_into().unwrap();
let s1: [u8; 32] = expanded[128..160].try_into().unwrap();
watch(&[k, s1]);
// Controls: an unwiped copy of the encoded expanded key is seen, a wiped one is not.
arm();
drop(std::hint::black_box(expanded.to_vec()));
assert!(disarm(), "control: an unwiped copy of the expanded key must be seen");
arm();
drop(std::hint::black_box(Zeroizing::new(expanded.to_vec())));
assert!(!disarm(), "control: a Zeroizing copy must not be seen");
expanded.as_mut_slice().zeroize();
// Arcanum: build, sign, bind and drop with the canary key, watching the expanded key's bytes.
arm();
let sk = Box::new(SigningKey::from_bytes(&seeds).unwrap());
let sig = sk.sign(b"message", &ctx, &mut rng(8)).unwrap();
let bound = sign::bind_kem_key(&sk, b"id", kem.public_key(), (0, 1), &mut rng(9)).unwrap();
drop((sig, bound, sk));
assert!(!disarm(), "a signing key was freed with expanded ML-DSA key material (K or s1) in memory");
watch(&[]);
}