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:
+92
-9
@@ -1,8 +1,11 @@
|
||||
//! Mandatory test 3, at the combiner: replacing either component secret by zeros must change the
|
||||
//! output, so both halves are bound. These are unit tests because the combiner is crate-private
|
||||
//! (exposing it would invite misuse).
|
||||
//! output, so both halves are bound. Also QA A-02 (the combiner is exactly the draft's SHA3-256)
|
||||
//! and A-07 (labels are prefix-free). These are unit tests because the combiner and the schedule
|
||||
//! are crate-private (exposing them would invite misuse).
|
||||
|
||||
use crate::schedule::session_prk;
|
||||
use sha3::{Digest, Sha3_256};
|
||||
|
||||
use crate::schedule::{ALL_LABELS, session_prk_raw};
|
||||
use crate::xwing::{SHARED_SECRET_LEN, combiner};
|
||||
|
||||
const SS_M: [u8; 32] = [0xa1; 32];
|
||||
@@ -55,14 +58,94 @@ fn every_bit_of_each_component_reaches_the_output() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The binding survives the key schedule: a zeroed component gives a different session key too.
|
||||
/// The binding survives the key schedule: a zeroed component of either direction's X-Wing
|
||||
/// secret gives a different session key.
|
||||
#[test]
|
||||
fn zeroed_components_give_a_different_session() {
|
||||
let pk = [0x5a; crate::xwing::PUBLIC_KEY_LEN];
|
||||
let ct = [0x6b; crate::xwing::CIPHERTEXT_LEN];
|
||||
let honest = session_prk(&combiner(&SS_M, &SS_X, &CT_X, &PK_X), &pk, &ct, b"ctx");
|
||||
let no_m = session_prk(&combiner(&ZERO, &SS_X, &CT_X, &PK_X), &pk, &ct, b"ctx");
|
||||
let no_x = session_prk(&combiner(&SS_M, &ZERO, &CT_X, &PK_X), &pk, &ct, b"ctx");
|
||||
let th = [0x5a; 32];
|
||||
let ss_ri = combiner(&[0x01; 32], &[0x02; 32], &CT_X, &PK_X);
|
||||
let honest = session_prk_raw(&combiner(&SS_M, &SS_X, &CT_X, &PK_X), &ss_ri, &th);
|
||||
let no_m = session_prk_raw(&combiner(&ZERO, &SS_X, &CT_X, &PK_X), &ss_ri, &th);
|
||||
let no_x = session_prk_raw(&combiner(&SS_M, &ZERO, &CT_X, &PK_X), &ss_ri, &th);
|
||||
let no_ri = session_prk_raw(&combiner(&SS_M, &SS_X, &CT_X, &PK_X), &ZERO, &th);
|
||||
assert_ne!(*honest, *no_m);
|
||||
assert_ne!(*honest, *no_x);
|
||||
assert_ne!(*honest, *no_ri);
|
||||
}
|
||||
|
||||
/// QA A-02: recomputed independently, the combiner is SHA3-256(ss_M || ss_X || ct_X || pk_X ||
|
||||
/// 5c2e2f2f5e5c), label last, no length prefixes.
|
||||
#[test]
|
||||
fn the_combiner_is_the_drafts_sha3_256() {
|
||||
let mut h = Sha3_256::new();
|
||||
for part in [&SS_M[..], &SS_X, &CT_X, &PK_X, &[0x5c, 0x2e, 0x2f, 0x2f, 0x5e, 0x5c]] {
|
||||
h.update(part);
|
||||
}
|
||||
let want: [u8; 32] = h.finalize().into();
|
||||
assert_eq!(combiner(&SS_M, &SS_X, &CT_X, &PK_X), want);
|
||||
}
|
||||
|
||||
/// QA A-07: every label is unique, ASCII, versioned, and none is a prefix of another.
|
||||
#[test]
|
||||
fn labels_are_versioned_and_prefix_free() {
|
||||
for (i, a) in ALL_LABELS.iter().enumerate() {
|
||||
assert!(a.is_ascii() && a.starts_with(b"domi/arcanum/v1/"));
|
||||
for (j, b) in ALL_LABELS.iter().enumerate() {
|
||||
if i != j {
|
||||
assert!(!b.starts_with(a), "{} is a prefix of {}", a.escape_ascii(), b.escape_ascii());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// QA A-05: with both X-Wing secrets held fixed, every part of the transcript (both public keys,
|
||||
/// both ciphertexts, the context and its length) still changes the session. This isolates the
|
||||
/// key-schedule binding from what X-Wing itself hashes.
|
||||
#[test]
|
||||
fn the_schedule_binds_the_full_transcript() {
|
||||
use crate::schedule::{Transcript, session_prk};
|
||||
use crate::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SecretKey, SharedSecret};
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
let ss_ir = SharedSecret(Zeroizing::new([7u8; 32]));
|
||||
let ss_ri = SharedSecret(Zeroizing::new([8u8; 32]));
|
||||
let pk_i = SecretKey::from_bytes(&[1u8; 32]).unwrap().public_key().clone();
|
||||
let pk_r = SecretKey::from_bytes(&[2u8; 32]).unwrap().public_key().clone();
|
||||
let ct_ir = Ciphertext::from_bytes(&[3u8; CIPHERTEXT_LEN]).unwrap();
|
||||
let ct_ri = Ciphertext::from_bytes(&[4u8; CIPHERTEXT_LEN]).unwrap();
|
||||
let prk = |i: &PublicKey, r: &PublicKey, a: &Ciphertext, b: &Ciphertext, ctx: &[u8]| {
|
||||
*session_prk(&ss_ir, &ss_ri, &Transcript { initiator: i, responder: r, ct_ir: a, ct_ri: b, context: ctx })
|
||||
};
|
||||
let honest = prk(&pk_i, &pk_r, &ct_ir, &ct_ri, b"ctx");
|
||||
|
||||
let flip_pk = |pk: &PublicKey, n: usize| {
|
||||
let mut b = *pk.as_bytes();
|
||||
b[n] ^= 0x01;
|
||||
PublicKey::from_bytes(&b).ok()
|
||||
};
|
||||
let flip_ct = |ct: &Ciphertext, n: usize| {
|
||||
let mut b = *ct.as_bytes();
|
||||
b[n] ^= 0x01;
|
||||
Ciphertext::from_bytes(&b).unwrap()
|
||||
};
|
||||
let mut checked = 0;
|
||||
for n in (0..PUBLIC_KEY_LEN).step_by(37).chain([0, 1183, 1184, PUBLIC_KEY_LEN - 1]) {
|
||||
if let Some(p) = flip_pk(&pk_i, n) {
|
||||
assert_ne!(prk(&p, &pk_r, &ct_ir, &ct_ri, b"ctx"), honest, "pk_I[{n}]");
|
||||
checked += 1;
|
||||
}
|
||||
if let Some(p) = flip_pk(&pk_r, n) {
|
||||
assert_ne!(prk(&pk_i, &p, &ct_ir, &ct_ri, b"ctx"), honest, "pk_R[{n}]");
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
assert!(checked > 20, "the sampled public-key flips must mostly parse");
|
||||
for n in (0..CIPHERTEXT_LEN).step_by(37).chain([0, 1087, 1088, CIPHERTEXT_LEN - 1]) {
|
||||
assert_ne!(prk(&pk_i, &pk_r, &flip_ct(&ct_ir, n), &ct_ri, b"ctx"), honest, "ct_IR[{n}]");
|
||||
assert_ne!(prk(&pk_i, &pk_r, &ct_ir, &flip_ct(&ct_ri, n), b"ctx"), honest, "ct_RI[{n}]");
|
||||
}
|
||||
assert_ne!(prk(&pk_r, &pk_i, &ct_ir, &ct_ri, b"ctx"), honest, "roles swapped");
|
||||
assert_ne!(prk(&pk_i, &pk_r, &ct_ri, &ct_ir, b"ctx"), honest, "ciphertexts swapped");
|
||||
assert_ne!(prk(&pk_i, &pk_r, &ct_ir, &ct_ri, b"ctX"), honest, "context");
|
||||
assert_ne!(prk(&pk_i, &pk_r, &ct_ir, &ct_ri, b"ctx\0"), honest, "context length");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user