fix: QA second review R2-01 to R2-05

R2-01: PublicKey::encapsulate, SecretKey::decapsulate and SharedSecret::as_bytes
are hazmat-only; release consumers reach keys only through exchange and seal.
R2-02: PublicKey::from_bytes refuses a non-canonical X25519 half (bit 255 set,
or a value of at least 2^255 - 19). Honest keys and the draft vectors pass.
R2-03: schedule::expand takes one static label and one fixed-width field, so
its info is injective by signature. v1 bytes unchanged (golden vectors pass).
R2-04: generate, initiate, respond and seal say the RNG must be the OS RNG.
R2-05: Initiation is ZeroizeOnDrop; seal's buffer stays Zeroizing until the
plaintext in it is encrypted, so the MessageTooLong path wipes it.
QA's A-33 case for PublicKey::encapsulate moves to exchange::respond, and its
A-08 test pins the new expand signature to the v1 encoding.
This commit is contained in:
2026-11-25 08:37:11 +01:00
parent f3e17f3d5c
commit 6780abc0c2
8 changed files with 152 additions and 31 deletions
+13 -6
View File
@@ -28,9 +28,16 @@ fn psk_info(epoch: u64) -> Vec<u8> {
#[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"]));
// (1) Builder fix for R2-03: the helper now takes exactly one label and one fixed-width field,
// so the old ["ab","c"] vs ["a","bc"] collision cannot be written any more. Its bytes are still
// the v1 encoding, HKDF-Expand(prk, label || field), so the golden vectors did not change.
let raw = |info: &[u8]| {
let mut o = [0u8; 32];
hkdf::Hkdf::<sha2::Sha256>::from_prk(prk.as_slice()).unwrap().expand(info, &mut o).unwrap();
o
};
assert_eq!(*expand::<32, 8>(&prk, LABEL_WIREGUARD_PSK, &7u64.to_be_bytes()), raw(&psk_info(7)));
assert_eq!(*expand::<32, 0>(&prk, LABEL_SEAL_KEY, &[]), raw(LABEL_SEAL_KEY));
// (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();
@@ -83,15 +90,15 @@ fn a09_one_secret_never_yields_the_same_key_for_two_purposes() {
let mut keys: HashSet<[u8; 32]> = HashSet::new();
for prk in [&seal, &session] {
let key = *expand::<32>(prk, &[LABEL_SEAL_KEY]);
let key = *expand::<32, 0>(prk, LABEL_SEAL_KEY, &[]);
assert!(keys.insert(key));
let mut nonce = [0u8; 32];
nonce[..12].copy_from_slice(&*expand::<12>(prk, &[LABEL_SEAL_NONCE]));
nonce[..12].copy_from_slice(&*expand::<12, 0>(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!(keys.insert(*expand::<32, 8>(prk, LABEL_WIREGUARD_PSK, &e.to_be_bytes())), "epoch {e}");
}
}
assert_eq!(keys.len(), 2 * (2 + 1024));