GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — crypto: avoid repeated OpenSSL lookups by anonrig · Pull Request #67109 · nodejs/node

crypto: avoid repeated OpenSSL lookups #67109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anonrig wants to merge 4 commits into nodejs:main from anonrig:crypto-perf

Uh oh!

There was an error while loading. Please reload this page.

Closed

crypto: avoid repeated OpenSSL lookups #67109

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
5dce5de
crypto: cache digests for synchronous operations
anonrig Oct 29, 2026
a58df6b
crypto: skip clearing empty OpenSSL error queues
anonrig Oct 28, 2026
1353791
crypto: reuse HMAC contexts in createHmac()
anonrig Oct 29, 2026
4a4eee7
test: cover crypto digest and HMAC caches
anonrig Oct 29, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
crypto: cache digests for synchronous operations
createHmac(), createSign(), createVerify() and the synchronous
one-shot sign, verify, HKDF and PBKDF2 functions looked up their
digest with Digest::FromName(). That returns OpenSSL's legacy EVP_MD,
so every EVP_DigestInit_ex() and HMAC init had to fetch the provider
implementation again through the method store.

Look these digests up through the per-Environment cache that
createHash() and crypto.hash() already use. Jobs on the thread pool
keep using Digest::FromName(): with the cache, Web Crypto digest and
HMAC throughput went down, because workers that finish sooner have to
be woken up for each new job more often.

Signed-off-by: Yagiz Nizipli <[email protected]>
Assisted-by: Grok
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@anonrig
anonrig committed Oct 29, 2026
commit 5dce5de83bfc2c1d6ff3e942d43b119449a9f512

There are no files selected for viewing

10 changes: 10 additions & 0 deletions 10 deps/ncrypto/ncrypto.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8580,6 +8580,16 @@ const Digest Digest::Fetch(const char* name) {
return Digest();
}

const Digest Digest::Ref(const EVP_MD* md) {
#if NCRYPTO_USE_OPENSSL_PROVIDER
if (md != nullptr && EVP_MD_get0_provider(md) != nullptr) {
if (EVP_MD_up_ref(const_cast<EVP_MD*>(md)) != 1) return Digest();
return Digest(DeleteFnPtr<EVP_MD, EVP_MD_free>(const_cast<EVP_MD*>(md)));
}
#endif
return Digest(md);
}

// ============================================================================
// KEM Implementation
#if OPENSSL_WITH_KEM
Expand Down
3 changes: 3 additions & 0 deletions 3 deps/ncrypto/ncrypto.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ class Digest final {

static const Digest FromName(const char* name);
static const Digest Fetch(const char* name);
// Returns a Digest that holds its own reference to md if md was fetched from
// a provider.
static const Digest Ref(const EVP_MD* md);

private:
const EVP_MD* md_ = nullptr;
Expand Down
19 changes: 19 additions & 0 deletions 19 src/crypto/crypto_hash.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ const EVP_MD* GetDigestImplementation(
#endif
}

const ncrypto::Digest GetDigest(Environment* env, const char* name) {
#if NCRYPTO_USE_OPENSSL_PROVIDER
MaybeCachedMD result = FetchAndMaybeCacheMD(env, name);
if (result.cached_md != nullptr) {
return ncrypto::Digest::Ref(result.cached_md);
}
return result.digest;
#elif NCRYPTO_USE_BORINGSSL
return ncrypto::Digest::FromName(name);
#endif
}

const ncrypto::Digest GetDigestForJob(Environment* env,
CryptoJobMode mode,
const char* name) {
if (mode == kCryptoJobSync) return GetDigest(env, name);
return ncrypto::Digest::FromName(name);
}

void MarkInvalidXofLength() {
#if NCRYPTO_USE_OPENSSL_PROVIDER
ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
Expand Down
12 changes: 12 additions & 0 deletions 12 src/crypto/crypto_hash.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

namespace node {
namespace crypto {
// Like ncrypto::Digest::FromName(), but caches provider implementations per
// Environment so that later lookups do not have to fetch them again.
const ncrypto::Digest GetDigest(Environment* env, const char* name);

// GetDigest() for synchronous jobs. Jobs on the thread pool keep using
// ncrypto::Digest::FromName(): there, the cache lowered Web Crypto digest and
// HMAC throughput, because workers that finish sooner have to be woken up for
// each new job more often.
const ncrypto::Digest GetDigestForJob(Environment* env,
CryptoJobMode mode,
const char* name);

class Hash final : public BaseObject {
public:
static void Initialize(Environment* env, v8::Local<v8::Object> target);
Expand Down
4 changes: 2 additions & 2 deletions 4 src/crypto/crypto_hkdf.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "crypto/crypto_hkdf.h"
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "crypto/crypto_hash.h"
#include "crypto/crypto_keys.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
Expand All @@ -9,7 +10,6 @@

namespace node {

using ncrypto::Digest;
using v8::FunctionCallbackInfo;
using v8::JustVoid;
using v8::Maybe;
Expand Down Expand Up @@ -54,7 +54,7 @@ Maybe<void> HKDFTraits::AdditionalConfig(
CHECK(args[offset + 4]->IsUint32()); // Length

Utf8Value hash(env->isolate(), args[offset]);
params->digest = Digest::FromName(*hash);
params->digest = GetDigestForJob(env, mode, *hash);
if (!params->digest) [[unlikely]] {
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", hash);
return Nothing<void>();
Expand Down
3 changes: 2 additions & 1 deletion 3 src/crypto/crypto_hmac.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "crypto/crypto_hmac.h"
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "crypto/crypto_hash.h"
#include "crypto/crypto_keys.h"
#include "crypto/crypto_sig.h"
#include "crypto/crypto_util.h"
Expand Down Expand Up @@ -69,7 +70,7 @@ void Hmac::New(const FunctionCallbackInfo<Value>& args) {
void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) {
HandleScope scope(env()->isolate());

Digest md = Digest::FromName(hash_type);
Digest md = GetDigest(env(), hash_type);
if (!md) [[unlikely]] {
return THROW_ERR_CRYPTO_INVALID_DIGEST(
env(), "Invalid digest: %s", hash_type);
Expand Down
4 changes: 2 additions & 2 deletions 4 src/crypto/crypto_pbkdf2.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "crypto/crypto_pbkdf2.h"
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "crypto/crypto_hash.h"
#include "crypto/crypto_keys.h"
#include "crypto/crypto_util.h"
#include "env-inl.h"
Expand All @@ -11,7 +12,6 @@

namespace node {

using ncrypto::Digest;
using v8::FunctionCallbackInfo;
using v8::Int32;
using v8::JustVoid;
Expand Down Expand Up @@ -104,7 +104,7 @@ Maybe<void> PBKDF2Traits::AdditionalConfig(
}

Utf8Value name(args.GetIsolate(), args[offset + 4]);
params->digest = Digest::FromName(*name);
params->digest = GetDigestForJob(env, mode, *name);
if (!params->digest) [[unlikely]] {
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", name);
return Nothing<void>();
Expand Down
5 changes: 3 additions & 2 deletions 5 src/crypto/crypto_sig.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "crypto/crypto_ec.h"
#include "crypto/crypto_hash.h"
#include "crypto/crypto_keys.h"
#include "crypto/crypto_util.h"
#include "env-inl.h"
Expand Down Expand Up @@ -315,7 +316,7 @@ bool VerifyPrehashed(const EVPKeyPointer& key,

SignBase::Error SignBase::Init(const char* digest) {
CHECK_NULL(mdctx_);
auto md = Digest::FromName(digest);
auto md = GetDigest(env(), digest);
if (!md) [[unlikely]]
return Error::UnknownDigest;

Expand Down Expand Up @@ -666,7 +667,7 @@ Maybe<void> SignTraits::AdditionalConfig(

if (args[offset + 7]->IsString()) {
Utf8Value digest(env->isolate(), args[offset + 7]);
params->digest = Digest::FromName(*digest);
params->digest = GetDigestForJob(env, mode, *digest);
if (!params->digest) [[unlikely]] {
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", digest);
return Nothing<void>();
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.