Skip to content

Commit 430e4af

Browse files
jasnellnodejs-github-bot
authored andcommitted
crypto: share digest length handling between HashInit overloads
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 <jasnell@gmail.com> PR-URL: #67495 Reviewed-By: Guilherme Araújo <arauujogui@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent 58fc230 commit 430e4af

2 files changed

Lines changed: 18 additions & 27 deletions

File tree

‎src/crypto/crypto_hash.cc‎

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
711711
}
712712
}
713713

714-
bool Hash::HashInit(const EVP_MD* digest, Maybe<unsigned int> xof_md_len) {
715-
mdctx_ = EVPMDCtxPointer::New();
716-
if (!mdctx_.digestInit(digest)) [[unlikely]] {
717-
mdctx_.reset();
718-
return false;
719-
}
720-
714+
bool Hash::FinishHashInit(const EVP_MD* digest,
715+
Maybe<unsigned int> xof_md_len) {
721716
md_len_ = mdctx_.getDigestSize();
722717
if (mdctx_.hasXofFlag() && !xof_md_len.IsJust() &&
723718
ShouldRejectMissingXofLength(digest, md_len_)) {
@@ -740,35 +735,26 @@ bool Hash::HashInit(const EVP_MD* digest, Maybe<unsigned int> xof_md_len) {
740735
return true;
741736
}
742737

743-
bool Hash::HashInit(const EVP_MD* digest,
744-
const CShakeOptions& options,
745-
Maybe<unsigned int> xof_md_len) {
738+
bool Hash::HashInit(const EVP_MD* digest, Maybe<unsigned int> xof_md_len) {
746739
mdctx_ = EVPMDCtxPointer::New();
747-
if (!options.Initialize(&mdctx_, digest)) [[unlikely]] {
740+
if (!mdctx_.digestInit(digest)) [[unlikely]] {
748741
mdctx_.reset();
749742
return false;
750743
}
751744

752-
md_len_ = mdctx_.getDigestSize();
753-
if (mdctx_.hasXofFlag() && !xof_md_len.IsJust() &&
754-
ShouldRejectMissingXofLength(digest, md_len_)) {
755-
MarkInvalidXofLength();
745+
return FinishHashInit(digest, xof_md_len);
746+
}
747+
748+
bool Hash::HashInit(const EVP_MD* digest,
749+
const CShakeOptions& options,
750+
Maybe<unsigned int> xof_md_len) {
751+
mdctx_ = EVPMDCtxPointer::New();
752+
if (!options.Initialize(&mdctx_, digest)) [[unlikely]] {
756753
mdctx_.reset();
757754
return false;
758755
}
759756

760-
if (xof_md_len.IsJust() && xof_md_len.FromJust() != md_len_) {
761-
// This is a little hack to cause createHash to fail when an incorrect
762-
// hashSize option was passed for a non-XOF hash function.
763-
if (!mdctx_.hasXofFlag()) [[unlikely]] {
764-
MarkInvalidXofLength();
765-
mdctx_.reset();
766-
return false;
767-
}
768-
md_len_ = xof_md_len.FromJust();
769-
}
770-
771-
return true;
757+
return FinishHashInit(digest, xof_md_len);
772758
}
773759

774760
bool Hash::HashUpdate(const char* data, size_t len) {

‎src/crypto/crypto_hash.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class Hash final : public BaseObject {
5757
Hash(Environment* env, v8::Local<v8::Object> wrap);
5858

5959
private:
60+
// Determines the digest length after mdctx_ has been initialized, applying
61+
// and validating the requested XOF output length. Resets mdctx_ and returns
62+
// false on failure.
63+
bool FinishHashInit(const EVP_MD* digest, v8::Maybe<unsigned int> xof_md_len);
64+
6065
ncrypto::EVPMDCtxPointer mdctx_{};
6166
unsigned int md_len_ = 0;
6267
ByteSource digest_;

0 commit comments

Comments
 (0)