//! Mandatory test 4: WireGuard PSK epochs. Successive epochs differ, the same epoch is reproducible //! from the same inputs, and the epoch helper follows the 120-second Rosenpass-style interval. use arcanum::exchange; use arcanum::xwing::SecretKey; use arcanum::{RECOMMENDED_EPOCH_SECONDS, WIREGUARD_PSK_LEN, epoch_at}; 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) { exchange_with([0x02; 64]) } #[test] fn successive_epochs_differ() { let (a, _) = sessions(); let mut seen = HashSet::new(); for epoch in 0..2048u64 { assert!(seen.insert(*a.wireguard_psk(epoch).as_bytes()), "epoch {epoch} repeats an earlier PSK"); } // The ends of the range and neighbours that differ only in the high bytes. for epoch in [u64::MAX, u64::MAX - 1, 1 << 32, (1 << 32) + 1, 1 << 63] { assert!(seen.insert(*a.wireguard_psk(epoch).as_bytes()), "epoch {epoch} repeats"); } } #[test] fn the_same_epoch_is_reproducible_from_the_same_inputs() { let (a1, b1) = sessions(); let (a2, b2) = sessions(); for epoch in [0, 1, 719, 720, u64::MAX] { let p = *a1.wireguard_psk(epoch).as_bytes(); assert_eq!(&p, a1.wireguard_psk(epoch).as_bytes(), "same session, asked twice"); assert_eq!(&p, a2.wireguard_psk(epoch).as_bytes(), "rebuilt from the same inputs"); assert_eq!(&p, b1.wireguard_psk(epoch).as_bytes(), "the other peer"); assert_eq!(&p, b2.wireguard_psk(epoch).as_bytes(), "the other peer, rebuilt"); } } #[test] fn a_different_exchange_gives_different_psks_for_the_same_epoch() { 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()); } } #[test] fn a_psk_is_32_bytes_and_not_trivial() { let (a, _) = sessions(); let p = a.wireguard_psk(0); assert_eq!(WIREGUARD_PSK_LEN, 32); assert_eq!(p.as_bytes().len(), 32); assert_ne!(p.as_bytes(), &[0u8; 32]); } #[test] fn the_psk_never_prints() { let (a, _) = sessions(); let p = a.wireguard_psk(0); let shown = format!("{p:?} {a:?}"); assert!(shown.contains("redacted"), "{shown}"); let k = p.as_bytes(); // Neither a hex dump nor a derived `[1, 2, 3, ...]` array may appear. assert!(!shown.contains(&hex::encode(&k[..4])), "{shown}"); assert!(!shown.contains(&format!("{}, {}, {}", k[0], k[1], k[2])), "{shown}"); } #[test] fn epoch_helper_uses_two_minute_epochs() { assert_eq!(RECOMMENDED_EPOCH_SECONDS, 120, "Rosenpass REKEY_AFTER_TIME_RESPONDER is 120 s"); assert_eq!(epoch_at(0), 0); assert_eq!(epoch_at(119), 0); assert_eq!(epoch_at(120), 1); assert_eq!(epoch_at(239), 1); assert_eq!(epoch_at(240), 2); 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)); }