fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
//! Untrusted peer bytes against the Double Ratchet frame parser.
|
|
|
|
|
//!
|
|
|
|
|
//! Two entry points, both reachable from the network:
|
|
|
|
|
//!
|
|
|
|
|
//! - `RatchetState::from_bytes` — persisted-state restore (a tampered or
|
|
|
|
|
//! truncated blob from the store must be an `Err`, never a panic).
|
|
|
|
|
//! - `RatchetState::decrypt` — the wire path (`Frame::parse` + `plan_*` +
|
|
|
|
|
//! `open`); every field in the header is attacker-controlled.
|
|
|
|
|
//!
|
|
|
|
|
//! Neither call may panic, and neither may leave the state mutated on a frame
|
|
|
|
|
//! it rejects (asserted in `tests/ratchet.rs`, re-checked cheaply here by
|
|
|
|
|
//! re-serializing after every decrypt).
|
2026-10-30 23:24:58 -04:00
|
|
|
//!
|
|
|
|
|
//! Both bootstrap shapes are exercised: a classical friend (`remote_kem_pub =
|
|
|
|
|
//! None`) and a hybrid friend, whose peer ML-KEM ratchet key is a real
|
|
|
|
|
//! encapsulation key of `kem::PK_LEN` bytes derived from a fixed seed. A
|
|
|
|
|
//! garbage key would only ever hit the "invalid encapsulation key" error path.
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
|
|
use libfuzzer_sys::fuzz_target;
|
2026-10-30 23:24:58 -04:00
|
|
|
use onionwire::kem::{self, PK_LEN, SEED_LEN};
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
use onionwire::ratchet::{RatchetKeyPair, RatchetState};
|
2026-10-30 23:24:58 -04:00
|
|
|
use std::sync::OnceLock;
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
|
2026-10-30 23:24:58 -04:00
|
|
|
/// A canonical ML-KEM-768 encapsulation key, built once: key generation is
|
|
|
|
|
/// deterministic for a fixed seed, so this only pays for itself on iteration 1.
|
|
|
|
|
fn peer_kem_pub() -> &'static [u8] {
|
|
|
|
|
static PK: OnceLock<Vec<u8>> = OnceLock::new();
|
|
|
|
|
PK.get_or_init(|| {
|
|
|
|
|
let pk = kem::pk_from_seed(&[0x44u8; SEED_LEN]).expect("64 bytes is a valid ML-KEM seed");
|
|
|
|
|
assert_eq!(pk.len(), PK_LEN, "crate ML-KEM public key length changed");
|
|
|
|
|
pk
|
|
|
|
|
})
|
|
|
|
|
}
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
|
2026-10-30 23:24:58 -04:00
|
|
|
fn bootstrap(remote_kem_pub: Option<&[u8]>) -> RatchetState {
|
|
|
|
|
// The keys are fixed (not fuzzed) on purpose: the interesting surface is
|
|
|
|
|
// `data` as the frame.
|
|
|
|
|
RatchetState::bootstrap(
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
[0x42u8; 32],
|
|
|
|
|
RatchetKeyPair::from_secret([0x11u8; 32]),
|
|
|
|
|
[0x33u8; 32],
|
2026-10-30 23:24:58 -04:00
|
|
|
remote_kem_pub,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Feed `data` to a fresh state and hold the existing invariants: a reject must
|
|
|
|
|
/// roll back, an accept must have advanced the receiving chain, and re-sending
|
|
|
|
|
/// from the resulting state must not panic.
|
|
|
|
|
fn exercise(remote_kem_pub: Option<&[u8]>, data: &[u8]) {
|
|
|
|
|
let mut state = bootstrap(remote_kem_pub);
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
let before = state.to_bytes();
|
|
|
|
|
|
|
|
|
|
match state.decrypt(data) {
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
// A frame this state accepts must have advanced the receiving chain.
|
|
|
|
|
let _ = state.recv_index();
|
|
|
|
|
}
|
|
|
|
|
Err(_) => {
|
|
|
|
|
// Refuse-and-roll-back: a rejected frame leaves the state untouched.
|
|
|
|
|
assert_eq!(state.to_bytes(), before, "rejected frame mutated the state");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Re-encrypting a byte-identical state must stay a pure function.
|
|
|
|
|
let _ = state.encrypt(b"fuzz");
|
2026-10-30 23:24:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fuzz_target!(|data: &[u8]| {
|
|
|
|
|
// Restore path: arbitrary bytes as a persisted ratchet state.
|
|
|
|
|
if let Ok(state) = RatchetState::from_bytes(data) {
|
|
|
|
|
// A state that parsed must re-serialize without panicking.
|
|
|
|
|
let _ = state.to_bytes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wire path: a fresh bootstrap epoch for every input, so one iteration can
|
|
|
|
|
// never influence the next. Classical and hybrid, both.
|
|
|
|
|
exercise(None, data);
|
|
|
|
|
exercise(Some(peer_kem_pub()), data);
|
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:
- ratchet_frame -> RatchetState::from_bytes + RatchetState::decrypt
(the 'rat ' frame parser: prefix, version, length,
N/PN bounds, DH step, AEAD open)
- qr_decode -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
including the 'rat ' -> Kind::Ratchet routing arm
- file_decode -> file::decode on 'fil ' chunk frames
No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.
Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).
Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 20:52:44 -04:00
|
|
|
});
|