Skip to content

Commit 9121206

Browse files
jasnelladuh95
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 8dc536f commit 9121206

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
@@ -709,13 +709,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
709709
}
710710
}
711711

712-
bool Hash::HashInit(const EVP_MD* digest, Maybe<unsigned int> xof_md_len) {
713-
mdctx_ = EVPMDCtxPointer::New();
714-
if (!mdctx_.digestInit(digest)) [[unlikely]] {
715-
mdctx_.reset();
716-
return false;
717-
}
718-
712+
bool Hash::FinishHashInit(const EVP_MD* digest,
713+
Maybe<unsigned int> xof_md_len) {
719714
md_len_ = mdctx_.getDigestSize();
720715
if (mdctx_.hasXofFlag() && !xof_md_len.IsJust() &&
721716
ShouldRejectMissingXofLength(digest, md_len_)) {
@@ -738,35 +733,26 @@ bool Hash::HashInit(const EVP_MD* digest, Maybe<unsigned int> xof_md_len) {
738733
return true;
739734
}
740735

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

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

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

772758
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)