-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathblock.rs
More file actions
196 lines (167 loc) · 5.4 KB
/
block.rs
File metadata and controls
196 lines (167 loc) · 5.4 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
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
//! Argon2 memory block functions
use core::{
convert::{AsMut, AsRef},
num::Wrapping,
ops::{BitXor, BitXorAssign},
slice,
};
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
const TRUNC: u64 = u32::MAX as u64;
#[rustfmt::skip]
macro_rules! permute_step {
($a:expr, $b:expr, $c:expr, $d:expr) => {
$a = (Wrapping($a) + Wrapping($b) + (Wrapping(2) * Wrapping(($a & TRUNC) * ($b & TRUNC)))).0;
$d = ($d ^ $a).rotate_right(32);
$c = (Wrapping($c) + Wrapping($d) + (Wrapping(2) * Wrapping(($c & TRUNC) * ($d & TRUNC)))).0;
$b = ($b ^ $c).rotate_right(24);
$a = (Wrapping($a) + Wrapping($b) + (Wrapping(2) * Wrapping(($a & TRUNC) * ($b & TRUNC)))).0;
$d = ($d ^ $a).rotate_right(16);
$c = (Wrapping($c) + Wrapping($d) + (Wrapping(2) * Wrapping(($c & TRUNC) * ($d & TRUNC)))).0;
$b = ($b ^ $c).rotate_right(63);
};
}
macro_rules! permute {
(
$v0:expr, $v1:expr, $v2:expr, $v3:expr,
$v4:expr, $v5:expr, $v6:expr, $v7:expr,
$v8:expr, $v9:expr, $v10:expr, $v11:expr,
$v12:expr, $v13:expr, $v14:expr, $v15:expr,
) => {
permute_step!($v0, $v4, $v8, $v12);
permute_step!($v1, $v5, $v9, $v13);
permute_step!($v2, $v6, $v10, $v14);
permute_step!($v3, $v7, $v11, $v15);
permute_step!($v0, $v5, $v10, $v15);
permute_step!($v1, $v6, $v11, $v12);
permute_step!($v2, $v7, $v8, $v13);
permute_step!($v3, $v4, $v9, $v14);
};
}
/// Structure for the (1 KiB) memory block implemented as 128 64-bit words.
#[derive(Copy, Clone, Debug)]
#[repr(align(64))]
pub struct Block([u64; Self::SIZE / 8]);
impl Block {
/// Memory block size in bytes
pub const SIZE: usize = 1024;
/// Returns a Block initialized with zeros.
pub const fn new() -> Self {
Self([0u64; Self::SIZE / 8])
}
/// Load a block from a block-sized byte slice
#[inline(always)]
pub(crate) fn load(&mut self, input: &[u8; Block::SIZE]) {
for (i, chunk) in input.chunks(8).enumerate() {
self.0[i] = u64::from_le_bytes(chunk.try_into().expect("should be 8 bytes"));
}
}
/// Iterate over the `u64` values contained in this block
#[inline(always)]
pub(crate) fn iter(&self) -> slice::Iter<'_, u64> {
self.0.iter()
}
/// NOTE: do not call this directly. It should only be called via
/// `Argon2::compress`.
#[inline(always)]
pub(crate) fn compress(rhs: &Self, lhs: &Self) -> Self {
let r = *rhs ^ lhs;
// Apply permutations rowwise
let mut q = r;
for chunk in q.0.chunks_exact_mut(16) {
#[rustfmt::skip]
permute!(
chunk[0], chunk[1], chunk[2], chunk[3],
chunk[4], chunk[5], chunk[6], chunk[7],
chunk[8], chunk[9], chunk[10], chunk[11],
chunk[12], chunk[13], chunk[14], chunk[15],
);
}
// Apply permutations columnwise
for i in 0..8 {
let b = i * 2;
#[rustfmt::skip]
permute!(
q.0[b], q.0[b + 1],
q.0[b + 16], q.0[b + 17],
q.0[b + 32], q.0[b + 33],
q.0[b + 48], q.0[b + 49],
q.0[b + 64], q.0[b + 65],
q.0[b + 80], q.0[b + 81],
q.0[b + 96], q.0[b + 97],
q.0[b + 112], q.0[b + 113],
);
}
q ^= &r;
q
}
}
impl Default for Block {
fn default() -> Self {
Self([0u64; Self::SIZE / 8])
}
}
impl AsRef<[u64]> for Block {
fn as_ref(&self) -> &[u64] {
&self.0
}
}
impl AsMut<[u64]> for Block {
fn as_mut(&mut self) -> &mut [u64] {
&mut self.0
}
}
impl BitXor<&Block> for Block {
type Output = Block;
fn bitxor(mut self, rhs: &Block) -> Self::Output {
self ^= rhs;
self
}
}
impl BitXorAssign<&Block> for Block {
fn bitxor_assign(&mut self, rhs: &Block) {
for (dst, src) in self.0.iter_mut().zip(rhs.0.iter()) {
*dst ^= src;
}
}
}
#[cfg(feature = "zeroize")]
impl Zeroize for Block {
fn zeroize(&mut self) {
self.0.zeroize();
}
}
/// Custom implementation of `Box<[Block]>` until `Box::try_new_zeroed_slice` is stabilized.
#[cfg(feature = "alloc")]
pub(crate) struct Blocks {
p: core::ptr::NonNull<Block>,
len: usize,
}
#[cfg(feature = "alloc")]
impl Blocks {
pub fn new(len: usize) -> Option<Self> {
use alloc::alloc::{Layout, alloc_zeroed};
use core::ptr::NonNull;
let layout = Layout::array::<Block>(len).ok()?;
// SAFETY: e use `alloc_zeroed` correctly
let p = unsafe { alloc_zeroed(layout) };
let p = NonNull::new(p.cast())?;
Some(Self { p, len })
}
pub fn as_slice(&mut self) -> &mut [Block] {
// SAFETY: `self.p` is a valid non-zero pointer that points to memory of the necessary size
unsafe { slice::from_raw_parts_mut(self.p.as_ptr(), self.len) }
}
}
#[cfg(feature = "alloc")]
impl Drop for Blocks {
fn drop(&mut self) {
use alloc::alloc::{Layout, dealloc};
// SAFETY: layout was checked during construction
let layout = unsafe { Layout::array::<Block>(self.len).unwrap_unchecked() };
// SAFETY: we use `dealloc` correctly with the previously allocated pointer
unsafe {
dealloc(self.p.as_ptr().cast(), layout);
}
}
}