GeezerVison is showing github.com — crypto: wire up SHAKE256 and SLH-DSA with BoringSSL by panva · Pull Request #67483 · nodejs/node
crypto: wire up SHAKE256 and SLH-DSA with BoringSSL
#67483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.cc
(2)
.cjs
(1)
.h
(1)
.js
(2)
All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
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 <[email protected] >
Assisted-by: Codex
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
panva
committed
Nov 17, 2026
commit 78cd7954c1ad6c5d8629de165b94055ac0bf5abc
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -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);
}
Expand Down
Expand Up
@@ -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 {
Expand Down
Expand Up
@@ -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)) {}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -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_; }
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -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
Expand Down
Expand Up
@@ -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
Expand Down
Expand Up
@@ -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
Expand Down
Expand Up
@@ -806,6 +806,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.
Expand All
@@ -826,6 +830,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)
Expand Down
Expand Up
@@ -896,8 +906,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>();
}
Expand Down
Original file line number
Diff line number
Diff line change
@@ -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 ) ;
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -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' ) ;
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -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' , / (?: m l - k e m - 5 1 2 | m l k e m ) / i] ,
[ 'modern-algos/encap_decap/encap_decap_keys.https.any.js' , / (?: m l - k e m - 5 1 2 | m l k e m ) / i] ,
[ 'modern-algos/digest/cshake.https.any.js' , / c S H A K E 1 2 8 / i] ,
[ 'modern-algos/encap_decap/encap_decap_bits.https.any.js' , / m l - k e m - 5 1 2 / i] ,
[ 'modern-algos/encap_decap/encap_decap_keys.https.any.js' , / m l - k e m - 5 1 2 / i] ,
[ 'modern-algos/generateKey/failures_ML-KEM.https.any.js' , / m l - k e m - 5 1 2 / i] ,
[ 'modern-algos/generateKey/successes_ML-KEM.https.any.js' , / m l - k e m - 5 1 2 / i] ,
[ 'modern-algos/getPublicKey.https.any.js' , / (?: e d 4 4 8 | x 4 4 8 | m l - k e m - 5 1 2 | m l k e m ) / i] ,
[ 'modern-algos/getPublicKey.https.any.js' , / (?: e d 4 4 8 | x 4 4 8 | m l - k e m - 5 1 2 ) / i] ,
[ 'modern-algos/import_export/ML-KEM_importKey.https.any.js' , / m l - k e m - 5 1 2 / i] ,
[ 'modern-algos/serialization/mlkem.https.any.js' , / m l - k e m - 5 1 2 / i] ,
[ 'modern-algos/supports-modern.https.any.js' , / (?: m l - k e m - 5 1 2 | m l k e m ) / i] ) ;
[ 'modern-algos/supports-modern.https.any.js' , / m l - k e m - 5 1 2 / i] ) ;
}
if ( hasFIPS ( 3 ) ) {
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.