Files
arcanum/tests/exchange.rs
T
mnovak 89b5329f06 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.
2026-11-25 07:38:55 +01:00

244 lines
11 KiB
Rust

//! Mandatory test 2: both sides of a hybrid exchange derive the same secret, and changing any
//! single input byte changes it. Also QA A-14 (roles bound) and A-15 (both directions needed).
//!
//! The exchange is mutual (each peer encapsulates to the other, docs/01_arcanum.md). Its inputs
//! are: both peers' seeds, both encapsulation seeds, both public keys (as the other side received
//! them), both ciphertexts (as the other side received them), and the context. Every byte of the
//! public keys, the ciphertexts, the seeds and the context is flipped in turn.
mod common;
use arcanum::exchange::{self, Session};
use arcanum::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SecretKey};
use common::rng;
use std::collections::HashSet;
const SEED_I: [u8; 32] = [0x11; 32];
const SEED_R: [u8; 32] = [0x12; 32];
const ESEED_I: [u8; 64] = [0x21; 64];
const ESEED_R: [u8; 64] = [0x22; 64];
const CONTEXT: &[u8] = b"via/test: device-a <-> device-b";
fn psk(session: &Session) -> [u8; 32] {
*session.wireguard_psk(0).as_bytes()
}
/// The honest exchange: (PSK, ct_IR, ct_RI).
fn honest() -> ([u8; 32], Ciphertext, Ciphertext) {
let i = SecretKey::from_seed(SEED_I);
let r = SecretKey::from_seed(SEED_R);
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &ESEED_I).unwrap();
let ct_ir = init.ciphertext().clone();
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), &ct_ir, CONTEXT, &ESEED_R).unwrap();
let is = init.finish(&ct_ri).unwrap();
assert_eq!(psk(&is), psk(&rs));
(psk(&rs), ct_ir, ct_ri)
}
#[test]
fn both_sides_derive_the_same_secret() {
let mut g = rng(1);
for _ in 0..32 {
let i = SecretKey::generate(&mut g);
let r = SecretKey::generate(&mut g);
let init = exchange::initiate(&i, r.public_key(), CONTEXT, &mut g).unwrap();
let (ct_ri, rs) = exchange::respond(&r, i.public_key(), init.ciphertext(), CONTEXT, &mut g).unwrap();
let is = init.finish(&ct_ri).unwrap();
for epoch in [0, 1, 7, u64::MAX] {
assert_eq!(is.wireguard_psk(epoch).as_bytes(), rs.wireguard_psk(epoch).as_bytes());
}
}
}
#[test]
fn both_sides_agree_after_keys_and_ciphertexts_go_through_bytes() {
let i = SecretKey::from_seed(SEED_I);
let r = SecretKey::from_seed(SEED_R);
let r_pk = PublicKey::from_bytes(r.public_key().as_bytes()).unwrap();
let i_pk = PublicKey::from_bytes(i.public_key().as_bytes()).unwrap();
let init = exchange::initiate_derand(&i, &r_pk, CONTEXT, &ESEED_I).unwrap();
let ct_ir = Ciphertext::from_bytes(init.ciphertext().as_bytes()).unwrap();
let (ct_ri, rs) = exchange::respond_derand(&r, &i_pk, &ct_ir, CONTEXT, &ESEED_R).unwrap();
let ct_ri = Ciphertext::from_bytes(ct_ri.as_bytes()).unwrap();
assert_eq!(psk(&init.finish(&ct_ri).unwrap()), psk(&rs));
}
#[test]
fn fresh_randomness_gives_a_fresh_secret() {
let mut g = rng(2);
let i = SecretKey::generate(&mut g);
let r = SecretKey::generate(&mut g);
let mut seen = HashSet::new();
for _ in 0..64 {
let init = exchange::initiate(&i, r.public_key(), CONTEXT, &mut g).unwrap();
let (_, rs) = exchange::respond(&r, i.public_key(), init.ciphertext(), CONTEXT, &mut g).unwrap();
assert!(seen.insert(psk(&rs)), "two exchanges produced the same secret");
}
}
/// Flip each byte of ct_IR as the responder receives it: the responder's secret changes.
#[test]
fn every_byte_of_the_initiators_ciphertext_changes_the_secret() {
let (honest_psk, ct_ir, _) = honest();
let i = SecretKey::from_seed(SEED_I);
let r = SecretKey::from_seed(SEED_R);
let mut accepted = 0;
for n in 0..CIPHERTEXT_LEN {
let mut bad = *ct_ir.as_bytes();
bad[n] ^= 0x01;
let bad = Ciphertext::from_bytes(&bad).unwrap();
// A refusal (a low-order X25519 value) is also "not the honest secret".
if let Ok((_, s)) = exchange::respond_derand(&r, i.public_key(), &bad, CONTEXT, &ESEED_R) {
assert_ne!(psk(&s), honest_psk, "ct_IR byte {n} did not change the secret");
accepted += 1;
}
}
assert!(accepted > CIPHERTEXT_LEN - 32, "flipped ciphertexts must mostly decapsulate (implicit rejection)");
}
/// Flip each byte of ct_RI as the initiator receives it: the initiator's secret changes.
#[test]
fn every_byte_of_the_responders_ciphertext_changes_the_secret() {
let (honest_psk, _, ct_ri) = honest();
let i = SecretKey::from_seed(SEED_I);
let r = SecretKey::from_seed(SEED_R);
let mut accepted = 0;
for n in 0..CIPHERTEXT_LEN {
let mut bad = *ct_ri.as_bytes();
bad[n] ^= 0x01;
let bad = Ciphertext::from_bytes(&bad).unwrap();
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &ESEED_I).unwrap();
if let Ok(s) = init.finish(&bad) {
assert_ne!(psk(&s), honest_psk, "ct_RI byte {n} did not change the secret");
accepted += 1;
}
}
assert!(accepted > CIPHERTEXT_LEN - 32);
}
/// Flip each byte of the responder's public key as the initiator received it, and of the
/// initiator's public key as the responder received it.
#[test]
fn every_public_key_byte_changes_the_secret() {
let (honest_psk, ct_ir, _) = honest();
let i = SecretKey::from_seed(SEED_I);
let r = SecretKey::from_seed(SEED_R);
let mut used = 0;
for n in 0..PUBLIC_KEY_LEN {
// The initiator's view of the responder's key.
let mut bad = *r.public_key().as_bytes();
bad[n] ^= 0x01;
// A flipped ML-KEM coefficient can fail the FIPS 203 modulus check: refused, not accepted.
if let Ok(pk) = PublicKey::from_bytes(&bad) {
let init = exchange::initiate_derand(&i, &pk, CONTEXT, &ESEED_I).unwrap();
let (ct_ri, rs) =
exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &ESEED_R).unwrap();
let is = init.finish(&ct_ri).unwrap();
assert_ne!(psk(&is), honest_psk, "initiator: responder key byte {n}");
assert_ne!(psk(&is), psk(&rs), "initiator: responder key byte {n} still agrees");
used += 1;
}
// The responder's view of the initiator's key.
let mut bad = *i.public_key().as_bytes();
bad[n] ^= 0x01;
if let Ok(pk) = PublicKey::from_bytes(&bad) {
let (_, rs) = exchange::respond_derand(&r, &pk, &ct_ir, CONTEXT, &ESEED_R).unwrap();
assert_ne!(psk(&rs), honest_psk, "responder: initiator key byte {n}");
used += 1;
}
}
assert!(used > PUBLIC_KEY_LEN, "most flipped keys still parse; the test must exercise them");
}
#[test]
fn every_seed_byte_changes_the_secret() {
let (honest_psk, _, _) = honest();
let run = |si: [u8; 32], sr: [u8; 32], ei: [u8; 64], er: [u8; 64]| {
let i = SecretKey::from_seed(si);
let r = SecretKey::from_seed(sr);
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &ei).unwrap();
let (_, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &er).unwrap();
psk(&rs)
};
for n in 0..32 {
let mut s = SEED_I;
s[n] ^= 1;
assert_ne!(run(s, SEED_R, ESEED_I, ESEED_R), honest_psk, "initiator seed byte {n}");
let mut s = SEED_R;
s[n] ^= 1;
assert_ne!(run(SEED_I, s, ESEED_I, ESEED_R), honest_psk, "responder seed byte {n}");
}
// Bit 4 of each byte: X25519 clamps bits 0-2 of byte 32 and bits 6-7 of byte 63 of the
// ephemeral scalar (RFC 7748 §5), so flipping those bits is, by design, no change at all.
for n in 0..64 {
let mut e = ESEED_I;
e[n] ^= 0x10;
assert_ne!(run(SEED_I, SEED_R, e, ESEED_R), honest_psk, "initiator eseed byte {n}");
let mut e = ESEED_R;
e[n] ^= 0x10;
assert_ne!(run(SEED_I, SEED_R, ESEED_I, e), honest_psk, "responder eseed byte {n}");
}
}
#[test]
fn every_context_byte_changes_the_secret() {
let (honest_psk, ct_ir, _) = honest();
let i = SecretKey::from_seed(SEED_I);
let r = SecretKey::from_seed(SEED_R);
let resp = |ctx: &[u8]| psk(&exchange::respond_derand(&r, i.public_key(), &ct_ir, ctx, &ESEED_R).unwrap().1);
for n in 0..CONTEXT.len() {
let mut ctx = CONTEXT.to_vec();
ctx[n] ^= 0x01;
assert_ne!(resp(&ctx), honest_psk, "context byte {n}");
}
// Length is bound too: a prefix or an extension of the context is a different context.
for ctx in [&CONTEXT[..CONTEXT.len() - 1], b"", &[CONTEXT, b"\0"].concat()[..]] {
assert_ne!(resp(ctx), honest_psk);
}
}
/// QA A-15: the attacker knows both public keys and plays the initiator without the initiator's
/// secret key. She can make ct_IR (she knows ss_IR), but she cannot decapsulate ct_RI, which the
/// responder encapsulated to the real initiator. So she cannot compute the responder's PSK.
#[test]
fn an_impersonated_initiator_cannot_compute_the_psk() {
let i = SecretKey::from_seed(SEED_I); // the real initiator, whom the attacker impersonates
let r = SecretKey::from_seed(SEED_R);
let mallory = SecretKey::from_seed([0x66; 32]);
let mut g = rng(3);
// Mallory encapsulates to the responder (anyone can) and sends it as "from I".
let forged = exchange::initiate(&mallory, r.public_key(), CONTEXT, &mut g).unwrap();
let (ct_ri, responder_session) =
exchange::respond(&r, i.public_key(), forged.ciphertext(), CONTEXT, &mut g).unwrap();
// Her best effort: finish with her own key. ct_RI was made for I's key, not hers.
let mallory_session = forged.finish(&ct_ri).unwrap();
assert_ne!(psk(&mallory_session), psk(&responder_session));
}
/// QA A-14: roles are bound by position. Running the exchange with the roles swapped (same two
/// keys, same randomness) gives a different PSK, so a reflected message cannot complete it.
#[test]
fn roles_are_bound() {
let a = SecretKey::from_seed(SEED_I);
let b = SecretKey::from_seed(SEED_R);
let ab = exchange::initiate_derand(&a, b.public_key(), CONTEXT, &ESEED_I).unwrap();
let (ct, ab_resp) = exchange::respond_derand(&b, a.public_key(), ab.ciphertext(), CONTEXT, &ESEED_R).unwrap();
let ab_init = ab.finish(&ct).unwrap();
let ba = exchange::initiate_derand(&b, a.public_key(), CONTEXT, &ESEED_I).unwrap();
let (ct, ba_resp) = exchange::respond_derand(&a, b.public_key(), ba.ciphertext(), CONTEXT, &ESEED_R).unwrap();
let ba_init = ba.finish(&ct).unwrap();
assert_eq!(psk(&ab_init), psk(&ab_resp));
assert_eq!(psk(&ba_init), psk(&ba_resp));
assert_ne!(psk(&ab_init), psk(&ba_init));
}
#[test]
fn the_wrong_secret_key_does_not_agree() {
let (honest_psk, ct_ir, _) = honest();
let i = SecretKey::from_seed(SEED_I);
let other = SecretKey::from_seed([0x33; 32]);
let (_, s) = exchange::respond_derand(&other, i.public_key(), &ct_ir, CONTEXT, &ESEED_R).unwrap();
assert_ne!(psk(&s), honest_psk);
}