Skip to content

Commit 76cde76

Browse files
panvatargos
authored andcommitted
crypto: add SHA-3 Web Cryptography digest algorithms
PR-URL: #59365 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 247d017 commit 76cde76

22 files changed

+784
-77
lines changed

doc/api/webcrypto.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
<!-- YAML
44
changes:
5+
- version: REPLACEME
6+
pr-url: https://github.com/nodejs/node/pull/59365
7+
description: SHA-3 algorithms are now supported.
58
- version: REPLACEME
69
pr-url: https://github.com/nodejs/node/pull/59365
710
description: SHAKE algorithms are now supported.
@@ -99,6 +102,9 @@ Algorithms:
99102
* `'ML-DSA-44'`[^openssl35]
100103
* `'ML-DSA-65'`[^openssl35]
101104
* `'ML-DSA-87'`[^openssl35]
105+
* `'SHA3-256'`
106+
* `'SHA3-384'`
107+
* `'SHA3-512'`
102108

103109
Key Formats:
104110

@@ -496,6 +502,9 @@ implementation and the APIs supported for each:
496502
| `'SHA-256'` | | | | | | | | | | | | ✔ |
497503
| `'SHA-384'` | | | | | | | | | | | | ✔ |
498504
| `'SHA-512'` | | | | | | | | | | | | ✔ |
505+
| `'SHA3-256'`[^modern-algos] | | | | | | | | | | | | ✔ |
506+
| `'SHA3-384'`[^modern-algos] | | | | | | | | | | | | ✔ |
507+
| `'SHA3-512'`[^modern-algos] | | | | | | | | | | | | ✔ |
499508
| `'X25519'` | ✔ | ✔ | ✔ | | | | | ✔ | ✔ | | | |
500509
| `'X448'`[^secure-curves] | ✔ | ✔ | ✔ | | | | | ✔ | ✔ | | | |
501510
@@ -808,6 +817,9 @@ The algorithms currently supported include:
808817
<!-- YAML
809818
added: v15.0.0
810819
changes:
820+
- version: REPLACEME
821+
pr-url: https://github.com/nodejs/node/pull/59365
822+
description: SHA-3 algorithms are now supported.
811823
- version: REPLACEME
812824
pr-url: https://github.com/nodejs/node/pull/59365
813825
description: SHAKE algorithms are now supported.
@@ -829,6 +841,9 @@ If `algorithm` is provided as a {string}, it must be one of:
829841
* `'SHA-256'`
830842
* `'SHA-384'`
831843
* `'SHA-512'`
844+
* `'SHA3-256'`[^modern-algos]
845+
* `'SHA3-384'`[^modern-algos]
846+
* `'SHA3-512'`[^modern-algos]
832847
833848
If `algorithm` is provided as an {Object}, it must have a `name` property
834849
whose value is one of the above.
@@ -1520,6 +1535,10 @@ added: v15.0.0
15201535
15211536
<!-- YAML
15221537
added: v15.0.0
1538+
changes:
1539+
- version: REPLACEME
1540+
pr-url: https://github.com/nodejs/node/pull/59365
1541+
description: SHA-3 algorithms are now supported.
15231542
-->
15241543
15251544
* Type: {string|Algorithm}
@@ -1530,6 +1549,9 @@ If represented as a {string}, the value must be one of:
15301549
* `'SHA-256'`
15311550
* `'SHA-384'`
15321551
* `'SHA-512'`
1552+
* `'SHA3-256'`[^modern-algos]
1553+
* `'SHA3-384'`[^modern-algos]
1554+
* `'SHA3-512'`[^modern-algos]
15331555
15341556
If represented as an {Algorithm}, the object's `name` property
15351557
must be one of the above listed values.
@@ -1649,6 +1671,10 @@ added: v15.0.0
16491671
16501672
<!-- YAML
16511673
added: v15.0.0
1674+
changes:
1675+
- version: REPLACEME
1676+
pr-url: https://github.com/nodejs/node/pull/59365
1677+
description: SHA-3 algorithms are now supported.
16521678
-->
16531679
16541680
* Type: {string|Algorithm}
@@ -1659,6 +1685,9 @@ If represented as a {string}, the value must be one of:
16591685
* `'SHA-256'`
16601686
* `'SHA-384'`
16611687
* `'SHA-512'`
1688+
* `'SHA3-256'`[^modern-algos]
1689+
* `'SHA3-384'`[^modern-algos]
1690+
* `'SHA3-512'`[^modern-algos]
16621691
16631692
If represented as an {Algorithm}, the object's `name` property
16641693
must be one of the above listed values.
@@ -1705,6 +1734,10 @@ added: v15.0.0
17051734
17061735
<!-- YAML
17071736
added: v15.0.0
1737+
changes:
1738+
- version: REPLACEME
1739+
pr-url: https://github.com/nodejs/node/pull/59365
1740+
description: SHA-3 algorithms are now supported.
17081741
-->
17091742
17101743
* Type: {string|Algorithm}
@@ -1715,6 +1748,9 @@ If represented as a {string}, the value must be one of:
17151748
* `'SHA-256'`
17161749
* `'SHA-384'`
17171750
* `'SHA-512'`
1751+
* `'SHA3-256'`[^modern-algos]
1752+
* `'SHA3-384'`[^modern-algos]
1753+
* `'SHA3-512'`[^modern-algos]
17181754
17191755
If represented as an {Algorithm}, the object's `name` property
17201756
must be one of the above listed values.
@@ -1780,6 +1816,10 @@ added: v15.0.0
17801816
17811817
<!-- YAML
17821818
added: v15.0.0
1819+
changes:
1820+
- version: REPLACEME
1821+
pr-url: https://github.com/nodejs/node/pull/59365
1822+
description: SHA-3 algorithms are now supported.
17831823
-->
17841824
17851825
* Type: {string|Algorithm}
@@ -1790,6 +1830,9 @@ If represented as a {string}, the value must be one of:
17901830
* `'SHA-256'`
17911831
* `'SHA-384'`
17921832
* `'SHA-512'`
1833+
* `'SHA3-256'`[^modern-algos]
1834+
* `'SHA3-384'`[^modern-algos]
1835+
* `'SHA3-512'`[^modern-algos]
17931836
17941837
If represented as an {Algorithm}, the object's `name` property
17951838
must be one of the above listed values.
@@ -1838,6 +1881,10 @@ added: v15.0.0
18381881
18391882
<!-- YAML
18401883
added: v15.0.0
1884+
changes:
1885+
- version: REPLACEME
1886+
pr-url: https://github.com/nodejs/node/pull/59365
1887+
description: SHA-3 algorithms are now supported.
18411888
-->
18421889
18431890
* Type: {string|Algorithm}
@@ -1848,6 +1895,9 @@ If represented as a {string}, the value must be one of:
18481895
* `'SHA-256'`
18491896
* `'SHA-384'`
18501897
* `'SHA-512'`
1898+
* `'SHA3-256'`[^modern-algos]
1899+
* `'SHA3-384'`[^modern-algos]
1900+
* `'SHA3-512'`[^modern-algos]
18511901
18521902
If represented as an {Algorithm}, the object's `name` property
18531903
must be one of the above listed values.
@@ -1890,6 +1940,10 @@ added: v15.0.0
18901940
18911941
<!-- YAML
18921942
added: v15.0.0
1943+
changes:
1944+
- version: REPLACEME
1945+
pr-url: https://github.com/nodejs/node/pull/59365
1946+
description: SHA-3 algorithms are now supported.
18931947
-->
18941948
18951949
* Type: {string|Algorithm}
@@ -1900,6 +1954,9 @@ If represented as a {string}, the value must be one of:
19001954
* `'SHA-256'`
19011955
* `'SHA-384'`
19021956
* `'SHA-512'`
1957+
* `'SHA3-256'`[^modern-algos]
1958+
* `'SHA3-384'`[^modern-algos]
1959+
* `'SHA3-512'`[^modern-algos]
19031960
19041961
If represented as an {Algorithm}, the object's `name` property
19051962
must be one of the above listed values.
@@ -1965,6 +2022,10 @@ added: v15.0.0
19652022
19662023
<!-- YAML
19672024
added: v15.0.0
2025+
changes:
2026+
- version: REPLACEME
2027+
pr-url: https://github.com/nodejs/node/pull/59365
2028+
description: SHA-3 algorithms are now supported.
19682029
-->
19692030
19702031
* Type: {string|Algorithm}
@@ -1975,6 +2036,9 @@ If represented as a {string}, the value must be one of:
19752036
* `'SHA-256'`
19762037
* `'SHA-384'`
19772038
* `'SHA-512'`
2039+
* `'SHA3-256'`[^modern-algos]
2040+
* `'SHA3-384'`[^modern-algos]
2041+
* `'SHA3-512'`[^modern-algos]
19782042
19792043
If represented as an {Algorithm}, the object's `name` property
19802044
must be one of the above listed values.

lib/internal/crypto/hash.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ async function asyncDigest(algorithm, data) {
212212
// Fall through
213213
case 'SHA-512':
214214
// Fall through
215+
case 'SHA3-256':
216+
// Fall through
217+
case 'SHA3-384':
218+
// Fall through
219+
case 'SHA3-512':
220+
// Fall through
215221
case 'cSHAKE128':
216222
// Fall through
217223
case 'cSHAKE256':

lib/internal/crypto/hashnames.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,58 @@ const kHashContextJwkHmac = 6;
1717
// make it easier in the code.
1818

1919
const kHashNames = {
20-
sha1: {
20+
'sha1': {
2121
[kHashContextNode]: 'sha1',
2222
[kHashContextWebCrypto]: 'SHA-1',
2323
[kHashContextJwkRsa]: 'RS1',
2424
[kHashContextJwkRsaPss]: 'PS1',
2525
[kHashContextJwkRsaOaep]: 'RSA-OAEP',
2626
[kHashContextJwkHmac]: 'HS1',
2727
},
28-
sha256: {
28+
'sha256': {
2929
[kHashContextNode]: 'sha256',
3030
[kHashContextWebCrypto]: 'SHA-256',
3131
[kHashContextJwkRsa]: 'RS256',
3232
[kHashContextJwkRsaPss]: 'PS256',
3333
[kHashContextJwkRsaOaep]: 'RSA-OAEP-256',
3434
[kHashContextJwkHmac]: 'HS256',
3535
},
36-
sha384: {
36+
'sha384': {
3737
[kHashContextNode]: 'sha384',
3838
[kHashContextWebCrypto]: 'SHA-384',
3939
[kHashContextJwkRsa]: 'RS384',
4040
[kHashContextJwkRsaPss]: 'PS384',
4141
[kHashContextJwkRsaOaep]: 'RSA-OAEP-384',
4242
[kHashContextJwkHmac]: 'HS384',
4343
},
44-
sha512: {
44+
'sha512': {
4545
[kHashContextNode]: 'sha512',
4646
[kHashContextWebCrypto]: 'SHA-512',
4747
[kHashContextJwkRsa]: 'RS512',
4848
[kHashContextJwkRsaPss]: 'PS512',
4949
[kHashContextJwkRsaOaep]: 'RSA-OAEP-512',
5050
[kHashContextJwkHmac]: 'HS512',
5151
},
52-
shake128: {
52+
'shake128': {
5353
[kHashContextNode]: 'shake128',
5454
[kHashContextWebCrypto]: 'cSHAKE128',
5555
},
56-
shake256: {
56+
'shake256': {
5757
[kHashContextNode]: 'shake256',
5858
[kHashContextWebCrypto]: 'cSHAKE256',
5959
},
60+
'sha3-256': {
61+
[kHashContextNode]: 'sha3-256',
62+
[kHashContextWebCrypto]: 'SHA3-256',
63+
},
64+
'sha3-384': {
65+
[kHashContextNode]: 'sha3-384',
66+
[kHashContextWebCrypto]: 'SHA3-384',
67+
},
68+
'sha3-512': {
69+
[kHashContextNode]: 'sha3-512',
70+
[kHashContextWebCrypto]: 'SHA3-512',
71+
},
6072
};
6173

6274
{

lib/internal/crypto/mac.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,6 @@ async function hmacGenerateKey(algorithm, extractable, keyUsages) {
6767
extractable);
6868
}
6969

70-
function getAlgorithmName(hash) {
71-
switch (hash) {
72-
case 'SHA-1': // Fall through
73-
case 'SHA-256': // Fall through
74-
case 'SHA-384': // Fall through
75-
case 'SHA-512': // Fall through
76-
return `HS${hash.slice(4)}`;
77-
default:
78-
throw lazyDOMException('Unsupported digest algorithm', 'DataError');
79-
}
80-
}
81-
8270
function hmacImportKey(
8371
format,
8472
keyData,
@@ -126,7 +114,9 @@ function hmacImportKey(
126114
}
127115

128116
if (keyData.alg !== undefined) {
129-
if (keyData.alg !== getAlgorithmName(algorithm.hash.name))
117+
const expected =
118+
normalizeHashName(algorithm.hash.name, normalizeHashName.kContextJwkHmac);
119+
if (expected && keyData.alg !== expected)
130120
throw lazyDOMException(
131121
'JWK "alg" does not match the requested algorithm',
132122
'DataError');

lib/internal/crypto/rsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function rsaImportKey(
281281
algorithm.name === 'RSA-PSS' ? normalizeHashName.kContextJwkRsaPss :
282282
normalizeHashName.kContextJwkRsaOaep);
283283

284-
if (keyData.alg !== expected)
284+
if (expected && keyData.alg !== expected)
285285
throw lazyDOMException(
286286
'JWK "alg" does not match the requested algorithm',
287287
'DataError');

lib/internal/crypto/util.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ const experimentalAlgorithms = ObjectEntries({
287287
},
288288
'cSHAKE128': { digest: 'CShakeParams' },
289289
'cSHAKE256': { digest: 'CShakeParams' },
290+
'SHA3-256': { digest: null },
291+
'SHA3-384': { digest: null },
292+
'SHA3-512': { digest: null },
290293
});
291294

292295
for (const { 0: algorithm, 1: nid } of [
@@ -552,15 +555,28 @@ function getBlockSize(name) {
552555
// Fall through
553556
case 'SHA-512':
554557
return 1024;
558+
case 'SHA3-256':
559+
return 1088;
560+
case 'SHA3-384':
561+
return 832;
562+
case 'SHA3-512':
563+
return 576;
555564
}
556565
}
557566

558567
function getDigestSizeInBytes(name) {
559568
switch (name) {
560-
case 'SHA-1': return 20;
561-
case 'SHA-256': return 32;
562-
case 'SHA-384': return 48;
563-
case 'SHA-512': return 64;
569+
case 'SHA-1':
570+
return 20;
571+
case 'SHA-256': // Fall through
572+
case 'SHA3-256':
573+
return 32;
574+
case 'SHA-384': // Fall through
575+
case 'SHA3-384':
576+
return 48;
577+
case 'SHA-512': // Fall through
578+
case 'SHA3-512':
579+
return 64;
564580
}
565581
}
566582

0 commit comments

Comments
 (0)