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

Commit d780cb0

Browse files
authored
Merge pull request #111 from TheBlueMatt/master
Use cfg(fuzzing) instead of a fuzztarget feature and don't allow 0-hashes.
2 parents 3b28fd4 + a786fd3 commit d780cb0

File tree

7 files changed

+25
-20
lines changed

7 files changed

+25
-20
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ default = [ "std" ]
1919
std = []
2020
serde-std = ["serde/std"]
2121
unstable = [] # for benchmarking
22-
fuzztarget = [] # used by other rust-bitcoin projects to make hashes almost-noops, DON'T USE THIS
2322

2423
[dependencies]
2524
serde = { version = "1.0", default-features = false, optional = true }

src/ripemd160.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Default for HashEngine {
4949
impl EngineTrait for HashEngine {
5050
type MidState = [u8; 20];
5151

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

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

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

120-
#[cfg(feature = "fuzztarget")]
120+
#[cfg(fuzzing)]
121121
fn from_engine(e: HashEngine) -> Hash {
122122
let mut res = e.midstate();
123123
res[0] ^= (e.length & 0xff) as u8;

src/sha1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Default for HashEngine {
4444
impl EngineTrait for HashEngine {
4545
type MidState = [u8; 20];
4646

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

56-
#[cfg(feature = "fuzztarget")]
56+
#[cfg(fuzzing)]
5757
fn midstate(&self) -> [u8; 20] {
5858
let mut ret = [0; 20];
5959
ret.copy_from_slice(&self.buffer[..20]);

src/sha256.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Default for HashEngine {
4545
impl EngineTrait for HashEngine {
4646
type MidState = Midstate;
4747

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

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

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

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

121127
const LEN: usize = 32;

src/sha512.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Default for HashEngine {
5252
impl EngineTrait for HashEngine {
5353
type MidState = [u8; 64];
5454

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

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

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

165-
#[cfg(feature = "fuzztarget")]
165+
#[cfg(fuzzing)]
166166
fn from_engine(e: HashEngine) -> Hash {
167167
let mut hash = e.midstate();
168168
hash[0] ^= 0xff; // Make this distinct from SHA-256

src/siphash24.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ impl HashTrait for Hash {
260260
type Engine = HashEngine;
261261
type Inner = [u8; 8];
262262

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

268-
#[cfg(feature = "fuzztarget")]
268+
#[cfg(fuzzing)]
269269
fn from_engine(e: HashEngine) -> Hash {
270270
let state = e.midstate();
271271
Hash::from_u64(state.v0 ^ state.v1 ^ state.v2 ^ state.v3)

src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ macro_rules! borrow_slice_impl(
117117

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

137-
#[cfg(feature = "fuzztarget")]
137+
#[cfg(fuzzing)]
138138
fn input(&mut self, inp: &[u8]) {
139139
for c in inp {
140140
self.buffer[0] ^= *c;

0 commit comments

Comments
 (0)