6780abc0c2
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.
106 lines
4.8 KiB
Rust
106 lines
4.8 KiB
Rust
//! 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) 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();
|
|
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, 0>(prk, LABEL_SEAL_KEY, &[]);
|
|
assert!(keys.insert(key));
|
|
let mut nonce = [0u8; 32];
|
|
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, 8>(prk, LABEL_WIREGUARD_PSK, &e.to_be_bytes())), "epoch {e}");
|
|
}
|
|
}
|
|
assert_eq!(keys.len(), 2 * (2 + 1024));
|
|
}
|