Skip to content

Commit 81e1ec4

Browse files
panvaruyadorno
authored andcommitted
crypto: fix webcrypto generateKey() AES key length validation error
PR-URL: #44170 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent ad8ef3a commit 81e1ec4

File tree

3 files changed

+7
-681
lines changed

3 files changed

+7
-681
lines changed

lib/internal/crypto/aes.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ const {
6060
generateKey: _generateKey,
6161
} = require('internal/crypto/keygen');
6262

63-
const {
64-
validateInteger,
65-
validateOneOf,
66-
} = require('internal/validators');
67-
6863
const kMaxCounterLength = 128;
6964
const kTagLengths = [32, 64, 96, 104, 112, 120, 128];
7065
const generateKey = promisify(_generateKey);
@@ -228,8 +223,11 @@ function aesCipher(mode, key, data, algorithm) {
228223

229224
async function aesGenerateKey(algorithm, extractable, keyUsages) {
230225
const { name, length } = algorithm;
231-
validateInteger(length, 'algorithm.length');
232-
validateOneOf(length, 'algorithm.length', kAesKeyLengths);
226+
if (!ArrayPrototypeIncludes(kAesKeyLengths, length)) {
227+
throw lazyDOMException(
228+
'AES key length must be 128, 192, or 256 bits',
229+
'OperationError');
230+
}
233231

234232
const checkUsages = ['wrapKey', 'unwrapKey'];
235233
if (name !== 'AES-KW')

test/parallel/test-webcrypto-keygen.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,14 @@ const vectors = {
512512
[1, 100, 257].forEach(async (length) => {
513513
await assert.rejects(
514514
subtle.generateKey({ name, length }, true, usages), {
515-
code: 'ERR_INVALID_ARG_VALUE'
515+
name: 'OperationError'
516516
});
517517
});
518518

519519
['', {}, [], false, null, undefined].forEach(async (length) => {
520520
await assert.rejects(
521521
subtle.generateKey({ name, length }, true, usages), {
522-
code: 'ERR_INVALID_ARG_TYPE'
522+
name: 'OperationError',
523523
});
524524
});
525525
}

0 commit comments

Comments
 (0)