79 lines
2.7 KiB
Rust
79 lines
2.7 KiB
Rust
//! Mandatory test 5, fuzzed: every entry point that takes bytes from the network must return a
|
|
//! typed result for any input, and never panic.
|
|
//!
|
|
//! The first byte picks the mode. Modes 0-3 parse raw bytes. Modes 4-6 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;
|
|
|
|
const SEED: [u8; 32] = [0x42; 32];
|
|
const ESEED: [u8; 64] = [0x24; 64];
|
|
|
|
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
|
|
}
|
|
|
|
fuzz_target!(|input: &[u8]| {
|
|
let Some((&mode, data)) = input.split_first() else { return };
|
|
let sk = SecretKey::from_seed(SEED);
|
|
match mode % 7 {
|
|
0 => {
|
|
if let Ok(pk) = PublicKey::from_bytes(data) {
|
|
let _ = exchange::initiate_derand(&pk, b"fuzz", &ESEED);
|
|
}
|
|
}
|
|
1 => {
|
|
if let Ok(ct) = Ciphertext::from_bytes(data) {
|
|
let _ = exchange::respond(&sk, &ct, b"fuzz");
|
|
}
|
|
}
|
|
2 => {
|
|
let _ = seal::open(&sk, data, b"fuzz");
|
|
}
|
|
3 => {
|
|
if let Ok(k) = SecretKey::from_bytes(data) {
|
|
let _ = k.public_key().as_bytes();
|
|
}
|
|
}
|
|
4 => {
|
|
let pk = xor_into(sk.public_key().as_bytes(), data);
|
|
if let Ok(pk) = PublicKey::from_bytes(&pk) {
|
|
let _ = exchange::initiate_derand(&pk, b"fuzz", &ESEED);
|
|
}
|
|
}
|
|
5 => {
|
|
let (ct, honest) = exchange::initiate_derand(sk.public_key(), b"fuzz", &ESEED).unwrap();
|
|
let bad = xor_into(ct.as_bytes(), data);
|
|
if let Ok(bad) = Ciphertext::from_bytes(&bad) {
|
|
if let Ok(s) = exchange::respond(&sk, &bad, b"fuzz") {
|
|
// Only an unchanged ciphertext may reproduce the honest PSK.
|
|
if bad.as_bytes() != ct.as_bytes() {
|
|
assert_ne!(s.wireguard_psk(0).as_bytes(), honest.wireguard_psk(0).as_bytes());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_ => {
|
|
let sealed = seal::seal_derand(sk.public_key(), b"fuzz plaintext", b"fuzz", &ESEED).unwrap();
|
|
let bad = xor_into(&sealed, data);
|
|
if let Ok(pt) = seal::open(&sk, &bad, b"fuzz") {
|
|
// Only the unchanged box may open.
|
|
assert_eq!(bad, sealed);
|
|
assert_eq!(pt.as_slice(), b"fuzz plaintext");
|
|
}
|
|
}
|
|
}
|
|
});
|