Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Use cfg(fuzzing) instead of a fuzztarget feature and don't allow 0-hashes. #111

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ default = [ "std" ]
std = []
serde-std = ["serde/std"]
unstable = [] # for benchmarking
fuzztarget = [] # used by other rust-bitcoin projects to make hashes almost-noops, DON'T USE THIS

[dependencies]
serde = { version = "1.0", default-features = false, optional = true }
Expand Down
8 changes: 4 additions & 4 deletions src/ripemd160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Default for HashEngine {
impl EngineTrait for HashEngine {
type MidState = [u8; 20];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn midstate(&self) -> [u8; 20] {
let mut ret = [0; 20];
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(4)) {
Expand All @@ -58,7 +58,7 @@ impl EngineTrait for HashEngine {
ret
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn midstate(&self) -> [u8; 20] {
let mut ret = [0; 20];
ret.copy_from_slice(&self.buffer[..20]);
Expand Down Expand Up @@ -97,7 +97,7 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 20];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn from_engine(mut e: HashEngine) -> Hash {
// pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining
let data_len = e.length as u64;
Expand All @@ -117,7 +117,7 @@ impl HashTrait for Hash {
Hash(e.midstate())
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn from_engine(e: HashEngine) -> Hash {
let mut res = e.midstate();
res[0] ^= (e.length & 0xff) as u8;
Expand Down
4 changes: 2 additions & 2 deletions src/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Default for HashEngine {
impl EngineTrait for HashEngine {
type MidState = [u8; 20];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn midstate(&self) -> [u8; 20] {
let mut ret = [0; 20];
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(4)) {
Expand All @@ -53,7 +53,7 @@ impl EngineTrait for HashEngine {
ret
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn midstate(&self) -> [u8; 20] {
let mut ret = [0; 20];
ret.copy_from_slice(&self.buffer[..20]);
Expand Down
16 changes: 11 additions & 5 deletions src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Default for HashEngine {
impl EngineTrait for HashEngine {
type MidState = Midstate;

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn midstate(&self) -> Midstate {
let mut ret = [0; 32];
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(4)) {
Expand All @@ -54,7 +54,7 @@ impl EngineTrait for HashEngine {
Midstate(ret)
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn midstate(&self) -> Midstate {
let mut ret = [0; 32];
ret.copy_from_slice(&self.buffer[..32]);
Expand Down Expand Up @@ -93,7 +93,7 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 32];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn from_engine(mut e: HashEngine) -> Hash {
// pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining
let data_len = e.length as u64;
Expand All @@ -113,9 +113,15 @@ impl HashTrait for Hash {
Hash(e.midstate().into_inner())
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn from_engine(e: HashEngine) -> Hash {
Hash(e.midstate().into_inner())
let mut hash = e.midstate().into_inner();
if hash == [0; 32] {
// Assume sha256 is secure and never generate 0-hashes (which represent invalid
// secp256k1 secret keys, causing downstream application breakage).
hash[0] = 1;
}
Hash(hash)
}

const LEN: usize = 32;
Expand Down
8 changes: 4 additions & 4 deletions src/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Default for HashEngine {
impl EngineTrait for HashEngine {
type MidState = [u8; 64];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn midstate(&self) -> [u8; 64] {
let mut ret = [0; 64];
for (val, ret_bytes) in self.h.iter().zip(ret.chunks_mut(8)) {
Expand All @@ -61,7 +61,7 @@ impl EngineTrait for HashEngine {
ret
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn midstate(&self) -> [u8; 64] {
let mut ret = [0; 64];
ret.copy_from_slice(&self.buffer[..64]);
Expand Down Expand Up @@ -141,7 +141,7 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 64];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn from_engine(mut e: HashEngine) -> Hash {
// pad buffer with a single 1-bit then all 0s, until there are exactly 16 bytes remaining
let data_len = e.length as u64;
Expand All @@ -162,7 +162,7 @@ impl HashTrait for Hash {
Hash(e.midstate())
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn from_engine(e: HashEngine) -> Hash {
let mut hash = e.midstate();
hash[0] ^= 0xff; // Make this distinct from SHA-256
Expand Down
4 changes: 2 additions & 2 deletions src/siphash24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 8];

#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn from_engine(e: HashEngine) -> Hash {
Hash::from_u64(Hash::from_engine_to_u64(e))
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn from_engine(e: HashEngine) -> Hash {
let state = e.midstate();
Hash::from_u64(state.v0 ^ state.v1 ^ state.v2 ^ state.v3)
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ macro_rules! borrow_slice_impl(

macro_rules! engine_input_impl(
() => (
#[cfg(not(feature = "fuzztarget"))]
#[cfg(not(fuzzing))]
fn input(&mut self, mut inp: &[u8]) {
while !inp.is_empty() {
let buf_idx = self.length % <Self as EngineTrait>::BLOCK_SIZE;
Expand All @@ -134,7 +134,7 @@ macro_rules! engine_input_impl(
}
}

#[cfg(feature = "fuzztarget")]
#[cfg(fuzzing)]
fn input(&mut self, inp: &[u8]) {
for c in inp {
self.buffer[0] ^= *c;
Expand Down