GeezerVison is showing github.com — crypto: cache digests for synchronous operations · nodejs/node@216f123
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.cc +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8942,6 +8942,16 @@ const Digest Digest::Fetch(const char* name) { | 8942 | 8942 | return Digest(); | 8943 | 8943 | } | 8944 | 8944 |
| | 8945 | +const Digest Digest::Ref(const EVP_MD* md) { | | 8946 | +#if NCRYPTO_USE_OPENSSL_PROVIDER | | 8947 | + if (md != nullptr && EVP_MD_get0_provider(md) != nullptr) { | | 8948 | + if (EVP_MD_up_ref(const_cast<EVP_MD*>(md)) != 1) return Digest(); | | 8949 | + return Digest(DeleteFnPtr<EVP_MD, EVP_MD_free>(const_cast<EVP_MD*>(md))); | | 8950 | + } | | 8951 | +#endif | | 8952 | + return Digest(md); | | 8953 | +} | | 8954 | + | 8945 | 8955 | // ============================================================================ | 8946 | 8956 | // KEM Implementation | 8947 | 8957 | #if OPENSSL_WITH_KEM | |
Collapse file Copy file name to clipboardExpand all lines: deps/ncrypto/ncrypto.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -368,6 +368,9 @@ class Digest final { | 368 | 368 |
| 369 | 369 | static const Digest FromName(const char* name); | 370 | 370 | static const Digest Fetch(const char* name); | | 371 | + // Returns a Digest that holds its own reference to md if md was fetched from | | 372 | + // a provider. | | 373 | + static const Digest Ref(const EVP_MD* md); | 371 | 374 |
| 372 | 375 | private: | 373 | 376 | const EVP_MD* md_ = nullptr; | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_hash.cc +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -363,6 +363,25 @@ const EVP_MD* GetDigestImplementation( | 363 | 363 | #endif | 364 | 364 | } | 365 | 365 |
| | 366 | +const ncrypto::Digest GetDigest(Environment* env, const char* name) { | | 367 | +#if NCRYPTO_USE_OPENSSL_PROVIDER | | 368 | + MaybeCachedMD result = FetchAndMaybeCacheMD(env, name); | | 369 | + if (result.cached_md != nullptr) { | | 370 | + return ncrypto::Digest::Ref(result.cached_md); | | 371 | + } | | 372 | + return result.digest; | | 373 | +#elif NCRYPTO_USE_BORINGSSL | | 374 | + return ncrypto::Digest::FromName(name); | | 375 | +#endif | | 376 | +} | | 377 | + | | 378 | +const ncrypto::Digest GetDigestForJob(Environment* env, | | 379 | + CryptoJobMode mode, | | 380 | + const char* name) { | | 381 | + if (mode == kCryptoJobSync) return GetDigest(env, name); | | 382 | + return ncrypto::Digest::FromName(name); | | 383 | +} | | 384 | + | 366 | 385 | void MarkInvalidXofLength() { | 367 | 386 | #if NCRYPTO_USE_OPENSSL_PROVIDER | 368 | 387 | ERR_raise(ERR_LIB_EVP, EVP_R_NOT_XOF_OR_INVALID_LENGTH); | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_hash.h +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 |
| 13 | 13 | namespace node { | 14 | 14 | namespace crypto { | | 15 | +// Like ncrypto::Digest::FromName(), but caches provider implementations per | | 16 | +// Environment so that later lookups do not have to fetch them again. | | 17 | +const ncrypto::Digest GetDigest(Environment* env, const char* name); | | 18 | + | | 19 | +// GetDigest() for synchronous jobs. Jobs on the thread pool keep using | | 20 | +// ncrypto::Digest::FromName(): there, the cache lowered Web Crypto digest and | | 21 | +// HMAC throughput, because workers that finish sooner have to be woken up for | | 22 | +// each new job more often. | | 23 | +const ncrypto::Digest GetDigestForJob(Environment* env, | | 24 | + CryptoJobMode mode, | | 25 | + const char* name); | | 26 | + | 15 | 27 | class Hash final : public BaseObject { | 16 | 28 | public: | 17 | 29 | static void Initialize(Environment* env, v8::Local<v8::Object> target); | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_hkdf.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "crypto/crypto_hkdf.h" | 2 | 2 | #include "async_wrap-inl.h" | 3 | 3 | #include "base_object-inl.h" | | 4 | +#include "crypto/crypto_hash.h" | 4 | 5 | #include "crypto/crypto_keys.h" | 5 | 6 | #include "env-inl.h" | 6 | 7 | #include "memory_tracker-inl.h" | | 9 | 10 |
| 10 | 11 | namespace node { | 11 | 12 |
| 12 | | -using ncrypto::Digest; | 13 | 13 | using v8::FunctionCallbackInfo; | 14 | 14 | using v8::JustVoid; | 15 | 15 | using v8::Maybe; | @@ -54,7 +54,7 @@ Maybe<void> HKDFTraits::AdditionalConfig( | 54 | 54 | CHECK(args[offset + 4]->IsUint32()); // Length | 55 | 55 |
| 56 | 56 | Utf8Value hash(env->isolate(), args[offset]); | 57 | | - params->digest = Digest::FromName(*hash); | | 57 | + params->digest = GetDigestForJob(env, mode, *hash); | 58 | 58 | if (!params->digest) [[unlikely]] { | 59 | 59 | THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", hash); | 60 | 60 | return Nothing<void>(); | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_hmac.cc +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "crypto/crypto_hmac.h" | 2 | 2 | #include "async_wrap-inl.h" | 3 | 3 | #include "base_object-inl.h" | | 4 | +#include "crypto/crypto_hash.h" | 4 | 5 | #include "crypto/crypto_keys.h" | 5 | 6 | #include "crypto/crypto_sig.h" | 6 | 7 | #include "crypto/crypto_util.h" | @@ -69,7 +70,7 @@ void Hmac::New(const FunctionCallbackInfo<Value>& args) { | 69 | 70 | void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) { | 70 | 71 | HandleScope scope(env()->isolate()); | 71 | 72 |
| 72 | | - Digest md = Digest::FromName(hash_type); | | 73 | + Digest md = GetDigest(env(), hash_type); | 73 | 74 | if (!md) [[unlikely]] { | 74 | 75 | return THROW_ERR_CRYPTO_INVALID_DIGEST( | 75 | 76 | env(), "Invalid digest: %s", hash_type); | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_pbkdf2.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "crypto/crypto_pbkdf2.h" | 2 | 2 | #include "async_wrap-inl.h" | 3 | 3 | #include "base_object-inl.h" | | 4 | +#include "crypto/crypto_hash.h" | 4 | 5 | #include "crypto/crypto_keys.h" | 5 | 6 | #include "crypto/crypto_util.h" | 6 | 7 | #include "env-inl.h" | | 11 | 12 |
| 12 | 13 | namespace node { | 13 | 14 |
| 14 | | -using ncrypto::Digest; | 15 | 15 | using v8::FunctionCallbackInfo; | 16 | 16 | using v8::Int32; | 17 | 17 | using v8::JustVoid; | @@ -104,7 +104,7 @@ Maybe<void> PBKDF2Traits::AdditionalConfig( | 104 | 104 | } | 105 | 105 |
| 106 | 106 | Utf8Value name(args.GetIsolate(), args[offset + 4]); | 107 | | - params->digest = Digest::FromName(*name); | | 107 | + params->digest = GetDigestForJob(env, mode, *name); | 108 | 108 | if (!params->digest) [[unlikely]] { | 109 | 109 | THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", name); | 110 | 110 | return Nothing<void>(); | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_sig.cc +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | #include "async_wrap-inl.h" | 3 | 3 | #include "base_object-inl.h" | 4 | 4 | #include "crypto/crypto_ec.h" | | 5 | +#include "crypto/crypto_hash.h" | 5 | 6 | #include "crypto/crypto_keys.h" | 6 | 7 | #include "crypto/crypto_util.h" | 7 | 8 | #include "env-inl.h" | @@ -315,7 +316,7 @@ bool VerifyPrehashed(const EVPKeyPointer& key, | 315 | 316 |
| 316 | 317 | SignBase::Error SignBase::Init(const char* digest) { | 317 | 318 | CHECK_NULL(mdctx_); | 318 | | - auto md = Digest::FromName(digest); | | 319 | + auto md = GetDigest(env(), digest); | 319 | 320 | if (!md) [[unlikely]] | 320 | 321 | return Error::UnknownDigest; | 321 | 322 |
| @@ -666,7 +667,7 @@ Maybe<void> SignTraits::AdditionalConfig( | 666 | 667 |
| 667 | 668 | if (args[offset + 7]->IsString()) { | 668 | 669 | Utf8Value digest(env->isolate(), args[offset + 7]); | 669 | | - params->digest = Digest::FromName(*digest); | | 670 | + params->digest = GetDigestForJob(env, mode, *digest); | 670 | 671 | if (!params->digest) [[unlikely]] { | 671 | 672 | THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", digest); | 672 | 673 | return Nothing<void>(); | |
|