diff --git a/src/sign.rs b/src/sign.rs index 332d967..54e30e6 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -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 { 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::::try_from(&bytes[..ML_DSA_PUBLIC_KEY_LEN]) .map_err(|_| Error::InvalidVerifyingKey)?; let ml = ml_dsa::VerifyingKey::::decode(&enc); diff --git a/tests/sig_malformed.rs b/tests/sig_malformed.rs index 18eb454..966a8e3 100644 --- a/tests/sig_malformed.rs +++ b/tests/sig_malformed.rs @@ -153,3 +153,60 @@ fn random_bytes_never_panic() { let _ = AppContext::new(&buf[..len.min(300)]); } } + +/// QA S-05 (their `qa_s07`, inverted): an ML-DSA-65 public key with t1 = 0 is the ML-DSA analogue +/// of a weak Ed25519 key. The component crate accepts it, and its ML-DSA half can be computed by +/// anyone, with no secret (s1 = s2 = t0 = 0); Arcanum refuses the key at parse. +#[test] +#[allow(deprecated)] +fn an_ml_dsa_key_with_zero_t1_is_refused() { + use ml_dsa::common::array::Array; + use shake::Update as _; + + let rho = [0x5au8; 32]; + let mut ml_pk = rho.to_vec(); + ml_pk.extend_from_slice(&[0u8; 1920]); // t1 = 0 + let ed = ed25519_dalek::SigningKey::from_bytes(&[0x22; 32]); // a throwaway Ed25519 key + let mut pk = ml_pk.clone(); + pk.extend_from_slice(ed.verifying_key().as_bytes()); + assert_eq!(VerifyingKey::from_bytes(&pk).unwrap_err(), Error::InvalidVerifyingKey); + + // Control: one non-zero t1 byte, anywhere, and the key parses. + for i in [32, 1000, 1951] { + let mut p = pk.clone(); + p[i] = 1; + assert!(VerifyingKey::from_bytes(&p).is_ok(), "t1 byte {i} set"); + } + // rho alone may be anything, including zero: only t1 = 0 is weak. + let mut p = pk.clone(); + p[..32].fill(0); + p[100] = 1; + assert!(VerifyingKey::from_bytes(&p).is_ok()); + + // Why: with the component crate alone, a secret-free key signs for this public key. The expanded + // key packs eta - 0 = 4 into 4 bits (0x44 per byte) for s1 and s2, and 2^12 - 0 into 13 bits for + // t0; K is public (zero) and tr is unused by sign_mu. + let mut sk = rho.to_vec(); + sk.extend_from_slice(&[0u8; 32 + 64]); + sk.extend(std::iter::repeat_n(0x44u8, 640 + 768)); + let mut t0 = vec![0u8; 2496]; + for i in 0..(6 * 256) { + let bit = 13 * i + 12; + t0[bit / 8] |= 1 << (bit % 8); + } + sk.extend_from_slice(&t0); + assert_eq!(sk.len(), 4032); + let ml_sk = ml_dsa::ExpandedSigningKey::::from_expanded(&Array::try_from(sk.as_slice()).unwrap()); + let ml_vk = ml_dsa::VerifyingKey::::decode(&Array::try_from(ml_pk.as_slice()).unwrap()); + let mu = ml_vk + .compute_mu( + |h| { + h.update(b"any message at all"); + Ok(()) + }, + b"", + ) + .unwrap(); + let half = ml_sk.sign_mu_randomized(&mu, &mut rng(68)).unwrap(); + assert!(ml_vk.verify_mu(&mu, &half), "the component crate accepts a secret-free signature"); +}