Skip to content

Commit ac2b10e

Browse files
panvaruyadorno
authored andcommitted
crypto: fix webcrypto deriveBits validations
PR-URL: #44173 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 4c902be commit ac2b10e

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
@@ -98,13 +98,16 @@ function check(password, salt, iterations, keylen, digest) {
9898
}
9999

100100
async function pbkdf2DeriveBits(algorithm, baseKey, length) {
101-
validateUint32(length, 'length');
102101
const { iterations } = algorithm;
103102
let { hash } = algorithm;
104103
const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
105104
if (hash === undefined)
106105
throw new ERR_MISSING_OPTION('algorithm.hash');
107-
validateInteger(iterations, 'algorithm.iterations', 1);
106+
validateInteger(iterations, 'algorithm.iterations');
107+
if (iterations === 0)
108+
throw lazyDOMException(
109+
'iterations cannot be zero',
110+
'OperationError');
108111

109112
hash = normalizeHashName(hash.name);
110113

@@ -114,6 +117,9 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
114117
if (length !== undefined) {
115118
if (length === 0)
116119
throw lazyDOMException('length cannot be zero', 'OperationError');
120+
if (length === null)
121+
throw lazyDOMException('length cannot be null', 'OperationError');
122+
validateUint32(length, 'length');
117123
if (length % 8) {
118124
throw lazyDOMException(
119125
'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)