2a432f5d86
The mandatory tests of docs/02_signatures.md, against the API the doc sets out (arcanum::sign). They do not compile yet: the module does not exist. 1 kat_mldsa65: ACVP keyGen, sigGen (deterministic and hedged groups, byte for byte, through sign_randomized/sign_mu_randomized fed the vector's rnd), sigVer on the corrected file; Wycheproof sign_seed and verify. 2 kat_ed25519: RFC 8032 section 7.1 and Wycheproof, with verify_strict. 3 sig_composite: the RFC 10073 vectors s and sWithContext verify, the RFC key derives the RFC public key, own signatures verify under an independent rebuild of M', ML-DSA hedged with ctx = Label. 4 sig_halves: either half swapped in from another signature, zeroed or cut, or the halves swapped, is refused; the ML-DSA half fails an empty-ctx verifier. Not SUF-CMA (RFC 10073 9.2) is pinned. 5 sig_every_byte: any flipped byte of message, ctx, public key or signature fails; contexts are prefix-free; key bindings and app messages never verify as each other; every binding field matters. 6 sig_malformed: wrong lengths, weak and non-canonical Ed25519 keys, 0- and 256-byte contexts, a 65536-byte key id, random bytes. 7 sig_hygiene and feature_hygiene: compile-fail checks against a default-features consumer (hazmat raw interface, ml-dsa internals, non-crypto RNG, Clone), ZeroizeOnDrop, Debug; ml-dsa and ed25519-dalek in the normal graph with zeroize and without hazmat or defaults. The allocator probe covers the signing keys. 8 golden_v1.rs is untouched. Adds ml-dsa =0.1.2 (alloc, rand_core, zeroize) and ed25519-dalek =3.0.0 (zeroize) with default features off, base64 for the tests, and bumps the version to 0.2.0. The fuzz target follows with the code.
136 lines
5.9 KiB
Rust
136 lines
5.9 KiB
Rust
//! Mandatory test 4 (v0.2.0): both halves of a composite signature are bound. A signature is
|
|
//! accepted only when **both** the ML-DSA-65 half and the Ed25519 half verify over this M'.
|
|
//!
|
|
//! Refused: either half replaced by a valid half of another signature (another message, another
|
|
//! key or another context), either half zeroed, the halves swapped. And the ML-DSA half alone is
|
|
//! no ordinary ML-DSA signature: an empty-ctx ML-DSA verifier refuses it, on M' and on M.
|
|
//!
|
|
//! Pinned, not a bug: the composite is not SUF-CMA (RFC 10073 §9.2). Two hedged signatures of the
|
|
//! same message by the same key under the same context can trade ML-DSA halves and still verify.
|
|
|
|
mod common;
|
|
|
|
use arcanum::Error;
|
|
use arcanum::sign::{AppContext, SIGNATURE_LEN, Signature, SigningKey};
|
|
use common::rng;
|
|
use ml_dsa::MlDsa65;
|
|
use ml_dsa::common::array::Array;
|
|
use sha2::{Digest, Sha512};
|
|
|
|
const ML: usize = 3309; // the ML-DSA-65 half comes first
|
|
const ML_PK: usize = 1952;
|
|
|
|
fn ctx(label: &[u8]) -> AppContext {
|
|
AppContext::new(label).unwrap()
|
|
}
|
|
|
|
fn splice(ml_from: &Signature, ed_from: &Signature) -> Signature {
|
|
let mut b = [0u8; SIGNATURE_LEN];
|
|
b[..ML].copy_from_slice(&ml_from.as_bytes()[..ML]);
|
|
b[ML..].copy_from_slice(&ed_from.as_bytes()[ML..]);
|
|
Signature::from_bytes(&b).unwrap()
|
|
}
|
|
|
|
fn refused(sk: &SigningKey, msg: &[u8], c: &AppContext, sig: &Signature, what: &str) {
|
|
assert_eq!(sk.verifying_key().verify(msg, c, sig), Err(Error::InvalidSignature), "{what}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_half_from_another_signature_is_refused() {
|
|
let mut r = rng(40);
|
|
let sk = SigningKey::generate(&mut r);
|
|
let other_key = SigningKey::generate(&mut r);
|
|
let c = ctx(b"halves");
|
|
let msg = b"the message";
|
|
let good = sk.sign(msg, &c, &mut r).unwrap();
|
|
assert_eq!(sk.verifying_key().verify(msg, &c, &good), Ok(()), "control");
|
|
|
|
let other_msg = sk.sign(b"another message", &c, &mut r).unwrap();
|
|
let other_ctx = sk.sign(msg, &ctx(b"another context"), &mut r).unwrap();
|
|
let other_signer = other_key.sign(msg, &c, &mut r).unwrap();
|
|
for (donor, what) in
|
|
[(&other_msg, "another message"), (&other_ctx, "another context"), (&other_signer, "another key")]
|
|
{
|
|
refused(&sk, msg, &c, &splice(donor, &good), &format!("ML-DSA half from {what}"));
|
|
refused(&sk, msg, &c, &splice(&good, donor), &format!("Ed25519 half from {what}"));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_zeroed_or_missing_half_is_refused() {
|
|
let mut r = rng(41);
|
|
let sk = SigningKey::generate(&mut r);
|
|
let c = ctx(b"halves");
|
|
let good = sk.sign(b"m", &c, &mut r).unwrap();
|
|
let mut b = *good.as_bytes();
|
|
b[..ML].fill(0);
|
|
refused(&sk, b"m", &c, &Signature::from_bytes(&b).unwrap(), "ML-DSA half zeroed");
|
|
let mut b = *good.as_bytes();
|
|
b[ML..].fill(0);
|
|
refused(&sk, b"m", &c, &Signature::from_bytes(&b).unwrap(), "Ed25519 half zeroed");
|
|
let mut b = *good.as_bytes();
|
|
b[ML..].fill(0xff);
|
|
refused(&sk, b"m", &c, &Signature::from_bytes(&b).unwrap(), "Ed25519 half all ones");
|
|
// A signature with one half cut off is the wrong length, a typed error at parse time.
|
|
assert!(Signature::from_bytes(&good.as_bytes()[..ML]).is_err(), "ML-DSA half alone");
|
|
assert!(Signature::from_bytes(&good.as_bytes()[ML..]).is_err(), "Ed25519 half alone");
|
|
}
|
|
|
|
#[test]
|
|
fn swapped_halves_are_refused() {
|
|
let mut r = rng(42);
|
|
let sk = SigningKey::generate(&mut r);
|
|
let c = ctx(b"halves");
|
|
let good = sk.sign(b"m", &c, &mut r).unwrap();
|
|
let g = good.as_bytes();
|
|
let mut swapped = Vec::with_capacity(SIGNATURE_LEN);
|
|
swapped.extend_from_slice(&g[ML..]);
|
|
swapped.extend_from_slice(&g[..ML]);
|
|
refused(&sk, b"m", &c, &Signature::from_bytes(&swapped).unwrap(), "Ed25519 || ML-DSA");
|
|
}
|
|
|
|
#[test]
|
|
fn the_ml_dsa_half_alone_is_not_an_ordinary_ml_dsa_signature() {
|
|
let mut r = rng(43);
|
|
let sk = SigningKey::generate(&mut r);
|
|
let c = ctx(b"halves");
|
|
let msg = b"m";
|
|
let sig = sk.sign(msg, &c, &mut r).unwrap();
|
|
let pk = sk.verifying_key().as_bytes();
|
|
let vk = ml_dsa::VerifyingKey::<MlDsa65>::decode(&Array::try_from(&pk[..ML_PK]).unwrap());
|
|
let half = ml_dsa::Signature::<MlDsa65>::try_from(&sig.as_bytes()[..ML]).unwrap();
|
|
|
|
let mut mp = Vec::new();
|
|
mp.extend_from_slice(b"CompositeAlgorithmSignatures2025COMPSIG-MLDSA65-Ed25519-SHA512");
|
|
mp.push(c.as_bytes().len() as u8);
|
|
mp.extend_from_slice(c.as_bytes());
|
|
mp.extend_from_slice(&Sha512::digest(msg));
|
|
|
|
// An application that verifies ML-DSA with the empty ctx (as the crate's `Verifier` trait does)
|
|
// refuses the half, whether it is shown M' or the message itself.
|
|
assert!(!vk.verify_with_context(&mp, b"", &half), "empty ctx over M'");
|
|
assert!(!vk.verify_with_context(msg, b"", &half), "empty ctx over M");
|
|
assert!(!vk.verify_with_context(msg, b"COMPSIG-MLDSA65-Ed25519-SHA512", &half), "Label ctx over M");
|
|
// Control: it is a valid ML-DSA signature over M' under ctx = Label, and nothing else.
|
|
assert!(vk.verify_with_context(&mp, b"COMPSIG-MLDSA65-Ed25519-SHA512", &half));
|
|
|
|
// Likewise the Ed25519 half: it signs M', never the message.
|
|
let ed = ed25519_dalek::VerifyingKey::from_bytes(pk[ML_PK..].try_into().unwrap()).unwrap();
|
|
let ed_half = ed25519_dalek::Signature::from_slice(&sig.as_bytes()[ML..]).unwrap();
|
|
assert!(ed.verify_strict(msg, &ed_half).is_err(), "Ed25519 over M");
|
|
assert!(ed.verify_strict(&mp, &ed_half).is_ok(), "control: Ed25519 over M'");
|
|
}
|
|
|
|
/// RFC 10073 §9.2: not SUF-CMA. Pinned so a change in this property is noticed, not hidden.
|
|
#[test]
|
|
fn halves_of_two_signatures_of_the_same_message_combine_as_the_rfc_says() {
|
|
let mut r = rng(44);
|
|
let sk = SigningKey::generate(&mut r);
|
|
let c = ctx(b"halves");
|
|
let a = sk.sign(b"m", &c, &mut r).unwrap();
|
|
let b = sk.sign(b"m", &c, &mut r).unwrap();
|
|
assert_ne!(a, b, "hedged ML-DSA");
|
|
assert_eq!(sk.verifying_key().verify(b"m", &c, &splice(&a, &b)), Ok(()));
|
|
assert_eq!(sk.verifying_key().verify(b"m", &c, &splice(&b, &a)), Ok(()));
|
|
}
|