Skip to content

Commit

Permalink
Merge pull request #2295 from reaperhulk/argon2-libctx
Browse files Browse the repository at this point in the history
add libctx arg to argon2id
  • Loading branch information
alex authored Sep 3, 2024
2 parents 01fe6f0 + 5ea9a12 commit 252e743
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions openssl/src/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ cfg_if::cfg_if! {
use std::ffi::{c_char, c_void};
use std::mem::MaybeUninit;
use std::ptr;
use foreign_types::ForeignTypeRef;
use crate::{cvt, cvt_p};
use crate::lib_ctx::LibCtxRef;
use crate::error::ErrorStack;

/// Derives a key using the argon2id algorithm.
Expand All @@ -38,6 +40,7 @@ cfg_if::cfg_if! {
/// Requires OpenSSL 3.2.0 or newer.
#[allow(clippy::too_many_arguments)]
pub fn argon2id(
ctx: Option<&LibCtxRef>,
pass: &[u8],
salt: &[u8],
ad: Option<&[u8]>,
Expand All @@ -49,6 +52,8 @@ cfg_if::cfg_if! {
) -> Result<(), ErrorStack> {
unsafe {
ffi::init();
let libctx = ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr);

let mut threads = 1;
let mut params: [ffi::OSSL_PARAM; 10] =
core::array::from_fn(|_| MaybeUninit::<ffi::OSSL_PARAM>::zeroed().assume_init());
Expand Down Expand Up @@ -100,7 +105,7 @@ cfg_if::cfg_if! {
params[idx] = ffi::OSSL_PARAM_construct_end();

let argon2 = EvpKdf(cvt_p(ffi::EVP_KDF_fetch(
ptr::null_mut(),
libctx,
b"ARGON2ID\0".as_ptr() as *const c_char,
ptr::null(),
))?);
Expand Down Expand Up @@ -132,6 +137,7 @@ mod tests {

let mut actual = [0u8; 32];
super::argon2id(
None,
&pass,
&salt,
Some(&ad),
Expand All @@ -154,7 +160,7 @@ mod tests {
let expected = "0a34f1abde67086c82e785eaf17c68382259a264f4e61b91cd2763cb75ac189a";

let mut actual = [0u8; 32];
super::argon2id(pass, &salt, None, None, 3, 4, 32, &mut actual).unwrap();
super::argon2id(None, pass, &salt, None, None, 3, 4, 32, &mut actual).unwrap();
assert_eq!(hex::encode(&actual[..]), expected);
}
}

0 comments on commit 252e743

Please sign in to comment.