From 8ddfb9cbfd08cb5f26abace643feff081ec8e598 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 15 Nov 2026 18:08:15 +0000 Subject: [PATCH] crypto: share digest length handling between HashInit overloads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two Hash::HashInit() overloads duplicated the logic that determines the digest length and validates the requested XOF output length after the digest context is initialized. Move it into a shared FinishHashInit() helper. Assisted-by: Claude Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/67495 Reviewed-By: Guilherme Araújo Reviewed-By: Filip Skokan Reviewed-By: Robert Nagy Reviewed-By: Ethan Arrowood --- src/crypto/crypto_hash.cc | 40 +++++++++++++-------------------------- src/crypto/crypto_hash.h | 5 +++++ 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc index b0ea75e88b2..77b7e7d4169 100644 --- a/src/crypto/crypto_hash.cc +++ b/src/crypto/crypto_hash.cc @@ -709,13 +709,8 @@ void Hash::New(const FunctionCallbackInfo& args) { } } -bool Hash::HashInit(const EVP_MD* digest, Maybe xof_md_len) { - mdctx_ = EVPMDCtxPointer::New(); - if (!mdctx_.digestInit(digest)) [[unlikely]] { - mdctx_.reset(); - return false; - } - +bool Hash::FinishHashInit(const EVP_MD* digest, + Maybe xof_md_len) { md_len_ = mdctx_.getDigestSize(); if (mdctx_.hasXofFlag() && !xof_md_len.IsJust() && ShouldRejectMissingXofLength(digest, md_len_)) { @@ -738,6 +733,16 @@ bool Hash::HashInit(const EVP_MD* digest, Maybe xof_md_len) { return true; } +bool Hash::HashInit(const EVP_MD* digest, Maybe xof_md_len) { + mdctx_ = EVPMDCtxPointer::New(); + if (!mdctx_.digestInit(digest)) [[unlikely]] { + mdctx_.reset(); + return false; + } + + return FinishHashInit(digest, xof_md_len); +} + bool Hash::HashInit(const EVP_MD* digest, const CShakeOptions& options, Maybe xof_md_len) { @@ -747,26 +752,7 @@ bool Hash::HashInit(const EVP_MD* digest, return false; } - md_len_ = mdctx_.getDigestSize(); - if (mdctx_.hasXofFlag() && !xof_md_len.IsJust() && - ShouldRejectMissingXofLength(digest, md_len_)) { - MarkInvalidXofLength(); - mdctx_.reset(); - return false; - } - - if (xof_md_len.IsJust() && xof_md_len.FromJust() != md_len_) { - // This is a little hack to cause createHash to fail when an incorrect - // hashSize option was passed for a non-XOF hash function. - if (!mdctx_.hasXofFlag()) [[unlikely]] { - MarkInvalidXofLength(); - mdctx_.reset(); - return false; - } - md_len_ = xof_md_len.FromJust(); - } - - return true; + return FinishHashInit(digest, xof_md_len); } bool Hash::HashUpdate(const char* data, size_t len) { diff --git a/src/crypto/crypto_hash.h b/src/crypto/crypto_hash.h index 322ff83da69..558c67822f4 100644 --- a/src/crypto/crypto_hash.h +++ b/src/crypto/crypto_hash.h @@ -57,6 +57,11 @@ class Hash final : public BaseObject { Hash(Environment* env, v8::Local wrap); private: + // Determines the digest length after mdctx_ has been initialized, applying + // and validating the requested XOF output length. Resets mdctx_ and returns + // false on failure. + bool FinishHashInit(const EVP_MD* digest, v8::Maybe xof_md_len); + ncrypto::EVPMDCtxPointer mdctx_{}; unsigned int md_len_ = 0; ByteSource digest_;