feat: X-Wing (draft -11), mutual exchange, WireGuard PSK per epoch, sealed box

X-Wing is composed from ml-kem, x25519-dalek, sha3 and shake and matches the
draft's vectors. The PSK comes from a mutual exchange (each peer encapsulates
to the other, QA A-15) bound to the full transcript in Arcanum's own HKDF layer
(QA D-QA-1). Low-order X25519 values are refused. Deterministic entry points
sit behind the off-by-default hazmat feature, enabled only for the tests.
This commit is contained in:
2026-11-25 07:38:55 +01:00
parent c7d050ed65
commit 89b5329f06
15 changed files with 1316 additions and 118 deletions
+52 -2
View File
@@ -48,16 +48,20 @@ fn ml_kem_key_failing_the_modulus_check_is_a_typed_error() {
fn random_bytes_never_panic() {
let mut r = rng(9);
let sk = SecretKey::from_seed([0x01; 32]);
let peer = SecretKey::from_seed([0x02; 32]);
let (honest_ct, _) = peer.public_key().encapsulate(&mut r).unwrap();
for round in 0..512 {
let len = [PUBLIC_KEY_LEN, CIPHERTEXT_LEN, seal::OVERHEAD, seal::OVERHEAD + 40, 0, 17][round % 6];
let mut buf = vec![0u8; len];
r.fill_bytes(&mut buf);
if let Ok(pk) = PublicKey::from_bytes(&buf) {
let _ = exchange::initiate(&pk, b"ctx", &mut r);
let _ = exchange::initiate(&sk, &pk, b"ctx", &mut r);
let _ = exchange::respond(&sk, &pk, &honest_ct, b"ctx", &mut r);
}
if let Ok(ct) = Ciphertext::from_bytes(&buf) {
// Random ML-KEM ciphertexts decapsulate (implicit rejection) to some unrelated secret.
let _ = exchange::respond(&sk, &ct, b"ctx");
let _ = exchange::respond(&sk, peer.public_key(), &ct, b"ctx", &mut r);
let _ = exchange::initiate(&sk, peer.public_key(), b"ctx", &mut r).unwrap().finish(&ct);
}
assert!(seal::open(&sk, &buf, b"aad").is_err(), "random bytes must never open");
}
@@ -129,3 +133,49 @@ fn errors_display_without_secrets() {
assert_eq!(e.to_string(), "ciphertext has the wrong length: expected 1120 bytes, got 3");
let _: &dyn std::error::Error = &e;
}
/// QA A-20: the modulus check is not over-eager. The last 32 bytes of the ML-KEM key are the
/// seed rho, where any value is legal.
#[test]
fn ml_kem_key_check_accepts_any_rho() {
let sk = SecretKey::from_seed([0x03; 32]);
let mut ok = *sk.public_key().as_bytes();
ok[1183] ^= 1;
assert!(PublicKey::from_bytes(&ok).is_ok());
}
/// QA A-21: the only stored private-key form is the 32-byte seed; an expanded ML-KEM key or an
/// X-Wing key plus anything else is refused by length.
#[test]
fn only_the_32_byte_seed_is_a_secret_key() {
for len in [64, 96, 2400, 2432] {
assert!(matches!(SecretKey::from_bytes(&vec![0u8; len]), Err(Error::InvalidLength { .. })), "{len}");
}
}
/// QA A-24: a one-byte extension and a splice (header of one box, body of another) are refused.
#[test]
fn sealed_box_extension_and_splice_are_refused() {
let mut r = rng(14);
let sk = SecretKey::generate(&mut r);
let b1 = seal::seal(sk.public_key(), b"one", b"", &mut r).unwrap();
let b2 = seal::seal(sk.public_key(), b"two", b"", &mut r).unwrap();
let mut ext = b1.clone();
ext.push(0);
assert_eq!(seal::open(&sk, &ext, b"").unwrap_err(), Error::DecryptionFailed);
let mut spliced = b1[..1121].to_vec();
spliced.extend_from_slice(&b2[1121..]);
assert_eq!(seal::open(&sk, &spliced, b"").unwrap_err(), Error::DecryptionFailed);
}
/// QA A-26: all randomness comes from the RNG parameter: the same seed gives the same box, a
/// different seed a different one.
#[test]
fn seal_randomness_comes_only_from_the_rng() {
let sk = SecretKey::from_seed([0x04; 32]);
let a = seal::seal(sk.public_key(), b"m", b"", &mut rng(1)).unwrap();
let b = seal::seal(sk.public_key(), b"m", b"", &mut rng(1)).unwrap();
let c = seal::seal(sk.public_key(), b"m", b"", &mut rng(2)).unwrap();
assert_eq!(a, b);
assert_ne!(a, c);
}