1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![cfg_attr(feature = "cargo-clippy", allow(identity_op, double_parens))]
#![no_std]
#[macro_use]
extern crate digest;
extern crate block_buffer;
extern crate byte_tools;
#[cfg(feature = "asm")]
extern crate whirlpool_asm as utils;
#[cfg(not(feature = "asm"))]
mod utils;
use utils::compress;
pub use digest::Digest;
#[cfg(not(feature = "asm"))]
use byte_tools::write_u64v_be;
use byte_tools::zero;
use block_buffer::{BlockBuffer512, ZeroPadding};
use digest::generic_array::GenericArray;
use digest::generic_array::typenum::U64;
#[cfg(not(feature = "asm"))]
mod consts;
type BlockSize = U64;
#[derive(Copy, Clone)]
pub struct Whirlpool {
bit_length: [u8; 32],
buffer: BlockBuffer512,
#[cfg(not(feature = "asm"))]
hash: [u64; 8],
#[cfg(feature = "asm")]
hash: [u8; 64],
}
impl Default for Whirlpool {
fn default() -> Self {
Self {
bit_length: [0u8; 32],
buffer: BlockBuffer512::default(),
#[cfg(not(feature = "asm"))]
hash: [0u64; 8],
#[cfg(feature = "asm")]
hash: [0u8; 64],
}
}
}
impl Whirlpool {
fn update_len(&mut self, len: u64) {
let len_bits = [
((len >> (56 + 5)) ) as u8,
((len >> (48 + 5)) & 0xff) as u8,
((len >> (40 + 5)) & 0xff) as u8,
((len >> (32 + 5)) & 0xff) as u8,
((len >> (24 + 5)) & 0xff) as u8,
((len >> (16 + 5)) & 0xff) as u8,
((len >> ( 8 + 5)) & 0xff) as u8,
((len >> ( 0 + 5)) & 0xff) as u8,
((len << 3) & 0xff) as u8,
];
let mut carry = false;
for i in 0..32 {
let mut x = u16::from(self.bit_length[self.bit_length.len() - i - 1]);
if i < len_bits.len() {
x += u16::from(len_bits[len_bits.len() - i - 1]);
} else if !carry {
break;
}
if carry {
x += 1;
}
carry = x > 0xff;
let pos = self.bit_length.len() -i - 1;
self.bit_length[pos] = (x & 0xff) as u8;
}
}
fn finalize(&mut self) {
let hash = &mut self.hash;
let pos = self.buffer.position();
let buf = self.buffer.pad_with::<ZeroPadding>();
buf[pos] = 0x80;
if pos + 1 > self.bit_length.len() {
compress(hash, buf);
zero(&mut buf[..pos+1]);
}
buf[32..].copy_from_slice(&self.bit_length);
compress(hash, buf);
}
}
impl digest::BlockInput for Whirlpool {
type BlockSize = BlockSize;
}
impl digest::Input for Whirlpool {
fn process(&mut self, input: &[u8]) {
self.update_len(input.len() as u64);
let hash = &mut self.hash;
self.buffer.input(input, |b| compress(hash, b));
}
}
impl digest::FixedOutput for Whirlpool {
type OutputSize = U64;
#[cfg(not(feature = "asm"))]
fn fixed_result(mut self) -> GenericArray<u8, Self::OutputSize> {
self.finalize();
let mut out = GenericArray::default();
write_u64v_be(&mut out, &self.hash[..]);
out
}
#[cfg(feature = "asm")]
fn fixed_result(mut self) -> GenericArray<u8, Self::OutputSize> {
self.finalize();
GenericArray::clone_from_slice(&self.hash)
}
}
impl_opaque_debug!(Whirlpool);