Files
arcanum/tests/sig_every_byte.rs
T
mnovak abcc375741 fix: AppContext::new is a const fn over a &'static label (QA S-09)
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.
2026-11-26 03:45:39 +01:00

215 lines
10 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. Labels are `&'static` (QA S-09);
// a test may leak a few to build them at run time, which a consumer must not do.
let label = b"every-byte";
for i in 0..label.len() {
let mut l = label.to_vec();
l[i] ^= 0x01;
let l: &'static [u8] = Box::leak(l.into_boxed_slice());
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".as_slice(), 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;
}
}
/// 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");
}