Skip to content

Commit f80cc1b

Browse files
committed
crypto: add TurboSHAKE and KangarooTwelve Web Cryptography algorithms
1 parent ae228c1 commit f80cc1b

File tree

12 files changed

+1494
-2
lines changed

12 files changed

+1494
-2
lines changed

doc/api/webcrypto.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
<!-- YAML
44
changes:
5+
- version: REPLACEME
6+
pr-url: https://github.com/nodejs/node/pull/62183
7+
description: TurboSHAKE and KangarooTwelve algorithms
8+
are now supported.
59
- version: v24.8.0
610
pr-url: https://github.com/nodejs/node/pull/59647
711
description: KMAC algorithms are now supported.
@@ -122,6 +126,8 @@ Algorithms:
122126
* `'cSHAKE256'`
123127
* `'KMAC128'`[^openssl30]
124128
* `'KMAC256'`[^openssl30]
129+
* `'KT128'`
130+
* `'KT256'`
125131
* `'ML-DSA-44'`[^openssl35]
126132
* `'ML-DSA-65'`[^openssl35]
127133
* `'ML-DSA-87'`[^openssl35]
@@ -131,6 +137,8 @@ Algorithms:
131137
* `'SHA3-256'`
132138
* `'SHA3-384'`
133139
* `'SHA3-512'`
140+
* `'TurboSHAKE128'`
141+
* `'TurboSHAKE256'`
134142

135143
Key Formats:
136144

@@ -575,6 +583,8 @@ implementation and the APIs supported for each:
575583
| `'HMAC'` | | ✔ | | | | |
576584
| `'KMAC128'`[^modern-algos] | | ✔ | | | | |
577585
| `'KMAC256'`[^modern-algos] | | ✔ | | | | |
586+
| `'KT128'`[^modern-algos] | | | | | | ✔ |
587+
| `'KT256'`[^modern-algos] | | | | | | ✔ |
578588
| `'ML-DSA-44'`[^modern-algos] | | ✔ | | | | |
579589
| `'ML-DSA-65'`[^modern-algos] | | ✔ | | | | |
580590
| `'ML-DSA-87'`[^modern-algos] | | ✔ | | | | |
@@ -592,6 +602,8 @@ implementation and the APIs supported for each:
592602
| `'SHA3-256'`[^modern-algos] | | | | | | ✔ |
593603
| `'SHA3-384'`[^modern-algos] | | | | | | ✔ |
594604
| `'SHA3-512'`[^modern-algos] | | | | | | ✔ |
605+
| `'TurboSHAKE128'`[^modern-algos] | | | | | | ✔ |
606+
| `'TurboSHAKE256'`[^modern-algos] | | | | | | ✔ |
595607
| `'X25519'` | | | ✔ | | | |
596608
| `'X448'`[^secure-curves] | | | ✔ | | | |
597609
@@ -999,6 +1011,10 @@ The algorithms currently supported include:
9991011
<!-- YAML
10001012
added: v15.0.0
10011013
changes:
1014+
- version: REPLACEME
1015+
pr-url: https://github.com/nodejs/node/pull/62183
1016+
description: TurboSHAKE and KangarooTwelve algorithms
1017+
are now supported.
10021018
- version: v24.7.0
10031019
pr-url: https://github.com/nodejs/node/pull/59365
10041020
description: SHA-3 algorithms are now supported.
@@ -1007,7 +1023,7 @@ changes:
10071023
description: SHAKE algorithms are now supported.
10081024
-->
10091025
1010-
* `algorithm` {string|Algorithm|CShakeParams}
1026+
* `algorithm` {string|Algorithm|CShakeParams|TurboShakeParams|KangarooTwelveParams}
10111027
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
10121028
* Returns: {Promise} Fulfills with an {ArrayBuffer} upon success.
10131029
@@ -1019,13 +1035,17 @@ If `algorithm` is provided as a {string}, it must be one of:
10191035
10201036
* `'cSHAKE128'`[^modern-algos]
10211037
* `'cSHAKE256'`[^modern-algos]
1038+
* `'KT128'`[^modern-algos]
1039+
* `'KT256'`[^modern-algos]
10221040
* `'SHA-1'`
10231041
* `'SHA-256'`
10241042
* `'SHA-384'`
10251043
* `'SHA-512'`
10261044
* `'SHA3-256'`[^modern-algos]
10271045
* `'SHA3-384'`[^modern-algos]
10281046
* `'SHA3-512'`[^modern-algos]
1047+
* `'TurboSHAKE128'`[^modern-algos]
1048+
* `'TurboSHAKE256'`[^modern-algos]
10291049
10301050
If `algorithm` is provided as an {Object}, it must have a `name` property
10311051
whose value is one of the above.
@@ -2308,6 +2328,38 @@ added: v15.0.0
23082328
23092329
* Type: {string}
23102330
2331+
### Class: `KangarooTwelveParams`
2332+
2333+
<!-- YAML
2334+
added: REPLACEME
2335+
-->
2336+
2337+
#### `kangarooTwelveParams.customization`
2338+
2339+
<!-- YAML
2340+
added: REPLACEME
2341+
-->
2342+
2343+
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
2344+
2345+
The optional customization string for KangarooTwelve.
2346+
2347+
#### `kangarooTwelveParams.name`
2348+
2349+
<!-- YAML
2350+
added: REPLACEME
2351+
-->
2352+
2353+
* Type: {string} Must be `'KT128'`[^modern-algos] or `'KT256'`[^modern-algos]
2354+
2355+
#### `kangarooTwelveParams.outputLength`
2356+
2357+
<!-- YAML
2358+
added: REPLACEME
2359+
-->
2360+
2361+
* Type: {number} represents the requested output length in bits.
2362+
23112363
### Class: `KmacImportParams`
23122364
23132365
<!-- YAML
@@ -2674,6 +2726,38 @@ added: v15.0.0
26742726
26752727
The length (in bytes) of the random salt to use.
26762728
2729+
### Class: `TurboShakeParams`
2730+
2731+
<!-- YAML
2732+
added: REPLACEME
2733+
-->
2734+
2735+
#### `turboShakeParams.domainSeparation`
2736+
2737+
<!-- YAML
2738+
added: REPLACEME
2739+
-->
2740+
2741+
* Type: {number|undefined}
2742+
2743+
The optional domain separation byte (0x01-0x7f). Defaults to `0x1f`.
2744+
2745+
#### `turboShakeParams.name`
2746+
2747+
<!-- YAML
2748+
added: REPLACEME
2749+
-->
2750+
2751+
* Type: {string} Must be `'TurboSHAKE128'`[^modern-algos] or `'TurboSHAKE256'`[^modern-algos]
2752+
2753+
#### `turboShakeParams.outputLength`
2754+
2755+
<!-- YAML
2756+
added: REPLACEME
2757+
-->
2758+
2759+
* Type: {number} represents the requested output length in bits.
2760+
26772761
[^secure-curves]: See [Secure Curves in the Web Cryptography API][]
26782762
26792763
[^modern-algos]: See [Modern Algorithms in the Web Cryptography API][]

lib/internal/crypto/hash.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const {
1414
Hmac: _Hmac,
1515
kCryptoJobAsync,
1616
oneShotDigest,
17+
TurboShakeJob,
18+
KangarooTwelveJob,
1719
} = internalBinding('crypto');
1820

1921
const {
@@ -224,6 +226,24 @@ async function asyncDigest(algorithm, data) {
224226
normalizeHashName(algorithm.name),
225227
data,
226228
algorithm.length));
229+
case 'TurboSHAKE128':
230+
// Fall through
231+
case 'TurboSHAKE256':
232+
return await jobPromise(() => new TurboShakeJob(
233+
kCryptoJobAsync,
234+
algorithm.name,
235+
algorithm.domainSeparation ?? 0x1f,
236+
algorithm.outputLength / 8,
237+
data));
238+
case 'KT128':
239+
// Fall through
240+
case 'KT256':
241+
return await jobPromise(() => new KangarooTwelveJob(
242+
kCryptoJobAsync,
243+
algorithm.name,
244+
algorithm.customization,
245+
algorithm.outputLength / 8,
246+
data));
227247
}
228248

229249
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');

lib/internal/crypto/util.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const {
4242
kKeyVariantAES_OCB_128: hasAesOcbMode,
4343
Argon2Job,
4444
KmacJob,
45+
TurboShakeJob,
46+
KangarooTwelveJob,
4547
} = internalBinding('crypto');
4648

4749
const { getOptionValue } = require('internal/options');
@@ -244,6 +246,10 @@ const kAlgorithmDefinitions = {
244246
},
245247
'cSHAKE128': { 'digest': 'CShakeParams' },
246248
'cSHAKE256': { 'digest': 'CShakeParams' },
249+
'KT128': { 'digest': 'KangarooTwelveParams' },
250+
'KT256': { 'digest': 'KangarooTwelveParams' },
251+
'TurboSHAKE128': { 'digest': 'TurboShakeParams' },
252+
'TurboSHAKE256': { 'digest': 'TurboShakeParams' },
247253
'ECDH': {
248254
'generateKey': 'EcKeyGenParams',
249255
'exportKey': null,
@@ -405,6 +411,10 @@ const conditionalAlgorithms = {
405411
'Ed448': !process.features.openssl_is_boringssl,
406412
'KMAC128': !!KmacJob,
407413
'KMAC256': !!KmacJob,
414+
'KT128': !!KangarooTwelveJob,
415+
'KT256': !!KangarooTwelveJob,
416+
'TurboSHAKE128': !!TurboShakeJob,
417+
'TurboSHAKE256': !!TurboShakeJob,
408418
'ML-DSA-44': !!EVP_PKEY_ML_DSA_44,
409419
'ML-DSA-65': !!EVP_PKEY_ML_DSA_65,
410420
'ML-DSA-87': !!EVP_PKEY_ML_DSA_87,
@@ -441,6 +451,10 @@ const experimentalAlgorithms = [
441451
'SHA3-256',
442452
'SHA3-384',
443453
'SHA3-512',
454+
'TurboSHAKE128',
455+
'TurboSHAKE256',
456+
'KT128',
457+
'KT256',
444458
'X448',
445459
];
446460

@@ -513,6 +527,10 @@ const simpleAlgorithmDictionaries = {
513527
KmacParams: {
514528
customization: 'BufferSource',
515529
},
530+
KangarooTwelveParams: {
531+
customization: 'BufferSource',
532+
},
533+
TurboShakeParams: {},
516534
};
517535

518536
function validateMaxBufferLength(data, name) {

lib/internal/crypto/webidl.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,52 @@ converters.KmacParams = createDictionaryConverter(
895895
},
896896
]);
897897

898+
converters.KangarooTwelveParams = createDictionaryConverter(
899+
'KangarooTwelveParams', [
900+
...new SafeArrayIterator(dictAlgorithm),
901+
{
902+
key: 'outputLength',
903+
converter: (V, opts) =>
904+
converters['unsigned long'](V, { ...opts, enforceRange: true }),
905+
validator: (V, opts) => {
906+
if (V % 8)
907+
throw lazyDOMException('Unsupported KangarooTwelveParams outputLength', 'NotSupportedError');
908+
},
909+
required: true,
910+
},
911+
{
912+
key: 'customization',
913+
converter: converters.BufferSource,
914+
},
915+
]);
916+
917+
converters.TurboShakeParams = createDictionaryConverter(
918+
'TurboShakeParams', [
919+
...new SafeArrayIterator(dictAlgorithm),
920+
{
921+
key: 'outputLength',
922+
converter: (V, opts) =>
923+
converters['unsigned long'](V, { ...opts, enforceRange: true }),
924+
validator: (V, opts) => {
925+
if (V % 8)
926+
throw lazyDOMException('Unsupported TurboShakeParams outputLength', 'NotSupportedError');
927+
},
928+
required: true,
929+
},
930+
{
931+
key: 'domainSeparation',
932+
converter: (V, opts) =>
933+
converters.octet(V, { ...opts, enforceRange: true }),
934+
validator: (V) => {
935+
if (V < 0x01 || V > 0x7F) {
936+
throw lazyDOMException(
937+
'TurboShakeParams.domainSeparation must be in range 0x01-0x7f',
938+
'NotSupportedError');
939+
}
940+
},
941+
},
942+
]);
943+
898944
module.exports = {
899945
converters,
900946
requiredArguments,

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
'src/crypto/crypto_kem.cc',
391391
'src/crypto/crypto_hmac.cc',
392392
'src/crypto/crypto_kmac.cc',
393+
'src/crypto/crypto_turboshake.cc',
393394
'src/crypto/crypto_random.cc',
394395
'src/crypto/crypto_rsa.cc',
395396
'src/crypto/crypto_spkac.cc',
@@ -408,6 +409,7 @@
408409
'src/crypto/crypto_dh.h',
409410
'src/crypto/crypto_hmac.h',
410411
'src/crypto/crypto_kmac.h',
412+
'src/crypto/crypto_turboshake.h',
411413
'src/crypto/crypto_rsa.h',
412414
'src/crypto/crypto_spkac.h',
413415
'src/crypto/crypto_util.h',

0 commit comments

Comments
 (0)