Skip to content

Commit fe956fe

Browse files
panvaaduh95
authored andcommitted
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 <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66237 Backport-PR-URL: #66970 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 06254be commit fe956fe

25 files changed

Lines changed: 367 additions & 953 deletions

‎doc/api/webcrypto.md‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,9 @@ changes:
18291829
description: Renamed `cShakeParams.length` to `cShakeParams.outputLength`.
18301830
-->
18311831
1832+
When both `functionName` and `customization` are empty or `undefined`, cSHAKE is
1833+
equivalent to plain SHAKE.
1834+
18321835
#### `cShakeParams.name`
18331836
18341837
<!-- YAML
@@ -1843,7 +1846,8 @@ added: v24.7.0
18431846
added: v24.15.0
18441847
-->
18451848
1846-
* Type: {number} represents the requested output length in bits.
1849+
* Type: {number} represents the requested output length in bits. Must be a
1850+
multiple of 8.
18471851
18481852
#### `cShakeParams.functionName`
18491853
@@ -1858,9 +1862,10 @@ changes:
18581862
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
18591863
18601864
The `functionName` member represents the NIST function-name byte string used to
1861-
domain-separate functions built on top of cSHAKE. Accepted values are:
1865+
domain-separate functions built on top of cSHAKE. Non-empty values require
1866+
OpenSSL 4.0 or later. Accepted values are:
18621867
1863-
* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE
1868+
* empty or `undefined`
18641869
* the ASCII byte sequence `'KMAC'`
18651870
* the ASCII byte sequence `'TupleHash'`
18661871
* the ASCII byte sequence `'ParallelHash'`
@@ -1877,11 +1882,11 @@ changes:
18771882
18781883
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
18791884
1880-
The `customization` member represents the customization data. Accepted
1881-
values are:
1885+
The `customization` member represents the customization data. Non-empty values
1886+
require OpenSSL 4.0 or later. Accepted values are:
18821887
1883-
* empty or `undefined`, in which case cSHAKE is equivalent to plain SHAKE
1884-
* up to 512 bytes of arbitrary data
1888+
* empty or `undefined`
1889+
* up to 512 bytes of data without null bytes
18851890
18861891
### Class: `EcdhKeyDeriveParams`
18871892
@@ -2331,7 +2336,7 @@ added: v24.8.0
23312336
* Type: {number}
23322337
23332338
The optional number of bits in the KMAC key. This is optional and should
2334-
be omitted for most cases.
2339+
be omitted for most cases. The key length must be at least 32 and a multiple of 8.
23352340
23362341
#### `kmacImportParams.name`
23372342
@@ -2381,7 +2386,8 @@ added: v24.8.0
23812386
23822387
The number of bits to generate for the KMAC key. If omitted,
23832388
the length will be determined by the KMAC algorithm used.
2384-
This is optional and should be omitted for most cases.
2389+
This is optional and should be omitted for most cases. Must be at least 32 and a
2390+
multiple of 8.
23852391
23862392
#### `kmacKeyGenParams.name`
23872393
@@ -2415,7 +2421,8 @@ added: v24.8.0
24152421
added: v24.15.0
24162422
-->
24172423
2418-
* Type: {number} represents the requested output length in bits.
2424+
* Type: {number} represents the requested output length in bits. Must be a
2425+
multiple of 8.
24192426
24202427
#### `kmacParams.customization`
24212428

‎lib/internal/crypto/hash.js‎

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ const {
66
StringPrototypeReplace,
77
StringPrototypeToLowerCase,
88
Symbol,
9-
TypedArrayPrototypeGetBuffer,
109
TypedArrayPrototypeIncludes,
1110
} = primordials;
1211

1312
const {
14-
CShakeJob,
1513
Hash: _Hash,
1614
HashJob,
1715
Hmac: _Hmac,
@@ -24,10 +22,7 @@ const {
2422
const {
2523
getStringOption,
2624
jobPromise,
27-
jobPromiseThen,
2825
normalizeHashName,
29-
numBitsToBytes,
30-
truncateToBitLength,
3126
validateAlgorithm,
3227
validateMaxBufferLength,
3328
kHandle,
@@ -43,7 +38,6 @@ const {
4338
} = require('internal/crypto/keys');
4439

4540
const {
46-
lazyDOMException,
4741
normalizeEncoding,
4842
encodingsMap,
4943
isPendingDeprecation,
@@ -286,30 +280,20 @@ function asyncDigest(algorithm, data) {
286280
const outputLength = algorithm.outputLength;
287281
if (getOptionalByteLength(algorithm.functionName) ||
288282
getOptionalByteLength(algorithm.customization)) {
289-
if (CShakeJob === undefined) {
290-
throw lazyDOMException(
291-
'Non-empty CShakeParams functionName or customization is not supported',
292-
'NotSupportedError');
293-
}
294-
295-
return jobPromise(() => new CShakeJob(
283+
return jobPromise(() => new HashJob(
296284
kCryptoJobWebCrypto,
297-
algorithm.name,
285+
StringPrototypeToLowerCase(algorithm.name),
298286
data,
287+
outputLength,
299288
algorithm.functionName,
300-
algorithm.customization,
301-
outputLength));
289+
algorithm.customization));
302290
}
303291

304-
const bits = jobPromise(() => new HashJob(
292+
return jobPromise(() => new HashJob(
305293
kCryptoJobWebCrypto,
306294
normalizeHashName(algorithm.name),
307295
data,
308-
numBitsToBytes(outputLength) * 8));
309-
if (outputLength % 8 === 0)
310-
return bits;
311-
return jobPromiseThen(bits, (bits) =>
312-
TypedArrayPrototypeGetBuffer(truncateToBitLength(outputLength, bits)));
296+
outputLength));
313297
}
314298
case 'TurboSHAKE128':
315299
// Fall through

‎lib/internal/crypto/mac.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ function kmacSignVerify(key, data, algorithm, signature) {
193193
getCryptoKeyHandle(key),
194194
algorithm.name,
195195
algorithm.customization,
196-
getCryptoKeyAlgorithm(key).length,
197196
algorithm.outputLength,
198197
data,
199198
signature));

‎lib/internal/crypto/util.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ function validateMaxBufferLength(data, name, max = kMaxBufferLength) {
722722
}
723723

724724
function validateKmacKeyLength(length) {
725-
if ((length < 32 || length % 8) && isFips())
725+
if (length < 32 || length % 8 !== 0)
726726
throw lazyDOMException('Invalid key length', 'NotSupportedError');
727727
}
728728

‎lib/internal/crypto/webcrypto.js‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const {
2424
} = primordials;
2525

2626
const {
27-
CShakeJob,
2827
kWebCryptoKeyFormatRaw,
2928
kWebCryptoKeyFormatPKCS8,
3029
kWebCryptoKeyFormatSPKI,
@@ -73,7 +72,6 @@ const {
7372
prepareWebCryptoResult,
7473
validateAlgorithm,
7574
validateMaxBufferLength,
76-
getOptionalByteLength,
7775
} = require('internal/crypto/util');
7876

7977
const {
@@ -1926,15 +1924,7 @@ function check(op, alg, length) {
19261924
}
19271925

19281926
switch (op) {
1929-
case 'digest': {
1930-
if ((normalizedAlgorithm.name === 'cSHAKE128' ||
1931-
normalizedAlgorithm.name === 'cSHAKE256') &&
1932-
(getOptionalByteLength(normalizedAlgorithm.functionName) ||
1933-
getOptionalByteLength(normalizedAlgorithm.customization))) {
1934-
return CShakeJob !== undefined;
1935-
}
1936-
return true;
1937-
}
1927+
case 'digest':
19381928
case 'decapsulate':
19391929
case 'decrypt':
19401930
case 'encapsulate':

‎lib/internal/crypto/webidl.js‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ const {
1111
StringPrototypeCharCodeAt,
1212
StringPrototypeSplit,
1313
StringPrototypeToLowerCase,
14+
TypedArrayPrototypeIncludes,
1415
} = primordials;
1516

1617
const {
1718
lazyDOMException,
1819
kEmptyObject,
1920
} = require('internal/util');
20-
const {
21-
isUint32,
22-
} = require('internal/validators');
2321
const {
2422
getCryptoKeyAlgorithm,
2523
getCryptoKeyType,
@@ -30,9 +28,9 @@ const {
3028
validateMaxBufferLength,
3129
getBufferSourceByteLength,
3230
getBufferSourceBytes,
31+
getHashes,
3332
isFips,
3433
kNamedCurveAliases,
35-
numBitsToBytes,
3634
validateKmacKeyLength,
3735
} = require('internal/crypto/util');
3836
const {
@@ -317,20 +315,20 @@ function validateZeroLength(parameterName) {
317315
}
318316

319317
function validateCShakeOutputLength(V) {
320-
if (!isUint32(numBitsToBytes(V) * 8)) {
318+
if (V % 8 !== 0) {
321319
throw lazyDOMException(
322320
'Invalid CShakeParams outputLength',
323-
'OperationError');
321+
'NotSupportedError');
324322
}
325323
}
326324

327325
const kCShakeFunctionNames = ['KMAC', 'TupleHash', 'ParallelHash'];
328326

329-
function validateCShakeFunctionName(V) {
327+
function validateCShakeFunctionName(V, dict) {
330328
const length = getBufferSourceByteLength(V);
331329
if (length === 0) return;
332330

333-
if (!isFips()) {
331+
if (ArrayPrototypeIncludes(getHashes(), StringPrototypeToLowerCase(dict.name))) {
334332
const bytes = getBufferSourceBytes(V);
335333
for (let i = 0; i < kCShakeFunctionNames.length; i++) {
336334
const functionName = kCShakeFunctionNames[i];
@@ -349,12 +347,17 @@ function validateCShakeFunctionName(V) {
349347
'NotSupportedError');
350348
}
351349

352-
function validateCShakeCustomization(V) {
353-
if (isFips() && getBufferSourceByteLength(V) !== 0)
350+
function validateCShakeCustomization(V, dict) {
351+
if (getBufferSourceByteLength(V) === 0) return;
352+
if (!ArrayPrototypeIncludes(getHashes(), StringPrototypeToLowerCase(dict.name)))
354353
throw lazyDOMException(
355354
'Unsupported CShakeParams customization',
356355
'NotSupportedError');
357356
validateMaxBufferLength(V, 'CShakeParams.customization', 512);
357+
if (TypedArrayPrototypeIncludes(getBufferSourceBytes(V), 0))
358+
throw lazyDOMException(
359+
'Unsupported CShakeParams customization',
360+
'NotSupportedError');
358361
}
359362

360363
converters.RsaPssParams = createAlgorithmDictionaryConverter(
@@ -814,7 +817,7 @@ converters.KmacParams = createAlgorithmDictionaryConverter(
814817
converter: (V, opts) =>
815818
converters['unsigned long'](V, enforceRangeOptions(opts)),
816819
validator: (V) => {
817-
if ((V === 0 || V % 8) && isFips())
820+
if (V % 8 !== 0 || (V === 0 && isFips()))
818821
throw lazyDOMException(
819822
'Invalid KmacParams outputLength',
820823
'NotSupportedError');

0 commit comments

Comments
 (0)