Skip to content

Commit

Permalink
update digest, improve argon2 code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 30, 2021
1 parent 88365d6 commit fc6918c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 20 additions & 19 deletions argon2/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::{Algorithm, Argon2, Block, Error, Memory, Params, Result, Version, SYNC_POINTS};
use blake2::{
digest::{self, Output, VariableOutput},
Blake2b512, Blake2bVar, Digest,
digest::{self, Digest, Output, VariableOutput},
Blake2b512, Blake2bVar,
};

#[cfg(feature = "parallel")]
Expand Down Expand Up @@ -416,14 +416,18 @@ fn blake2b_long(inputs: &[&[u8]], mut out: &mut [u8]) -> Result<()> {
let outlen_bytes = (out.len() as u32).to_le_bytes();

if out.len() <= BLAKE2B_OUTBYTES {
let mut digest = Blake2bVar::new(out.len()).unwrap();
digest::Update::update(&mut digest, &outlen_bytes);
use digest::Update;

let mut digest = Blake2bVar::new(out.len()).expect("`out` length is valid for Blake2bVar");
Update::update(&mut digest, &outlen_bytes);

for input in inputs {
digest::Update::update(&mut digest, input);
Update::update(&mut digest, input);
}

digest.finalize_variable(|hash| out.copy_from_slice(hash));
digest
.finalize_variable(out)
.expect("`out` length is valid for Blake2bVar");
} else {
let mut digest = Blake2b512::new();
digest.update(&outlen_bytes);
Expand All @@ -432,25 +436,22 @@ fn blake2b_long(inputs: &[&[u8]], mut out: &mut [u8]) -> Result<()> {
digest.update(input);
}

let mut out_buffer = [0u8; BLAKE2B_OUTBYTES];
out_buffer.copy_from_slice(&digest.finalize());
let mut hash = digest.finalize();

out[..(BLAKE2B_OUTBYTES / 2)].copy_from_slice(&out_buffer[..(BLAKE2B_OUTBYTES / 2)]);
out = &mut out[(BLAKE2B_OUTBYTES / 2)..];
let n = BLAKE2B_OUTBYTES / 2;

let mut in_buffer = [0u8; BLAKE2B_OUTBYTES];
let (chunk, tail) = out.split_at_mut(n);
out = tail;
chunk.copy_from_slice(&hash[..n]);

while out.len() > BLAKE2B_OUTBYTES {
in_buffer.copy_from_slice(&out_buffer);
out_buffer.copy_from_slice(&Blake2b512::digest(&in_buffer));

out[..(BLAKE2B_OUTBYTES / 2)].copy_from_slice(&out_buffer[..(BLAKE2B_OUTBYTES / 2)]);
out = &mut out[(BLAKE2B_OUTBYTES / 2)..];
let (chunk, tail) = out.split_at_mut(n);
out = tail;
hash = Blake2b512::digest(&hash);
chunk.copy_from_slice(&hash[..n]);
}

let mut digest = Blake2bVar::new(out.len()).unwrap();
digest::Update::update(&mut digest, &out_buffer);
digest.finalize_variable(|hash| out.copy_from_slice(hash));
Blake2bVar::digest_variable(&hash, out).expect("`out` length is valid for Blake2bVar");
}

Ok(())
Expand Down
18 changes: 9 additions & 9 deletions argon2/tests/kat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ fn argon2id_v0x13() {
assert_eq!(out, expected_tag);
}

/// =======================================
/// Basic error checks
/// =======================================
// =======================================
// Basic error checks
// =======================================

#[test]
fn salt_bad_length() {
Expand All @@ -306,7 +306,7 @@ fn salt_bad_length() {
let ret = ctx.hash_password_into(b"password", &too_short_salt, &mut out);
assert_eq!(ret, Err(Error::SaltTooShort));

// 4Go of RAM seems big, but as long as we ask for a zero-initialized vector
// 4 GiB of RAM seems big, but as long as we ask for a zero-initialized vector
// optimizations kicks in an nothing is really allocated
let too_long_salt = vec![0u8; argon2::MAX_SALT_LEN + 1];
let ret = ctx.hash_password_into(b"password", &too_long_salt, &mut out);
Expand All @@ -320,17 +320,17 @@ fn output_bad_length() {
let ret = ctx.hash_password_into(b"password", b"diffsalt", &mut out);
assert_eq!(ret, Err(Error::OutputTooShort));

// 4Go of RAM seems big, but as long as we ask for a zero-initialized vector
// 4 GiB of RAM seems big, but as long as we ask for a zero-initialized vector
// optimizations kicks in an nothing is really allocated
let mut out = vec![0u8; Params::MAX_OUTPUT_LEN + 1];
let ret = ctx.hash_password_into(b"password", b"diffsalt", &mut out);
assert_eq!(ret, Err(Error::OutputTooLong));
}

/// =======================================
/// Reference implementation's test suite
/// =======================================
/// Taken from https://github.com/P-H-C/phc-winner-argon2/blob/master/src/test.c
// =======================================
// Reference implementation's test suite
// =======================================
// Taken from https://github.com/P-H-C/phc-winner-argon2/blob/master/src/test.c

fn hashtest(
algorithm: Algorithm,
Expand Down

0 comments on commit fc6918c

Please sign in to comment.