Skip to content

Commit

Permalink
Fix argon2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Oct 22, 2023
1 parent d7100c4 commit 21bc8e2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
50 changes: 47 additions & 3 deletions rcrypto-sys/src/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::slice;

use argon2::password_hash::errors::InvalidValue;
use argon2::password_hash::{errors::Error as PwHashErr, PasswordHash, PasswordVerifier, Salt};
use argon2::PasswordHasher;
use argon2::{Argon2, PasswordHasher};

/// Maximum length for a password output string. Actual value may be shorter
pub const RC_PWHASH_STRBYTES: usize = 128;
Expand Down Expand Up @@ -91,7 +91,7 @@ pub unsafe extern "C" fn rc_pwhash_argon2(
Err(e) => return dbg!(e).into(),
};

let a2 = argon2::Argon2::default();
let a2 = Argon2::default();

let hash = match a2.hash_password(pw, salt) {
Ok(v) => v,
Expand Down Expand Up @@ -130,10 +130,54 @@ pub unsafe extern "C" fn rc_pwhash_argon2_verify(
Ok(v) => v,
Err(e) => return dbg!(e).into(),
};
let res = argon2::Argon2::default().verify_password(pw, &parsed_hash);
let res = Argon2::default().verify_password(pw, &parsed_hash);
if let Err(e) = res {
dbg!(e).into()
} else {
RcPwhashresult::RcPwhashOk
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_roundtrip() {
let pw = "password";
let salt = Salt::from_b64("c2FsdHlzYWx0eXNhbHR5").unwrap();
let mut out = [0u8; RC_PWHASH_STRBYTES];

let a2 = Argon2::default();
let expected = a2.hash_password(pw.as_bytes(), salt).unwrap().to_string();
dbg!(&expected);
let mut outlen = 0usize;

let res = unsafe {
rc_pwhash_argon2(
pw.as_ptr(),
pw.len(),
salt.as_ref().as_ptr(),
salt.as_ref().len(),
out.as_mut_ptr(),
RC_PWHASH_STRBYTES,
&mut outlen,
)
};

let hash = &out[..outlen];
let outstr = core::str::from_utf8(hash).unwrap();
assert_eq!(outstr, expected);
assert_eq!(res as i32, 0);

let bad = "nopassword";

let res =
unsafe { rc_pwhash_argon2_verify(bad.as_ptr(), bad.len(), hash.as_ptr(), hash.len()) };
assert_eq!(res as i32, 1);

let res =
unsafe { rc_pwhash_argon2_verify(pw.as_ptr(), pw.len(), hash.as_ptr(), hash.len()) };
assert_eq!(res as i32, 0);
}
}
8 changes: 4 additions & 4 deletions rcrypto-sys/tests/pwhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ void test_argon2() {
char *expected = "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHlzYWx0eXNhbHR5$LTCCralPbFpMyrU5fOFhSIR37CV6B19KICx1uph6jn0";
int pwlen = strlen(pw);
int saltlen = strlen(salt);
size_t newout;
size_t outlen = 0;

uchar out[RC_PWHASH_STRBYTES+100];

int cres = rc_pwhash_argon2((uchar*)pw, pwlen, (uchar*)salt, saltlen, out,
RC_PWHASH_STRBYTES, &newout);
RC_PWHASH_STRBYTES, &outlen);
TEST_ASSERT_EQUAL(0, cres);
// TEST_ASSERT_EQUAL(strlen(expected), newout); // FIXME
TEST_ASSERT_EQUAL(strlen(expected), outlen);
TEST_ASSERT_EQUAL_STRING(expected, out);
printf("pw: %s\n", out);

int vres = rc_pwhash_argon2_verify((uchar*)pw, pwlen, (uchar*)salt, saltlen);
int vres = rc_pwhash_argon2_verify((uchar*)pw, pwlen, (uchar*)out, outlen);
TEST_ASSERT_EQUAL(0, vres);
}

Expand Down

0 comments on commit 21bc8e2

Please sign in to comment.