Skip to content

Commit

Permalink
test: add fuzz tests for keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Jan 15, 2025
1 parent 91040d5 commit 3075679
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions fuzz/fuzz_targets/keccak/memory_safety.rs
Original file line number Diff line number Diff line change
@@ -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();
});
15 changes: 15 additions & 0 deletions fuzz/fuzz_targets/keccak/no_panics.rs
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 3075679

Please sign in to comment.