//! Mandatory test 3, at the combiner: replacing either component secret by zeros must change the //! 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 sha3::{Digest, Sha3_256}; use crate::schedule::{ALL_LABELS, Transcript, session_prk}; use crate::xwing::{CIPHERTEXT_LEN, Ciphertext, PUBLIC_KEY_LEN, PublicKey, SecretKey, SharedSecret}; use crate::xwing::{SHARED_SECRET_LEN, combiner}; use zeroize::Zeroizing; const SS_M: [u8; 32] = [0xa1; 32]; const SS_X: [u8; 32] = [0xb2; 32]; const CT_X: [u8; 32] = [0xc3; 32]; const PK_X: [u8; 32] = [0xd4; 32]; const ZERO: [u8; SHARED_SECRET_LEN] = [0; SHARED_SECRET_LEN]; #[test] fn zeroing_the_ml_kem_secret_changes_the_output() { let honest = combiner(&SS_M, &SS_X, &CT_X, &PK_X); assert_ne!(combiner(&ZERO, &SS_X, &CT_X, &PK_X), honest); } #[test] fn zeroing_the_x25519_secret_changes_the_output() { let honest = combiner(&SS_M, &SS_X, &CT_X, &PK_X); assert_ne!(combiner(&SS_M, &ZERO, &CT_X, &PK_X), honest); } #[test] fn zeroing_both_differs_from_zeroing_either() { let only_m = combiner(&ZERO, &SS_X, &CT_X, &PK_X); let only_x = combiner(&SS_M, &ZERO, &CT_X, &PK_X); let both = combiner(&ZERO, &ZERO, &CT_X, &PK_X); assert_ne!(only_m, only_x); assert_ne!(both, only_m); assert_ne!(both, only_x); } /// Swapping the two secrets must not give the same output: the positions are bound, not just the set. #[test] fn the_two_halves_are_not_interchangeable() { assert_ne!(combiner(&SS_M, &SS_X, &CT_X, &PK_X), combiner(&SS_X, &SS_M, &CT_X, &PK_X)); } /// Every single bit of either component secret reaches the output. #[test] fn every_bit_of_each_component_reaches_the_output() { let honest = combiner(&SS_M, &SS_X, &CT_X, &PK_X); for byte in 0..32 { for bit in 0..8 { let mut m = SS_M; m[byte] ^= 1 << bit; assert_ne!(combiner(&m, &SS_X, &CT_X, &PK_X), honest, "ss_M byte {byte} bit {bit}"); let mut x = SS_X; x[byte] ^= 1 << bit; assert_ne!(combiner(&SS_M, &x, &CT_X, &PK_X), honest, "ss_X byte {byte} bit {bit}"); } } } fn ss(b: [u8; 32]) -> SharedSecret { SharedSecret(Zeroizing::new(b)) } /// The binding survives the key schedule, through the real `session_prk`: a zeroed component of /// the first direction's X-Wing secret, or a missing second direction, gives a different session. /// The last case is QA A-15: an attacker who impersonates the initiator knows ss_IR (she made it) /// and the whole transcript, but not ss_RI; every guess of hers that reuses known values fails. #[test] fn zeroed_components_and_a_missing_direction_give_a_different_session() { 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 t = Transcript { initiator: &pk_i, responder: &pk_r, ct_ir: &ct_ir, ct_ri: &ct_ri, context: b"ctx" }; let ss_ir = combiner(&SS_M, &SS_X, &CT_X, &PK_X); let ss_ri = combiner(&[0x01; 32], &[0x02; 32], &CT_X, &PK_X); let honest = *session_prk(&ss(ss_ir), &ss(ss_ri), &t); let no_m = *session_prk(&ss(combiner(&ZERO, &SS_X, &CT_X, &PK_X)), &ss(ss_ri), &t); let no_x = *session_prk(&ss(combiner(&SS_M, &ZERO, &CT_X, &PK_X)), &ss(ss_ri), &t); assert_ne!(honest, no_m); assert_ne!(honest, no_x); for guess in [ZERO, ss_ir, [0xff; 32]] { assert_ne!(*session_prk(&ss(ss_ir), &ss(guess), &t), honest, "PSK computable without ss_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() { 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"); }