crypto: add SHA-3 Web Cryptography digest algorithms · nodejs/node@76cde76 · GitHub
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

Lines changed: 784 additions & 77 deletions

doc/api/webcrypto.md

Lines changed: 64 additions & 0 deletions

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

0 commit comments

Comments
 (0)