Skip to content

Commit 989fd73

Browse files
tniessentargos
authored andcommitted
crypto: fix incorrect use of INT_MAX in validation
The native crypto module doesn't export INT_MAX, so all occurrences in the JavaScript layer evaluated to undefined. This change removes all such occurrences and replaces validateInt32 with validateUint32 since the native layer assumes uint32_t anyway. The alternative would be to use the constant from the constants module, but that would be pointless as far as I can tell. PR-URL: #22581 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent c47c79e commit 989fd73

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

lib/internal/crypto/pbkdf2.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const { AsyncWrap, Providers } = process.binding('async_wrap');
44
const { Buffer } = require('buffer');
5-
const { INT_MAX, pbkdf2: _pbkdf2 } = process.binding('crypto');
6-
const { validateInt32 } = require('internal/validators');
5+
const { pbkdf2: _pbkdf2 } = process.binding('crypto');
6+
const { validateUint32 } = require('internal/validators');
77
const {
88
ERR_CRYPTO_INVALID_DIGEST,
99
ERR_CRYPTO_PBKDF2_ERROR,
@@ -59,8 +59,8 @@ function check(password, salt, iterations, keylen, digest, callback) {
5959

6060
password = validateArrayBufferView(password, 'password');
6161
salt = validateArrayBufferView(salt, 'salt');
62-
iterations = validateInt32(iterations, 'iterations', 0, INT_MAX);
63-
keylen = validateInt32(keylen, 'keylen', 0, INT_MAX);
62+
iterations = validateUint32(iterations, 'iterations', 0);
63+
keylen = validateUint32(keylen, 'keylen', 0);
6464

6565
return { password, salt, iterations, keylen, digest };
6666
}

lib/internal/crypto/scrypt.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const { AsyncWrap, Providers } = process.binding('async_wrap');
44
const { Buffer } = require('buffer');
5-
const { INT_MAX, scrypt: _scrypt } = process.binding('crypto');
6-
const { validateInt32 } = require('internal/validators');
5+
const { scrypt: _scrypt } = process.binding('crypto');
6+
const { validateUint32 } = require('internal/validators');
77
const {
88
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
99
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
@@ -76,31 +76,31 @@ function check(password, salt, keylen, options, callback) {
7676

7777
password = validateArrayBufferView(password, 'password');
7878
salt = validateArrayBufferView(salt, 'salt');
79-
keylen = validateInt32(keylen, 'keylen', 0, INT_MAX);
79+
keylen = validateUint32(keylen, 'keylen');
8080

8181
let { N, r, p, maxmem } = defaults;
8282
if (options && options !== defaults) {
8383
let has_N, has_r, has_p;
8484
if (has_N = (options.N !== undefined))
85-
N = validateInt32(options.N, 'N', 0, INT_MAX);
85+
N = validateUint32(options.N, 'N');
8686
if (options.cost !== undefined) {
8787
if (has_N) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
88-
N = validateInt32(options.cost, 'cost', 0, INT_MAX);
88+
N = validateUint32(options.cost, 'cost');
8989
}
9090
if (has_r = (options.r !== undefined))
91-
r = validateInt32(options.r, 'r', 0, INT_MAX);
91+
r = validateUint32(options.r, 'r');
9292
if (options.blockSize !== undefined) {
9393
if (has_r) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
94-
r = validateInt32(options.blockSize, 'blockSize', 0, INT_MAX);
94+
r = validateUint32(options.blockSize, 'blockSize');
9595
}
9696
if (has_p = (options.p !== undefined))
97-
p = validateInt32(options.p, 'p', 0, INT_MAX);
97+
p = validateUint32(options.p, 'p');
9898
if (options.parallelization !== undefined) {
9999
if (has_p) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
100-
p = validateInt32(options.parallelization, 'parallelization', 0, INT_MAX);
100+
p = validateUint32(options.parallelization, 'parallelization');
101101
}
102102
if (options.maxmem !== undefined)
103-
maxmem = validateInt32(options.maxmem, 'maxmem', 0, INT_MAX);
103+
maxmem = validateUint32(options.maxmem, 'maxmem');
104104
if (N === 0) N = defaults.N;
105105
if (r === 0) r = defaults.r;
106106
if (p === 0) p = defaults.p;

test/parallel/test-crypto-pbkdf2.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ if (!common.hasCrypto)
66
const assert = require('assert');
77
const crypto = require('crypto');
88

9-
const { INT_MAX } = process.binding('constants').crypto;
10-
119
//
1210
// Test PBKDF2 with RFC 6070 test vectors (except #4)
1311
//
@@ -71,7 +69,7 @@ assert.throws(
7169
code: 'ERR_OUT_OF_RANGE',
7270
name: 'RangeError [ERR_OUT_OF_RANGE]',
7371
message: 'The value of "iterations" is out of range. ' +
74-
'It must be >= 0 && <= 2147483647. Received -1'
72+
'It must be >= 0 && < 4294967296. Received -1'
7573
}
7674
);
7775

@@ -100,7 +98,7 @@ assert.throws(
10098
});
10199
});
102100

103-
[-1, 4073741824, INT_MAX + 1].forEach((input) => {
101+
[-1, 4294967297].forEach((input) => {
104102
assert.throws(
105103
() => {
106104
crypto.pbkdf2('password', 'salt', 1, input, 'sha256',
@@ -109,7 +107,7 @@ assert.throws(
109107
code: 'ERR_OUT_OF_RANGE',
110108
name: 'RangeError [ERR_OUT_OF_RANGE]',
111109
message: 'The value of "keylen" is out of range. It ' +
112-
`must be >= 0 && <= 2147483647. Received ${input}`
110+
`must be >= 0 && < 4294967296. Received ${input}`
113111
});
114112
});
115113

0 commit comments

Comments
 (0)