Skip to content

Commit 2ea332a

Browse files
authored
argon2: use Block::SIZE constant (RustCrypto#161)
Also deprecates the previous `argon2::BLOCK_SIZE` constant.
1 parent 9db9720 commit 2ea332a

File tree

3 files changed

+57
-58
lines changed

3 files changed

+57
-58
lines changed

argon2/src/block.rs

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Argon2 memory block functions
22
3-
use crate::BLOCK_SIZE;
43
use core::{
54
convert::TryInto,
65
num::Wrapping,
@@ -11,62 +10,23 @@ use core::{
1110
#[cfg(feature = "zeroize")]
1211
use zeroize::Zeroize;
1312

14-
/// Quadwords in block
15-
const QWORDS_IN_BLOCK: usize = BLOCK_SIZE / 8;
16-
1713
/// Structure for the (1KB) memory block implemented as 128 64-bit words.
1814
#[derive(Copy, Clone, Debug)]
19-
pub(crate) struct Block([u64; QWORDS_IN_BLOCK]);
15+
pub(crate) struct Block([u64; Self::SIZE / 8]);
2016

2117
impl Default for Block {
2218
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])
6320
}
6421
}
6522

6623
impl Block {
24+
/// Memory block size in bytes
25+
pub const SIZE: usize = 1024;
26+
6727
/// Load a block from a block-sized byte slice
6828
pub fn load(&mut self, input: &[u8]) {
69-
debug_assert_eq!(input.len(), BLOCK_SIZE);
29+
debug_assert_eq!(input.len(), Block::SIZE);
7030

7131
for (i, chunk) in input.chunks(8).enumerate() {
7232
self[i] = u64::from_le_bytes(chunk.try_into().unwrap());
@@ -97,6 +57,13 @@ impl Block {
9757
// block_tmp = ref_block + prev_block + next_block
9858
}
9959

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+
10067
/// Blake2 round function
10168
// TODO(tarcieri): use the `blake2` crate
10269
macro_rules! blake2_round {
@@ -178,9 +145,41 @@ impl Block {
178145
}
179146
}
180147

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+
}
186185
}

argon2/src/instance.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Argon2 instance (i.e. state)
22
33
use crate::{
4-
Algorithm, Argon2, Block, Error, Memory, Version, BLOCK_SIZE, MAX_OUTLEN, MIN_OUTLEN,
5-
SYNC_POINTS,
4+
Algorithm, Argon2, Block, Error, Memory, Version, MAX_OUTLEN, MIN_OUTLEN, SYNC_POINTS,
65
};
76
use blake2::{
87
digest::{self, VariableOutput},
@@ -201,7 +200,7 @@ impl<'a> Instance<'a> {
201200
}
202201

203202
// Hash the result
204-
let mut blockhash_bytes = [0u8; BLOCK_SIZE];
203+
let mut blockhash_bytes = [0u8; Block::SIZE];
205204

206205
for (chunk, v) in blockhash_bytes.chunks_mut(8).zip(blockhash.iter()) {
207206
chunk.copy_from_slice(&v.to_le_bytes())
@@ -220,7 +219,7 @@ impl<'a> Instance<'a> {
220219

221220
/// Function creates first 2 blocks per lane
222221
fn fill_first_blocks(&mut self, blockhash: &[u8]) -> Result<(), Error> {
223-
let mut hash = [0u8; BLOCK_SIZE];
222+
let mut hash = [0u8; Block::SIZE];
224223

225224
for l in 0..self.lanes {
226225
// Make the first and second block in each lane as G(H0||0||i) or

argon2/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ pub const MIN_OUTLEN: usize = 4;
123123
/// Maximum digest size in bytes
124124
pub const MAX_OUTLEN: usize = 0xFFFFFFFF;
125125

126-
/// Minimum number of memory blocks (each of [`BLOCK_SIZE`] bytes)
126+
/// Minimum number of memory blocks.
127127
pub const MIN_MEMORY: u32 = 2 * SYNC_POINTS; // 2 blocks per slice
128128

129-
/// Maximum number of memory blocks (each of [`BLOCK_SIZE`] bytes)
129+
/// Maximum number of memory blocks.
130130
pub const MAX_MEMORY: u32 = 0x0FFFFFFF;
131131

132132
/// Minimum number of passes
@@ -151,7 +151,8 @@ pub const MAX_SALT_LENGTH: usize = 0xFFFFFFFF;
151151
pub const MAX_SECRET: usize = 0xFFFFFFFF;
152152

153153
/// Memory block size in bytes
154-
pub const BLOCK_SIZE: usize = 1024;
154+
#[deprecated(since = "0.1.6", note = "use Block::SIZE instead")]
155+
pub const BLOCK_SIZE: usize = Block::SIZE;
155156

156157
/// Argon2d algorithm identifier
157158
#[cfg(feature = "password-hash")]

0 commit comments

Comments
 (0)