Files
arcanum/tests/psk_epochs.rs
T

86 lines
3.2 KiB
Rust

//! 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 sessions() -> (exchange::Session, exchange::Session) {
let sk = SecretKey::from_seed([0x01; 32]);
let (ct, a) = exchange::initiate_derand(sk.public_key(), CONTEXT, &[0x02; 64]).unwrap();
let b = exchange::respond(&sk, &ct, CONTEXT).unwrap();
(a, b)
}
#[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 sk = SecretKey::from_seed([0x01; 32]);
let (_, a) = exchange::initiate_derand(sk.public_key(), CONTEXT, &[0x02; 64]).unwrap();
let (_, c) = exchange::initiate_derand(sk.public_key(), CONTEXT, &[0x03; 64]).unwrap();
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);
}