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
@@ -33,8 +33,8 @@ const HEADER_LEN: usize = 1 + CIPHERTEXT_LEN;
pub const OVERHEAD: usize = HEADER_LEN + TAG_LEN;
fn cipher(prk: &Prk) -> (ChaCha20Poly1305, Nonce) {
let key = expand::<32>(prk, &[LABEL_SEAL_KEY]);
let nonce = expand::<12>(prk, &[LABEL_SEAL_NONCE]);
let key = expand::<32, 0>(prk, LABEL_SEAL_KEY, &[]);
let nonce = expand::<12, 0>(prk, LABEL_SEAL_NONCE, &[]);
// Infallible: the key is exactly 32 bytes.
let aead = ChaCha20Poly1305::new_from_slice(key.as_slice()).expect("32-byte key");
(aead, Nonce::from(*nonce))
@@ -43,6 +43,10 @@ fn cipher(prk: &Prk) -> (ChaCha20Poly1305, Nonce) {
/// Seal `plaintext` to `recipient`, bound to `aad` (which is not encrypted and must be given again
/// to open).
///
/// **`rng` must be the operating system's RNG** (`getrandom`, `OsRng`) or a CSPRNG seeded from it
/// once per process; never a fixed, derived or configured seed. `CryptoRng` is only a marker: a
/// CSPRNG with a constant seed satisfies it and makes this call fully deterministic (QA R2-04).
///
/// # Errors
/// [`Error::NonContributory`] if the recipient's X25519 key is a low-order point.
pub fn seal<R: CryptoRng + ?Sized>(
@@ -80,8 +84,10 @@ fn seal_inner(
let (ct, ss) = recipient.encapsulate_inner(eseed)?;
let (aead, nonce) = cipher(&seal_prk(&ss, recipient, &ct, aad));
// Allocated once at its final size, so no buffer that held plaintext is ever reallocated.
let mut out = Vec::with_capacity(plaintext.len() + OVERHEAD);
// Allocated once at its final size, so no buffer that held plaintext is ever reallocated. It is
// `Zeroizing` until the plaintext has been encrypted in place, so an early return (the
// `MessageTooLong` path) wipes the copy instead of freeing it unwiped (QA R2-05).
let mut out = Zeroizing::new(Vec::with_capacity(plaintext.len() + OVERHEAD));
out.push(VERSION);
out.extend_from_slice(ct.as_bytes());
out.extend_from_slice(plaintext);
@@ -90,7 +96,8 @@ fn seal_inner(
.encrypt_inout_detached(&nonce, &[VERSION], (&mut out[HEADER_LEN..]).into())
.map_err(|_| Error::MessageTooLong)?;
out.extend_from_slice(tag.as_slice());
Ok(out)
// Now only ciphertext: move the buffer out without copying it (the wrapper keeps an empty Vec).
Ok(core::mem::take(&mut *out))
}
/// Open a sealed box with the recipient's secret key and the same `aad`.
@@ -108,7 +115,7 @@ pub fn open(recipient: &SecretKey, sealed: &[u8], aad: &[u8]) -> Result<Zeroizin
return Err(Error::UnsupportedVersion(version));
}
let ct = Ciphertext::from_bytes(ct)?;
let ss = recipient.decapsulate(&ct)?;
let ss = recipient.decapsulate_inner(&ct)?;
let (aead, nonce) = cipher(&seal_prk(&ss, recipient.public_key(), &ct, aad));
let (body, tag) = rest.split_at(rest.len() - TAG_LEN);