//! Mandatory test 5, fuzzed: every entry point that takes bytes from the network must return a //! typed result for any input, and never panic. Also QA A-43: a sealed box opens only if it is //! exactly what `seal` produced. //! //! The first byte picks the mode. Modes 0-3 parse raw bytes. Modes 4-7 XOR the input into a valid //! public key, ciphertext or sealed box, so the fuzzer reaches decapsulation and the AEAD instead //! of stopping at the length checks. #![no_main] use arcanum::xwing::{Ciphertext, PublicKey, SecretKey}; use arcanum::{exchange, seal}; use libfuzzer_sys::fuzz_target; use std::sync::OnceLock; const ESEED_I: [u8; 64] = [0x24; 64]; const ESEED_R: [u8; 64] = [0x25; 64]; 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 } fuzz_target!(|input: &[u8]| { let Some((&mode, data)) = input.split_first() else { return }; // Keys are built once; expanding them on every run would dominate the fuzzing time. static KEYS: OnceLock<(SecretKey, SecretKey)> = OnceLock::new(); let (me, peer) = KEYS.get_or_init(|| (SecretKey::from_seed([0x42; 32]), SecretKey::from_seed([0x43; 32]))); match mode % 8 { 0 => { if let Ok(pk) = PublicKey::from_bytes(data) { let _ = exchange::initiate_derand(me, &pk, b"fuzz", &ESEED_I); } } 1 => { if let Ok(ct) = Ciphertext::from_bytes(data) { let _ = exchange::respond_derand(me, peer.public_key(), &ct, b"fuzz", &ESEED_R); if let Ok(init) = exchange::initiate_derand(me, peer.public_key(), b"fuzz", &ESEED_I) { let _ = init.finish(&ct); } } } 2 => { let _ = seal::open(me, data, b"fuzz"); } 3 => { if let Ok(k) = SecretKey::from_bytes(data) { let _ = k.public_key().as_bytes(); } } 4 => { let pk = xor_into(peer.public_key().as_bytes(), data); if let Ok(pk) = PublicKey::from_bytes(&pk) { if let Ok(init) = exchange::initiate_derand(me, &pk, b"fuzz", &ESEED_I) { let _ = exchange::respond_derand(peer, me.public_key(), init.ciphertext(), b"fuzz", &ESEED_R); } } } 5 | 6 => { // Tamper with ct_IR (mode 5) or ct_RI (mode 6) of an honest exchange. let init = exchange::initiate_derand(me, peer.public_key(), b"fuzz", &ESEED_I).unwrap(); let honest_ir = init.ciphertext().clone(); let (honest_ri, responder) = exchange::respond_derand(peer, me.public_key(), &honest_ir, b"fuzz", &ESEED_R).unwrap(); let honest_psk = *responder.wireguard_psk(0).as_bytes(); if mode % 8 == 5 { let bad = xor_into(honest_ir.as_bytes(), data); if let Ok(bad) = Ciphertext::from_bytes(&bad) { if let Ok((_, s)) = exchange::respond_derand(peer, me.public_key(), &bad, b"fuzz", &ESEED_R) { // Only an unchanged ciphertext may reproduce the honest PSK. if bad != honest_ir { assert_ne!(s.wireguard_psk(0).as_bytes(), &honest_psk); } } } } else { let bad = xor_into(honest_ri.as_bytes(), data); if let Ok(bad) = Ciphertext::from_bytes(&bad) { if let Ok(s) = init.finish(&bad) { if bad != honest_ri { assert_ne!(s.wireguard_psk(0).as_bytes(), &honest_psk); } } } } } _ => { let sealed = seal::seal_derand(me.public_key(), b"fuzz plaintext", b"fuzz", &ESEED_I).unwrap(); let bad = xor_into(&sealed, data); if let Ok(pt) = seal::open(me, &bad, b"fuzz") { // Only the unchanged box may open. assert_eq!(bad, sealed); assert_eq!(pt.as_slice(), b"fuzz plaintext"); } } } });