Files
arcanum/tests/sig_every_byte.rs
T
mnovak 2a432f5d86 test: the v0.2.0 signature tests, written before the code
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.
2026-11-25 12:48:39 +01:00

197 lines
9.3 KiB
Rust

//! Mandatory test 5 (v0.2.0): every byte matters. Flipping any byte of the message, the context,
//! the public key or the signature fails verification. Contexts are prefix-free (the length of
//! ctx is inside M'), and a key-binding signature never verifies as an application message, nor
//! the other way round.
mod common;
use arcanum::Error;
use arcanum::sign::{
self, AppContext, KEM_KEY_CONTEXT, SIGNATURE_LEN, Signature, SigningKey, VERIFYING_KEY_LEN, VerifyingKey,
};
use arcanum::xwing;
use common::rng;
const MSG: &[u8] = b"Built on Domi: every byte of this message is signed.";
fn setup(tag: u8) -> (SigningKey, AppContext, Signature) {
let mut r = rng(tag);
let sk = SigningKey::generate(&mut r);
let c = AppContext::new(b"every-byte").unwrap();
let sig = sk.sign(MSG, &c, &mut r).unwrap();
assert_eq!(sk.verifying_key().verify(MSG, &c, &sig), Ok(()), "control");
(sk, c, sig)
}
#[test]
fn flipping_any_byte_of_the_message_fails() {
let (sk, c, sig) = setup(50);
for i in 0..MSG.len() {
for mask in [0x01, 0x80] {
let mut m = MSG.to_vec();
m[i] ^= mask;
assert_eq!(sk.verifying_key().verify(&m, &c, &sig), Err(Error::InvalidSignature), "byte {i}");
}
}
let mut longer = MSG.to_vec();
longer.push(0);
assert_eq!(sk.verifying_key().verify(&longer, &c, &sig), Err(Error::InvalidSignature), "appended byte");
assert_eq!(sk.verifying_key().verify(&MSG[..MSG.len() - 1], &c, &sig), Err(Error::InvalidSignature));
}
#[test]
fn flipping_any_byte_of_the_context_fails() {
let (sk, c, sig) = setup(51);
let vk = sk.verifying_key();
// Through the release API: every byte of the caller's label.
let label = b"every-byte";
for i in 0..label.len() {
let mut l = label.to_vec();
l[i] ^= 0x01;
assert_eq!(vk.verify(MSG, &AppContext::new(&l).unwrap(), &sig), Err(Error::InvalidSignature), "label byte {i}");
}
// Through the raw interface: every byte of the whole ctx, Arcanum's prefix included.
let full = c.as_bytes().to_vec();
assert_eq!(sign::verify_composite_raw(vk, MSG, &full, &sig), Ok(()), "control");
for i in 0..full.len() {
let mut f = full.clone();
f[i] ^= 0x01;
assert_eq!(sign::verify_composite_raw(vk, MSG, &f, &sig), Err(Error::InvalidSignature), "ctx byte {i}");
}
}
#[test]
fn flipping_any_byte_of_the_public_key_fails() {
let (sk, c, sig) = setup(52);
let pk = *sk.verifying_key().as_bytes();
let (mut refused_at_parse, mut refused_at_verify) = (0, 0);
for i in 0..VERIFYING_KEY_LEN {
let mut b = pk;
b[i] ^= 0x01;
match VerifyingKey::from_bytes(&b) {
Err(e) => {
assert_eq!(e, Error::InvalidVerifyingKey, "byte {i}");
assert!(i >= VERIFYING_KEY_LEN - 32, "byte {i}: only the Ed25519 point can fail to parse");
refused_at_parse += 1;
}
Ok(vk) => {
assert_eq!(vk.verify(MSG, &c, &sig), Err(Error::InvalidSignature), "byte {i}");
refused_at_verify += 1;
}
}
}
assert_eq!(refused_at_parse + refused_at_verify, VERIFYING_KEY_LEN);
assert!(refused_at_verify >= 1952, "every ML-DSA byte reaches verification");
}
#[test]
fn flipping_any_byte_of_the_signature_fails() {
let (sk, c, sig) = setup(53);
for i in 0..SIGNATURE_LEN {
let mut b = *sig.as_bytes();
b[i] ^= 0x01;
let bad = Signature::from_bytes(&b).unwrap();
assert_eq!(sk.verifying_key().verify(MSG, &c, &bad), Err(Error::InvalidSignature), "byte {i}");
}
}
#[test]
fn contexts_are_prefix_free() {
let mut r = rng(54);
let sk = SigningKey::generate(&mut r);
let vk = sk.verifying_key();
let a = AppContext::new(b"a").unwrap();
let ab = AppContext::new(b"ab").unwrap();
let sig_a = sk.sign(MSG, &a, &mut r).unwrap();
let sig_ab = sk.sign(MSG, &ab, &mut r).unwrap();
assert_eq!(vk.verify(MSG, &ab, &sig_a), Err(Error::InvalidSignature), "a is a prefix of ab");
assert_eq!(vk.verify(MSG, &a, &sig_ab), Err(Error::InvalidSignature), "ab extends a");
assert_eq!(vk.verify(MSG, &AppContext::new(b"a\0").unwrap(), &sig_a), Err(Error::InvalidSignature));
// Moving a byte between ctx and message: M' holds SHA-512(M), and u8(len ctx) fixes the split.
assert_eq!(vk.verify(b"bBuilt", &a, &sk.sign(b"Built", &ab, &mut r).unwrap()), Err(Error::InvalidSignature));
// The raw interface: the empty ctx, Arcanum's prefixes and the RFC's empty ctx are all distinct.
let raw_empty = sign::sign_composite_raw(&sk, MSG, b"", &mut r).unwrap();
assert_eq!(sign::verify_composite_raw(vk, MSG, b"\0", &raw_empty), Err(Error::InvalidSignature));
assert_eq!(vk.verify(MSG, &a, &raw_empty), Err(Error::InvalidSignature));
let app_prefix_only = sign::sign_composite_raw(&sk, MSG, b"domi/arcanum/v2/sig/app/", &mut r).unwrap();
assert_eq!(vk.verify(MSG, &a, &app_prefix_only), Err(Error::InvalidSignature));
// Arcanum's fixed contexts: neither is a prefix of the other.
let app = b"domi/arcanum/v2/sig/app/";
assert_eq!(KEM_KEY_CONTEXT, b"domi/arcanum/v2/sig/kem-key");
assert!(!app.starts_with(KEM_KEY_CONTEXT) && !KEM_KEY_CONTEXT.starts_with(app));
}
/// The key-binding message, written out here from docs/02_signatures.md, independently of the code:
/// u8(1) || u64be(not_before) || u64be(not_after) || u16be(len id) || id || xwing_pk.
fn binding_message(id: &[u8], kem: &xwing::PublicKey, (nb, na): (u64, u64)) -> Vec<u8> {
let mut m = vec![1u8];
m.extend_from_slice(&nb.to_be_bytes());
m.extend_from_slice(&na.to_be_bytes());
m.extend_from_slice(&(id.len() as u16).to_be_bytes());
m.extend_from_slice(id);
m.extend_from_slice(kem.as_bytes());
m
}
#[test]
fn a_key_binding_never_verifies_as_an_app_message_and_back() {
let mut r = rng(55);
let sk = SigningKey::generate(&mut r);
let vk = sk.verifying_key();
let kem = xwing::SecretKey::generate(&mut r);
let (id, valid, now) = (b"device-7".as_slice(), (1_000, 2_000), 1_500);
let binding = sign::bind_kem_key(&sk, id, kem.public_key(), valid, &mut r).unwrap();
assert_eq!(sign::verify_binding(vk, id, kem.public_key(), valid, now, &binding), Ok(()), "control");
// The binding is exactly the documented message under the kem-key ctx.
let msg = binding_message(id, kem.public_key(), valid);
assert_eq!(sign::verify_composite_raw(vk, &msg, KEM_KEY_CONTEXT, &binding), Ok(()), "documented encoding");
// As an application message, under any application label: refused.
for label in [&b"kem-key"[..], b"", b"domi/arcanum/v2/sig/kem-key", b"x"] {
if let Ok(c) = AppContext::new(label) {
assert_eq!(vk.verify(&msg, &c, &binding), Err(Error::InvalidSignature), "label {label:?}");
}
}
// The other way round: an application signature over the very same bytes is no binding.
let app_sig = sk.sign(&msg, &AppContext::new(b"kem-key").unwrap(), &mut r).unwrap();
assert_eq!(sign::verify_binding(vk, id, kem.public_key(), valid, now, &app_sig), Err(Error::InvalidSignature));
}
#[test]
fn every_field_of_a_key_binding_matters() {
let mut r = rng(56);
let sk = SigningKey::generate(&mut r);
let vk = sk.verifying_key();
let kem = xwing::SecretKey::generate(&mut r);
let other_kem = xwing::SecretKey::generate(&mut r);
let (id, valid) = (b"device-7".as_slice(), (1_000u64, 2_000u64));
let sig = sign::bind_kem_key(&sk, id, kem.public_key(), valid, &mut r).unwrap();
let check =
|id: &[u8], k: &xwing::PublicKey, v: (u64, u64), now: u64| sign::verify_binding(vk, id, k, v, now, &sig);
assert_eq!(check(id, kem.public_key(), valid, 1_000), Ok(()), "not_before is inclusive");
assert_eq!(check(id, kem.public_key(), valid, 2_000), Ok(()), "not_after is inclusive");
assert_eq!(check(id, kem.public_key(), valid, 999), Err(Error::OutsideValidity));
assert_eq!(check(id, kem.public_key(), valid, 2_001), Err(Error::OutsideValidity));
assert_eq!(check(b"device-8", kem.public_key(), valid, 1_500), Err(Error::InvalidSignature), "id");
assert_eq!(check(b"device-7\0", kem.public_key(), valid, 1_500), Err(Error::InvalidSignature), "id length");
assert_eq!(check(id, other_kem.public_key(), valid, 1_500), Err(Error::InvalidSignature), "X-Wing key");
assert_eq!(check(id, kem.public_key(), (1_000, 2_001), 1_500), Err(Error::InvalidSignature), "not_after");
assert_eq!(check(id, kem.public_key(), (999, 2_000), 1_500), Err(Error::InvalidSignature), "not_before");
// A forged window is refused as a bad signature before its dates are looked at.
assert_eq!(check(id, kem.public_key(), (0, u64::MAX), 5_000), Err(Error::InvalidSignature));
// An inverted window is refused when binding and when verifying.
assert_eq!(sign::bind_kem_key(&sk, id, kem.public_key(), (2, 1), &mut r), Err(Error::InvalidValidity));
assert_eq!(check(id, kem.public_key(), (2_000, 1_000), 1_500), Err(Error::InvalidValidity));
// Every byte of the X-Wing key matters (its encoding is fixed-width and signed whole).
let mut flipped = *kem.public_key().as_bytes();
for i in [0, 600, 1183, 1184, 1215] {
flipped[i] ^= 0x01;
if let Ok(k) = xwing::PublicKey::from_bytes(&flipped) {
assert_eq!(check(id, &k, valid, 1_500), Err(Error::InvalidSignature), "X-Wing byte {i}");
}
flipped[i] ^= 0x01;
}
}