diff --git a/src/binding_tests.rs b/src/binding_tests.rs index 40aecb2..c5b25d0 100644 --- a/src/binding_tests.rs +++ b/src/binding_tests.rs @@ -5,8 +5,10 @@ use sha3::{Digest, Sha3_256}; -use crate::schedule::{ALL_LABELS, session_prk_raw}; +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]; @@ -58,19 +60,32 @@ fn every_bit_of_each_component_reaches_the_output() { } } -/// The binding survives the key schedule: a zeroed component of either direction's X-Wing -/// secret gives a different session key. +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_give_a_different_session() { - let th = [0x5a; 32]; +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_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); + + 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 || @@ -103,10 +118,6 @@ fn labels_are_versioned_and_prefix_free() { /// 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(); diff --git a/src/schedule.rs b/src/schedule.rs index 9c04331..a065bad 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -85,12 +85,6 @@ pub(crate) fn session_prk(ss_ir: &SharedSecret, ss_ri: &SharedSecret, t: &Transc extract(&[ss_ir.as_bytes(), ss_ri.as_bytes()], &t.hash()) } -/// The same derivation on raw component values, for the in-crate binding tests. -#[cfg(test)] -pub(crate) fn session_prk_raw(ss_ir: &[u8; 32], ss_ri: &[u8; 32], th: &[u8; 32]) -> Prk { - extract(&[ss_ir, ss_ri], th) -} - /// The PRK of one sealed box: the X-Wing secret, the recipient key, the ciphertext and the aad. pub(crate) fn seal_prk(ss: &SharedSecret, recipient: &PublicKey, ct: &Ciphertext, aad: &[u8]) -> Prk { let mut h = Sha256::new(); diff --git a/tests/feature_hygiene.rs b/tests/feature_hygiene.rs index 2f1aafe..7a3f839 100644 --- a/tests/feature_hygiene.rs +++ b/tests/feature_hygiene.rs @@ -1,8 +1,10 @@ //! 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. +//! unification. Checks the normal-edge graph a consumer of Arcanum gets with default features. //! -//! `arcanum/hazmat` exposes the deterministic entry points; `ml-kem/hazmat` only un-hides docs in +//! `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. +//! Consumers (via-core, via) must run the same check in their own CI: a `[dependencies]` entry +//! there could still enable `arcanum/hazmat`. use std::process::Command; @@ -17,18 +19,33 @@ fn tree(args: &[&str]) -> String { 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}"); +fn assert_no_hazmat(t: &str, what: &str) { for line in t.lines() { - assert!(!line.contains("feature \"hazmat\""), "hazmat reached a normal build: {line}\n{t}"); + assert!(!line.contains("feature \"hazmat\""), "{what}: hazmat reached a normal build: {line}\n{t}"); } } +#[test] +fn arcanum_default_features_do_not_include_hazmat() { + let t = tree(&["-i", "arcanum"]); + assert!(t.contains("arcanum feature \"default\""), "the tree must list arcanum's features:\n{t}"); + assert_no_hazmat(&t, "arcanum"); + // Prove the check can see the feature at all: asking for it must make it appear. + let with = tree(&["-i", "arcanum", "--features", "hazmat"]); + assert!(with.contains("arcanum feature \"hazmat\""), "the check is blind:\n{with}"); +} + +#[test] +fn ml_kem_hazmat_is_not_in_the_normal_graph() { + let t = tree(&["-i", "ml-kem"]); + assert!(t.contains("ml-kem feature \"zeroize\""), "the tree must list ml-kem's features:\n{t}"); + assert_no_hazmat(&t, "ml-kem"); +} + #[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("rand_core"), "the tree must include rand_core:\n{t}"); assert!(!t.contains("getrandom"), "getrandom reached a normal build:\n{t}"); }