Commit 65f22467359 for nodejs

commit 65f22467359441135b0d3b295f400c3a863b5a9d
Author: Filip Skokan <panva.ip@gmail.com>
Date:   Sun Nov 15 08:58:10 2026 +0100

    crypto: wire up SHAKE256 with BoringSSL

    Add SHAKE256 and SHAKE-256 to name lookup and getHashes(). BoringSSL
    exposes EVP_shake256() but omits it from its digest registry and does
    not set EVP_MD_FLAG_XOF.

    Enable uncustomized cSHAKE256 and the hybrid ML-KEM algorithms that
    require SHAKE256. Release consumed XOF contexts after stream flushes to
    reject update() and copy() while retaining cached digest retrieval.

    Signed-off-by: Filip Skokan <panva.ip@gmail.com>
    Assisted-by: Codex
    PR-URL: https://github.com/nodejs/node/pull/67483
    Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
    Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>

diff --git a/deps/ncrypto/ncrypto.cc b/deps/ncrypto/ncrypto.cc
index b92619097a6..322e16279ad 100644
--- a/deps/ncrypto/ncrypto.cc
+++ b/deps/ncrypto/ncrypto.cc
@@ -3107,11 +3107,15 @@ bool KDF::derive(const Buffer<unsigned char>& out,

 const EVP_MD* getDigestByName(const char* name) {
 #if NCRYPTO_USE_BORINGSSL
-  // BoringSSL exposes SHA-3 through EVP_MD descriptors without registering
-  // their names or assigning them NIDs.
+  // BoringSSL exposes SHA-3 and SHAKE256 through EVP_MD descriptors without
+  // registering their names or assigning them NIDs.
   if (CaseInsensitiveNameEqual()(name, "sha3-256")) return EVP_sha3_256();
   if (CaseInsensitiveNameEqual()(name, "sha3-384")) return EVP_sha3_384();
   if (CaseInsensitiveNameEqual()(name, "sha3-512")) return EVP_sha3_512();
+  if (CaseInsensitiveNameEqual()(name, "shake256") ||
+      CaseInsensitiveNameEqual()(name, "shake-256")) {
+    return EVP_shake256();
+  }
 #endif
   return EVP_get_digestbyname(name);
 }
@@ -8277,8 +8281,7 @@ const EVP_MD* EVPMDCtxPointer::getDigest() const {
 }

 bool EVPMDCtxPointer::hasXofFlag() const {
-  if (!ctx_) return false;
-  return (EVP_MD_flags(getDigest()) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF;
+  return Digest(getDigest()).isXof();
 }

 bool EVPMDCtxPointer::copyTo(const EVPMDCtxPointer& other) const {
@@ -9054,6 +9057,18 @@ size_t Digest::size() const {
   return EVP_MD_size(md_);
 }

+bool Digest::isXof() const {
+  if (md_ == nullptr) return false;
+#if NCRYPTO_USE_OPENSSL_PROVIDER
+  return (EVP_MD_flags(md_) & EVP_MD_FLAG_XOF) != 0;
+#elif defined(OPENSSL_IS_BORINGSSL)
+  // BoringSSL does not set EVP_MD_FLAG_XOF on its SHAKE256 descriptor.
+  return md_ == EVP_shake256();
+#else
+#error "Unsupported crypto backend"
+#endif
+}
+
 #if !NCRYPTO_USE_BORINGSSL
 Digest::Digest(DeleteFnPtr<EVP_MD, EVP_MD_free> md)
     : md_(md.get()), fetched_md_(std::move(md)) {}
diff --git a/deps/ncrypto/ncrypto.h b/deps/ncrypto/ncrypto.h
index b6fc4a10f88..3e2ec991882 100644
--- a/deps/ncrypto/ncrypto.h
+++ b/deps/ncrypto/ncrypto.h
@@ -309,6 +309,7 @@ class Digest final {
   NCRYPTO_DISALLOW_MOVE(Digest)

   size_t size() const;
+  bool isXof() const;

   inline const EVP_MD* get() const { return md_; }
   inline operator const EVP_MD*() const { return md_; }
diff --git a/src/crypto/crypto_hash.cc b/src/crypto/crypto_hash.cc
index b8936263053..e2c30eb7222 100644
--- a/src/crypto/crypto_hash.cc
+++ b/src/crypto/crypto_hash.cc
@@ -261,8 +261,10 @@ const std::vector<std::string>& GetSupportedHashAlgorithms(Environment* env) {
 #error "Unsupported crypto backend"
 #endif
 #if NCRYPTO_USE_BORINGSSL
-      // SHA-3 descriptors are not included in BoringSSL's name registry.
-      for (const char* name : {"sha3-256", "sha3-384", "sha3-512"}) {
+      // SHA-3 and SHAKE256 descriptors are absent from BoringSSL's name
+      // registry.
+      for (const char* name :
+           {"sha3-256", "sha3-384", "sha3-512", "shake256", "shake-256"}) {
         env->supported_hash_algorithms.emplace_back(name);
       }
 #endif
@@ -417,9 +419,7 @@ bool IsShakeDigest(const EVP_MD* md) {
 #if NCRYPTO_USE_OPENSSL_PROVIDER
   return EVP_MD_is_a(md, "SHAKE128") || EVP_MD_is_a(md, "SHAKE256");
 #elif NCRYPTO_USE_BORINGSSL
-  const char* name = OBJ_nid2sn(EVP_MD_type(md));
-  return name != nullptr &&
-         (strcmp(name, "SHAKE128") == 0 || strcmp(name, "SHAKE256") == 0);
+  return md == EVP_shake256();
 #else
 #error "Unsupported crypto backend"
 #endif
@@ -451,7 +451,7 @@ void OneShotDigestWithMD(Environment* env,

   enum encoding output_enc = ParseEncoding(isolate, args[4], args[5], HEX);

-  bool is_xof = (EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0;
+  const bool is_xof = ncrypto::Digest(md).isXof();
   OPENSSL_SIZE_T output_length = EVP_MD_size(md);

   // This is to cause hash() to fail when an incorrect
@@ -792,6 +792,10 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {

   unsigned int len = hash->md_len_;

+#if NCRYPTO_USE_BORINGSSL
+  const bool is_xof = hash->mdctx_.hasXofFlag();
+#endif
+
   // TODO(tniessen): SHA3_squeeze does not work for zero-length outputs on all
   // platforms and will cause a segmentation fault if called. This workaround
   // causes hash.digest() to correctly return an empty buffer / string.
@@ -812,6 +816,12 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
     hash->digest_ = ByteSource::Allocated(data.release());
   }

+#if NCRYPTO_USE_BORINGSSL
+  // BoringSSL consumes XOF contexts during finalization. Release the context
+  // so update() and copy() after a stream flush cannot reuse that state.
+  if (is_xof) hash->mdctx_.reset();
+#endif
+
   Local<Value> ret;
   if (StringBytes::Encode(
           env->isolate(), hash->digest_.data<char>(), len, encoding)
@@ -882,8 +892,7 @@ Maybe<void> HashTraits::AdditionalConfig(
         static_cast<uint32_t>(args[offset + 2].As<Uint32>()->Value()) /
         CHAR_BIT;
     if (params->length != expected) {
-      if ((EVP_MD_flags(params->digest.get()) & EVP_MD_FLAG_XOF) == 0)
-          [[unlikely]] {
+      if (!params->digest.isXof()) [[unlikely]] {
         THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Digest method not supported");
         return Nothing<void>();
       }
diff --git a/test/parallel/test-crypto-shake256-stream.js b/test/parallel/test-crypto-shake256-stream.js
new file mode 100644
index 00000000000..66a7ede5040
--- /dev/null
+++ b/test/parallel/test-crypto-shake256-stream.js
@@ -0,0 +1,34 @@
+'use strict';
+
+const common = require('../common');
+if (!common.hasCrypto)
+  common.skip('missing crypto');
+
+const assert = require('assert');
+const { createHash } = require('crypto');
+const { isBoringSSL } = require('../common/crypto');
+
+const expected =
+  '483366601360a8771c6863080cc4114d8db44530f8f1e1ee4f94ea37e78b5739' +
+  'd5a15bef186a5386c75744c0527e1faa9f8726e462a12a4feb06bd8801e751e4';
+
+for (const outputLength of [32, 64]) {
+  const stream = createHash('shake256', { outputLength });
+  stream.end('abc');
+  const digest = expected.slice(0, outputLength * 2);
+  assert.strictEqual(stream.read().toString('hex'), digest);
+
+  // A stream flush caches the digest without setting Hash's JavaScript
+  // finalized state. Reusing BoringSSL's cleared context used to hang or crash.
+  if (isBoringSSL) {
+    assert.throws(() => stream.update('def'), {
+      code: 'ERR_CRYPTO_HASH_UPDATE_FAILED',
+    });
+    assert.throws(() => stream.copy({ outputLength }), {
+      name: 'Error',
+      message: 'Digest method not supported',
+    });
+  }
+
+  assert.strictEqual(stream.digest('hex'), digest);
+}
diff --git a/test/parallel/test-webcrypto-kem-hybrids.js b/test/parallel/test-webcrypto-kem-hybrids.js
index 0d1ec410c16..8738f27c4fb 100644
--- a/test/parallel/test-webcrypto-kem-hybrids.js
+++ b/test/parallel/test-webcrypto-kem-hybrids.js
@@ -5,10 +5,10 @@ const common = require('../common');
 if (!common.hasCrypto)
   common.skip('missing crypto');

-const { hasFIPS, hasOpenSSL } = require('../common/crypto');
+const { hasFIPS, hasOpenSSL, isBoringSSL } = require('../common/crypto');

-if (!hasOpenSSL(3, 5))
-  common.skip('requires OpenSSL >= 3.5');
+if (!hasOpenSSL(3, 5) && !isBoringSSL)
+  common.skip('requires OpenSSL >= 3.5 or BoringSSL');

 const assert = require('assert');
 const { once } = require('events');
diff --git a/test/wpt/status/WebCryptoAPI.cjs b/test/wpt/status/WebCryptoAPI.cjs
index be5ee9c9e54..f11d2888c82 100644
--- a/test/wpt/status/WebCryptoAPI.cjs
+++ b/test/wpt/status/WebCryptoAPI.cjs
@@ -80,32 +80,28 @@ if (isBoringSSL) {
   skip(
     'secure-curves/derive_bits_keys/cfrg_curves_bits_curve448.https.any.js',
     'secure-curves/derive_bits_keys/cfrg_curves_keys_curve448.https.any.js',
-    'modern-algos/digest/cshake.https.any.js',
     'secure-curves/generateKey/failures_Ed448.https.any.js',
-    'modern-algos/generateKey/failures_Hybrid-KEM.https.any.js',
     'secure-curves/generateKey/failures_X448.https.any.js',
     'secure-curves/generateKey/successes_Ed448.https.any.js',
-    'modern-algos/generateKey/successes_Hybrid-KEM.https.any.js',
     'secure-curves/generateKey/successes_X448.https.any.js',
-    'modern-algos/import_export/Hybrid-KEM_importKey.https.any.js',
     'secure-curves/import_export/okp_importKey_Ed448.https.any.js',
     'secure-curves/import_export/okp_importKey_failures_Ed448.https.any.js',
     'secure-curves/import_export/okp_importKey_failures_X448.https.any.js',
     'secure-curves/import_export/okp_importKey_X448.https.any.js',
     'secure-curves/serialization/ed448.https.any.js',
-    'modern-algos/serialization/hybridkem.https.window.js',
     'secure-curves/serialization/x448.https.any.js',
     'secure-curves/sign_verify/eddsa_curve448.https.any.js');

   skipSubtests(
-    ['modern-algos/encap_decap/encap_decap_bits.https.any.js', /(?:ml-kem-512|mlkem)/i],
-    ['modern-algos/encap_decap/encap_decap_keys.https.any.js', /(?:ml-kem-512|mlkem)/i],
+    ['modern-algos/digest/cshake.https.any.js', /cSHAKE128/i],
+    ['modern-algos/encap_decap/encap_decap_bits.https.any.js', /ml-kem-512/i],
+    ['modern-algos/encap_decap/encap_decap_keys.https.any.js', /ml-kem-512/i],
     ['modern-algos/generateKey/failures_ML-KEM.https.any.js', /ml-kem-512/i],
     ['modern-algos/generateKey/successes_ML-KEM.https.any.js', /ml-kem-512/i],
-    ['modern-algos/getPublicKey.https.any.js', /(?:ed448|x448|ml-kem-512|mlkem)/i],
+    ['modern-algos/getPublicKey.https.any.js', /(?:ed448|x448|ml-kem-512)/i],
     ['modern-algos/import_export/ML-KEM_importKey.https.any.js', /ml-kem-512/i],
     ['modern-algos/serialization/mlkem.https.any.js', /ml-kem-512/i],
-    ['modern-algos/supports-modern.https.any.js', /(?:ml-kem-512|mlkem)/i]);
+    ['modern-algos/supports-modern.https.any.js', /ml-kem-512/i]);
 }

 if (hasFIPS(3)) {