Skip to content

Commit

Permalink
Merge branch 'master' into sha-512-224-and-256
Browse files Browse the repository at this point in the history
  • Loading branch information
silmeth committed Nov 15, 2015
2 parents e87d691 + bdd7d33 commit 588e1f8
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 58 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ version = "1.4"

[dependencies.clippy]
optional = true
version = "0.0.22"
version = "0.0"

[dependencies.num]
optional = true
Expand All @@ -32,6 +32,7 @@ openssl = "0.6"
quickcheck = "0.2"

[features]
no-std = []
bcrypt = ["blowfish"]
block = ["blowfish"]
blowfish = []
Expand Down
2 changes: 2 additions & 0 deletions src/digest/md4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use utils::buffer::{FixedBuffer64, FixedBuffer, StandardPadding};

use byteorder::{ByteOrder, LittleEndian};

#[derive(Copy, Clone, Debug)]
struct State {
s0: u32,
s1: u32,
Expand Down Expand Up @@ -109,6 +110,7 @@ impl State {
}
}

#[derive(Clone)]
pub struct Md4 {
state: State,
length: u64,
Expand Down
3 changes: 2 additions & 1 deletion src/digest/md5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use byteorder::{ByteOrder, LittleEndian};
use digest::Digest;
use utils::buffer::{FixedBuffer64, FixedBuffer, StandardPadding};

#[derive(Debug)]
#[derive(Copy, Clone, Debug)]
struct State {
s0: u32,
s1: u32,
Expand Down Expand Up @@ -155,6 +155,7 @@ static C4: [u32; 16] = [0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59
0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391];

#[derive(Clone)]
pub struct Md5 {
state: State,
length: u64,
Expand Down
2 changes: 2 additions & 0 deletions src/digest/ripemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const RIGHT_ROTATE: [u32; 80] = [8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14
const LEFT_CONST: [u32; 5] = [0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E];
const RIGHT_CONST: [u32; 5] = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000];

#[derive(Copy, Clone, Debug)]
struct State {
state: [u32; 5],
}
Expand Down Expand Up @@ -105,6 +106,7 @@ impl State {
}
}

#[derive(Clone)]
pub struct Ripemd160 {
state: State,
length: u64,
Expand Down
2 changes: 2 additions & 0 deletions src/digest/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use byteorder::{ByteOrder, BigEndian};
use digest::Digest;
use utils::buffer::{FixedBuffer, FixedBuffer64, StandardPadding};

#[derive(Copy, Clone, Debug)]
struct State {
state: [u32; 5],
}
Expand Down Expand Up @@ -68,6 +69,7 @@ impl State {
}
}

#[derive(Clone)]
pub struct Sha1 {
state: State,
buffer: FixedBuffer64,
Expand Down
16 changes: 13 additions & 3 deletions src/digest/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ const U64_ROUNDS: [u64; 80] = [0x428a2f98d728ae22,
0x5fcb6fab3ad6faec,
0x6c44198c4a475817];

struct State<T> {
#[derive(Copy, Clone, Debug)]
struct State<T: Copy> {
state: [T; 8],
}

Expand Down Expand Up @@ -258,6 +259,7 @@ impl State<u64> {

macro_rules! impl_sha(
($name:ident, $buffer:ty, $init:ident, $write:ident, $state:ty, $chunk:expr, $bsize:expr, $bits:expr) => {
#[derive(Clone)]
pub struct $name {
state: State<$state>,
buffer: $buffer,
Expand Down Expand Up @@ -296,9 +298,17 @@ macro_rules! impl_sha(
BigEndian::write_u64(self.buffer.next(8), self.length << 3);
state.process_block(self.buffer.full_buffer());

for (c, &v) in out[..Self::output_bytes()].chunks_mut($chunk).zip(state.state.iter()) {
BigEndian::$write(c, v);
for i in &mut state.state {
*i = i.to_be();
}

unsafe {
use std::ptr;
ptr::copy_nonoverlapping(
state.state.as_ptr() as *const u8,
out.as_mut_ptr(),
Self::output_bytes())
};
}
}
};
Expand Down
28 changes: 20 additions & 8 deletions src/digest/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ use byteorder::{ByteOrder, LittleEndian};

use std::io::Read;

#[derive(Copy)]
struct State {
hash: [u64; 25],
message: [u8; 144],
rest: usize,
block_size: usize,
}

impl Clone for State {
fn clone(&self) -> Self {
State {
hash: self.hash,
message: self.message,
rest: self.rest,
block_size: self.block_size,
}
}
}

const ROUND_CONSTS: [u64; 24] = [0x0000000000000001,
0x0000000000008082,
0x800000000000808a,
Expand Down Expand Up @@ -170,6 +182,7 @@ impl State {

macro_rules! sha3_impl {
($name:ident -> $size:expr) => {
#[derive(Clone)]
pub struct $name {
state: State
}
Expand All @@ -194,14 +207,13 @@ macro_rules! sha3_impl {

self.state.finish();

let mut tmp = [0u8; 200];
for (&v, c) in self.state.hash.iter().zip(tmp.chunks_mut(8)) {
LittleEndian::write_u64(c, v);
}

for i in 0..Self::output_bytes() {
ret[i] = tmp[i];
}
unsafe {
use std::ptr;
ptr::copy_nonoverlapping(
self.state.hash.as_ptr() as *const u8,
ret.as_mut_ptr(),
Self::output_bytes())
};
}
}
}
Expand Down
96 changes: 52 additions & 44 deletions src/digest/tiger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,57 +109,65 @@ impl State {
}
}

pub struct Tiger {
state: State,
buffer: FixedBuffer64,
length: u64,
}

impl Default for Tiger {
fn default() -> Self {
Tiger {
state: State::new(),
buffer: FixedBuffer64::new(),
length: 0,
macro_rules! tiger_impl {
($name:ident, $padding:expr) => {
#[derive(Clone)]
pub struct $name {
state: State,
buffer: FixedBuffer64,
length: u64,
}
}
}

impl digest::Digest for Tiger {
fn update<T>(&mut self, update: T)
where T: AsRef<[u8]>
{
let update = update.as_ref();
self.length += update.len() as u64;

let state = &mut self.state;
self.buffer.input(update, |d| state.compress(d));
}
impl Default for $name {
fn default() -> Self {
$name {
state: State::new(),
buffer: FixedBuffer64::new(),
length: 0,
}
}
}

fn output_bits() -> usize {
192
}
fn block_size() -> usize {
64
}
impl digest::Digest for $name {
fn update<T>(&mut self, update: T)
where T: AsRef<[u8]>
{
let update = update.as_ref();
self.length += update.len() as u64;

fn result<T>(mut self, mut out: T)
where T: AsMut<[u8]>
{
let state = &mut self.state;
let state = &mut self.state;
self.buffer.input(update, |d| state.compress(d));
}

self.buffer.pad(0x01, 8, |d| state.compress(d));
BigEndian::write_u64(self.buffer.next(8), self.length << 3);
state.compress(self.buffer.full_buffer());
fn output_bits() -> usize {
192
}
fn block_size() -> usize {
64
}

let mut out = out.as_mut();
assert!(out.len() >= Self::output_bytes());
BigEndian::write_u64(&mut out[0..8], state.a.0);
BigEndian::write_u64(&mut out[8..16], state.b.0);
BigEndian::write_u64(&mut out[16..24], state.c.0);
}
fn result<T>(mut self, mut out: T)
where T: AsMut<[u8]>
{
let state = &mut self.state;

self.buffer.pad($padding, 8, |d| state.compress(d));
BigEndian::write_u64(self.buffer.next(8), self.length << 3);
state.compress(self.buffer.full_buffer());

let mut out = out.as_mut();
assert!(out.len() >= Self::output_bytes());
BigEndian::write_u64(&mut out[0..8], state.a.0);
BigEndian::write_u64(&mut out[8..16], state.b.0);
BigEndian::write_u64(&mut out[16..24], state.c.0);
}
}
};
}

tiger_impl!(Tiger, 0x01);
tiger_impl!(Tiger2, 0x80);

#[cfg(test)]
mod tests {
use digest::test::Test;
Expand Down Expand Up @@ -198,6 +206,6 @@ mod tests {

assert_eq!(&result[..],
&[0xcd, 0x7e, 0xb9, 0x64, 0x5f, 0xb4, 0x05, 0xc6, 0x48, 0x5d, 0xd1, 0xaa, 0x14,
0x59, 0x6a, 0x63, 0xe5, 0x70, 0x4c, 0xc2, 0xff, 0x28, 0xf2, 0x4a])
0x59, 0x6a, 0x63, 0xe5, 0x70, 0x4c, 0xc2, 0xff, 0x28, 0xf2, 0x4a])
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#![cfg_attr(not(test), deny(trivial_casts))]
#![warn(missing_docs)]

// Support Redox (http://www.redox-os.org/). This is temporary fix until `redox` crate will be
// renamed as `std`.
#![cfg_attr(feature = "no-std", no_std)]
#[cfg(target_os = "redox")] extern crate redox as std;

#[cfg(test)] extern crate quickcheck;
#[cfg(test)] extern crate openssl;

Expand Down
11 changes: 10 additions & 1 deletion src/utils/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ macro_rules! impl_fixed_buffer( ($name:ident, $size:expr) => (
pub fn new() -> Self {
$name {
buffer: [0u8; $size],
position: 0
position: 0,
}
}
}

impl Clone for $name {
fn clone(&self) -> Self {
$name {
buffer: self.buffer,
position: self.position,
}
}
}
Expand Down

0 comments on commit 588e1f8

Please sign in to comment.