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.
134 lines
5.3 KiB
Rust
134 lines
5.3 KiB
Rust
//! v0.2.0 mandatory test 6, fuzzed: every signature entry point that takes bytes from the network
|
|
//! returns a typed result for any input and never panics, and only the exact bytes that were
|
|
//! signed verify (one key, one message, one context: the hedged ML-DSA half is not re-signed).
|
|
//!
|
|
//! The first byte picks the mode. Modes 0-1 parse raw bytes. Modes 2-7 XOR the input into a valid
|
|
//! verifying key, signature, message, context or key binding, so the fuzzer reaches ML-DSA's hint
|
|
//! and norm checks and Ed25519's point decoding instead of stopping at the length checks.
|
|
#![no_main]
|
|
|
|
use arcanum::sign::{self, AppContext, Signature, SigningKey, VerifyingKey};
|
|
use arcanum::xwing;
|
|
use chacha20::ChaCha20Rng;
|
|
use libfuzzer_sys::fuzz_target;
|
|
use rand_core::SeedableRng;
|
|
use std::sync::OnceLock;
|
|
|
|
const MSG: &[u8] = b"fuzz message";
|
|
const LABEL: &[u8] = b"fuzz";
|
|
const ID: &[u8] = b"fuzz-device";
|
|
const VALID: (u64, u64) = (100, 200);
|
|
|
|
struct Fixture {
|
|
sk: SigningKey,
|
|
ctx: AppContext,
|
|
sig: Signature,
|
|
kem: xwing::SecretKey,
|
|
binding: Signature,
|
|
}
|
|
|
|
fn xor_into(base: &[u8], data: &[u8]) -> Vec<u8> {
|
|
let mut out = base.to_vec();
|
|
for (i, b) in data.iter().enumerate() {
|
|
if i < out.len() {
|
|
out[i] ^= b;
|
|
} else {
|
|
out.push(*b);
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
fn be_u64(data: &[u8], at: usize) -> u64 {
|
|
let mut b = [0u8; 8];
|
|
for (i, x) in b.iter_mut().enumerate() {
|
|
*x = data.get(at + i).copied().unwrap_or(0);
|
|
}
|
|
u64::from_be_bytes(b)
|
|
}
|
|
|
|
fuzz_target!(|input: &[u8]| {
|
|
let Some((&mode, data)) = input.split_first() else { return };
|
|
// Key generation and signing are built once; redoing them per input would dominate the time.
|
|
static FIX: OnceLock<Fixture> = OnceLock::new();
|
|
let f = FIX.get_or_init(|| {
|
|
let mut rng = ChaCha20Rng::from_seed([0x5a; 32]);
|
|
let sk = SigningKey::from_bytes(&[0x42; 64]).unwrap();
|
|
let ctx = AppContext::new(LABEL).unwrap();
|
|
let sig = sk.sign(MSG, &ctx, &mut rng).unwrap();
|
|
let kem = xwing::SecretKey::from_bytes(&[0x43; 32]).unwrap();
|
|
let binding = sign::bind_kem_key(&sk, ID, kem.public_key(), VALID, &mut rng).unwrap();
|
|
Fixture { sk, ctx, sig, kem, binding }
|
|
});
|
|
let vk = f.sk.verifying_key();
|
|
match mode % 8 {
|
|
0 => {
|
|
if let Ok(k) = VerifyingKey::from_bytes(data) {
|
|
assert!(k.verify(MSG, &f.ctx, &f.sig).is_err() || &k == vk);
|
|
}
|
|
}
|
|
1 => {
|
|
if let Ok(s) = Signature::from_bytes(data) {
|
|
assert!(vk.verify(MSG, &f.ctx, &s).is_err() || s == f.sig);
|
|
}
|
|
let _ = SigningKey::from_bytes(data).map(|k| k.verifying_key().as_bytes()[0]);
|
|
}
|
|
2 => {
|
|
let bad = xor_into(f.sig.as_bytes(), data);
|
|
if let Ok(s) = Signature::from_bytes(&bad) {
|
|
if vk.verify(MSG, &f.ctx, &s).is_ok() {
|
|
assert_eq!(s, f.sig, "a changed signature verified");
|
|
}
|
|
}
|
|
}
|
|
3 => {
|
|
let bad = xor_into(vk.as_bytes(), data);
|
|
if let Ok(k) = VerifyingKey::from_bytes(&bad) {
|
|
if k.verify(MSG, &f.ctx, &f.sig).is_ok() {
|
|
assert_eq!(&k, vk, "a signature verified under a changed key");
|
|
}
|
|
}
|
|
}
|
|
4 => {
|
|
let m = xor_into(MSG, data);
|
|
if vk.verify(&m, &f.ctx, &f.sig).is_ok() {
|
|
assert_eq!(m, MSG, "a changed message verified");
|
|
}
|
|
}
|
|
5 => {
|
|
// Any raw ctx (0..=255 bytes, or a typed error for longer). `AppContext` labels are
|
|
// `&'static` and fixed at compile time, so there is no runtime label to fuzz.
|
|
let raw = xor_into(f.ctx.as_bytes(), data);
|
|
match sign::verify_composite_raw(vk, MSG, &raw, &f.sig) {
|
|
Ok(()) => assert_eq!(raw, f.ctx.as_bytes()),
|
|
Err(e) => assert!(raw.len() <= 255 || matches!(e, arcanum::Error::TooLong { .. })),
|
|
}
|
|
}
|
|
6 => {
|
|
// A key binding with a changed id, window and time.
|
|
let id = xor_into(ID, data.get(17..).unwrap_or(&[]));
|
|
let valid = (VALID.0 ^ be_u64(data, 0), VALID.1 ^ be_u64(data, 8));
|
|
let now = 150 ^ u64::from(data.first().copied().unwrap_or(0));
|
|
if sign::verify_binding(vk, &id, f.kem.public_key(), valid, now, &f.binding).is_ok() {
|
|
assert_eq!((id.as_slice(), valid), (ID, VALID), "a changed binding verified");
|
|
assert!((VALID.0..=VALID.1).contains(&now));
|
|
}
|
|
}
|
|
_ => {
|
|
// A changed X-Wing key in a binding, and a changed binding signature.
|
|
let kem = xor_into(f.kem.public_key().as_bytes(), data);
|
|
if let Ok(k) = xwing::PublicKey::from_bytes(&kem) {
|
|
if sign::verify_binding(vk, ID, &k, VALID, 150, &f.binding).is_ok() {
|
|
assert_eq!(&k, f.kem.public_key(), "a binding verified for another X-Wing key");
|
|
}
|
|
}
|
|
let bad = xor_into(f.binding.as_bytes(), data);
|
|
if let Ok(s) = Signature::from_bytes(&bad) {
|
|
if sign::verify_binding(vk, ID, f.kem.public_key(), VALID, 150, &s).is_ok() {
|
|
assert_eq!(s, f.binding, "a changed binding signature verified");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|