test(qa): second review, A-03/A-06/A-08/A-09/A-18/A-28/A-30/A-31 and R2-02 (non-canonical pk_X)

Completes the partial QA cases and adds the not-done ones that need no new
dependency: compile-time checks that no secret type is Copy, Clone, PartialEq
or Display (A-28, A-31, hand-rolled assert_not_impl), ZeroizeOnDrop markers,
redacted Debug, no input bytes in errors (A-30), distinct secrets for all 1120
ciphertext flips (A-03), a spliced key (A-06), deterministic implicit rejection
(A-18), and the KDF-encoding and purpose-separation unit tests (A-08, A-09).

qa_r2_02_non_canonical_x25519_public_key_is_refused FAILS on purpose: a pk_X
with bit 255 set parses, and the exchange then silently derives mismatched PSKs.
This commit is contained in:
2026-11-25 08:16:44 +01:00
parent 853f59ca60
commit 3dfc75075f
3 changed files with 312 additions and 0 deletions
+2
View File
@@ -27,6 +27,8 @@ pub mod xwing;
#[cfg(test)]
mod binding_tests;
#[cfg(test)]
mod qa_review_tests;
pub use error::{Error, Input};
pub use exchange::{Session, WIREGUARD_PSK_LEN, WireGuardPsk};
+98
View File
@@ -0,0 +1,98 @@
//! QA second review (PR #1): A-08 and A-09, which need the crate-private key schedule.
use alloc::vec::Vec;
use std::collections::HashSet;
use crate::schedule::{
LABEL_SEAL_KEY, LABEL_SEAL_NONCE, LABEL_WIREGUARD_PSK, Prk, Transcript, expand, seal_prk, session_prk,
};
use crate::xwing::{CIPHERTEXT_LEN, Ciphertext, SecretKey, SharedSecret};
use zeroize::Zeroizing;
extern crate std;
fn ss(b: u8) -> SharedSecret {
SharedSecret(Zeroizing::new([b; 32]))
}
/// The `info` strings every `expand` call in the crate uses, built exactly as the call sites do.
fn psk_info(epoch: u64) -> Vec<u8> {
[LABEL_WIREGUARD_PSK, &epoch.to_be_bytes()].concat()
}
/// QA A-08. `expand` concatenates its parts raw, so it is injective only because every call site
/// passes a fixed label followed by fixed-width fields. This test pins both halves of that
/// argument: (1) the helper itself is NOT length-prefixed (so a future variable-width part is a
/// collision, and this test documents it for whoever adds one), and (2) the encodings actually in
/// use are injective: fixed lengths per purpose, and no two purposes share a length-and-prefix.
#[test]
fn a08_kdf_info_encodings_in_use_are_injective() {
let prk: Prk = Zeroizing::new([9u8; 32]);
// (1) The helper is raw concatenation. If this ever fails, the helper became length-prefixed:
// update this test and the golden vectors together.
assert_eq!(*expand::<32>(&prk, &[b"ab", b"c"]), *expand::<32>(&prk, &[b"a", b"bc"]));
// (2) Every info string in use: one fixed length per purpose, and none a prefix of another.
let mut infos: Vec<Vec<u8>> = [0u64, 1, 255, 256, 1 << 32, u64::MAX].iter().map(|&e| psk_info(e)).collect();
infos.push(LABEL_SEAL_KEY.to_vec());
infos.push(LABEL_SEAL_NONCE.to_vec());
for info in &infos[..6] {
assert_eq!(info.len(), LABEL_WIREGUARD_PSK.len() + 8, "a PSK info has one fixed width");
}
for (i, a) in infos.iter().enumerate() {
for (j, b) in infos.iter().enumerate() {
if i != j {
assert_ne!(a, b);
assert!(!b.starts_with(a), "info {i} is a prefix of info {j}");
}
}
}
// The exchange transcript: the only variable field (the context) is last and length-prefixed,
// so moving a byte across the context boundary changes the hash.
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 = Ciphertext::from_bytes(&[3u8; CIPHERTEXT_LEN]).unwrap();
let t = |ctx: &[u8]| {
*session_prk(
&ss(1),
&ss(2),
&Transcript { initiator: &pk_i, responder: &pk_r, ct_ir: &ct, ct_ri: &ct, context: ctx },
)
};
assert_ne!(t(b""), t(&[0u8; 8]), "an empty context is not 8 zero bytes");
assert_ne!(t(b"x"), t(b"x\0"));
}
/// QA A-09: from one X-Wing secret, no two purposes ever yield the same key. The seal key and
/// nonce, and the PSKs of 1024 epochs, are all distinct; and even under a single PRK the labels
/// keep the purposes apart.
#[test]
fn a09_one_secret_never_yields_the_same_key_for_two_purposes() {
let r = SecretKey::from_bytes(&[5u8; 32]).unwrap();
let ct = Ciphertext::from_bytes(&[6u8; CIPHERTEXT_LEN]).unwrap();
let secret = ss(7);
// The same secret, used as a seal and as both directions of an exchange.
let seal = seal_prk(&secret, r.public_key(), &ct, b"");
let session = session_prk(
&secret,
&secret,
&Transcript { initiator: r.public_key(), responder: r.public_key(), ct_ir: &ct, ct_ri: &ct, context: b"" },
);
assert_ne!(*seal, *session, "seal and exchange PRKs must differ for one secret");
let mut keys: HashSet<[u8; 32]> = HashSet::new();
for prk in [&seal, &session] {
let key = *expand::<32>(prk, &[LABEL_SEAL_KEY]);
assert!(keys.insert(key));
let mut nonce = [0u8; 32];
nonce[..12].copy_from_slice(&*expand::<12>(prk, &[LABEL_SEAL_NONCE]));
// HKDF output is prefix-consistent: a shared label would make the nonce the key's prefix.
assert_ne!(nonce[..12], key[..12], "the nonce is a prefix of the key: labels shared");
assert!(keys.insert(nonce));
for e in 0..1024u64 {
assert!(keys.insert(*expand::<32>(prk, &[LABEL_WIREGUARD_PSK, &e.to_be_bytes()])), "epoch {e}");
}
}
assert_eq!(keys.len(), 2 * (2 + 1024));
}