Skip to content

Commit

Permalink
set a threadpool number
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperhulk committed Oct 29, 2024
1 parent 5a6a9fc commit 9156762
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/rust/src/backend/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ impl Argon2id {
) -> CryptographyResult<Self> {
cfg_if::cfg_if! {
if #[cfg(not(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER))] {
_ = py;
_ = salt;
_ = length;
_ = iterations;
_ = lanes;
_ = memory_cost;
Expand Down Expand Up @@ -274,7 +276,7 @@ impl Argon2id {
}
}

#[cfg(not(CRYPTOGRAPHY_IS_LIBRESSL))]
#[cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)]
fn derive<'p>(
&mut self,
py: pyo3::Python<'p>,
Expand All @@ -284,7 +286,6 @@ impl Argon2id {
return Err(exceptions::already_finalized_error());
}
self.used = true;

Ok(pyo3::types::PyBytes::new_bound_with(
py,
self.length,
Expand All @@ -306,7 +307,7 @@ impl Argon2id {
)?)
}

#[cfg(not(CRYPTOGRAPHY_IS_LIBRESSL))]
#[cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)]
fn verify(
&mut self,
py: pyo3::Python<'_>,
Expand Down
17 changes: 17 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::error::CryptographyResult;
use openssl::provider;
#[cfg(CRYPTOGRAPHY_OPENSSL_300_OR_GREATER)]
use std::env;
#[cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)]
use std::ptr;

mod asn1;
mod backend;
Expand Down Expand Up @@ -74,6 +76,16 @@ fn _initialize_providers() -> CryptographyResult<LoadedProviders> {
})
}

fn set_max_global_threads() {
// if we can't get available parallelism we do nothing
if let Ok(available) = std::thread::available_parallelism() {
// SAFETY: This sets a libctx provider limit, but we always use the same libctx by passing NULL.
unsafe {
openssl_sys::OSSL_set_max_threads(ptr::null_mut(), available.get() as u64);
}
};
}

fn _legacy_provider_error(success: bool) -> pyo3::PyResult<()> {
if !success {
return Err(pyo3::exceptions::PyRuntimeError::new_err(
Expand Down Expand Up @@ -210,6 +222,11 @@ mod _rust {
cfg_if::cfg_if! {
if #[cfg(CRYPTOGRAPHY_OPENSSL_300_OR_GREATER)] {
let providers = super::super::_initialize_providers()?;
cfg_if::cfg_if! {
if #[cfg(CRYPTOGRAPHY_OPENSSL_320_OR_GREATER)] {
super::super::set_max_global_threads();
}
}
if providers.legacy.is_some() {
openssl_mod.add("_legacy_provider_loaded", true)?;
} else {
Expand Down

0 comments on commit 9156762

Please sign in to comment.