Skip to content

Commit

Permalink
Merge pull request #2296 from reaperhulk/argon2-threads
Browse files Browse the repository at this point in the history
support using threads in argon2id
  • Loading branch information
alex committed Sep 3, 2024
2 parents 252e743 + b3bb5f4 commit a94ccf2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions openssl/src/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Drop for EvpKdfCtx {

cfg_if::cfg_if! {
if #[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))] {
use std::cmp;
use std::ffi::{c_char, c_void};
use std::mem::MaybeUninit;
use std::ptr;
Expand All @@ -34,8 +35,9 @@ cfg_if::cfg_if! {

/// Derives a key using the argon2id algorithm.
///
/// This function currently does not support multi-threaded operation, so
/// lanes greater than 1 will be processed sequentially.
/// To use multiple cores to process the lanes in parallel you must
/// set a global max thread count using `OSSL_set_max_threads`. On
/// builds with no threads all lanes will be processed sequentially.
///
/// Requires OpenSSL 3.2.0 or newer.
#[allow(clippy::too_many_arguments)]
Expand All @@ -54,7 +56,14 @@ cfg_if::cfg_if! {
ffi::init();
let libctx = ctx.map_or(ptr::null_mut(), ForeignTypeRef::as_ptr);

let max_threads = ffi::OSSL_get_max_threads(libctx);
let mut threads = 1;
// If max_threads is 0, then this isn't a threaded build.
// If max_threads is > u32::MAX we need to clamp since
// argon2id's threads parameter is a u32.
if max_threads > 0 {
threads = cmp::min(lanes, cmp::min(max_threads, u32::MAX as u64) as u32);
}
let mut params: [ffi::OSSL_PARAM; 10] =
core::array::from_fn(|_| MaybeUninit::<ffi::OSSL_PARAM>::zeroed().assume_init());
let mut idx = 0;
Expand Down

0 comments on commit a94ccf2

Please sign in to comment.