Skip to content

Commit

Permalink
test(fuzz): add some libFuzzer targets
Browse files Browse the repository at this point in the history
  • Loading branch information
codahale committed Oct 25, 2023
1 parent 4a6b4e9 commit c4eec32
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/target
/fuzz/corpus
/fuzz/artifacts
/fuzz/coverage
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ portable = ["aes"]
std = []

[workspace]
members = ["xtask"]
members = ["fuzz", "xtask"]

[dev-dependencies]
divan = "0.1.0"
Expand Down
27 changes: 27 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "fuzz"
version = "0.1.0"
edition = "2021"

[dependencies]
arbitrary = { version = "1.3.1", features = ["derive"] }
libfuzzer-sys = "0.4.7"
lockstitch = { path = ".." }

[[bin]]
name = "aead"
path = "fuzz_targets/aead.rs"

[[bin]]
name = "hash"
path = "fuzz_targets/hash.rs"

[[bin]]
name = "new"
path = "fuzz_targets/new.rs"

[package.metadata]
cargo-fuzz = true

[package.metadata.release]
release = false
23 changes: 23 additions & 0 deletions fuzz/fuzz_targets/aead.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_main]
use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use lockstitch::{Protocol, TAG_LEN};

#[derive(Debug, Arbitrary)]
struct Input {
key: Vec<u8>,
nonce: Vec<u8>,
ad: Vec<u8>,
plaintext: Vec<u8>,
}

fuzz_target!(|input: Input| {
let mut aead = Protocol::new("lockstitch.fuzz.aead");
aead.mix(&input.key);
aead.mix(&input.nonce);
aead.mix(&input.ad);

let mut ciphertext = input.plaintext.clone();
ciphertext.extend_from_slice(&[0u8; TAG_LEN]);
aead.seal(&mut ciphertext);
});
9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use lockstitch::Protocol;

fuzz_target!(|data: &[u8]| {
let mut hash = Protocol::new("lockstitch.fuzz.hash");
hash.mix(data);
let _ = hash.derive_array::<32>();
});
11 changes: 11 additions & 0 deletions fuzz/fuzz_targets/new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_main]
use std::str;

use libfuzzer_sys::fuzz_target;
use lockstitch::Protocol;

fuzz_target!(|data: &[u8]| {
if let Ok(utf8) = str::from_utf8(data) {
let _ = Protocol::new(utf8);
}
});

0 comments on commit c4eec32

Please sign in to comment.