Skip to content

Commit c989497

Browse files
panvaaduh95
authored andcommitted
crypto: add TurboSHAKE and KangarooTwelve Web Cryptography algorithms
PR-URL: #62183 Backport-PR-URL: #62539 Refs: https://wicg.github.io/webcrypto-modern-algos/#kangarootwelve Refs: https://wicg.github.io/webcrypto-modern-algos/#turboshake Refs: https://www.rfc-editor.org/rfc/rfc9861.html Refs: https://redirect.github.com/openssl/openssl/issues/30304 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 2395fc0 commit c989497

File tree

13 files changed

+1523
-2
lines changed

13 files changed

+1523
-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.
@@ -121,6 +125,8 @@ Algorithms:
121125
* `'cSHAKE256'`
122126
* `'KMAC128'`[^openssl30]
123127
* `'KMAC256'`[^openssl30]
128+
* `'KT128'`
129+
* `'KT256'`
124130
* `'ML-DSA-44'`[^openssl35]
125131
* `'ML-DSA-65'`[^openssl35]
126132
* `'ML-DSA-87'`[^openssl35]
@@ -130,6 +136,8 @@ Algorithms:
130136
* `'SHA3-256'`
131137
* `'SHA3-384'`
132138
* `'SHA3-512'`
139+
* `'TurboSHAKE128'`
140+
* `'TurboSHAKE256'`
133141

134142
Key Formats:
135143

@@ -574,6 +582,8 @@ implementation and the APIs supported for each:
574582
| `'HMAC'` | | ✔ | | | | |
575583
| `'KMAC128'`[^modern-algos] | | ✔ | | | | |
576584
| `'KMAC256'`[^modern-algos] | | ✔ | | | | |
585+
| `'KT128'`[^modern-algos] | | | | | | ✔ |
586+
| `'KT256'`[^modern-algos] | | | | | | ✔ |
577587
| `'ML-DSA-44'`[^modern-algos] | | ✔ | | | | |
578588
| `'ML-DSA-65'`[^modern-algos] | | ✔ | | | | |
579589
| `'ML-DSA-87'`[^modern-algos] | | ✔ | | | | |
@@ -591,6 +601,8 @@ implementation and the APIs supported for each:
591601
| `'SHA3-256'`[^modern-algos] | | | | | | ✔ |
592602
| `'SHA3-384'`[^modern-algos] | | | | | | ✔ |
593603
| `'SHA3-512'`[^modern-algos] | | | | | | ✔ |
604+
| `'TurboSHAKE128'`[^modern-algos] | | | | | | ✔ |
605+
| `'TurboSHAKE256'`[^modern-algos] | | | | | | ✔ |
594606
| `'X25519'` | | | ✔ | | | |
595607
| `'X448'`[^secure-curves] | | | ✔ | | | |
596608
@@ -998,6 +1010,10 @@ The algorithms currently supported include:
9981010
<!-- YAML
9991011
added: v15.0.0
10001012
changes:
1013+
- version: REPLACEME
1014+
pr-url: https://github.com/nodejs/node/pull/62183
1015+
description: TurboSHAKE and KangarooTwelve algorithms
1016+
are now supported.
10011017
- version: v24.7.0
10021018
pr-url: https://github.com/nodejs/node/pull/59365
10031019
description: SHA-3 algorithms are now supported.
@@ -1006,7 +1022,7 @@ changes:
10061022
description: SHAKE algorithms are now supported.
10071023
-->
10081024
1009-
* `algorithm` {string|Algorithm|CShakeParams}
1025+
* `algorithm` {string|Algorithm|CShakeParams|TurboShakeParams|KangarooTwelveParams}
10101026
* `data` {ArrayBuffer|TypedArray|DataView|Buffer}
10111027
* Returns: {Promise} Fulfills with an {ArrayBuffer} upon success.
10121028
@@ -1018,13 +1034,17 @@ If `algorithm` is provided as a {string}, it must be one of:
10181034
10191035
* `'cSHAKE128'`[^modern-algos]
10201036
* `'cSHAKE256'`[^modern-algos]
1037+
* `'KT128'`[^modern-algos]
1038+
* `'KT256'`[^modern-algos]
10211039
* `'SHA-1'`
10221040
* `'SHA-256'`
10231041
* `'SHA-384'`
10241042
* `'SHA-512'`
10251043
* `'SHA3-256'`[^modern-algos]
10261044
* `'SHA3-384'`[^modern-algos]
10271045
* `'SHA3-512'`[^modern-algos]
1046+
* `'TurboSHAKE128'`[^modern-algos]
1047+
* `'TurboSHAKE256'`[^modern-algos]
10281048
10291049
If `algorithm` is provided as an {Object}, it must have a `name` property
10301050
whose value is one of the above.
@@ -2311,6 +2331,38 @@ added: v15.0.0
23112331
23122332
* Type: {string}
23132333
2334+
### Class: `KangarooTwelveParams`
2335+
2336+
<!-- YAML
2337+
added: REPLACEME
2338+
-->
2339+
2340+
#### `kangarooTwelveParams.customization`
2341+
2342+
<!-- YAML
2343+
added: REPLACEME
2344+
-->
2345+
2346+
* Type: {ArrayBuffer|TypedArray|DataView|Buffer|undefined}
2347+
2348+
The optional customization string for KangarooTwelve.
2349+
2350+
#### `kangarooTwelveParams.name`
2351+
2352+
<!-- YAML
2353+
added: REPLACEME
2354+
-->
2355+
2356+
* Type: {string} Must be `'KT128'`[^modern-algos] or `'KT256'`[^modern-algos]
2357+
2358+
#### `kangarooTwelveParams.outputLength`
2359+
2360+
<!-- YAML
2361+
added: REPLACEME
2362+
-->
2363+
2364+
* Type: {number} represents the requested output length in bits.
2365+
23142366
### Class: `KmacImportParams`
23152367
23162368
<!-- YAML
@@ -2681,6 +2733,38 @@ added: v15.0.0
26812733
26822734
The length (in bytes) of the random salt to use.
26832735
2736+
### Class: `TurboShakeParams`
2737+
2738+
<!-- YAML
2739+
added: REPLACEME
2740+
-->
2741+
2742+
#### `turboShakeParams.domainSeparation`
2743+
2744+
<!-- YAML
2745+
added: REPLACEME
2746+
-->
2747+
2748+
* Type: {number|undefined}
2749+
2750+
The optional domain separation byte (0x01-0x7f). Defaults to `0x1f`.
2751+
2752+
#### `turboShakeParams.name`
2753+
2754+
<!-- YAML
2755+
added: REPLACEME
2756+
-->
2757+
2758+
* Type: {string} Must be `'TurboSHAKE128'`[^modern-algos] or `'TurboSHAKE256'`[^modern-algos]
2759+
2760+
#### `turboShakeParams.outputLength`
2761+
2762+
<!-- YAML
2763+
added: REPLACEME
2764+
-->
2765+
2766+
* Type: {number} represents the requested output length in bits.
2767+
26842768
[^secure-curves]: See [Secure Curves in the Web Cryptography API][]
26852769
26862770
[^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 {
@@ -226,6 +228,24 @@ async function asyncDigest(algorithm, data) {
226228
normalizeHashName(algorithm.name),
227229
data,
228230
algorithm.outputLength));
231+
case 'TurboSHAKE128':
232+
// Fall through
233+
case 'TurboSHAKE256':
234+
return await jobPromise(() => new TurboShakeJob(
235+
kCryptoJobAsync,
236+
algorithm.name,
237+
algorithm.domainSeparation ?? 0x1f,
238+
algorithm.outputLength / 8,
239+
data));
240+
case 'KT128':
241+
// Fall through
242+
case 'KT256':
243+
return await jobPromise(() => new KangarooTwelveJob(
244+
kCryptoJobAsync,
245+
algorithm.name,
246+
algorithm.customization,
247+
algorithm.outputLength / 8,
248+
data));
229249
}
230250

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

lib/internal/crypto/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ const kAlgorithmDefinitions = {
244244
},
245245
'cSHAKE128': { 'digest': 'CShakeParams' },
246246
'cSHAKE256': { 'digest': 'CShakeParams' },
247+
'KT128': { 'digest': 'KangarooTwelveParams' },
248+
'KT256': { 'digest': 'KangarooTwelveParams' },
249+
'TurboSHAKE128': { 'digest': 'TurboShakeParams' },
250+
'TurboSHAKE256': { 'digest': 'TurboShakeParams' },
247251
'ECDH': {
248252
'generateKey': 'EcKeyGenParams',
249253
'exportKey': null,
@@ -441,6 +445,10 @@ const experimentalAlgorithms = [
441445
'SHA3-256',
442446
'SHA3-384',
443447
'SHA3-512',
448+
'TurboSHAKE128',
449+
'TurboSHAKE256',
450+
'KT128',
451+
'KT256',
444452
'X448',
445453
];
446454

@@ -511,6 +519,10 @@ const simpleAlgorithmDictionaries = {
511519
KmacParams: {
512520
customization: 'BufferSource',
513521
},
522+
KangarooTwelveParams: {
523+
customization: 'BufferSource',
524+
},
525+
TurboShakeParams: {},
514526
};
515527

516528
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 === 0 || V % 8)
907+
throw lazyDOMException('Invalid KangarooTwelveParams outputLength', 'OperationError');
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 === 0 || V % 8)
926+
throw lazyDOMException('Invalid TurboShakeParams outputLength', 'OperationError');
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+
'OperationError');
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
@@ -344,6 +344,7 @@
344344
'src/crypto/crypto_kem.cc',
345345
'src/crypto/crypto_hmac.cc',
346346
'src/crypto/crypto_kmac.cc',
347+
'src/crypto/crypto_turboshake.cc',
347348
'src/crypto/crypto_random.cc',
348349
'src/crypto/crypto_rsa.cc',
349350
'src/crypto/crypto_spkac.cc',
@@ -362,6 +363,7 @@
362363
'src/crypto/crypto_dh.h',
363364
'src/crypto/crypto_hmac.h',
364365
'src/crypto/crypto_kmac.h',
366+
'src/crypto/crypto_turboshake.h',
365367
'src/crypto/crypto_rsa.h',
366368
'src/crypto/crypto_spkac.h',
367369
'src/crypto/crypto_util.h',

0 commit comments

Comments
 (0)