Skip to content

Commit 26f25c9

Browse files
tniessenRafaelGSS
authored andcommitted
crypto: restrict PBKDF2 args to signed int
OpenSSL internally represents the output length and the iteration count as signed integers, which is why node's C++ implementation expects these arguments to fit into signed integers as well. The JavaScript validation logic, however, only requires the arguments to be unsigned 32-bit integers, which is a superset of non-negative (signed) 32-bit integers. Change the JavaScript validation to match the expectation within C++. Fixes: #44570 PR-URL: #44575 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent eaa675d commit 26f25c9

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/internal/crypto/pbkdf2.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515

1616
const {
1717
validateFunction,
18+
validateInt32,
1819
validateInteger,
1920
validateString,
2021
validateUint32,
@@ -91,8 +92,10 @@ function check(password, salt, iterations, keylen, digest) {
9192

9293
password = getArrayBufferOrView(password, 'password');
9394
salt = getArrayBufferOrView(salt, 'salt');
94-
validateUint32(iterations, 'iterations', true);
95-
validateUint32(keylen, 'keylen');
95+
// OpenSSL uses a signed int to represent these values, so we are restricted
96+
// to the 31-bit range here (which is plenty).
97+
validateInt32(iterations, 'iterations', 1);
98+
validateInt32(keylen, 'keylen', 0);
9699

97100
return { password, salt, iterations, keylen, digest };
98101
}

test/parallel/test-crypto-pbkdf2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ assert.throws(
6363
}
6464
);
6565

66-
for (const iterations of [-1, 0]) {
66+
for (const iterations of [-1, 0, 2147483648]) {
6767
assert.throws(
6868
() => crypto.pbkdf2Sync('password', 'salt', iterations, 20, 'sha1'),
6969
{
@@ -98,7 +98,7 @@ for (const iterations of [-1, 0]) {
9898
});
9999
});
100100

101-
[-1, 4294967297].forEach((input) => {
101+
[-1, 2147483648, 4294967296].forEach((input) => {
102102
assert.throws(
103103
() => {
104104
crypto.pbkdf2('password', 'salt', 1, input, 'sha256',

0 commit comments

Comments
 (0)