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
+42 -1
View File
@@ -68,14 +68,55 @@ fn low_order_x25519_ciphertext_is_refused() {
fn low_order_x25519_public_key_is_refused() {
let i = SecretKey::from_seed([0x43; 32]);
let r = SecretKey::from_seed([0x44; 32]);
let mut refused_at_parse = 0;
for point in low_order_points() {
let mut bad = *r.public_key().as_bytes();
bad[1184..].copy_from_slice(&point);
let pk = PublicKey::from_bytes(&bad).expect("the ML-KEM part is still valid");
// Non-canonical encodings (bit 255 set, or p and p + 1) are refused when parsed (QA R2-02);
// the canonical low-order points parse and are refused when used.
let canonical = point[31] & 0x80 == 0 && !(point[31] == 0x7f && point[0] >= 0xed);
let pk = match PublicKey::from_bytes(&bad) {
Ok(pk) => {
assert!(canonical, "a non-canonical pk_X parsed: {}", hex::encode(point));
pk
}
Err(e) => {
assert_eq!(e, Error::InvalidPublicKey);
assert!(!canonical, "a canonical pk_X was refused: {}", hex::encode(point));
refused_at_parse += 1;
continue;
}
};
assert_eq!(pk.encapsulate_derand(&[0x55; 64]).err(), Some(Error::NonContributory));
assert_eq!(exchange::initiate_derand(&i, &pk, b"ctx", &[0x55; 64]).err(), Some(Error::NonContributory));
assert_eq!(arcanum::seal::seal(&pk, b"x", b"", &mut rng(1)).err(), Some(Error::NonContributory));
}
// 7 points with bit 255 set, plus p and p + 1.
assert_eq!(refused_at_parse, 9);
}
/// QA R2-02 boundary: p - 1 is the largest canonical u-coordinate (and low order), p is the
/// smallest non-canonical one, and an honest key is always canonical.
#[test]
fn the_canonical_boundary_is_exactly_p() {
let r = SecretKey::from_seed([0x44; 32]);
let with_x = |x: [u8; 32]| {
let mut b = *r.public_key().as_bytes();
b[1184..].copy_from_slice(&x);
PublicKey::from_bytes(&b)
};
let mut p_minus_1 = [0xff; 32];
p_minus_1[0] = 0xec;
p_minus_1[31] = 0x7f;
let mut p = p_minus_1;
p[0] = 0xed;
let mut below = [0xff; 32];
below[31] = 0x7f;
below[1] = 0xfe; // 2^255 - 1 - 2^8: below p although its first byte is 0xff
assert!(with_x(p_minus_1).is_ok());
assert_eq!(with_x(p).err(), Some(Error::InvalidPublicKey));
assert!(with_x(below).is_ok());
assert!(PublicKey::from_bytes(r.public_key().as_bytes()).is_ok());
}
/// QA A-04: X25519 ignores bit 255 of ct_X, so ct_X and ct_X ^ 0x80 give the same ss_X. The
+6 -2
View File
@@ -116,10 +116,14 @@ fn control_the_safe_api_compiles_without_hazmat() {
fn a33_a_non_crypto_rng_does_not_compile() {
let calls = [
("SecretKey::generate", "let _ = arcanum::xwing::SecretKey::generate(&mut Counter(0));"),
// Builder, with the R2-01 fix: `PublicKey::encapsulate` is hazmat-only now, so it no longer
// exists for a consumer (qa_r2_01 checks that). Its place is taken by the other release
// function that takes an RNG, `exchange::respond`.
(
"PublicKey::encapsulate",
"exchange::respond",
"let k = arcanum::xwing::SecretKey::from_bytes(&[1; 32]).unwrap(); \
let _ = k.public_key().encapsulate(&mut Counter(0));",
let ct = arcanum::xwing::Ciphertext::from_bytes(&[0; 1120]).unwrap(); \
let _ = arcanum::exchange::respond(&k, k.public_key(), &ct, b\"\", &mut Counter(0));",
),
(
"exchange::initiate",
+1
View File
@@ -54,6 +54,7 @@ fn a28_secret_types_are_zeroize_on_drop() {
assert_zeroize_on_drop::<SharedSecret>();
assert_zeroize_on_drop::<Session>();
assert_zeroize_on_drop::<WireGuardPsk>();
assert_zeroize_on_drop::<exchange::Initiation<'static>>(); // R2-05, added by the builder with the fix
}
/// QA A-28: `Debug` of every secret type is redacted (the PSK and Session case is already in