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

Commit

Permalink
Merge pull request #57 from dongcarl/2019-07-clippy-cleanup
Browse files Browse the repository at this point in the history
Repository cleanup w/ help from Clippy
  • Loading branch information
apoelstra committed Jul 22, 2019
2 parents 1ce1aac + 835e37a commit 793c506
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/hex.rs
Expand Up @@ -127,7 +127,7 @@ pub fn format_hex(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
for _ in (2 * data.len())..width {
f.write_str("0")?;
}
for ch in data.into_iter().take(prec / 2) {
for ch in data.iter().take(prec / 2) {
write!(f, "{:02x}", *ch)?;
}
if prec < 2 * data.len() && prec % 2 == 1 {
Expand Down
64 changes: 34 additions & 30 deletions src/hmac.rs
Expand Up @@ -23,13 +23,15 @@ use core::{borrow, fmt, ops, str};
#[cfg(feature="serde")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};

use {Error, Hash, HashEngine};
use HashEngine as EngineTrait;
use Hash as HashTrait;
use Error;

/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
pub struct Hmac<T: Hash>(T);
pub struct Hmac<T: HashTrait>(T);

impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {
impl<T: HashTrait + str::FromStr> str::FromStr for Hmac<T> {
type Err = <T as str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Hmac(str::FromStr::from_str(s)?))
Expand All @@ -38,21 +40,27 @@ impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {

/// Pair of underlying hash midstates which represent the current state
/// of an `HmacEngine`
pub struct HmacMidState<T: Hash> {
pub struct HmacMidState<T: HashTrait> {
/// Midstate of the inner hash engine
pub inner: <T::Engine as HashEngine>::MidState,
pub inner: <T::Engine as EngineTrait>::MidState,
/// Midstate of the outer hash engine
pub outer: <T::Engine as HashEngine>::MidState,
pub outer: <T::Engine as EngineTrait>::MidState,
}

/// Pair of underyling hash engines, used for the inner and outer hash of HMAC
#[derive(Clone)]
pub struct HmacEngine<T: Hash> {
pub struct HmacEngine<T: HashTrait> {
iengine: T::Engine,
oengine: T::Engine,
}

impl<T: Hash> HmacEngine<T> {
impl<T: HashTrait> Default for HmacEngine<T> {
fn default() -> Self {
HmacEngine::new(&[])
}
}

impl<T: HashTrait> HmacEngine<T> {
/// Construct a new keyed HMAC with the given key. We only support underlying hashes
/// whose block sizes are ≤ 128 bytes; larger hashes will result in panics.
pub fn new(key: &[u8]) -> HmacEngine<T> {
Expand All @@ -61,12 +69,12 @@ impl<T: Hash> HmacEngine<T> {
let mut ipad = [0x36u8; 128];
let mut opad = [0x5cu8; 128];
let mut ret = HmacEngine {
iengine: <T as Hash>::engine(),
oengine: <T as Hash>::engine(),
iengine: <T as HashTrait>::engine(),
oengine: <T as HashTrait>::engine(),
};

if key.len() > T::Engine::BLOCK_SIZE {
let hash = <T as Hash>::hash(key);
let hash = <T as HashTrait>::hash(key);
for (b_i, b_h) in ipad.iter_mut().zip(&hash[..]) {
*b_i ^= *b_h;
}
Expand All @@ -82,13 +90,13 @@ impl<T: Hash> HmacEngine<T> {
}
};

HashEngine::input(&mut ret.iengine, &ipad[..T::Engine::BLOCK_SIZE]);
HashEngine::input(&mut ret.oengine, &opad[..T::Engine::BLOCK_SIZE]);
EngineTrait::input(&mut ret.iengine, &ipad[..T::Engine::BLOCK_SIZE]);
EngineTrait::input(&mut ret.oengine, &opad[..T::Engine::BLOCK_SIZE]);
ret
}
}

impl<T: Hash> HashEngine for HmacEngine<T> {
impl<T: HashTrait> EngineTrait for HmacEngine<T> {
type MidState = HmacMidState<T>;

fn midstate(&self) -> Self::MidState {
Expand All @@ -105,73 +113,69 @@ impl<T: Hash> HashEngine for HmacEngine<T> {
}
}

impl<T: Hash> fmt::Debug for Hmac<T> {
impl<T: HashTrait> fmt::Debug for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}

impl<T: Hash> fmt::Display for Hmac<T> {
impl<T: HashTrait> fmt::Display for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

impl<T: Hash> fmt::LowerHex for Hmac<T> {
impl<T: HashTrait> fmt::LowerHex for Hmac<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}

impl<T: Hash> ops::Index<usize> for Hmac<T> {
impl<T: HashTrait> ops::Index<usize> for Hmac<T> {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
&self.0[index]
}
}

impl<T: Hash> ops::Index<ops::Range<usize>> for Hmac<T> {
impl<T: HashTrait> ops::Index<ops::Range<usize>> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::Range<usize>) -> &[u8] {
&self.0[index]
}
}

impl<T: Hash> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
impl<T: HashTrait> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
&self.0[index]
}
}

impl<T: Hash> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
impl<T: HashTrait> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeTo<usize>) -> &[u8] {
&self.0[index]
}
}

impl<T: Hash> ops::Index<ops::RangeFull> for Hmac<T> {
impl<T: HashTrait> ops::Index<ops::RangeFull> for Hmac<T> {
type Output = [u8];
fn index(&self, index: ops::RangeFull) -> &[u8] {
&self.0[index]
}
}

impl<T: Hash> borrow::Borrow<[u8]> for Hmac<T> {
impl<T: HashTrait> borrow::Borrow<[u8]> for Hmac<T> {
fn borrow(&self) -> &[u8] {
&self[..]
}
}

impl<T: Hash> Hash for Hmac<T> {
impl<T: HashTrait> HashTrait for Hmac<T> {
type Engine = HmacEngine<T>;
type Inner = T::Inner;

fn engine() -> HmacEngine<T> {
HmacEngine::new(&[])
}

fn from_engine(mut e: HmacEngine<T>) -> Hmac<T> {
let ihash = T::from_engine(e.iengine);
e.oengine.input(&ihash[..]);
Expand All @@ -195,14 +199,14 @@ impl<T: Hash> Hash for Hmac<T> {
}

#[cfg(feature="serde")]
impl<T: Hash + Serialize> Serialize for Hmac<T> {
impl<T: HashTrait + Serialize> Serialize for Hmac<T> {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
Serialize::serialize(&self.0, s)
}
}

#[cfg(feature="serde")]
impl<'de, T: Hash + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
impl<'de, T: HashTrait + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hmac<T>, D::Error> {
let inner = Deserialize::deserialize(d)?;
Ok(Hmac(inner))
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Expand Up @@ -59,7 +59,7 @@ pub use error::Error;
/// A hashing engine which bytes can be serialized into. It is expected
/// to implement the `io::Write` trait, but to never return errors under
/// any conditions.
pub trait HashEngine: Clone {
pub trait HashEngine: Clone + Default {
/// Byte array representing the internal state of the hash engine
type MidState;

Expand Down Expand Up @@ -93,7 +93,9 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
type Inner: hex::FromHex;

/// Construct a new engine
fn engine() -> Self::Engine;
fn engine() -> Self::Engine {
Self::Engine::default()
}

/// Produce a hash from the current state of a given engine
fn from_engine(e: Self::Engine) -> Self;
Expand Down
25 changes: 9 additions & 16 deletions src/ripemd160.rs
Expand Up @@ -29,18 +29,19 @@ use Error;
const BLOCK_SIZE: usize = 64;

/// Engine to compute RIPEMD160 hash function
#[derive(Clone)]
pub struct HashEngine {
buffer: [u8; BLOCK_SIZE],
h: [u32; 5],
length: usize,
}

impl Clone for HashEngine {
fn clone(&self) -> HashEngine {
impl Default for HashEngine {
fn default() -> Self {
HashEngine {
h: self.h,
length: self.length,
buffer: self.buffer,
h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0],
length: 0,
buffer: [0; BLOCK_SIZE],
}
}
}
Expand Down Expand Up @@ -89,14 +90,6 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 20];

fn engine() -> HashEngine {
HashEngine {
h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0],
length: 0,
buffer: [0; BLOCK_SIZE],
}
}

#[cfg(not(feature = "fuzztarget"))]
fn from_engine(mut e: HashEngine) -> Hash {
// pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining
Expand Down Expand Up @@ -179,8 +172,8 @@ macro_rules! process_block(
$( par_round5: h_ordering $pf0:expr, $pf1:expr, $pf2:expr, $pf3:expr, $pf4:expr;
data_index $pdata_index5:expr; roll_shift $pbits5:expr; )*
) => ({
let mut bb = *$h;
let mut bbb = *$h;
let mut bb = $h;
let mut bbb = $h;

// Round 1
$( round!(bb[$f0], bb[$f1], bb[$f2], bb[$f3], bb[$f4],
Expand Down Expand Up @@ -253,7 +246,7 @@ impl HashEngine {

let mut w = [0u32; 16];
LittleEndian::read_u32_into(&self.buffer, &mut w);
process_block!(&mut self.h, &mut w,
process_block!(self.h, w,
// Round 1
round1: h_ordering 0, 1, 2, 3, 4; data_index 0; roll_shift 11;
round1: h_ordering 4, 0, 1, 2, 3; data_index 1; roll_shift 14;
Expand Down
19 changes: 6 additions & 13 deletions src/sha1.rs
Expand Up @@ -24,18 +24,19 @@ use Error;
const BLOCK_SIZE: usize = 64;

/// Engine to compute SHA1 hash function
#[derive(Clone)]
pub struct HashEngine {
buffer: [u8; BLOCK_SIZE],
h: [u32; 5],
length: usize,
}

impl Clone for HashEngine {
fn clone(&self) -> HashEngine {
impl Default for HashEngine {
fn default() -> Self {
HashEngine {
h: self.h,
length: self.length,
buffer: self.buffer,
h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0],
length: 0,
buffer: [0; BLOCK_SIZE],
}
}
}
Expand Down Expand Up @@ -84,14 +85,6 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 20];

fn engine() -> HashEngine {
HashEngine {
h: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0],
length: 0,
buffer: [0; BLOCK_SIZE],
}
}

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 Down
19 changes: 6 additions & 13 deletions src/sha256.rs
Expand Up @@ -25,18 +25,19 @@ use Error;
const BLOCK_SIZE: usize = 64;

/// Engine to compute SHA256 hash function
#[derive(Clone)]
pub struct HashEngine {
buffer: [u8; BLOCK_SIZE],
h: [u32; 8],
length: usize,
}

impl Clone for HashEngine {
fn clone(&self) -> HashEngine {
impl Default for HashEngine {
fn default() -> Self {
HashEngine {
h: self.h,
length: self.length,
buffer: self.buffer,
h: [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19],
length: 0,
buffer: [0; BLOCK_SIZE],
}
}
}
Expand Down Expand Up @@ -85,14 +86,6 @@ impl HashTrait for Hash {
type Engine = HashEngine;
type Inner = [u8; 32];

fn engine() -> HashEngine {
HashEngine {
h: [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19],
length: 0,
buffer: [0; BLOCK_SIZE],
}
}

#[cfg(not(feature = "fuzztarget"))]
fn from_engine(mut e: HashEngine) -> Hash {
// pad buffer with a single 1-bit then all 0s, until there are exactly 8 bytes remaining
Expand Down

0 comments on commit 793c506

Please sign in to comment.