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
Prev Previous commit
Next Next commit
crypto: reuse HMAC contexts in createHmac()
Every HMAC context was created with EVP_MAC_fetch() and keyed with the
digest passed by name, so the provider fetched the digest again on
each init. Both lookups go through OpenSSL's method store.

Keep an HMAC context without a key for each digest in the
per-Environment digest cache, and duplicate it in createHmac(). A
duplicated context already has its digest, so keying it with the same
digest no longer fetches anything.

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 13537916a1545856a917a511eb123d573574f4db

There are no files selected for viewing

59 changes: 59 additions & 0 deletions 59 deps/ncrypto/ncrypto.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5216,16 +5216,39 @@ DigestCache::Result DigestCache::insert(const char* name,
#endif
}

DigestCache::~DigestCache() = default;

void DigestCache::reset(uint64_t generation) {
#if NCRYPTO_USE_OPENSSL_PROVIDER
if (generation_ == generation) return;
aliases_.clear();
hmac_templates_.clear();
digests_.clear();
first_id_ = next_id_;
#endif
generation_ = generation;
}

#if NCRYPTO_USE_OPENSSL_PROVIDER
const HMACCtxPointer* DigestCache::getHmacTemplate(const EVP_MD* digest,
uint64_t generation) {
if (generation_ != generation || digest == nullptr) return nullptr;
for (size_t index = 0; index < digests_.size(); index++) {
if (digests_[index].get() != digest) continue;
if (hmac_templates_.size() <= index) {
hmac_templates_.resize(digests_.size());
}
if (!hmac_templates_[index]) {
auto ctx = HMACCtxPointer::New(Digest::Ref(digest));
if (!ctx) return nullptr;
hmac_templates_[index] = std::make_unique<HMACCtxPointer>(std::move(ctx));
}
return hmac_templates_[index].get();
}
return nullptr;
}
#endif

const DigestCache::AliasMap& DigestCache::aliases() const {
#if NCRYPTO_USE_OPENSSL_PROVIDER
return aliases_;
Expand Down Expand Up @@ -8232,15 +8255,19 @@ HMACCtxPointer::HMACCtxPointer(EVPMacPointer&& mac, EVPMacCtxPointer&& ctx)
HMACCtxPointer::HMACCtxPointer(HMACCtxPointer&& other) noexcept
: mac_(std::move(other.mac_)),
ctx_(std::move(other.ctx_)),
md_(other.md_),
md_size_(other.md_size_) {
other.md_ = nullptr;
other.md_size_ = 0;
}

HMACCtxPointer& HMACCtxPointer::operator=(HMACCtxPointer&& other) noexcept {
if (this == &other) return *this;
mac_ = std::move(other.mac_);
ctx_ = std::move(other.ctx_);
md_ = other.md_;
md_size_ = other.md_size_;
other.md_ = nullptr;
other.md_size_ = 0;
return *this;
}
Expand All @@ -8252,11 +8279,13 @@ HMACCtxPointer::~HMACCtxPointer() {
void HMACCtxPointer::reset() {
ctx_.reset();
mac_.reset();
md_ = nullptr;
md_size_ = 0;
}

bool HMACCtxPointer::init(const Buffer<const void>& buf, const Digest& md) {
if (!ctx_ || !md) return false;
if (md.get() == md_) return ctx_.init(buf);

const char* md_name = EVP_MD_get0_name(md);
if (md_name == nullptr) return false;
Expand All @@ -8268,6 +8297,7 @@ bool HMACCtxPointer::init(const Buffer<const void>& buf, const Digest& md) {
};

if (!ctx_.init(buf, params)) return false;
md_ = md.get();
md_size_ = md.size();
return true;
}
Expand Down Expand Up @@ -8299,6 +8329,16 @@ bool HMACCtxPointer::digestInto(Buffer<void>* buf) {
return true;
}

HMACCtxPointer HMACCtxPointer::clone() const {
if (!ctx_) return {};
EVPMacCtxPointer ctx(EVP_MAC_CTX_dup(ctx_.get()));
if (!ctx) return {};
HMACCtxPointer result(EVPMacPointer(), std::move(ctx));
result.md_ = md_;
result.md_size_ = md_size_;
return result;
}

HMACCtxPointer HMACCtxPointer::New() {
auto mac = EVPMacPointer::Fetch(OSSL_MAC_NAME_HMAC);
if (!mac) return {};
Expand All @@ -8308,6 +8348,25 @@ HMACCtxPointer HMACCtxPointer::New() {

return HMACCtxPointer(std::move(mac), std::move(ctx));
}

HMACCtxPointer HMACCtxPointer::New(const Digest& md) {
if (!md) return {};
const char* md_name = EVP_MD_get0_name(md);
if (md_name == nullptr) return {};

auto result = New();
if (!result) return {};

OSSL_PARAM params[] = {
OSSL_PARAM_construct_utf8_string(
OSSL_MAC_PARAM_DIGEST, const_cast<char*>(md_name), 0),
OSSL_PARAM_construct_end(),
};
if (EVP_MAC_CTX_set_params(result.ctx_.get(), params) != 1) return {};
result.md_ = md.get();
result.md_size_ = md.size();
return result;
}
#endif // OPENSSL_WITH_EVP_MAC

DataPointer hashDigest(const Buffer<const unsigned char>& buf,
Expand Down
14 changes: 14 additions & 0 deletions 14 deps/ncrypto/ncrypto.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class DataPointer;
class DHPointer;
class ECKeyPointer;
class EVPKeyPointer;
class HMACCtxPointer;
class KeyAlgorithm;
class MacCache;
class EVPMacCtxPointer;
Expand Down Expand Up @@ -392,6 +393,7 @@ class DigestCache final {
CaseInsensitiveNameEqual>;

DigestCache() = default;
~DigestCache();
NCRYPTO_DISALLOW_COPY_AND_MOVE(DigestCache)

Result lookup(const char* name, uint64_t generation) const;
Expand All @@ -412,6 +414,12 @@ class DigestCache final {
Result insert(const char* name, const EVP_MD* digest, uint64_t generation);
void reset(uint64_t generation);
const AliasMap& aliases() const;
#if NCRYPTO_USE_OPENSSL_PROVIDER
// Returns an HMAC context for the cached digest that has no key yet.
// Duplicating it avoids fetching HMAC and the digest for every context.
const HMACCtxPointer* getHmacTemplate(const EVP_MD* digest,
uint64_t generation);
#endif

private:
uint64_t generation_ = 0;
Expand All @@ -423,6 +431,7 @@ class DigestCache final {
uint32_t first_id_ = 0;
uint32_t next_id_ = 0;
std::vector<EVPMDPointer> digests_;
std::vector<std::unique_ptr<HMACCtxPointer>> hmac_templates_;
AliasMap aliases_;
#endif
};
Expand Down Expand Up @@ -2087,14 +2096,19 @@ class HMACCtxPointer final {
bool update(const Buffer<const void>& buf);
DataPointer digest();
bool digestInto(Buffer<void>* buf);
HMACCtxPointer clone() const;

static HMACCtxPointer New();
// Creates a context that already uses md, so that init() does not have to
// fetch md again when it is called with the same digest.
static HMACCtxPointer New(const Digest& md);

private:
HMACCtxPointer(EVPMacPointer&& mac, EVPMacCtxPointer&& ctx);

EVPMacPointer mac_;
EVPMacCtxPointer ctx_;
const EVP_MD* md_ = nullptr;
size_t md_size_ = 0;
};
#endif // OPENSSL_WITH_EVP_MAC
Expand Down
12 changes: 12 additions & 0 deletions 12 src/crypto/crypto_hash.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,18 @@ const ncrypto::Digest GetDigestForJob(Environment* env,
return ncrypto::Digest::FromName(name);
}

ncrypto::HMACCtxPointer NewHmacContext(Environment* env,
const ncrypto::Digest& md) {
#if NCRYPTO_USE_OPENSSL_PROVIDER
SynchronizeHashCache(env);
if (const auto* hmac_template = env->provider_digest_cache->getHmacTemplate(
md, env->hash_cache_generation)) {
if (auto ctx = hmac_template->clone()) return ctx;
}
#endif
return ncrypto::HMACCtxPointer::New();
}

void MarkInvalidXofLength() {
#if NCRYPTO_USE_OPENSSL_PROVIDER
ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
Expand Down
6 changes: 6 additions & 0 deletions 6 src/crypto/crypto_hash.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ const ncrypto::Digest GetDigestForJob(Environment* env,
CryptoJobMode mode,
const char* name);

// Returns an HMAC context to be keyed with md. Contexts for digests from
// GetDigest() are duplicated from a cached template, so keying them does not
// fetch HMAC or md again.
ncrypto::HMACCtxPointer NewHmacContext(Environment* env,
const ncrypto::Digest& md);

class Hash final : public BaseObject {
public:
static void Initialize(Environment* env, v8::Local<v8::Object> target);
Expand Down
2 changes: 1 addition & 1 deletion 2 src/crypto/crypto_hmac.cc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) {
key = "";
}

ctx_ = HMACCtxPointer::New();
ctx_ = NewHmacContext(env(), md);
ncrypto::Buffer<const void> key_buf{
.data = key,
.len = static_cast<size_t>(key_len),
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.