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:
@@ -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::<ml_dsa::MlDsa65>::from_expanded(&Array::try_from(sk.as_slice()).unwrap());
|
||||
let ml_vk = ml_dsa::VerifyingKey::<ml_dsa::MlDsa65>::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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user