Skip to content

Commit 10f5fa7

Browse files
tniessenaddaleax
authored andcommitted
crypto: forbid setting the PBKDF2 iter count to 0
RFC 2898 does not permit an iteration count of zero, and OpenSSL 1.1.1 will treat it as one iteration internally. Future OpenSSL versions will reject such inputs (already on master branch), but until that happens, Node.js should manually reject them. Refs: nodejs/webcrypto#29 PR-URL: #30578 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 74f8196 commit 10f5fa7

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

doc/api/crypto.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,10 @@ console.log(hashes); // ['DSA', 'DSA-SHA', 'DSA-SHA1', ...]
22942294
<!-- YAML
22952295
added: v0.5.5
22962296
changes:
2297+
- version: REPLACEME
2298+
pr-url: https://github.com/nodejs/node/pull/30578
2299+
description: The `iterations` parameter is now restricted to positive
2300+
values. Earlier releases treated other values as one.
22972301
- version: v8.0.0
22982302
pr-url: https://github.com/nodejs/node/pull/11305
22992303
description: The `digest` parameter is always required now.
@@ -2369,6 +2373,10 @@ negative performance implications for some applications; see the
23692373
<!-- YAML
23702374
added: v0.9.3
23712375
changes:
2376+
- version: REPLACEME
2377+
pr-url: https://github.com/nodejs/node/pull/30578
2378+
description: The `iterations` parameter is now restricted to positive
2379+
values. Earlier releases treated other values as one.
23722380
- version: v6.0.0
23732381
pr-url: https://github.com/nodejs/node/pull/4047
23742382
description: Calling this function without passing the `digest` parameter

lib/internal/crypto/pbkdf2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function check(password, salt, iterations, keylen, digest) {
6666

6767
password = getArrayBufferView(password, 'password');
6868
salt = getArrayBufferView(salt, 'salt');
69-
validateUint32(iterations, 'iterations');
69+
validateUint32(iterations, 'iterations', true);
7070
validateUint32(keylen, 'keylen');
7171

7272
return { password, salt, iterations, keylen, digest };

test/parallel/test-crypto-pbkdf2.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ assert.throws(
6868
}
6969
);
7070

71-
assert.throws(
72-
() => crypto.pbkdf2Sync('password', 'salt', -1, 20, 'sha1'),
73-
{
74-
code: 'ERR_OUT_OF_RANGE',
75-
name: 'RangeError',
76-
message: 'The value of "iterations" is out of range. ' +
77-
'It must be >= 0 && < 4294967296. Received -1'
78-
}
79-
);
71+
for (const iterations of [-1, 0]) {
72+
assert.throws(
73+
() => crypto.pbkdf2Sync('password', 'salt', iterations, 20, 'sha1'),
74+
{
75+
code: 'ERR_OUT_OF_RANGE',
76+
name: 'RangeError',
77+
message: 'The value of "iterations" is out of range. ' +
78+
`It must be >= 1 && < 4294967296. Received ${iterations}`
79+
}
80+
);
81+
}
8082

8183
['str', null, undefined, [], {}].forEach((notNumber) => {
8284
assert.throws(

0 commit comments

Comments
 (0)