Skip to content

Commit

Permalink
More fuzzer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallpierce committed Nov 30, 2022
1 parent bcbc669 commit 250323c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ cargo-fuzz = true
[dependencies]
rand = "0.6.1"
rand_pcg = "0.1.1"
ring = "0.13.5"
sha2 = "0.10.6"

[dependencies.base64]
path = ".."
[dependencies.libfuzzer-sys]
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzzers/roundtrip_no_pad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
extern crate libfuzzer_sys;
extern crate base64;

use base64::engine::fast_portable;
use base64::engine::{self, fast_portable};

fuzz_target!(|data: &[u8]| {
let config = fast_portable::FastPortableConfig::new()
.with_encode_padding(false)
.with_decode_padding_mode(fast_portable::DecodePaddingMode::RequireNone);
.with_decode_padding_mode(engine::DecodePaddingMode::RequireNone);
let engine = fast_portable::FastPortable::from(&base64::alphabet::STANDARD, config);

let encoded = base64::encode_engine(&data, &engine);
Expand Down
16 changes: 9 additions & 7 deletions fuzz/fuzzers/utils.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
extern crate rand;
extern crate rand_pcg;
extern crate ring;
extern crate sha2;

use base64::{alphabet, engine::fast_portable};
use base64::{alphabet, engine::{self, fast_portable}};
use self::rand::{Rng, SeedableRng};
use self::rand_pcg::Pcg32;
use self::ring::digest;
use self::sha2::Digest as _;

pub fn random_engine(data: &[u8]) -> fast_portable::FastPortable {
// use sha256 of data as rng seed so it's repeatable
let sha = digest::digest(&digest::SHA256, data);
let mut hasher = sha2::Sha256::new();
hasher.update(data);
let sha = hasher.finalize();

let mut seed: [u8; 16] = [0; 16];
seed.copy_from_slice(&sha.as_ref()[0..16]);
seed.copy_from_slice(&sha.as_slice()[0..16]);

let mut rng = Pcg32::from_seed(seed);

Expand All @@ -24,9 +26,9 @@ pub fn random_engine(data: &[u8]) -> fast_portable::FastPortable {

let encode_padding = rng.gen();
let decode_padding = if encode_padding {
fast_portable::DecodePaddingMode::RequireCanonical
engine::DecodePaddingMode::RequireCanonical
} else {
fast_portable::DecodePaddingMode::RequireNone
engine::DecodePaddingMode::RequireNone
};
let config = fast_portable::FastPortableConfig::new()
.with_encode_padding(encode_padding)
Expand Down

0 comments on commit 250323c

Please sign in to comment.