diff --git a/tests/exchange.rs b/tests/exchange.rs index be16347..4b3ff23 100644 --- a/tests/exchange.rs +++ b/tests/exchange.rs @@ -197,9 +197,10 @@ fn every_context_byte_changes_the_secret() { } } -/// 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. +/// QA A-15, through the public API: an attacker who plays the initiator without its secret key +/// can send ct_IR, but cannot finish: ct_RI is encapsulated to the real initiator. (The API cannot +/// express her full knowledge, so this alone would pass even with a one-direction schedule; the +/// test that fails then is `src/binding_tests.rs`, checked by reintroducing that bug.) #[test] fn an_impersonated_initiator_cannot_compute_the_psk() { let i = SecretKey::from_seed(SEED_I); // the real initiator, whom the attacker impersonates diff --git a/tests/golden_v1.rs b/tests/golden_v1.rs new file mode 100644 index 0000000..a7a568a --- /dev/null +++ b/tests/golden_v1.rs @@ -0,0 +1,47 @@ +//! QA A-10 and A-38: the v1 key schedule and the v1 sealed-box format are frozen. These values +//! were produced by this implementation (not by an outside source, so they prove stability, not +//! correctness; correctness is the KATs' job). If one fails, a label, an encoding or a field order +//! changed: every sealed blob Custos stored would stop opening and two peers on different builds +//! would silently disagree. Never regenerate these; a v2 adds new ones next to them. + +use arcanum::exchange; +use arcanum::seal; +use arcanum::xwing::SecretKey; +use sha2::{Digest, Sha256}; + +const CONTEXT: &[u8] = b"domi/arcanum golden v1"; + +fn golden_session() -> exchange::Session { + let i = SecretKey::from_seed([0xa0; 32]); + let r = SecretKey::from_seed([0xb0; 32]); + let init = exchange::initiate_derand(&i, r.public_key(), CONTEXT, &[0xc0; 64]).unwrap(); + let (ct_ri, rs) = exchange::respond_derand(&r, i.public_key(), init.ciphertext(), CONTEXT, &[0xd0; 64]).unwrap(); + let is = init.finish(&ct_ri).unwrap(); + assert_eq!(is.wireguard_psk(0).as_bytes(), rs.wireguard_psk(0).as_bytes()); + rs +} + +#[test] +fn v1_wireguard_psks_are_frozen() { + let s = golden_session(); + for (epoch, want) in [ + (0u64, "9fcb41a416cbd9d678e559bebe907cf9e7e483b7d9f7ded6583c871017cbe73a"), + (1, "3db1a6ede8d5ccec1faaa420d5a69e1b03ad4592525294d57dd29ae6d3fa8d91"), + (1 << 32, "39d30c3a9180d03264cca955a7fbba06ebd6e90b40e409ada57611253f0c042b"), + (u64::MAX - 1, "1931400ca68353ecc85fe76a1e01e4cce5f4f99a51d309f776235ab459ea464e"), + ] { + assert_eq!(hex::encode(s.wireguard_psk(epoch).as_bytes()), want, "epoch {epoch}"); + } +} + +#[test] +fn v1_sealed_box_is_frozen_and_opens() { + let r = SecretKey::from_seed([0xb0; 32]); + let sealed = seal::seal_derand(r.public_key(), b"Built on Domi", b"golden aad", &[0xe0; 64]).unwrap(); + assert_eq!( + hex::encode(Sha256::digest(&sealed)), + "4effac5a1d96cc34d663d5c9873dfb816552389becdfd415a2ca33f244a3018c", + "the v1 sealed format changed" + ); + assert_eq!(seal::open(&r, &sealed, b"golden aad").unwrap().as_slice(), b"Built on Domi"); +}