test: pin verify_strict, the verify_binding order and independent seeds (QA S-01 to S-03)
Ported from QA's proof tests for PR #2 (qa_s03 to qa_s05): - S-01, sig_halves: an Ed25519 half with a small-order R (the identity, s = H(R || A || M')·a from a throwaway key's own scalar) is accepted by plain verify and refused by verify_strict; Arcanum must refuse it. - S-02, sig_every_byte: a forged window that also excludes now gives InvalidSignature, never OutsideValidity. - S-03, sig_composite: generate draws exactly 64 bytes, and the two seeds are those bytes in order (RFC 10073 3.1, independent component keys). Each mutation QA describes now fails (applied to src/sign.rs, reverted): Q1 verify_strict -> verify fails the S-01 test; Q3 now checked before the signature fails the S-02 test; Q4 the Ed25519 seed copied from the ML-DSA seed fails the S-03 test. Adds curve25519-dalek =5.0.0 as a dev-dependency (already in the lock) for the scalar arithmetic.
This commit is contained in:
Generated
+1
@@ -20,6 +20,7 @@ dependencies = [
|
||||
"base64",
|
||||
"chacha20",
|
||||
"chacha20poly1305",
|
||||
"curve25519-dalek",
|
||||
"ed25519-dalek",
|
||||
"hex",
|
||||
"hkdf",
|
||||
|
||||
@@ -44,6 +44,8 @@ hex = "0.4"
|
||||
sha2 = "0.11"
|
||||
# The RFC 10073 vectors are base64.
|
||||
base64 = "0.22"
|
||||
# Ed25519 scalar arithmetic, for the small-order-R half (QA S-01). Already in the lock via ed25519-dalek.
|
||||
curve25519-dalek = { version = "=5.0.0", default-features = false }
|
||||
|
||||
# ML-KEM in a debug build is slow; the byte-flip tests run thousands of exchanges.
|
||||
[profile.dev.package."*"]
|
||||
|
||||
@@ -175,3 +175,36 @@ fn ml_dsa_half_uses_the_label_as_its_context() {
|
||||
assert!(!ml_vk.verify_with_context(&mp, b"", &ml_sig), "not the empty ctx");
|
||||
assert!(!ml_vk.verify_with_context(&mp, ctx.as_bytes(), &ml_sig), "not the application ctx");
|
||||
}
|
||||
|
||||
/// An RNG that records what it hands out (backed by the test ChaCha20).
|
||||
struct Recording(chacha20::ChaCha20Rng, Vec<u8>);
|
||||
|
||||
impl rand_core::TryRng for Recording {
|
||||
type Error = core::convert::Infallible;
|
||||
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
|
||||
unimplemented!("signing draws bytes, not words")
|
||||
}
|
||||
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
|
||||
unimplemented!("signing draws bytes, not words")
|
||||
}
|
||||
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
|
||||
rand_core::Rng::fill_bytes(&mut self.0, dst);
|
||||
self.1.extend_from_slice(dst);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl rand_core::TryCryptoRng for Recording {}
|
||||
|
||||
/// QA S-03 (their `qa_s05`), RFC 10073 §3.1: `generate` draws 64 fresh bytes, the ML-DSA seed then
|
||||
/// the Ed25519 seed, independently. Kills deriving one seed from the other, or drawing only 32.
|
||||
#[test]
|
||||
fn generate_draws_two_independent_seeds() {
|
||||
let mut rec = Recording(rng(35), Vec::new());
|
||||
let sk = SigningKey::generate(&mut rec);
|
||||
assert_eq!(rec.1.len(), 64, "exactly 64 bytes drawn");
|
||||
assert_eq!(sk.to_bytes().as_slice(), rec.1.as_slice(), "the seeds are the RNG output, in order");
|
||||
assert_ne!(rec.1[..32], rec.1[32..], "two different seeds");
|
||||
let again = SigningKey::from_bytes(&rec.1).unwrap();
|
||||
assert_eq!(again.verifying_key(), sk.verifying_key());
|
||||
}
|
||||
|
||||
@@ -194,3 +194,19 @@ fn every_field_of_a_key_binding_matters() {
|
||||
flipped[i] ^= 0x01;
|
||||
}
|
||||
}
|
||||
|
||||
/// QA S-02 (their `qa_s04`): `OutsideValidity` only ever means "authentic, but not now". A forged
|
||||
/// window that also excludes `now` is `InvalidSignature`. Kills checking `now` before the signature.
|
||||
#[test]
|
||||
fn a_forged_window_that_excludes_now_is_invalid_signature_not_outside_validity() {
|
||||
let mut r = rng(57);
|
||||
let sk = SigningKey::generate(&mut r);
|
||||
let kem = xwing::SecretKey::generate(&mut r);
|
||||
let sig = sign::bind_kem_key(&sk, b"dev", kem.public_key(), (1_000, 2_000), &mut r).unwrap();
|
||||
let check =
|
||||
|v: (u64, u64), now: u64| sign::verify_binding(sk.verifying_key(), b"dev", kem.public_key(), v, now, &sig);
|
||||
assert_eq!(check((1_000, 2_000), 1_500), Ok(()), "control");
|
||||
assert_eq!(check((1_000, 2_000), 2_500), Err(Error::OutsideValidity), "control: authentic, expired");
|
||||
assert_eq!(check((1_000, 2_001), 2_500), Err(Error::InvalidSignature), "forged window, now after it");
|
||||
assert_eq!(check((3_000, 4_000), 2_500), Err(Error::InvalidSignature), "forged window, now before it");
|
||||
}
|
||||
|
||||
@@ -133,3 +133,58 @@ fn halves_of_two_signatures_of_the_same_message_combine_as_the_rfc_says() {
|
||||
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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user