48 lines
1.8 KiB
Rust
48 lines
1.8 KiB
Rust
|
|
//! 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).
|
||
|
|
#![no_main]
|
||
|
|
|
||
|
|
use libfuzzer_sys::fuzz_target;
|
||
|
|
use onionwire::ratchet::{RatchetKeyPair, RatchetState};
|
||
|
|
|
||
|
|
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. The keys are fixed (not fuzzed) on purpose:
|
||
|
|
// the interesting surface is `data` as the frame.
|
||
|
|
let mut state = RatchetState::bootstrap(
|
||
|
|
[0x42u8; 32],
|
||
|
|
RatchetKeyPair::from_secret([0x11u8; 32]),
|
||
|
|
[0x33u8; 32],
|
||
|
|
);
|
||
|
|
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");
|
||
|
|
});
|