fix: refuse an ML-DSA-65 public key with t1 = 0 (QA S-05)

With t1 = 0 (Wycheproof's ZeroPublicKey), ML-DSA verification only checks
c~ = H(mu || w1(A·z)), which anyone can satisfy with s1 = s2 = t0 = 0: such
a composite key is silently no stronger than its Ed25519 half.
VerifyingKey::from_bytes now refuses it as InvalidVerifyingKey, like a
small-order Ed25519 half. Invisible to honest keys (t1 = 0 has negligible
probability); rho may be anything.

sig_malformed::an_ml_dsa_key_with_zero_t1_is_refused, from QA's qa_s07
inverted: the key is refused, one non-zero t1 byte anywhere makes it parse,
and with the component crate alone a secret-free expanded key signs for it.
Removing the check makes the test fail.
This commit is contained in:
2026-11-26 03:44:02 +01:00
parent 99db18af16
commit 2329eb0719
2 changed files with 71 additions and 4 deletions
+14 -4
View File
@@ -22,8 +22,10 @@
//! under another: `ctx` and its length are inside M'.
//!
//! **Stricter than the RFC**, invisible to honest signers: a verifying key whose Ed25519 half is a
//! small-order point or not the canonical encoding of its point is refused when parsed
//! ([`Error::InvalidVerifyingKey`]), and Ed25519 is checked with `verify_strict`.
//! small-order point or not the canonical encoding of its point, or whose ML-DSA-65 half has
//! t1 = 0, is refused when parsed ([`Error::InvalidVerifyingKey`]), and Ed25519 is checked with
//! `verify_strict`. Both refused shapes are "weak keys": with t1 = 0 an ML-DSA signature needs no
//! secret at all (QA S-05), as a small-order Ed25519 key lets signatures verify for many messages.
//!
//! **Not SUF-CMA** (RFC 10073 §9.2): two signatures of one message under one key can trade their
//! hedged ML-DSA halves. Never use a signature as an identifier.
@@ -69,6 +71,7 @@ pub const SIGNATURE_LEN: usize = 3373;
const ML_DSA_PUBLIC_KEY_LEN: usize = 1952;
const ML_DSA_SIGNATURE_LEN: usize = 3309;
const ML_DSA_RHO_LEN: usize = 32;
const SEED_LEN: usize = 32;
const M_PRIME_MAX_LEN: usize = 32 + 30 + 1 + MAX_CONTEXT_LEN + 64;
@@ -165,8 +168,9 @@ impl VerifyingKey {
///
/// # Errors
/// [`Error::InvalidLength`] if it is not 1984 bytes; [`Error::InvalidVerifyingKey`] if the
/// Ed25519 half is not a point, is not the canonical encoding of its point, or has small order.
/// Every 1952-byte ML-DSA-65 public key is well formed (FIPS 204 `pkDecode` cannot fail).
/// Ed25519 half is not a point, is not the canonical encoding of its point, or has small order,
/// or if the ML-DSA-65 half's t1 (its bytes 32..1952) is all zero. Every other 1952-byte
/// ML-DSA-65 public key is well formed (FIPS 204 `pkDecode` cannot fail).
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
let bytes: &[u8; VERIFYING_KEY_LEN] = exact(Input::VerifyingKey, bytes)?;
let ed_bytes: [u8; 32] = array::from_fn(|i| bytes[ML_DSA_PUBLIC_KEY_LEN + i]);
@@ -176,6 +180,12 @@ impl VerifyingKey {
if ed.is_weak() || ed.to_edwards().compress().to_bytes() != ed_bytes {
return Err(Error::InvalidVerifyingKey);
}
// t1 = 0 (Wycheproof's `ZeroPublicKey`): ML-DSA verification then checks only
// c~ = H(mu || w1(A·z)), which anyone can satisfy with s1 = s2 = t0 = 0. An honest key has
// t1 = 0 with negligible probability.
if bytes[ML_DSA_RHO_LEN..ML_DSA_PUBLIC_KEY_LEN].iter().all(|&b| b == 0) {
return Err(Error::InvalidVerifyingKey);
}
let enc = EncodedVerifyingKey::<MlDsa65>::try_from(&bytes[..ML_DSA_PUBLIC_KEY_LEN])
.map_err(|_| Error::InvalidVerifyingKey)?;
let ml = ml_dsa::VerifyingKey::<MlDsa65>::decode(&enc);