Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,13 @@ function getBlockSize(name) {
case 'SHA-512':
return 1024;
case 'SHA3-256':
return 1088;
// Fall through
case 'SHA3-384':
return 832;
// Fall through
case 'SHA3-512':
return 576;
// This interaction is not defined for now.
// https://github.com/WICG/webcrypto-modern-algos/issues/23
throw lazyDOMException('Explicit algorithm length member is required', 'NotSupportedError');
}
}

Expand Down
13 changes: 12 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
ReflectConstruct,
StringPrototypeRepeat,
StringPrototypeSlice,
StringPrototypeStartsWith,
SymbolToStringTag,
} = primordials;

Expand Down Expand Up @@ -1230,7 +1231,6 @@ function check(op, alg, length) {
case 'sign':
case 'verify':
case 'digest':
case 'generateKey':
case 'importKey':
case 'exportKey':
case 'wrapKey':
Expand All @@ -1255,6 +1255,17 @@ function check(op, alg, length) {

return true;
}
case 'generateKey': {
if (
normalizedAlgorithm.name === 'HMAC' &&
normalizedAlgorithm.length === undefined &&
StringPrototypeStartsWith(normalizedAlgorithm.hash.name, 'SHA3-')
) {
return false;
}

return true;
}
default: {
const assert = require('internal/assert');
assert.fail('Unreachable code');
Expand Down
27 changes: 22 additions & 5 deletions test/fixtures/webcrypto/supports-sha3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ export const vectors = {
[!boringSSL, 'SHA3-512'],
],
'generateKey': [
[!boringSSL, { name: 'HMAC', hash: 'SHA3-256' }],
[!boringSSL, { name: 'HMAC', hash: 'SHA3-256', length: 256 }],
[false, { name: 'HMAC', hash: 'SHA3-256', length: 25 }],
[!boringSSL, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA3-256', ...RSA_KEY_GEN }],
[!boringSSL, { name: 'RSA-PSS', hash: 'SHA3-256', ...RSA_KEY_GEN }],
[!boringSSL, { name: 'RSA-OAEP', hash: 'SHA3-256', ...RSA_KEY_GEN }],
[!boringSSL, { name: 'HMAC', hash: 'SHA3-256' }],
[!boringSSL, { name: 'HMAC', hash: 'SHA3-256', length: 256 }],
[false, { name: 'HMAC', hash: 'SHA3-256', length: 25 }],
[false, { name: 'HMAC', hash: 'SHA3-256', length: 0 }],

// This interaction is not defined for now.
// https://github.com/WICG/webcrypto-modern-algos/issues/23
[false, { name: 'HMAC', hash: 'SHA3-256' }],
],
'deriveKey': [
[!boringSSL,
{ name: 'HKDF', hash: 'SHA3-256', salt: Buffer.alloc(0), info: Buffer.alloc(0) },
{ name: 'AES-CBC', length: 128 }],
[!boringSSL,
{ name: 'HKDF', hash: 'SHA3-256', salt: Buffer.alloc(0), info: Buffer.alloc(0) },
{ name: 'HMAC', hash: 'SHA3-256' }],
{ name: 'HMAC', hash: 'SHA3-256', length: 256 }],
[false,
{ name: 'HKDF', hash: 'SHA3-256', salt: Buffer.alloc(0), info: Buffer.alloc(0) },
'HKDF'],
Expand All @@ -45,14 +47,29 @@ export const vectors = {
{ name: 'AES-CBC', length: 128 }],
[!boringSSL,
{ name: 'PBKDF2', hash: 'SHA3-256', salt: Buffer.alloc(0), iterations: 1 },
{ name: 'HMAC', hash: 'SHA3-256' }],
{ name: 'HMAC', hash: 'SHA3-256', length: 256 }],
[false,
{ name: 'PBKDF2', hash: 'SHA3-256', salt: Buffer.alloc(0), iterations: 1 },
'HKDF'],
[!boringSSL,
{ name: 'X25519', public: X25519.publicKey },
{ name: 'HMAC', hash: 'SHA3-256' }],
{ name: 'HMAC', hash: 'SHA3-256', length: 256 }],
[!boringSSL,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA3-256', length: 256 }],

// This interaction is not defined for now.
// https://github.com/WICG/webcrypto-modern-algos/issues/23
[false,
{ name: 'HKDF', hash: 'SHA3-256', salt: Buffer.alloc(0), info: Buffer.alloc(0) },
{ name: 'HMAC', hash: 'SHA3-256' }],
[false,
{ name: 'PBKDF2', hash: 'SHA3-256', salt: Buffer.alloc(0), iterations: 1 },
{ name: 'HMAC', hash: 'SHA3-256' }],
[false,
{ name: 'X25519', public: X25519.publicKey },
{ name: 'HMAC', hash: 'SHA3-256' }],
[false,
{ name: 'ECDH', public: ECDH.publicKey },
{ name: 'HMAC', hash: 'SHA3-256' }],
],
Expand Down
22 changes: 16 additions & 6 deletions test/parallel/test-webcrypto-derivekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ const { KeyObject } = require('crypto');
// Not long enough secret generated by ECDH
[{ name: 'HMAC', hash: 'SHA-384' }, 'sign', 1024],
[{ name: 'HMAC', hash: 'SHA-512' }, 'sign', 1024],
[{ name: 'HMAC', hash: 'SHA3-256' }, 'sign', 1088],
[{ name: 'HMAC', hash: 'SHA3-384' }, 'sign', 832],
[{ name: 'HMAC', hash: 'SHA3-512' }, 'sign', 576],
[{ name: 'HMAC', hash: 'SHA3-256', length: 256 }, 'sign', 256],
[{ name: 'HMAC', hash: 'SHA3-384', length: 384 }, 'sign', 384],
[{ name: 'HMAC', hash: 'SHA3-512', length: 512 }, 'sign', 512],
// This interaction is not defined for now.
// https://github.com/WICG/webcrypto-modern-algos/issues/23
// [{ name: 'HMAC', hash: 'SHA3-256' }, 'sign', 256],
// [{ name: 'HMAC', hash: 'SHA3-384' }, 'sign', 384],
// [{ name: 'HMAC', hash: 'SHA3-512' }, 'sign', 512],
];

(async () => {
Expand Down Expand Up @@ -196,9 +201,14 @@ const { KeyObject } = require('crypto');
[{ name: 'HMAC', hash: 'SHA-256' }, 'sign', 512],
[{ name: 'HMAC', hash: 'SHA-384' }, 'sign', 1024],
[{ name: 'HMAC', hash: 'SHA-512' }, 'sign', 1024],
[{ name: 'HMAC', hash: 'SHA3-256' }, 'sign', 1088],
[{ name: 'HMAC', hash: 'SHA3-384' }, 'sign', 832],
[{ name: 'HMAC', hash: 'SHA3-512' }, 'sign', 576],
[{ name: 'HMAC', hash: 'SHA3-256', length: 256 }, 'sign', 256],
[{ name: 'HMAC', hash: 'SHA3-384', length: 384 }, 'sign', 384],
[{ name: 'HMAC', hash: 'SHA3-512', length: 512 }, 'sign', 512],
// This interaction is not defined for now.
// https://github.com/WICG/webcrypto-modern-algos/issues/23
// [{ name: 'HMAC', hash: 'SHA3-256' }, 'sign', 256],
// [{ name: 'HMAC', hash: 'SHA3-384' }, 'sign', 384],
// [{ name: 'HMAC', hash: 'SHA3-512' }, 'sign', 512],
];

(async () => {
Expand Down
27 changes: 14 additions & 13 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,6 @@ if (hasOpenSSL(3, 5)) {
case 'SHA-256': length = 512; break;
case 'SHA-384': length = 1024; break;
case 'SHA-512': length = 1024; break;
case 'SHA3-256': length = 1088; break;
case 'SHA3-384': length = 832; break;
case 'SHA3-512': length = 576; break;
}
}

Expand All @@ -636,20 +633,24 @@ if (hasOpenSSL(3, 5)) {
}

const kTests = [
[ undefined, 'SHA-1', ['sign', 'verify']],
[ undefined, 'SHA-256', ['sign', 'verify']],
[ undefined, 'SHA-384', ['sign', 'verify']],
[ undefined, 'SHA-512', ['sign', 'verify']],
[ 128, 'SHA-256', ['sign', 'verify']],
[ 1024, 'SHA-512', ['sign', 'verify']],
[undefined, 'SHA-1', ['sign', 'verify']],
[undefined, 'SHA-256', ['sign', 'verify']],
[undefined, 'SHA-384', ['sign', 'verify']],
[undefined, 'SHA-512', ['sign', 'verify']],
[128, 'SHA-256', ['sign', 'verify']],
[1024, 'SHA-512', ['sign', 'verify']],
];

if (!process.features.openssl_is_boringssl) {
kTests.push(

[ undefined, 'SHA3-256', ['sign', 'verify']],
[ undefined, 'SHA3-384', ['sign', 'verify']],
[ undefined, 'SHA3-512', ['sign', 'verify']],
[256, 'SHA3-256', ['sign', 'verify']],
[384, 'SHA3-384', ['sign', 'verify']],
[512, 'SHA3-512', ['sign', 'verify']],
// This interaction is not defined for now.
// https://github.com/WICG/webcrypto-modern-algos/issues/23
// [undefined, 'SHA3-256', ['sign', 'verify']],
// [undefined, 'SHA3-384', ['sign', 'verify']],
// [undefined, 'SHA3-512', ['sign', 'verify']],
);
} else {
common.printSkipMessage('Skipping unsupported SHA-3 test cases');
Expand Down
Loading