Skip to content

Commit

Permalink
argon2: move Memory to its own module (#158)
Browse files Browse the repository at this point in the history
Also makes `Instance` accept `Memory` as a parameter.

This is in preparation for moving slice handling to `Memory`.
  • Loading branch information
tarcieri authored Apr 19, 2021
1 parent dfc9815 commit a1b7413
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
42 changes: 4 additions & 38 deletions argon2/src/instance.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -107,7 +75,7 @@ impl<'a> Instance<'a> {
context: &Argon2<'_>,
alg: Algorithm,
initial_hash: digest::Output<Blake2b>,
memory: &'a mut [Block],
memory: Memory<'a>,
out: &mut [u8],
) -> Result<(), Error> {
let mut instance = Self::new(context, alg, initial_hash, memory)?;
Expand All @@ -127,10 +95,8 @@ impl<'a> Instance<'a> {
context: &Argon2<'_>,
alg: Algorithm,
mut initial_hash: digest::Output<Blake2b>,
memory: &'a mut [Block],
memory: Memory<'a>,
) -> Result<Self, Error> {
let memory = Memory::new(memory);

let mut instance = Instance {
version: context.version,
memory,
Expand Down
5 changes: 3 additions & 2 deletions argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ extern crate alloc;
mod block;
mod error;
mod instance;
mod memory;

pub use crate::error::Error;

#[cfg(feature = "password-hash")]
#[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,
Expand Down Expand Up @@ -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]`.
Expand Down
36 changes: 36 additions & 0 deletions argon2/src/memory.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit a1b7413

Please sign in to comment.