//! 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 { 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 = 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 label, and any raw ctx (0..=255 bytes, or a typed error for longer). if let Ok(c) = AppContext::new(data) { assert!(vk.verify(MSG, &c, &f.sig).is_err() || c == f.ctx); } 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"); } } } } });