Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions argon2/src/blake2b_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use blake2::{
},
};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

pub fn blake2b_long(inputs: &[&[u8]], out: &mut [u8]) -> Result<()> {
if out.is_empty() {
return Err(Error::OutputTooShort);
Expand All @@ -33,6 +36,9 @@ pub fn blake2b_long(inputs: &[&[u8]], out: &mut [u8]) -> Result<()> {
let out_src = &full_out[..out.len()];
out.copy_from_slice(out_src);

#[cfg(feature = "zeroize")]
full_out.zeroize();

return Ok(());
}

Expand Down Expand Up @@ -70,5 +76,11 @@ pub fn blake2b_long(inputs: &[&[u8]], out: &mut [u8]) -> Result<()> {
let out_src = &full_out[..out.len()];
out.copy_from_slice(out_src);

#[cfg(feature = "zeroize")]
{
last_output.zeroize();
full_out.zeroize();
}

Ok(())
}
25 changes: 18 additions & 7 deletions argon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ impl<'key> Argon2<'key> {
Self::verify_inputs(pwd, salt)?;

// Hashing all inputs
let initial_hash = self.initial_hash(pwd, salt, out);
self.fill_blocks(memory_blocks.as_mut(), initial_hash)?;
let mut initial_hash = self.initial_hash(pwd, salt, out);
self.fill_blocks(memory_blocks.as_mut(), &mut initial_hash)?;
self.finalize(memory_blocks.as_mut(), out)
}

Expand All @@ -346,15 +346,15 @@ impl<'key> Argon2<'key> {
) -> Result<()> {
Self::verify_inputs(pwd, salt)?;

let initial_hash = self.initial_hash(pwd, salt, &[]);
self.fill_blocks(memory_blocks.as_mut(), initial_hash)
let mut initial_hash = self.initial_hash(pwd, salt, &[]);
self.fill_blocks(memory_blocks.as_mut(), &mut initial_hash)
}

#[allow(clippy::cast_possible_truncation, unused_mut)]
#[allow(clippy::cast_possible_truncation)]
fn fill_blocks(
&self,
memory_blocks: &mut [Block],
mut initial_hash: digest::Output<Blake2b512>,
initial_hash: &mut digest::Output<Blake2b512>,
) -> Result<()> {
let block_count = self.params.block_count();
let mut memory_blocks = memory_blocks
Expand Down Expand Up @@ -383,6 +383,9 @@ impl<'key> Argon2<'key> {
let mut hash = [0u8; Block::SIZE];
blake2b_long(inputs, &mut hash)?;
block.load(&hash);

#[cfg(feature = "zeroize")]
hash.zeroize();
}
}

Expand All @@ -400,6 +403,8 @@ impl<'key> Argon2<'key> {
let mut address_block = Block::default();
let mut input_block = Block::default();
let zero_block = Block::default();
#[allow(unused_assignments)] // initial value is only read by the zeroize
let mut result = Block::default();

if data_independent_addressing {
input_block.as_mut()[..6].copy_from_slice(&[
Expand Down Expand Up @@ -503,7 +508,7 @@ impl<'key> Argon2<'key> {
let ref_index = ref_lane * lane_length + lane_index;

// Calculate new block
let result = self.compress(
result = self.compress(
memory_view.get_block(prev_index),
memory_view.get_block(ref_index),
);
Expand All @@ -517,6 +522,9 @@ impl<'key> Argon2<'key> {
prev_index = cur_index;
cur_index += 1;
}

#[cfg(feature = "zeroize")]
result.zeroize();
});
}

Expand Down Expand Up @@ -699,6 +707,9 @@ impl PasswordHasher<PasswordHash> for Argon2<'_> {
self.hash_password_into(password, &salt, out)?;
let output = Output::new(out)?;

#[cfg(feature = "zeroize")]
buffer.zeroize();

Ok(PasswordHash {
algorithm: self.algorithm.ident(),
version: Some(self.version.into()),
Expand Down
Loading