From 3075679b2b9ed03cd02c31b3019d4482d716bfed Mon Sep 17 00:00:00 2001 From: 0xNeshi Date: Wed, 15 Jan 2025 12:14:36 +0100 Subject: [PATCH] test: add fuzz tests for keccak --- fuzz/Cargo.toml | 14 +++++++++++++ fuzz/fuzz_targets/keccak/memory_safety.rs | 24 +++++++++++++++++++++++ fuzz/fuzz_targets/keccak/no_panics.rs | 15 ++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 fuzz/fuzz_targets/keccak/memory_safety.rs create mode 100644 fuzz/fuzz_targets/keccak/no_panics.rs diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 897fda2f3..4335bc73e 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -31,6 +31,20 @@ test = false doc = false bench = false +[[bin]] +name = "keccak_doesnt_panic" +path = "fuzz_targets/keccak/no_panics.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "keccak_memory_safety" +path = "fuzz_targets/keccak/memory_safety.rs" +test = false +doc = false +bench = false + [[bin]] name = "openzeppelin_stylus_utils" path = "fuzz_targets/openzeppelin_stylus_utils.rs" diff --git a/fuzz/fuzz_targets/keccak/memory_safety.rs b/fuzz/fuzz_targets/keccak/memory_safety.rs new file mode 100644 index 000000000..0b72b8a2b --- /dev/null +++ b/fuzz/fuzz_targets/keccak/memory_safety.rs @@ -0,0 +1,24 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate openzeppelin_crypto; + +use crate::openzeppelin_crypto::{ + hash::{BuildHasher, Hasher}, + keccak::KeccakBuilder, +}; + +fuzz_target!(|data: &[u8]| { + let mut hasher = KeccakBuilder.build_hasher(); + + // Test memory safety by updating with different slice patterns + for i in 0..data.len() { + hasher.update(&data[..i]); + let mut new_hasher = KeccakBuilder.build_hasher(); + new_hasher.update(&data[i..]); + let _ = new_hasher.finalize(); + } + + // Finalize the original hasher + let _ = hasher.finalize(); +}); diff --git a/fuzz/fuzz_targets/keccak/no_panics.rs b/fuzz/fuzz_targets/keccak/no_panics.rs new file mode 100644 index 000000000..333a6b9fd --- /dev/null +++ b/fuzz/fuzz_targets/keccak/no_panics.rs @@ -0,0 +1,15 @@ +#![no_main] +#[macro_use] +extern crate libfuzzer_sys; +extern crate openzeppelin_crypto; + +use crate::openzeppelin_crypto::{ + hash::{BuildHasher, Hasher}, + keccak::KeccakBuilder, +}; + +fuzz_target!(|data: &[u8]| { + let mut hasher = KeccakBuilder.build_hasher(); + hasher.update(data); + _ = hasher.finalize(); +});