Switch fuzztarget SHA256 to simply XOR'ing all input bytes

This commit is contained in:
Matt Corallo 2018-06-04 10:23:26 -04:00
parent 881972b2a5
commit dab2f0b6b6
1 changed files with 14 additions and 15 deletions

View File

@ -1,64 +1,63 @@
//! fuzztarget-only Sha2 context with a dummy Sha256 and Sha512 hashers. //! fuzztarget-only Sha2 context with a dummy Sha256 and Sha512 hashers.
use crypto::digest::Digest; use crypto::digest::Digest;
use crypto::sha2;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
/// Dummy Sha256 that hashes the input, but only returns the first byte of output, masking the /// Dummy Sha256 that hashes the input, but only returns the first byte of output, masking the
/// rest to 0s. /// rest to 0s.
pub struct Sha256 { pub struct Sha256 {
state: sha2::Sha256, state: u8,
} }
impl Sha256 { impl Sha256 {
/// Constructs a new dummy Sha256 context /// Constructs a new dummy Sha256 context
pub fn new() -> Sha256 { pub fn new() -> Sha256 {
Sha256 { Sha256 {
state: sha2::Sha256::new(), state: 0,
} }
} }
} }
impl Digest for Sha256 { impl Digest for Sha256 {
fn result(&mut self, data: &mut [u8]) { fn result(&mut self, data: &mut [u8]) {
self.state.result(data); data[0] = self.state;
for i in 1..32 { for i in 1..32 {
data[i] = 0; data[i] = 0;
} }
} }
fn input(&mut self, data: &[u8]) { self.state.input(data); } fn input(&mut self, data: &[u8]) { for i in data { self.state ^= i; } }
fn reset(&mut self) { self.state.reset(); } fn reset(&mut self) { self.state = 0; }
fn output_bits(&self) -> usize { self.state.output_bits() } fn output_bits(&self) -> usize { 256 }
fn block_size(&self) -> usize { self.state.block_size() } fn block_size(&self) -> usize { 64 }
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
/// Dummy Sha512 that hashes the input, but only returns the first byte of output, masking the /// Dummy Sha512 that hashes the input, but only returns the first byte of output, masking the
/// rest to 0s. /// rest to 0s.
pub struct Sha512 { pub struct Sha512 {
state: sha2::Sha512, state: u8,
} }
impl Sha512 { impl Sha512 {
/// Constructs a new dummy Sha512 context /// Constructs a new dummy Sha512 context
pub fn new() -> Sha512 { pub fn new() -> Sha512 {
Sha512 { Sha512 {
state: sha2::Sha512::new(), state: 0xff,
} }
} }
} }
impl Digest for Sha512 { impl Digest for Sha512 {
fn result(&mut self, data: &mut [u8]) { fn result(&mut self, data: &mut [u8]) {
self.state.result(data); data[0] = self.state;
for i in 1..64 { for i in 1..64 {
data[i] = 0; data[i] = 0;
} }
} }
fn input(&mut self, data: &[u8]) { self.state.input(data); } fn input(&mut self, data: &[u8]) { for i in data { self.state ^= i; } }
fn reset(&mut self) { self.state.reset(); } fn reset(&mut self) { self.state = 0xff; }
fn output_bits(&self) -> usize { self.state.output_bits() } fn output_bits(&self) -> usize { 512 }
fn block_size(&self) -> usize { self.state.block_size() } fn block_size(&self) -> usize { 128 }
} }