From ddda8415eb5575b7a815908827599cb1781bbcce Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 13:33:53 +0100 Subject: [PATCH] test: fuzz target for the signature API (mandatory test 6) fuzz/fuzz_targets/signatures.rs: raw bytes into VerifyingKey, Signature, SigningKey and AppContext; and the input XORed into a valid verifying key, signature, message, raw ctx, key-binding id/window/time, X-Wing key and binding signature. Beyond "never panics" it asserts that only the exact signed bytes verify. Run on 5a52854 plus this target, Windows 10, nightly 1.102.0 (739adecd4 2026-11-24), libFuzzer + ASan (MSVC 14.44 asan runtime), corpus and artifacts on D:, 12 seed inputs: 605 s, 38,609 executions, 63 exec/s, 0 crashes, 0 artifacts, coverage 3,549, 122 corpus units. --- fuzz/Cargo.lock | 98 ++++++++++++++++++++++- fuzz/Cargo.toml | 10 +++ fuzz/fuzz_targets/signatures.rs | 135 ++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 fuzz/fuzz_targets/signatures.rs diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 28fd905..84f487a 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -20,11 +20,13 @@ checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" [[package]] name = "arcanum" -version = "0.1.0" +version = "0.2.0" dependencies = [ "chacha20poly1305", + "ed25519-dalek", "hkdf", "hmac", + "ml-dsa", "ml-kem", "rand_core", "sha2", @@ -39,7 +41,9 @@ name = "arcanum-fuzz" version = "0.0.0" dependencies = [ "arcanum", + "chacha20", "libfuzzer-sys", + "rand_core", ] [[package]] @@ -48,6 +52,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + [[package]] name = "block-buffer" version = "0.12.1" @@ -85,6 +95,7 @@ dependencies = [ "cfg-if", "cipher", "cpufeatures", + "rand_core", "zeroize", ] @@ -118,6 +129,12 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + [[package]] name = "cpufeatures" version = "0.3.1" @@ -155,6 +172,7 @@ dependencies = [ "cfg-if", "cpufeatures", "curve25519-dalek-derive", + "digest", "fiat-crypto", "rustc_version", "subtle", @@ -172,6 +190,16 @@ dependencies = [ "syn", ] +[[package]] +name = "der" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "digest" version = "0.11.3" @@ -184,6 +212,28 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ed25519" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29fcf32e6c73d1079f83ab4d782de2d81620346a5f38c6237a86a22f8368980a" +dependencies = [ + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ebaa1a2bf1290ab3bfe5a7b771d050ebffab2711c19a81691c683a5144a25de" +dependencies = [ + "curve25519-dalek", + "ed25519", + "sha2", + "subtle", + "zeroize", +] + [[package]] name = "fiat-crypto" version = "0.3.0" @@ -291,6 +341,22 @@ dependencies = [ "cc", ] +[[package]] +name = "ml-dsa" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f6d49c3f3f0e10e18ca975ffda66f8da0ec36521c1d911adcbc521bf0743f5c" +dependencies = [ + "crypto-common", + "ctutils", + "hybrid-array", + "module-lattice", + "pkcs8", + "shake", + "signature", + "zeroize", +] + [[package]] name = "ml-kem" version = "0.3.4" @@ -327,6 +393,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "pkcs8" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7" +dependencies = [ + "der", + "spki", +] + [[package]] name = "poly1305" version = "0.9.1" @@ -421,6 +497,26 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "signature" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" +dependencies = [ + "digest", + "rand_core", +] + +[[package]] +name = "spki" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef958a98b9d5da290cfc78946e9f3e61e1e62a18db0d92cac0b83cc161491a9" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "sponge-cursor" version = "0.1.0" diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 02f6fbf..a159c5e 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -11,6 +11,9 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" arcanum = { path = "..", features = ["hazmat"] } +# A seeded RNG for the signing fixture (fuzz input never chooses randomness). +chacha20 = { version = "0.10", default-features = false, features = ["rng"] } +rand_core = { version = "0.10", default-features = false } # Keep the fuzz crate out of the library's own dependency graph and checks. [workspace] @@ -22,3 +25,10 @@ path = "fuzz_targets/untrusted_input.rs" test = false doc = false bench = false + +[[bin]] +name = "signatures" +path = "fuzz_targets/signatures.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/signatures.rs b/fuzz/fuzz_targets/signatures.rs new file mode 100644 index 0000000..a980815 --- /dev/null +++ b/fuzz/fuzz_targets/signatures.rs @@ -0,0 +1,135 @@ +//! 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"); + } + } + } + } +});