Skip to content

Commit 5e04680

Browse files
panvaaduh95
authored andcommitted
crypto: derive Argon2 without worker threads
Keep Argon2 lane count independent from backend worker availability. When the OpenSSL thread pool is unavailable, compute lanes serially. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent fe09120 commit 5e04680

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,7 +2937,9 @@ DataPointer argon2(const Buffer<const char>& pass,
29372937
// per-context. It inherits no configuration, so availability is checked
29382938
// against the default context, otherwise Argon2 works in FIPS mode.
29392939
DeleteFnPtr<OSSL_LIB_CTX, OSSL_LIB_CTX_free> ctx;
2940-
if (lanes > 1) {
2940+
uint32_t threads = lanes == 0 ? 0 : 1;
2941+
if (lanes > 1 && (OSSL_get_thread_support_flags() &
2942+
OSSL_THREAD_SUPPORT_FLAG_DEFAULT_SPAWN) != 0) {
29412943
if (!KDF::Fetch(algorithm.data())) {
29422944
return {};
29432945
}
@@ -2947,8 +2949,13 @@ DataPointer argon2(const Buffer<const char>& pass,
29472949
return {};
29482950
}
29492951

2950-
if (OSSL_set_max_threads(ctx.get(), lanes) != 1) {
2951-
return {};
2952+
MarkPopErrorOnReturn mark_pop_error_on_return;
2953+
if (OSSL_set_max_threads(ctx.get(), lanes) == 1) {
2954+
threads = lanes;
2955+
} else {
2956+
// Lane count is an Argon2 input; worker threads are only an
2957+
// optimization. Compute the same lanes serially if unavailable.
2958+
ctx.reset();
29522959
}
29532960
}
29542961

@@ -2966,7 +2973,8 @@ DataPointer argon2(const Buffer<const char>& pass,
29662973
pass.len));
29672974
params.push_back(OSSL_PARAM_construct_octet_string(
29682975
OSSL_KDF_PARAM_SALT, const_cast<unsigned char*>(salt.data), salt.len));
2969-
params.push_back(OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_THREADS, &lanes));
2976+
params.push_back(
2977+
OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_THREADS, &threads));
29702978
params.push_back(
29712979
OSSL_PARAM_construct_uint32(OSSL_KDF_PARAM_ARGON2_LANES, &lanes));
29722980
params.push_back(

0 commit comments

Comments
 (0)