From a1b7413b08f0d8f521e2821b51ae7c6a8b7fd0ba Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 19 Apr 2021 10:18:29 -0700 Subject: [PATCH] argon2: move `Memory` to its own module (#158) Also makes `Instance` accept `Memory` as a parameter. This is in preparation for moving slice handling to `Memory`. --- argon2/src/instance.rs | 42 ++++-------------------------------------- argon2/src/lib.rs | 5 +++-- argon2/src/memory.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 argon2/src/memory.rs diff --git a/argon2/src/instance.rs b/argon2/src/instance.rs index ebcaad3c..c0c6c054 100644 --- a/argon2/src/instance.rs +++ b/argon2/src/instance.rs @@ -1,7 +1,8 @@ //! Argon2 instance (i.e. state) use crate::{ - Algorithm, Argon2, Block, Error, Version, BLOCK_SIZE, MAX_OUTLEN, MIN_OUTLEN, SYNC_POINTS, + Algorithm, Argon2, Block, Error, Memory, Version, BLOCK_SIZE, MAX_OUTLEN, MIN_OUTLEN, + SYNC_POINTS, }; use blake2::{ digest::{self, VariableOutput}, @@ -36,39 +37,6 @@ struct Position { index: u32, } -/// Structure containing references to the memory blocks -struct Memory<'a> { - /// Memory blocks - data: &'a mut [Block], - - /// Size of the memory in blocks - size: usize, -} - -impl<'a> Memory<'a> { - /// Instantiate a new memory struct - fn new(data: &'a mut [Block]) -> Self { - let size = data.len(); - - Self { data, size } - } - - /// Get a copy of the block - fn get_block(&self, idx: usize) -> Block { - self.data[idx] - } - - /// Get a mutable reference to the block - fn get_block_mut(&mut self, idx: usize) -> &mut Block { - &mut self.data[idx] - } - - /// Size of the memory - fn len(&self) -> usize { - self.size - } -} - /// Argon2 instance: memory pointer, number of passes, amount of memory, type, /// and derived values. /// @@ -107,7 +75,7 @@ impl<'a> Instance<'a> { context: &Argon2<'_>, alg: Algorithm, initial_hash: digest::Output, - memory: &'a mut [Block], + memory: Memory<'a>, out: &mut [u8], ) -> Result<(), Error> { let mut instance = Self::new(context, alg, initial_hash, memory)?; @@ -127,10 +95,8 @@ impl<'a> Instance<'a> { context: &Argon2<'_>, alg: Algorithm, mut initial_hash: digest::Output, - memory: &'a mut [Block], + memory: Memory<'a>, ) -> Result { - let memory = Memory::new(memory); - let mut instance = Instance { version: context.version, memory, diff --git a/argon2/src/lib.rs b/argon2/src/lib.rs index cbe328b0..54e6fea2 100644 --- a/argon2/src/lib.rs +++ b/argon2/src/lib.rs @@ -79,6 +79,7 @@ extern crate alloc; mod block; mod error; mod instance; +mod memory; pub use crate::error::Error; @@ -86,7 +87,7 @@ pub use crate::error::Error; #[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))] pub use password_hash::{self, PasswordHash, PasswordHasher, PasswordVerifier}; -use crate::{block::Block, instance::Instance}; +use crate::{block::Block, instance::Instance, memory::Memory}; use blake2::{digest, Blake2b, Digest}; use core::{ convert::TryFrom, @@ -485,7 +486,7 @@ impl<'key> Argon2<'key> { // TODO(tarcieri): support for stack-allocated memory blocks (i.e. no alloc) let mut memory = vec![Block::default(); memory_blocks]; - Instance::hash(self, alg, initial_hash, &mut memory, out) + Instance::hash(self, alg, initial_hash, Memory::new(&mut memory), out) } /// Hashes all the inputs into `blockhash[PREHASH_DIGEST_LENGTH]`. diff --git a/argon2/src/memory.rs b/argon2/src/memory.rs new file mode 100644 index 00000000..ef7e7a98 --- /dev/null +++ b/argon2/src/memory.rs @@ -0,0 +1,36 @@ +//! Memory blocks + +use crate::Block; + +/// Structure containing references to the memory blocks +pub(crate) struct Memory<'a> { + /// Memory blocks + data: &'a mut [Block], + + /// Size of the memory in blocks + size: usize, +} + +impl<'a> Memory<'a> { + /// Instantiate a new memory struct + pub(crate) fn new(data: &'a mut [Block]) -> Self { + let size = data.len(); + + Self { data, size } + } + + /// Get a copy of the block + pub(crate) fn get_block(&self, idx: usize) -> Block { + self.data[idx] + } + + /// Get a mutable reference to the block + pub(crate) fn get_block_mut(&mut self, idx: usize) -> &mut Block { + &mut self.data[idx] + } + + /// Size of the memory + pub(crate) fn len(&self) -> usize { + self.size + } +}