Files
arcanum/tests/exchange.rs
T

146 lines
5.4 KiB
Rust

//! Mandatory test 2: both sides of a hybrid exchange derive the same secret, and changing any
//! single input byte changes it.
//!
//! The inputs of one exchange are: the responder's seed, the initiator's encapsulation seed, the
//! responder's public key (as the initiator received it), the ciphertext (as the responder
//! received it), and the context each side passes. Every byte of the public key, the ciphertext,
//! 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: [u8; 32] = [0x11; 32];
const ESEED: [u8; 64] = [0x22; 64];
const CONTEXT: &[u8] = b"via/test: device-a <-> device-b";
fn honest() -> ([u8; 32], Ciphertext) {
let sk = SecretKey::from_seed(SEED);
let (ct, session) = exchange::initiate_derand(sk.public_key(), CONTEXT, &ESEED).unwrap();
(*session.wireguard_psk(0).as_bytes(), ct)
}
fn psk(session: &Session) -> [u8; 32] {
*session.wireguard_psk(0).as_bytes()
}
#[test]
fn both_sides_derive_the_same_secret() {
let mut r = rng(1);
for _ in 0..32 {
let sk = SecretKey::generate(&mut r);
let (ct, initiator) = exchange::initiate(sk.public_key(), CONTEXT, &mut r).unwrap();
let responder = exchange::respond(&sk, &ct, CONTEXT).unwrap();
for epoch in [0, 1, 7, u64::MAX] {
assert_eq!(initiator.wireguard_psk(epoch).as_bytes(), responder.wireguard_psk(epoch).as_bytes());
}
}
}
#[test]
fn both_sides_agree_after_the_public_key_and_ciphertext_go_through_bytes() {
let sk = SecretKey::from_seed(SEED);
let pk = PublicKey::from_bytes(sk.public_key().as_bytes()).unwrap();
let (ct, initiator) = exchange::initiate_derand(&pk, CONTEXT, &ESEED).unwrap();
let ct = Ciphertext::from_bytes(ct.as_bytes()).unwrap();
let responder = exchange::respond(&sk, &ct, CONTEXT).unwrap();
assert_eq!(psk(&initiator), psk(&responder));
}
#[test]
fn fresh_randomness_gives_a_fresh_secret() {
let mut r = rng(2);
let sk = SecretKey::generate(&mut r);
let mut seen = HashSet::new();
for _ in 0..64 {
let (_, s) = exchange::initiate(sk.public_key(), CONTEXT, &mut r).unwrap();
assert!(seen.insert(psk(&s)), "two exchanges produced the same secret");
}
}
#[test]
fn every_ciphertext_byte_changes_the_responders_secret() {
let (honest_psk, ct) = honest();
let sk = SecretKey::from_seed(SEED);
let mut accepted = 0;
for i in 0..CIPHERTEXT_LEN {
let mut bad = *ct.as_bytes();
bad[i] ^= 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(&sk, &bad, CONTEXT) {
assert_ne!(psk(&s), honest_psk, "ciphertext byte {i} did not change the secret");
accepted += 1;
}
}
assert!(accepted > CIPHERTEXT_LEN - 32, "flipped ciphertexts must mostly decapsulate (implicit rejection)");
}
#[test]
fn every_public_key_byte_changes_the_initiators_secret() {
let (honest_psk, _) = honest();
let sk = SecretKey::from_seed(SEED);
let mut refused = 0;
for i in 0..PUBLIC_KEY_LEN {
let mut bad = *sk.public_key().as_bytes();
bad[i] ^= 0x01;
// A flipped ML-KEM coefficient can fail the FIPS 203 modulus check: refused, not accepted.
let Ok(pk) = PublicKey::from_bytes(&bad) else {
refused += 1;
continue;
};
match exchange::initiate_derand(&pk, CONTEXT, &ESEED) {
Ok((_, s)) => assert_ne!(psk(&s), honest_psk, "public key byte {i} did not change the secret"),
Err(_) => refused += 1,
}
}
assert!(refused < 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();
for i in 0..32 {
let mut seed = SEED;
seed[i] ^= 0x01;
let sk = SecretKey::from_seed(seed);
let (_, s) = exchange::initiate_derand(sk.public_key(), CONTEXT, &ESEED).unwrap();
assert_ne!(psk(&s), honest_psk, "seed byte {i}");
}
for i in 0..64 {
let mut eseed = ESEED;
eseed[i] ^= 0x01;
let sk = SecretKey::from_seed(SEED);
let (_, s) = exchange::initiate_derand(sk.public_key(), CONTEXT, &eseed).unwrap();
assert_ne!(psk(&s), honest_psk, "eseed byte {i}");
}
}
#[test]
fn every_context_byte_changes_the_secret_and_a_mismatch_disagrees() {
let (honest_psk, ct) = honest();
let sk = SecretKey::from_seed(SEED);
for i in 0..CONTEXT.len() {
let mut ctx = CONTEXT.to_vec();
ctx[i] ^= 0x01;
let s = exchange::respond(&sk, &ct, &ctx).unwrap();
assert_ne!(psk(&s), honest_psk, "context byte {i}");
}
// 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()[..]] {
let s = exchange::respond(&sk, &ct, ctx).unwrap();
assert_ne!(psk(&s), honest_psk);
}
}
#[test]
fn the_wrong_secret_key_does_not_agree() {
let (honest_psk, ct) = honest();
let other = SecretKey::from_seed([0x33; 32]);
let s = exchange::respond(&other, &ct, CONTEXT).unwrap();
assert_ne!(psk(&s), honest_psk);
}