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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use core::cmp::min;
use core::convert::TryInto;
use crate::{BLOCK_LEN, CHUNK_LEN, KEY_LEN, OUT_LEN};
const CHUNK_START: u32 = 1 << 0;
const CHUNK_END: u32 = 1 << 1;
const PARENT: u32 = 1 << 2;
const ROOT: u32 = 1 << 3;
const KEYED_HASH: u32 = 1 << 4;
const DERIVE_KEY_CONTEXT: u32 = 1 << 5;
const DERIVE_KEY_MATERIAL: u32 = 1 << 6;
const IV: [u32; 8] = [
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
];
const MSG_PERMUTATION: [usize; 16] = [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8];
fn g(state: &mut [u32; 16], a: usize, b: usize, c: usize, d: usize, mx: u32, my: u32) {
state[a] = state[a].wrapping_add(state[b]).wrapping_add(mx);
state[d] = (state[d] ^ state[a]).rotate_right(16);
state[c] = state[c].wrapping_add(state[d]);
state[b] = (state[b] ^ state[c]).rotate_right(12);
state[a] = state[a].wrapping_add(state[b]).wrapping_add(my);
state[d] = (state[d] ^ state[a]).rotate_right(8);
state[c] = state[c].wrapping_add(state[d]);
state[b] = (state[b] ^ state[c]).rotate_right(7);
}
fn round(state: &mut [u32; 16], m: &[u32; 16]) {
g(state, 0, 4, 8, 12, m[0], m[1]);
g(state, 1, 5, 9, 13, m[2], m[3]);
g(state, 2, 6, 10, 14, m[4], m[5]);
g(state, 3, 7, 11, 15, m[6], m[7]);
g(state, 0, 5, 10, 15, m[8], m[9]);
g(state, 1, 6, 11, 12, m[10], m[11]);
g(state, 2, 7, 8, 13, m[12], m[13]);
g(state, 3, 4, 9, 14, m[14], m[15]);
}
fn permute(m: &mut [u32; 16]) {
let mut permuted = [0; 16];
for i in 0..16 {
permuted[i] = m[MSG_PERMUTATION[i]];
}
*m = permuted;
}
fn compress(
chaining_value: &[u32; 8],
block_words: &[u32; 16],
counter: u64,
block_len: u32,
flags: u32,
) -> [u32; 16] {
let mut state = [
chaining_value[0],
chaining_value[1],
chaining_value[2],
chaining_value[3],
chaining_value[4],
chaining_value[5],
chaining_value[6],
chaining_value[7],
IV[0],
IV[1],
IV[2],
IV[3],
counter as u32,
(counter >> 32) as u32,
block_len,
flags,
];
let mut block = *block_words;
round(&mut state, &block);
permute(&mut block);
round(&mut state, &block);
permute(&mut block);
round(&mut state, &block);
permute(&mut block);
round(&mut state, &block);
permute(&mut block);
round(&mut state, &block);
permute(&mut block);
round(&mut state, &block);
permute(&mut block);
round(&mut state, &block);
for i in 0..8 {
state[i] ^= state[i + 8];
state[i + 8] ^= chaining_value[i];
}
state
}
fn first_8_words(compression_output: [u32; 16]) -> [u32; 8] {
compression_output[0..8].try_into().unwrap()
}
fn words_from_little_endian_bytes(bytes: &[u8], words: &mut [u32]) {
for (bytes_block, word) in bytes.chunks_exact(4).zip(words.iter_mut()) {
*word = u32::from_le_bytes(bytes_block.try_into().unwrap());
}
}
struct Output {
input_chaining_value: [u32; 8],
block_words: [u32; 16],
counter: u64,
block_len: u32,
flags: u32,
}
impl Output {
fn chaining_value(&self) -> [u32; 8] {
first_8_words(compress(
&self.input_chaining_value,
&self.block_words,
self.counter,
self.block_len,
self.flags,
))
}
fn root_output_bytes(&self, out_slice: &mut [u8]) {
let mut output_block_counter = 0;
for out_block in out_slice.chunks_mut(2 * OUT_LEN) {
let words = compress(
&self.input_chaining_value,
&self.block_words,
output_block_counter,
self.block_len,
self.flags | ROOT,
);
for (word, out_word) in words.iter().zip(out_block.chunks_mut(4)) {
out_word.copy_from_slice(&word.to_le_bytes()[..out_word.len()]);
}
output_block_counter += 1;
}
}
}
struct ChunkState {
chaining_value: [u32; 8],
chunk_counter: u64,
block: [u8; BLOCK_LEN],
block_len: u8,
blocks_compressed: u8,
flags: u32,
}
impl ChunkState {
fn new(key_words: [u32; 8], chunk_counter: u64, flags: u32) -> Self {
Self {
chaining_value: key_words,
chunk_counter,
block: [0; BLOCK_LEN],
block_len: 0,
blocks_compressed: 0,
flags,
}
}
fn len(&self) -> usize {
BLOCK_LEN * self.blocks_compressed as usize + self.block_len as usize
}
fn start_flag(&self) -> u32 {
if self.blocks_compressed == 0 {
CHUNK_START
} else {
0
}
}
fn update(&mut self, mut input: &[u8]) {
while !input.is_empty() {
if self.block_len as usize == BLOCK_LEN {
let mut block_words = [0; 16];
words_from_little_endian_bytes(&self.block, &mut block_words);
self.chaining_value = first_8_words(compress(
&self.chaining_value,
&block_words,
self.chunk_counter,
BLOCK_LEN as u32,
self.flags | self.start_flag(),
));
self.blocks_compressed += 1;
self.block = [0; BLOCK_LEN];
self.block_len = 0;
}
let want = BLOCK_LEN - self.block_len as usize;
let take = min(want, input.len());
self.block[self.block_len as usize..][..take].copy_from_slice(&input[..take]);
self.block_len += take as u8;
input = &input[take..];
}
}
fn output(&self) -> Output {
let mut block_words = [0; 16];
words_from_little_endian_bytes(&self.block, &mut block_words);
Output {
input_chaining_value: self.chaining_value,
block_words,
counter: self.chunk_counter,
block_len: self.block_len as u32,
flags: self.flags | self.start_flag() | CHUNK_END,
}
}
}
fn parent_output(
left_child_cv: [u32; 8],
right_child_cv: [u32; 8],
key_words: [u32; 8],
flags: u32,
) -> Output {
let mut block_words = [0; 16];
block_words[..8].copy_from_slice(&left_child_cv);
block_words[8..].copy_from_slice(&right_child_cv);
Output {
input_chaining_value: key_words,
block_words,
counter: 0,
block_len: BLOCK_LEN as u32,
flags: PARENT | flags,
}
}
fn parent_cv(
left_child_cv: [u32; 8],
right_child_cv: [u32; 8],
key_words: [u32; 8],
flags: u32,
) -> [u32; 8] {
parent_output(left_child_cv, right_child_cv, key_words, flags).chaining_value()
}
pub struct Hasher {
chunk_state: ChunkState,
key_words: [u32; 8],
cv_stack: [[u32; 8]; 54],
cv_stack_len: u8,
flags: u32,
}
impl Hasher {
fn new_internal(key_words: [u32; 8], flags: u32) -> Self {
Self {
chunk_state: ChunkState::new(key_words, 0, flags),
key_words,
cv_stack: [[0; 8]; 54],
cv_stack_len: 0,
flags,
}
}
pub fn new() -> Self {
Self::new_internal(IV, 0)
}
pub fn new_keyed(key: &[u8; KEY_LEN]) -> Self {
let mut key_words = [0; 8];
words_from_little_endian_bytes(key, &mut key_words);
Self::new_internal(key_words, KEYED_HASH)
}
pub fn new_derive_key(context: &str) -> Self {
let mut context_hasher = Self::new_internal(IV, DERIVE_KEY_CONTEXT);
context_hasher.update(context.as_bytes());
let mut context_key = [0; KEY_LEN];
context_hasher.finalize(&mut context_key);
let mut context_key_words = [0; 8];
words_from_little_endian_bytes(&context_key, &mut context_key_words);
Self::new_internal(context_key_words, DERIVE_KEY_MATERIAL)
}
fn push_stack(&mut self, cv: [u32; 8]) {
self.cv_stack[self.cv_stack_len as usize] = cv;
self.cv_stack_len += 1;
}
fn pop_stack(&mut self) -> [u32; 8] {
self.cv_stack_len -= 1;
self.cv_stack[self.cv_stack_len as usize]
}
fn add_chunk_chaining_value(&mut self, mut new_cv: [u32; 8], mut total_chunks: u64) {
while total_chunks & 1 == 0 {
new_cv = parent_cv(self.pop_stack(), new_cv, self.key_words, self.flags);
total_chunks >>= 1;
}
self.push_stack(new_cv);
}
pub fn update(&mut self, mut input: &[u8]) {
while !input.is_empty() {
if self.chunk_state.len() == CHUNK_LEN {
let chunk_cv = self.chunk_state.output().chaining_value();
let total_chunks = self.chunk_state.chunk_counter + 1;
self.add_chunk_chaining_value(chunk_cv, total_chunks);
self.chunk_state = ChunkState::new(self.key_words, total_chunks, self.flags);
}
let want = CHUNK_LEN - self.chunk_state.len();
let take = min(want, input.len());
self.chunk_state.update(&input[..take]);
input = &input[take..];
}
}
pub fn finalize(&self, out_slice: &mut [u8]) {
let mut output = self.chunk_state.output();
let mut parent_nodes_remaining = self.cv_stack_len as usize;
while parent_nodes_remaining > 0 {
parent_nodes_remaining -= 1;
output = parent_output(
self.cv_stack[parent_nodes_remaining],
output.chaining_value(),
self.key_words,
self.flags,
);
}
output.root_output_bytes(out_slice);
}
}