Skip to content

Commit

Permalink
Tweak scrypt_fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored Oct 23, 2024
1 parent 6ff6fc4 commit 930fb04
Showing 1 changed file with 33 additions and 49 deletions.
82 changes: 33 additions & 49 deletions fuzz/fuzz_targets/scrypt_fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use libfuzzer_sys::fuzz_target;
use scrypt::{scrypt, Params, Scrypt};
use scrypt::password_hash::{Ident, PasswordHash, PasswordHasher, PasswordVerifier, SaltString};

const SAMPLE_HASH: &str =
"$scrypt$ln=16,r=8,p=1$aM15713r3Xsvxbi31lqr1Q$nFNh2CVHVjNldFVKDHDlm4CbdRSCdEBsjjJxD+iCs5E";
const SAMPLE_HASH: &str = "$scrypt$ln=16,r=8,p=1$\
aM15713r3Xsvxbi31lqr1Q$nFNh2CVHVjNldFVKDHDlm4CbdRSCdEBsjjJxD+iCs5E";

// Generate random params
fn fuzzed_params(data: &[u8]) -> Option<Params> {
Expand Down Expand Up @@ -89,53 +89,37 @@ fuzz_target!(|data: &[u8]| {
let splits = split_fuzz_data(data, &[32, 32, 32]);
let password = splits.first().unwrap_or(&data);
let salt = splits.get(1).unwrap_or(&data);
let mut result = vec![0u8; 256];

if let Some(salt_string) = fuzzed_salt(salt) {
if !validate_salt(salt_string.as_str()) {
return;
}

let salt_value = salt_string.as_salt(); // Safe to use now

let formatted_hash = format!("$scrypt$ln=16,r=8,p=1${}$invalid$", base16ct::lower::encode_string(password));

if let Ok(hash) =
PasswordHash::new(SAMPLE_HASH).or_else(|_| PasswordHash::new(formatted_hash.as_str()))
{
// Randomly choose the fuzz target function
let target_selector = if !data.is_empty() { data[0] % 5 } else { 0 };
match target_selector {
0 => {
let _ = scrypt(password, salt, &params, &mut result);
}
1 => {
let _ = Scrypt.verify_password(password, &hash).is_err();
}
2 => {
let _ = Scrypt.hash_password_customized(
password,
Some(Ident::new_unwrap("scrypt")),
None,
params,
salt_value,
);
}
3 => {
if let Some(random_params) = fuzzed_params(password) {
let _ = scrypt(password, salt, &random_params, &mut result);
}
}
4 => {
let _ = PasswordHash::new(SAMPLE_HASH).is_ok();
}
_ => {
let _ = scrypt(password, salt, &params, &mut result);
}
let mut result = [0u8; 256];

let salt_value = match fuzzed_salt(salt) {
Some(s) if validate_salt(s) => s.as_salt(),
_ => return,
};

let hash = PasswordHash::new(SAMPLE_HASH).expect("SAMPLE_HASH is valid");

// Randomly choose the fuzz target function
let target_selector = if !data.is_empty() { data[0] % 4 } else { 0 };
let res = match target_selector {
0 => scrypt(password, salt, &params, &mut result).is_ok(),
1 => Scrypt.verify_password(password, &hash).is_err(),
2 => Scrypt
.hash_password_customized(
password,
Some(Ident::new_unwrap("scrypt")),
None,
params,
salt_value,
)
.is_ok(),
3 => {
if let Some(random_params) = fuzzed_params(password) {
scrypt(password, salt, &random_params, &mut result).is_ok()
} else {
true
}
}
} else {
// Skip this iteration if the salt is invalid
return;
}
_ => unreachable!(),
};
assert!(res);
});

0 comments on commit 930fb04

Please sign in to comment.