-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathsoft.rs
More file actions
70 lines (58 loc) · 2.06 KB
/
soft.rs
File metadata and controls
70 lines (58 loc) · 2.06 KB
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
//! Portable implementation which does not rely on architecture-specific
//! intrinsics.
use crate::{Block, STATE_WORDS, SalsaCore, Unsigned};
use cipher::{
BlockSizeUser, ParBlocksSizeUser, StreamCipherBackend, StreamCipherSeekCore,
consts::{U1, U64},
};
pub(crate) struct Backend<'a, R: Unsigned, KeySize>(pub(crate) &'a mut SalsaCore<R, KeySize>);
impl<R: Unsigned, KeySize> BlockSizeUser for Backend<'_, R, KeySize> {
type BlockSize = U64;
}
impl<R: Unsigned, KeySize> ParBlocksSizeUser for Backend<'_, R, KeySize> {
type ParBlocksSize = U1;
}
impl<R: Unsigned, KeySize> StreamCipherBackend for Backend<'_, R, KeySize> {
#[inline(always)]
fn gen_ks_block(&mut self, block: &mut Block<Self>) {
let res = run_rounds::<R>(&self.0.state);
self.0.set_block_pos(self.0.get_block_pos() + 1);
for (chunk, val) in block.chunks_exact_mut(4).zip(res.iter()) {
chunk.copy_from_slice(&val.to_le_bytes());
}
}
}
#[inline]
#[allow(clippy::many_single_char_names)]
pub(crate) fn quarter_round(
a: usize,
b: usize,
c: usize,
d: usize,
state: &mut [u32; STATE_WORDS],
) {
state[b] ^= state[a].wrapping_add(state[d]).rotate_left(7);
state[c] ^= state[b].wrapping_add(state[a]).rotate_left(9);
state[d] ^= state[c].wrapping_add(state[b]).rotate_left(13);
state[a] ^= state[d].wrapping_add(state[c]).rotate_left(18);
}
#[inline(always)]
fn run_rounds<R: Unsigned>(state: &[u32; STATE_WORDS]) -> [u32; STATE_WORDS] {
let mut res = *state;
for _ in 0..R::USIZE {
// column rounds
quarter_round(0, 4, 8, 12, &mut res);
quarter_round(5, 9, 13, 1, &mut res);
quarter_round(10, 14, 2, 6, &mut res);
quarter_round(15, 3, 7, 11, &mut res);
// diagonal rounds
quarter_round(0, 1, 2, 3, &mut res);
quarter_round(5, 6, 7, 4, &mut res);
quarter_round(10, 11, 8, 9, &mut res);
quarter_round(15, 12, 13, 14, &mut res);
}
for (s1, s0) in res.iter_mut().zip(state.iter()) {
*s1 = s1.wrapping_add(*s0);
}
res
}