abcc375741
A label is one fixed string per purpose; taking any runtime bytes let a consumer derive it from a peer name, a file name or a message field, and two purposes could then collide. AppContext::new now takes &'static [u8] and is a const fn, so contexts are built as consts and a bad label fails the consumer's build; there is no runtime constructor (the simplest API: one constructor, no escape hatch until a need appears). Tests: a const OK and a const EMPTY context; sig_hygiene refuses a consumer that passes a runtime Vec (E0597/E0716); the one test that needs runtime labels (flipping label bytes) leaks them on purpose. The random labels in sig_malformed and fuzz mode 5's AppContext part are dropped (the raw-ctx part of mode 5 stays). A doctest shows the const pattern, and docs/02_signatures.md says so.
191 lines
8.2 KiB
Rust
191 lines
8.2 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: &'static [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(()));
|
|
}
|
|
|
|
/// QA S-01 (PR #2 review, their `qa_s03`): the Ed25519 half is checked with `verify_strict`, not
|
|
/// plain `verify`. A signer who knows its own Ed25519 scalar `a` can make a half with R = the
|
|
/// identity (a small-order point) and s = H(R || A || M')·a. Plain `verify` accepts it,
|
|
/// `verify_strict` refuses it, and so must Arcanum. Kills `verify_strict` -> `verify`.
|
|
#[test]
|
|
fn a_small_order_r_in_the_ed25519_half_is_refused() {
|
|
use curve25519_dalek::scalar::Scalar;
|
|
use ed25519_dalek::Verifier as _;
|
|
|
|
let seeds = [0x11u8; 64]; // a throwaway key
|
|
let sk = SigningKey::from_bytes(&seeds).unwrap();
|
|
let vk = sk.verifying_key();
|
|
let c = ctx(b"small-order-r");
|
|
let msg = b"m";
|
|
let good = sk.sign(msg, &c, &mut rng(45)).unwrap();
|
|
|
|
// a = clamp(SHA-512(Ed25519 seed)[..32]) (RFC 8032 §5.1.5).
|
|
let h = Sha512::digest(&seeds[32..]);
|
|
let mut a = [0u8; 32];
|
|
a.copy_from_slice(&h[..32]);
|
|
a[0] &= 248;
|
|
a[31] &= 127;
|
|
a[31] |= 64;
|
|
let a = Scalar::from_bytes_mod_order(a);
|
|
|
|
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));
|
|
|
|
let mut r = [0u8; 32];
|
|
r[0] = 1; // the identity, canonically encoded: order 1
|
|
let a_bytes = &vk.as_bytes()[ML_PK..];
|
|
let mut kh = Sha512::new();
|
|
kh.update(r);
|
|
kh.update(a_bytes);
|
|
kh.update(&mp);
|
|
let k = Scalar::from_bytes_mod_order_wide(&kh.finalize().into());
|
|
let mut ed_half = [0u8; 64];
|
|
ed_half[..32].copy_from_slice(&r);
|
|
ed_half[32..].copy_from_slice((k * a).as_bytes());
|
|
|
|
// Controls: this is the one difference between the two Ed25519 checks.
|
|
let ed_vk = ed25519_dalek::VerifyingKey::from_bytes(a_bytes.try_into().unwrap()).unwrap();
|
|
let ed_sig = ed25519_dalek::Signature::from_bytes(&ed_half);
|
|
assert!(ed_vk.verify(&mp, &ed_sig).is_ok(), "control: plain verify accepts a small-order R");
|
|
assert!(ed_vk.verify_strict(&mp, &ed_sig).is_err(), "control: verify_strict refuses it");
|
|
|
|
let mut b = [0u8; SIGNATURE_LEN];
|
|
b[..ML].copy_from_slice(&good.as_bytes()[..ML]);
|
|
b[ML..].copy_from_slice(&ed_half);
|
|
refused(&sk, msg, &c, &Signature::from_bytes(&b).unwrap(), "small-order R in the Ed25519 half");
|
|
}
|