Skip to content

Commit a94ccf2

Browse files
authored
Merge pull request #2296 from reaperhulk/argon2-threads
support using threads in argon2id
2 parents 252e743 + b3bb5f4 commit a94ccf2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

openssl/src/kdf.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl Drop for EvpKdfCtx {
2424

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

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

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

0 commit comments

Comments
 (0)