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:
+65
-22
@@ -1,14 +1,17 @@
|
||||
//! Mandatory test 3, through the public API: an attacker can force the X25519 half to zero by
|
||||
//! sending a low-order point. Arcanum must refuse that with a typed error instead of producing a
|
||||
//! secret that only one half protects. (The combiner-level half of test 3, which zeroes each
|
||||
//! component directly, is `src/binding_tests.rs`.)
|
||||
//! sending a low-order point. Arcanum refuses that with a typed error instead of producing a
|
||||
//! secret that only one half protects (QA A-16, A-17). The combiner-level half of test 3, which
|
||||
//! zeroes each component directly, is `src/binding_tests.rs`. Also QA A-04 (raw ct_X is hashed).
|
||||
|
||||
mod common;
|
||||
|
||||
use arcanum::Error;
|
||||
use arcanum::exchange;
|
||||
use arcanum::xwing::{Ciphertext, PublicKey, SecretKey};
|
||||
use common::rng;
|
||||
|
||||
/// Curve25519 u-coordinates of small order, and non-canonical encodings of them (the list
|
||||
/// libsodium rejects). X25519 of any of these with any clamped scalar is all zeros.
|
||||
/// Curve25519 u-coordinates of small order and non-canonical encodings of them (libsodium's
|
||||
/// blocklist). Each is also used with bit 255 set, which X25519 ignores (RFC 7748 §5).
|
||||
const LOW_ORDER: [&str; 7] = [
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
"0100000000000000000000000000000000000000000000000000000000000000",
|
||||
@@ -19,40 +22,80 @@ const LOW_ORDER: [&str; 7] = [
|
||||
"eeffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f",
|
||||
];
|
||||
|
||||
fn low_order_points() -> impl Iterator<Item = [u8; 32]> {
|
||||
LOW_ORDER.iter().map(|h| hex::decode(h).unwrap().try_into().unwrap())
|
||||
fn low_order_points() -> Vec<[u8; 32]> {
|
||||
LOW_ORDER
|
||||
.iter()
|
||||
.flat_map(|h| {
|
||||
let p: [u8; 32] = hex::decode(h).unwrap().try_into().unwrap();
|
||||
let mut high = p;
|
||||
high[31] |= 0x80;
|
||||
[p, high]
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Guards the fixture: if a byte above were mistyped, the point would not be low order.
|
||||
#[test]
|
||||
fn the_fixture_points_really_are_low_order() {
|
||||
let s = x25519_dalek::StaticSecret::from([0x5a; 32]);
|
||||
for p in low_order_points() {
|
||||
assert!(!s.diffie_hellman(&p.into()).was_contributory(), "{}", hex::encode(p));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_order_x25519_ciphertext_is_refused_by_the_responder() {
|
||||
let sk = SecretKey::from_seed([0x44; 32]);
|
||||
let (ct, _) = exchange::initiate_derand(sk.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
fn low_order_x25519_ciphertext_is_refused() {
|
||||
let i = SecretKey::from_seed([0x43; 32]);
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
let init = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
let (ct_ri, _) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), b"ctx", &[0x56; 64]).unwrap();
|
||||
for point in low_order_points() {
|
||||
let mut bad = *ct.as_bytes();
|
||||
let mut bad = *init.ciphertext().as_bytes();
|
||||
bad[1088..].copy_from_slice(&point);
|
||||
let bad = Ciphertext::from_bytes(&bad).unwrap();
|
||||
assert_eq!(exchange::respond(&sk, &bad, b"ctx").err(), Some(Error::NonContributory));
|
||||
assert_eq!(sk.decapsulate(&bad).err(), Some(Error::NonContributory));
|
||||
assert_eq!(r.decapsulate(&bad).err(), Some(Error::NonContributory));
|
||||
let resp = exchange::respond_derand(&r, i.public_key(), &bad, b"ctx", &[0x56; 64]);
|
||||
assert_eq!(resp.err(), Some(Error::NonContributory));
|
||||
|
||||
let mut bad = *ct_ri.as_bytes();
|
||||
bad[1088..].copy_from_slice(&point);
|
||||
let again = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
assert_eq!(again.finish(&Ciphertext::from_bytes(&bad).unwrap()).err(), Some(Error::NonContributory));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn low_order_x25519_public_key_is_refused_by_the_initiator() {
|
||||
let sk = SecretKey::from_seed([0x44; 32]);
|
||||
fn low_order_x25519_public_key_is_refused() {
|
||||
let i = SecretKey::from_seed([0x43; 32]);
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
for point in low_order_points() {
|
||||
let mut bad = *sk.public_key().as_bytes();
|
||||
let mut bad = *r.public_key().as_bytes();
|
||||
bad[1184..].copy_from_slice(&point);
|
||||
let pk = PublicKey::from_bytes(&bad).expect("the ML-KEM part is still valid");
|
||||
assert_eq!(exchange::initiate_derand(&pk, b"ctx", &[0x55; 64]).err(), Some(Error::NonContributory));
|
||||
assert_eq!(pk.encapsulate_derand(&[0x55; 64]).err(), Some(Error::NonContributory));
|
||||
assert_eq!(exchange::initiate_derand(&i, &pk, b"ctx", &[0x55; 64]).err(), Some(Error::NonContributory));
|
||||
assert_eq!(arcanum::seal::seal(&pk, b"x", b"", &mut rng(1)).err(), Some(Error::NonContributory));
|
||||
}
|
||||
}
|
||||
|
||||
/// The honest exchange is not affected by the check: the same keys with honest points still agree.
|
||||
/// QA A-04: X25519 ignores bit 255 of ct_X, so ct_X and ct_X ^ 0x80 give the same ss_X. The
|
||||
/// output must still differ, because the combiner hashes the raw bytes.
|
||||
#[test]
|
||||
fn the_high_bit_of_ct_x_is_bound() {
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
let (ct, ss) = r.public_key().encapsulate(&mut rng(4)).unwrap();
|
||||
let mut b = *ct.as_bytes();
|
||||
b[1119] ^= 0x80;
|
||||
let out = r.decapsulate(&Ciphertext::from_bytes(&b).unwrap()).unwrap();
|
||||
assert_ne!(out.as_bytes(), ss.as_bytes());
|
||||
}
|
||||
|
||||
/// The honest exchange is not affected by the check.
|
||||
#[test]
|
||||
fn honest_points_pass_the_check() {
|
||||
let sk = SecretKey::from_seed([0x44; 32]);
|
||||
let (ct, a) = exchange::initiate_derand(sk.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
let b = exchange::respond(&sk, &ct, b"ctx").unwrap();
|
||||
assert_eq!(a.wireguard_psk(3).as_bytes(), b.wireguard_psk(3).as_bytes());
|
||||
let i = SecretKey::from_seed([0x43; 32]);
|
||||
let r = SecretKey::from_seed([0x44; 32]);
|
||||
let init = exchange::initiate_derand(&i, r.public_key(), b"ctx", &[0x55; 64]).unwrap();
|
||||
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), b"ctx", &[0x56; 64]).unwrap();
|
||||
assert_eq!(init.finish(&ct_ri).unwrap().wireguard_psk(3).as_bytes(), rs.wireguard_psk(3).as_bytes());
|
||||
}
|
||||
|
||||
+175
-77
@@ -1,10 +1,10 @@
|
||||
//! Mandatory test 2: both sides of a hybrid exchange derive the same secret, and changing any
|
||||
//! single input byte changes it.
|
||||
//! single input byte changes it. Also QA A-14 (roles bound) and A-15 (both directions needed).
|
||||
//!
|
||||
//! 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.
|
||||
//! 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;
|
||||
|
||||
@@ -13,133 +13,231 @@ use arcanum::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, Secr
|
||||
use common::rng;
|
||||
use std::collections::HashSet;
|
||||
|
||||
const SEED: [u8; 32] = [0x11; 32];
|
||||
const ESEED: [u8; 64] = [0x22; 64];
|
||||
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 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()
|
||||
}
|
||||
|
||||
/// 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 r = rng(1);
|
||||
let mut g = 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();
|
||||
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!(initiator.wireguard_psk(epoch).as_bytes(), responder.wireguard_psk(epoch).as_bytes());
|
||||
assert_eq!(is.wireguard_psk(epoch).as_bytes(), rs.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));
|
||||
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 r = rng(2);
|
||||
let sk = SecretKey::generate(&mut r);
|
||||
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 (_, s) = exchange::initiate(sk.public_key(), CONTEXT, &mut r).unwrap();
|
||||
assert!(seen.insert(psk(&s)), "two exchanges produced the same secret");
|
||||
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_ciphertext_byte_changes_the_responders_secret() {
|
||||
let (honest_psk, ct) = honest();
|
||||
let sk = SecretKey::from_seed(SEED);
|
||||
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 i in 0..CIPHERTEXT_LEN {
|
||||
let mut bad = *ct.as_bytes();
|
||||
bad[i] ^= 0x01;
|
||||
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(&sk, &bad, CONTEXT) {
|
||||
assert_ne!(psk(&s), honest_psk, "ciphertext byte {i} did not change the 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_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,
|
||||
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!(refused < PUBLIC_KEY_LEN, "most flipped keys still parse; the test must exercise them");
|
||||
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();
|
||||
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}");
|
||||
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}");
|
||||
}
|
||||
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}");
|
||||
// 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_and_a_mismatch_disagrees() {
|
||||
let (honest_psk, ct) = honest();
|
||||
let sk = SecretKey::from_seed(SEED);
|
||||
for i in 0..CONTEXT.len() {
|
||||
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[i] ^= 0x01;
|
||||
let s = exchange::respond(&sk, &ct, &ctx).unwrap();
|
||||
assert_ne!(psk(&s), honest_psk, "context byte {i}");
|
||||
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()[..]] {
|
||||
let s = exchange::respond(&sk, &ct, ctx).unwrap();
|
||||
assert_ne!(psk(&s), honest_psk);
|
||||
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) = honest();
|
||||
let (honest_psk, ct_ir, _) = honest();
|
||||
let i = SecretKey::from_seed(SEED_I);
|
||||
let other = SecretKey::from_seed([0x33; 32]);
|
||||
let s = exchange::respond(&other, &ct, CONTEXT).unwrap();
|
||||
let (_, s) = exchange::respond_derand(&other, i.public_key(), &ct_ir, CONTEXT, &ESEED_R).unwrap();
|
||||
assert_ne!(psk(&s), honest_psk);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//! QA A-35: no test-only feature may reach a normal (release) build through Cargo feature
|
||||
//! unification. Checks the normal-edge dependency graph that a consumer of Arcanum would get.
|
||||
//!
|
||||
//! `arcanum/hazmat` exposes the deterministic entry points; `ml-kem/hazmat` only un-hides docs in
|
||||
//! 0.3.4 (its `encapsulate_deterministic` is public either way), but is kept out on principle.
|
||||
|
||||
use std::process::Command;
|
||||
|
||||
fn tree(args: &[&str]) -> String {
|
||||
let out = Command::new(env!("CARGO"))
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.args(["tree", "--offline", "--locked", "-e", "features,normal", "--prefix", "none"])
|
||||
.args(args)
|
||||
.output()
|
||||
.expect("run cargo tree");
|
||||
assert!(out.status.success(), "cargo tree failed: {}", String::from_utf8_lossy(&out.stderr));
|
||||
String::from_utf8(out.stdout).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hazmat_is_not_in_the_normal_graph() {
|
||||
let t = tree(&["-p", "arcanum"]);
|
||||
assert!(t.contains("ml-kem"), "the tree must include ml-kem, or this test checks nothing:\n{t}");
|
||||
for line in t.lines() {
|
||||
assert!(!line.contains("feature \"hazmat\""), "hazmat reached a normal build: {line}\n{t}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_os_rng_in_the_normal_graph() {
|
||||
// Randomness is a parameter (QA A-36): nothing in the normal graph may pull an OS RNG.
|
||||
let t = tree(&["-p", "arcanum"]);
|
||||
assert!(!t.contains("getrandom"), "getrandom reached a normal build:\n{t}");
|
||||
}
|
||||
+52
-2
@@ -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);
|
||||
}
|
||||
|
||||
+2
-1
@@ -31,7 +31,8 @@ fn no_source_file_contains_unsafe() {
|
||||
let text = fs::read_to_string(&f).unwrap();
|
||||
for (n, line) in text.lines().enumerate() {
|
||||
let code = line.split("//").next().unwrap_or("");
|
||||
assert!(!code.contains("unsafe"), "{}:{}: {line}", f.display(), n + 1);
|
||||
let opts_in = code.contains("unsafe") && code.trim() != "#![forbid(unsafe_code)]";
|
||||
assert!(!opts_in, "{}:{}: {line}", f.display(), n + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
-7
@@ -8,11 +8,16 @@ use std::collections::HashSet;
|
||||
|
||||
const CONTEXT: &[u8] = b"via/test: epochs";
|
||||
|
||||
fn exchange_with(eseed_i: [u8; 64]) -> (exchange::Session, exchange::Session) {
|
||||
let i = SecretKey::from_seed([0x01; 32]);
|
||||
let r = SecretKey::from_seed([0x04; 32]);
|
||||
let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &eseed_i).unwrap();
|
||||
let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &[0x05; 64]).unwrap();
|
||||
(init.finish(&ct_ri).unwrap(), rs)
|
||||
}
|
||||
|
||||
fn sessions() -> (exchange::Session, exchange::Session) {
|
||||
let sk = SecretKey::from_seed([0x01; 32]);
|
||||
let (ct, a) = exchange::initiate_derand(sk.public_key(), CONTEXT, &[0x02; 64]).unwrap();
|
||||
let b = exchange::respond(&sk, &ct, CONTEXT).unwrap();
|
||||
(a, b)
|
||||
exchange_with([0x02; 64])
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -43,9 +48,8 @@ fn the_same_epoch_is_reproducible_from_the_same_inputs() {
|
||||
|
||||
#[test]
|
||||
fn a_different_exchange_gives_different_psks_for_the_same_epoch() {
|
||||
let sk = SecretKey::from_seed([0x01; 32]);
|
||||
let (_, a) = exchange::initiate_derand(sk.public_key(), CONTEXT, &[0x02; 64]).unwrap();
|
||||
let (_, c) = exchange::initiate_derand(sk.public_key(), CONTEXT, &[0x03; 64]).unwrap();
|
||||
let (a, _) = exchange_with([0x02; 64]);
|
||||
let (c, _) = exchange_with([0x03; 64]);
|
||||
for epoch in 0..16 {
|
||||
assert_ne!(a.wireguard_psk(epoch).as_bytes(), c.wireguard_psk(epoch).as_bytes());
|
||||
}
|
||||
@@ -83,3 +87,13 @@ fn epoch_helper_uses_two_minute_epochs() {
|
||||
assert_eq!(epoch_at(1_764_000_000), 1_764_000_000 / 120);
|
||||
assert_eq!(epoch_at(u64::MAX), u64::MAX / 120);
|
||||
}
|
||||
|
||||
/// QA A-11: the epoch is encoded at its full 64-bit width; nothing truncates it.
|
||||
#[test]
|
||||
fn the_epoch_is_not_truncated() {
|
||||
let (a, _) = sessions();
|
||||
let p = |e: u64| *a.wireguard_psk(e).as_bytes();
|
||||
assert_ne!(p(0), p(1 << 32));
|
||||
assert_ne!(p(1), p(1 << 8));
|
||||
assert_ne!(p(u64::MAX), p(u64::MAX >> 1));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user