Skip to content
/ node Public
forked from nodejs/node

Commit 9dcc228

Browse files
committed
crypto: fix webcrypto deriveBits validations
PR-URL: nodejs#44173 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Backport-PR-URL: nodejs#44872
1 parent c9080aa commit 9dcc228

File tree

5 files changed

+23
-521
lines changed

5 files changed

+23
-521
lines changed

lib/internal/crypto/hkdf.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ function hkdfSync(hash, key, salt, info, length) {
142142
}
143143

144144
async function hkdfDeriveBits(algorithm, baseKey, length) {
145-
validateUint32(length, 'length');
146145
const { hash } = algorithm;
147146
const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
148147
const info = getArrayBufferOrView(algorithm.info, 'algorithm.info');
@@ -153,6 +152,9 @@ async function hkdfDeriveBits(algorithm, baseKey, length) {
153152
if (length !== undefined) {
154153
if (length === 0)
155154
throw lazyDOMException('length cannot be zero', 'OperationError');
155+
if (length === null)
156+
throw lazyDOMException('length cannot be null', 'OperationError');
157+
validateUint32(length, 'length');
156158
if (length % 8) {
157159
throw lazyDOMException(
158160
'length must be a multiple of 8',

lib/internal/crypto/pbkdf2.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ function check(password, salt, iterations, keylen, digest) {
101101
}
102102

103103
async function pbkdf2DeriveBits(algorithm, baseKey, length) {
104-
validateUint32(length, 'length');
105104
const { iterations } = algorithm;
106105
let { hash } = algorithm;
107106
const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
108107
if (hash === undefined)
109108
throw new ERR_MISSING_OPTION('algorithm.hash');
110-
validateInteger(iterations, 'algorithm.iterations', 1);
109+
validateInteger(iterations, 'algorithm.iterations');
110+
if (iterations === 0)
111+
throw lazyDOMException(
112+
'iterations cannot be zero',
113+
'OperationError');
111114

112115
hash = normalizeHashName(hash.name);
113116

@@ -117,6 +120,9 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
117120
if (length !== undefined) {
118121
if (length === 0)
119122
throw lazyDOMException('length cannot be zero', 'OperationError');
123+
if (length === null)
124+
throw lazyDOMException('length cannot be null', 'OperationError');
125+
validateUint32(length, 'length');
120126
if (length % 8) {
121127
throw lazyDOMException(
122128
'length must be a multiple of 8',

test/parallel/test-webcrypto-derivebits-hkdf.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,18 @@ async function testDeriveBitsBadLengths(
259259
return Promise.all([
260260
assert.rejects(
261261
subtle.deriveBits(algorithm, baseKeys[size], 0), {
262-
message: /length cannot be zero/
262+
message: /length cannot be zero/,
263+
name: 'OperationError',
263264
}),
264265
assert.rejects(
265266
subtle.deriveBits(algorithm, baseKeys[size], null), {
266-
code: 'ERR_INVALID_ARG_TYPE'
267+
message: 'length cannot be null',
268+
name: 'OperationError',
267269
}),
268270
assert.rejects(
269271
subtle.deriveBits(algorithm, baseKeys[size], 15), {
270-
message: /length must be a multiple of 8/
272+
message: /length must be a multiple of 8/,
273+
name: 'OperationError',
271274
}),
272275
]);
273276
}

test/pummel/test-webcrypto-derivebits-pbkdf2.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,18 @@ async function testDeriveBitsBadLengths(
448448
return Promise.all([
449449
assert.rejects(
450450
subtle.deriveBits(algorithm, baseKeys[size], 0), {
451-
message: /length cannot be zero/
451+
message: /length cannot be zero/,
452+
name: 'OperationError',
452453
}),
453454
assert.rejects(
454455
subtle.deriveBits(algorithm, baseKeys[size], null), {
455-
code: 'ERR_INVALID_ARG_TYPE'
456+
message: 'length cannot be null',
457+
name: 'OperationError',
456458
}),
457459
assert.rejects(
458460
subtle.deriveBits(algorithm, baseKeys[size], 15), {
459-
message: /length must be a multiple of 8/
461+
message: /length must be a multiple of 8/,
462+
name: 'OperationError',
460463
}),
461464
]);
462465
}

0 commit comments

Comments
 (0)