1
1
//! Argon2 memory block functions
2
2
3
- use crate :: BLOCK_SIZE ;
4
3
use core:: {
5
4
convert:: TryInto ,
6
5
num:: Wrapping ,
@@ -11,62 +10,23 @@ use core::{
11
10
#[ cfg( feature = "zeroize" ) ]
12
11
use zeroize:: Zeroize ;
13
12
14
- /// Quadwords in block
15
- const QWORDS_IN_BLOCK : usize = BLOCK_SIZE / 8 ;
16
-
17
13
/// Structure for the (1KB) memory block implemented as 128 64-bit words.
18
14
#[ derive( Copy , Clone , Debug ) ]
19
- pub ( crate ) struct Block ( [ u64 ; QWORDS_IN_BLOCK ] ) ;
15
+ pub ( crate ) struct Block ( [ u64 ; Self :: SIZE / 8 ] ) ;
20
16
21
17
impl Default for Block {
22
18
fn default ( ) -> Self {
23
- Self ( [ 0u64 ; QWORDS_IN_BLOCK ] )
24
- }
25
- }
26
-
27
- impl BitXor for Block {
28
- type Output = Self ;
29
-
30
- fn bitxor ( self , rhs : Self ) -> Self :: Output {
31
- let mut res = self ;
32
- res ^= rhs;
33
- res
34
- }
35
- }
36
-
37
- impl BitXorAssign for Block {
38
- fn bitxor_assign ( & mut self , rhs : Self ) {
39
- for ( a, b) in self . iter_mut ( ) . zip ( rhs. iter ( ) ) {
40
- * a ^= * b;
41
- }
42
- }
43
- }
44
-
45
- impl Index < usize > for Block {
46
- type Output = u64 ;
47
-
48
- fn index ( & self , index : usize ) -> & u64 {
49
- & self . 0 [ index]
50
- }
51
- }
52
-
53
- impl IndexMut < usize > for Block {
54
- fn index_mut ( & mut self , index : usize ) -> & mut u64 {
55
- & mut self . 0 [ index]
56
- }
57
- }
58
-
59
- #[ cfg( feature = "zeroize" ) ]
60
- impl Zeroize for Block {
61
- fn zeroize ( & mut self ) {
62
- self . 0 . zeroize ( ) ;
19
+ Self ( [ 0u64 ; Self :: SIZE / 8 ] )
63
20
}
64
21
}
65
22
66
23
impl Block {
24
+ /// Memory block size in bytes
25
+ pub const SIZE : usize = 1024 ;
26
+
67
27
/// Load a block from a block-sized byte slice
68
28
pub fn load ( & mut self , input : & [ u8 ] ) {
69
- debug_assert_eq ! ( input. len( ) , BLOCK_SIZE ) ;
29
+ debug_assert_eq ! ( input. len( ) , Block :: SIZE ) ;
70
30
71
31
for ( i, chunk) in input. chunks ( 8 ) . enumerate ( ) {
72
32
self [ i] = u64:: from_le_bytes ( chunk. try_into ( ) . unwrap ( ) ) ;
@@ -97,6 +57,13 @@ impl Block {
97
57
// block_tmp = ref_block + prev_block + next_block
98
58
}
99
59
60
+ /// Designed by the Lyra PHC team
61
+ fn blake2_mult ( x : u64 , y : u64 ) -> u64 {
62
+ let m = 0xFFFFFFFF ;
63
+ let xy = Wrapping ( ( x & m) * ( y & m) ) * Wrapping ( 2 ) ;
64
+ ( Wrapping ( x) + Wrapping ( y) + xy) . 0
65
+ }
66
+
100
67
/// Blake2 round function
101
68
// TODO(tarcieri): use the `blake2` crate
102
69
macro_rules! blake2_round {
@@ -178,9 +145,41 @@ impl Block {
178
145
}
179
146
}
180
147
181
- /// Designed by the Lyra PHC team
182
- fn blake2_mult ( x : u64 , y : u64 ) -> u64 {
183
- let m = 0xFFFFFFFF ;
184
- let xy = Wrapping ( ( x & m) * ( y & m) ) * Wrapping ( 2 ) ;
185
- ( Wrapping ( x) + Wrapping ( y) + xy) . 0
148
+ impl BitXor for Block {
149
+ type Output = Self ;
150
+
151
+ fn bitxor ( self , rhs : Self ) -> Self :: Output {
152
+ let mut res = self ;
153
+ res ^= rhs;
154
+ res
155
+ }
156
+ }
157
+
158
+ impl BitXorAssign for Block {
159
+ fn bitxor_assign ( & mut self , rhs : Self ) {
160
+ for ( a, b) in self . iter_mut ( ) . zip ( rhs. iter ( ) ) {
161
+ * a ^= * b;
162
+ }
163
+ }
164
+ }
165
+
166
+ impl Index < usize > for Block {
167
+ type Output = u64 ;
168
+
169
+ fn index ( & self , index : usize ) -> & u64 {
170
+ & self . 0 [ index]
171
+ }
172
+ }
173
+
174
+ impl IndexMut < usize > for Block {
175
+ fn index_mut ( & mut self , index : usize ) -> & mut u64 {
176
+ & mut self . 0 [ index]
177
+ }
178
+ }
179
+
180
+ #[ cfg( feature = "zeroize" ) ]
181
+ impl Zeroize for Block {
182
+ fn zeroize ( & mut self ) {
183
+ self . 0 . zeroize ( ) ;
184
+ }
186
185
}
0 commit comments