GeezerVison is showing github.com — [v26.x backport] crypto: fix various edge cases by panva · Pull Request #66391 · nodejs/node
[v26.x backport] crypto: fix various edge cases
#66391
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.cc
(2)
.h
(2)
.js
(16)
.md
(1)
.mjs
(3)
.ts
(1)
All 6 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: use backend cSHAKE and KMAC
Require cSHAKE and KMAC output lengths and KMAC key lengths to be
multiples of 8 bits. KMAC keys must be at least 32 bits. Share these
restrictions between operations and supports.
Use OpenSSL's KMAC provider for all supported inputs and its cSHAKE
implementation for non-empty function names or customization strings.
Keep using SHAKE when both cSHAKE parameters are empty. Remove the
custom Keccak framing, partial-bit handling, and short-key fallback.
Document the OpenSSL 4.0 requirement for non-empty cSHAKE parameters and
reject customization strings containing null bytes. Keep the documented
512-byte customization limit and let backend failures reach callers.
Signed-off-by: Filip Skokan <[email protected] >
Assisted-by: Codex
PR-URL: #66237
Reviewed-By: James M Snell <[email protected] >
Reviewed-By: Aviv Keller <[email protected] >
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
panva
committed
Sep 29, 2026
commit b81beca53b67aaadb5a1032ee41b322f821e0caf
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -1830,6 +1830,9 @@ changes:
description: Renamed `cShakeParams.length` to `cShakeParams.outputLength`.
-->
When both `functionName` and `customization` are empty or `undefined`, cSHAKE is
equivalent to plain SHAKE.
#### `cShakeParams.name`
<!-- YAML
Expand All
@@ -1844,7 +1847,8 @@ added: v24.7.0
added: v25.9.0
-->
* Type: {number} represents the requested output length in bits.
* Type: {number} represents the requested output length in bits. Must be a
multiple of 8.
#### `cShakeParams.functionName`
Expand All
@@ -1859,9 +1863,10 @@ changes:
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
The `functionName` member represents the NIST function-name byte string used to
domain-separate functions built on top of cSHAKE. Accepted values are:
domain-separate functions built on top of cSHAKE. Non-empty values require
OpenSSL 4.0 or later. Accepted values are:
* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE
* empty or `undefined`
* the ASCII byte sequence `'KMAC'`
* the ASCII byte sequence `'TupleHash'`
* the ASCII byte sequence `'ParallelHash'`
Expand All
@@ -1878,11 +1883,11 @@ changes:
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
The `customization` member represents the customization data. Accepted
values are:
The `customization` member represents the customization data. Non-empty values
require OpenSSL 4.0 or later. Accepted values are:
* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE
* up to 512 bytes of arbitrary data
* empty or `undefined`
* up to 512 bytes of data without null bytes
### Class: `EcdhKeyDeriveParams`
Expand Down
Expand Up
@@ -2332,7 +2337,7 @@ added: v24.8.0
* Type: {number}
The optional number of bits in the KMAC key. This is optional and should
be omitted for most cases.
be omitted for most cases. The key length must be at least 32 and a multiple of 8.
#### `kmacImportParams.name`
Expand Down
Expand Up
@@ -2382,7 +2387,8 @@ added: v24.8.0
The number of bits to generate for the KMAC key. If omitted,
the length will be determined by the KMAC algorithm used.
This is optional and should be omitted for most cases.
This is optional and should be omitted for most cases. Must be at least 32 and a
multiple of 8.
#### `kmacKeyGenParams.name`
Expand Down
Expand Up
@@ -2416,7 +2422,8 @@ added: v24.8.0
added: v25.9.0
-->
* Type: {number} represents the requested output length in bits.
* Type: {number} represents the requested output length in bits. Must be a
multiple of 8.
#### `kmacParams.customization`
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -6,12 +6,10 @@ const {
StringPrototypeReplace,
StringPrototypeToLowerCase,
Symbol,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeIncludes,
} = primordials;
const {
CShakeJob,
Hash: _Hash,
HashJob,
Hmac: _Hmac,
Expand All
@@ -24,10 +22,7 @@ const {
const {
getStringOption,
jobPromise,
jobPromiseThen,
normalizeHashName,
numBitsToBytes,
truncateToBitLength,
validateAlgorithm,
validateMaxBufferLength,
kHandle,
Expand All
@@ -43,7 +38,6 @@ const {
} = require('internal/crypto/keys');
const {
lazyDOMException,
normalizeEncoding,
encodingsMap,
getDeprecationWarningEmitter,
Expand Down
Expand Up
@@ -284,30 +278,20 @@ function asyncDigest(algorithm, data) {
const outputLength = algorithm.outputLength;
if (getOptionalByteLength(algorithm.functionName) ||
getOptionalByteLength(algorithm.customization)) {
if (CShakeJob === undefined) {
throw lazyDOMException(
'Non-empty CShakeParams functionName or customization is not supported',
'NotSupportedError');
}
return jobPromise(() => new CShakeJob(
return jobPromise(() => new HashJob(
kCryptoJobWebCrypto,
algorithm.name,
StringPrototypeToLowerCase( algorithm.name) ,
data,
outputLength,
algorithm.functionName,
algorithm.customization,
outputLength));
algorithm.customization));
}
const bits = jobPromise(() => new HashJob(
return jobPromise(() => new HashJob(
kCryptoJobWebCrypto,
normalizeHashName(algorithm.name),
data,
numBitsToBytes(outputLength) * 8));
if (outputLength % 8 === 0)
return bits;
return jobPromiseThen(bits, (bits) =>
TypedArrayPrototypeGetBuffer(truncateToBitLength(outputLength, bits)));
outputLength));
}
case 'TurboSHAKE128':
// Fall through
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -193,7 +193,6 @@ function kmacSignVerify(key, data, algorithm, signature) {
getCryptoKeyHandle(key),
algorithm.name,
algorithm.customization,
getCryptoKeyAlgorithm(key).length,
algorithm.outputLength,
data,
signature));
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -721,7 +721,7 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) {
}
function validateKmacKeyLength(length) {
if (( length < 32 || length % 8) && isFips() )
if (length < 32 || length % 8 !== 0 )
throw lazyDOMException('Invalid key length', 'NotSupportedError');
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -23,7 +23,6 @@ const {
} = primordials;
const {
CShakeJob,
kWebCryptoKeyFormatRaw,
kWebCryptoKeyFormatPKCS8,
kWebCryptoKeyFormatSPKI,
Expand Down
Expand Up
@@ -72,7 +71,6 @@ const {
prepareWebCryptoResult,
validateAlgorithm,
validateMaxBufferLength,
getOptionalByteLength,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -1923,15 +1921,7 @@ function check(op, alg, length) {
}
switch (op) {
case 'digest': {
if ((normalizedAlgorithm.name === 'cSHAKE128' ||
normalizedAlgorithm.name === 'cSHAKE256') &&
(getOptionalByteLength(normalizedAlgorithm.functionName) ||
getOptionalByteLength(normalizedAlgorithm.customization))) {
return CShakeJob !== undefined;
}
return true;
}
case 'digest':
case 'decapsulate':
case 'decrypt':
case 'encapsulate':
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -11,15 +11,13 @@ const {
StringPrototypeCharCodeAt,
StringPrototypeSplit,
StringPrototypeToLowerCase,
TypedArrayPrototypeIncludes,
} = primordials;
const {
lazyDOMException,
kEmptyObject,
} = require('internal/util');
const {
isUint32,
} = require('internal/validators');
const {
getCryptoKeyAlgorithm,
getCryptoKeyType,
Expand All
@@ -30,9 +28,9 @@ const {
validateMaxBufferLength,
getBufferSourceByteLength,
getBufferSourceBytes,
getHashes,
isFips,
kNamedCurveAliases,
numBitsToBytes,
validateKmacKeyLength,
} = require('internal/crypto/util');
const {
Expand Down
Expand Up
@@ -317,20 +315,20 @@ function validateZeroLength(parameterName) {
}
function validateCShakeOutputLength(V) {
if (!isUint32(numBitsToBytes(V) * 8) ) {
if (V % 8 !== 0 ) {
throw lazyDOMException(
'Invalid CShakeParams outputLength',
'OperationError ');
'NotSupportedError ');
}
}
const kCShakeFunctionNames = ['KMAC', 'TupleHash', 'ParallelHash'];
function validateCShakeFunctionName(V) {
function validateCShakeFunctionName(V, dict ) {
const length = getBufferSourceByteLength(V);
if (length === 0) return;
if (!isFips( )) {
if (ArrayPrototypeIncludes(getHashes(), StringPrototypeToLowerCase(dict.name) )) {
const bytes = getBufferSourceBytes(V);
for (let i = 0; i < kCShakeFunctionNames.length; i++) {
const functionName = kCShakeFunctionNames[i];
Expand All
@@ -349,12 +347,17 @@ function validateCShakeFunctionName(V) {
'NotSupportedError');
}
function validateCShakeCustomization(V) {
if (isFips() && getBufferSourceByteLength(V) !== 0)
function validateCShakeCustomization(V, dict) {
if (getBufferSourceByteLength(V) === 0) return;
if (!ArrayPrototypeIncludes(getHashes(), StringPrototypeToLowerCase(dict.name)))
throw lazyDOMException(
'Unsupported CShakeParams customization',
'NotSupportedError');
validateMaxBufferLength(V, 'CShakeParams.customization', 512);
if (TypedArrayPrototypeIncludes(getBufferSourceBytes(V), 0))
throw lazyDOMException(
'Unsupported CShakeParams customization',
'NotSupportedError');
}
converters.RsaPssParams = createAlgorithmDictionaryConverter(
Expand Down
Expand Up
@@ -814,7 +817,7 @@ converters.KmacParams = createAlgorithmDictionaryConverter(
converter: (V, opts) =>
converters['unsigned long'](V, enforceRangeOptions(opts)),
validator: (V) => {
if ((V === 0 || V % 8) && isFips())
if (V % 8 !== 0 || (V === 0 && isFips() ))
throw lazyDOMException(
'Invalid KmacParams outputLength',
'NotSupportedError');
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.