From 93a83f1bb240e93004555c6d9092e9cda7bd15e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 23 Oct 2024 13:57:27 +0300 Subject: [PATCH] Merge all fuzz targets into one --- fuzz/Cargo.toml | 18 ++-------- fuzz/fuzz_targets/scrypt.rs | 50 ++++++++++++++++++++++++++ fuzz/fuzz_targets/scrypt_hash.rs | 12 ------- fuzz/fuzz_targets/scrypt_phc_hash.rs | 22 ------------ fuzz/fuzz_targets/scrypt_phc_verify.rs | 13 ------- fuzz/src/lib.rs | 16 --------- 6 files changed, 52 insertions(+), 79 deletions(-) create mode 100644 fuzz/fuzz_targets/scrypt.rs delete mode 100644 fuzz/fuzz_targets/scrypt_hash.rs delete mode 100644 fuzz/fuzz_targets/scrypt_phc_hash.rs delete mode 100644 fuzz/fuzz_targets/scrypt_phc_verify.rs delete mode 100644 fuzz/src/lib.rs diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index c5b805db..fe54effb 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -12,22 +12,8 @@ libfuzzer-sys = "0.4" scrypt = { path = "../scrypt", features = ["simple"]} [[bin]] -name = "scrypt_hash" -path = "fuzz_targets/scrypt_hash.rs" -test = false -doc = false -bench = false - -[[bin]] -name = "scrypt_phc_verify" -path = "fuzz_targets/scrypt_phc_verify.rs" -test = false -doc = false -bench = false - -[[bin]] -name = "scrypt_phc_hash" -path = "fuzz_targets/scrypt_phc_hash.rs" +name = "scrypt" +path = "fuzz_targets/scrypt.rs" test = false doc = false bench = false diff --git a/fuzz/fuzz_targets/scrypt.rs b/fuzz/fuzz_targets/scrypt.rs new file mode 100644 index 00000000..48916dab --- /dev/null +++ b/fuzz/fuzz_targets/scrypt.rs @@ -0,0 +1,50 @@ +#![no_main] +use libfuzzer_sys::arbitrary::{Arbitrary, Result, Unstructured}; +use libfuzzer_sys::fuzz_target; +use scrypt::password_hash::{ + Ident, PasswordHash, PasswordHasher, PasswordVerifier, Salt, SaltString, +}; +use scrypt::{scrypt, Scrypt}; + +#[derive(Debug)] +pub struct ScryptRandParams(pub scrypt::Params); + +impl<'a> Arbitrary<'a> for ScryptRandParams { + fn arbitrary(u: &mut Unstructured<'a>) -> Result { + let log_n = u.int_in_range(0..=15)?; + let r = u.int_in_range(1..=32)?; + let p = u.int_in_range(1..=16)?; + let len = u.int_in_range(10..=64)?; + + let params = scrypt::Params::new(log_n, r, p, len).unwrap(); + Ok(Self(params)) + } +} + +fuzz_target!(|data: (&[u8], &[u8], ScryptRandParams)| { + let (password, salt, ScryptRandParams(params)) = data; + + // Check direct hashing + let mut result = [0u8; 64]; + scrypt(password, salt, ¶ms, &mut result).unwrap(); + + // Check PHC hashing + if salt.len() < Salt::MIN_LENGTH { + return; + } + let salt_string = SaltString::encode_b64(salt).unwrap(); + let phc_hash = Scrypt + .hash_password_customized( + password, + Some(Ident::new_unwrap("scrypt")), + None, + params, + &salt_string, + ) + .unwrap() + .to_string(); + + // Check PHC verification + let hash = PasswordHash::new(&phc_hash).unwrap(); + Scrypt.verify_password(password, &hash).unwrap(); +}); diff --git a/fuzz/fuzz_targets/scrypt_hash.rs b/fuzz/fuzz_targets/scrypt_hash.rs deleted file mode 100644 index 03ec1cea..00000000 --- a/fuzz/fuzz_targets/scrypt_hash.rs +++ /dev/null @@ -1,12 +0,0 @@ -#![no_main] -use libfuzzer_sys::fuzz_target; -use scrypt::scrypt; - -use fuzz::ScryptRandParams; - -fuzz_target!(|data: (&[u8], &[u8], ScryptRandParams)| { - let (password, salt, ScryptRandParams(params)) = data; - let mut result = [0u8; 64]; - let res = scrypt(password, salt, ¶ms, &mut result); - assert!(res.is_ok()); -}); diff --git a/fuzz/fuzz_targets/scrypt_phc_hash.rs b/fuzz/fuzz_targets/scrypt_phc_hash.rs deleted file mode 100644 index 39c30408..00000000 --- a/fuzz/fuzz_targets/scrypt_phc_hash.rs +++ /dev/null @@ -1,22 +0,0 @@ -#![no_main] -use libfuzzer_sys::fuzz_target; -use scrypt::password_hash::{Ident, PasswordHasher, Salt, SaltString}; -use scrypt::Scrypt; - -use fuzz::ScryptRandParams; - -fuzz_target!(|data: (&[u8], &[u8], ScryptRandParams)| { - let (password, salt, ScryptRandParams(params)) = data; - if salt.len() < Salt::MIN_LENGTH { - return; - } - let salt_string = SaltString::encode_b64(salt).unwrap(); - let res = Scrypt.hash_password_customized( - password, - Some(Ident::new_unwrap("scrypt")), - None, - params, - &salt_string, - ); - assert!(res.is_ok()); -}); diff --git a/fuzz/fuzz_targets/scrypt_phc_verify.rs b/fuzz/fuzz_targets/scrypt_phc_verify.rs deleted file mode 100644 index 4052d9f6..00000000 --- a/fuzz/fuzz_targets/scrypt_phc_verify.rs +++ /dev/null @@ -1,13 +0,0 @@ -#![no_main] -use libfuzzer_sys::fuzz_target; -use scrypt::password_hash::{PasswordHash, PasswordVerifier}; -use scrypt::Scrypt; - -const SAMPLE_HASH: &str = "$scrypt$ln=16,r=8,p=1$\ - aM15713r3Xsvxbi31lqr1Q$nFNh2CVHVjNldFVKDHDlm4CbdRSCdEBsjjJxD+iCs5E"; - -fuzz_target!(|password: &[u8]| { - let hash = PasswordHash::new(SAMPLE_HASH).expect("SAMPLE_HASH is valid"); - let res = Scrypt.verify_password(password, &hash); - assert!(res.is_err()); -}); diff --git a/fuzz/src/lib.rs b/fuzz/src/lib.rs deleted file mode 100644 index 1ba2c8a4..00000000 --- a/fuzz/src/lib.rs +++ /dev/null @@ -1,16 +0,0 @@ -use libfuzzer_sys::arbitrary::{Arbitrary, Result, Unstructured}; - -#[derive(Debug)] -pub struct ScryptRandParams(pub scrypt::Params); - -impl<'a> Arbitrary<'a> for ScryptRandParams { - fn arbitrary(u: &mut Unstructured<'a>) -> Result { - let log_n = u.int_in_range(0..=15)?; - let r = u.int_in_range(1..=32)?; - let p = u.int_in_range(1..=16)?; - let len = u.int_in_range(10..=64)?; - - let params = scrypt::Params::new(log_n, r, p, len).unwrap(); - Ok(Self(params)) - } -}