GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — crypto: coerce -0 keylen to +0 in pbkdf2 and scrypt · nodejs/node@61826df

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/crypto
    • pbkdf2.js
    • scrypt.js
  • test/parallel
    • test-crypto-pbkdf2.js
    • test-crypto-scrypt.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/crypto/pbkdf2.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ function check(password, salt, iterations, keylen, digest) {
8888
// to the 31-bit range here (which is plenty).
8989
validateInt32(iterations, 'iterations', 1);
9090
validateInt32(keylen, 'keylen', 0);
91+
// Coerce -0 to +0.
92+
keylen += 0;
9193

9294
return { password, salt, iterations, keylen, digest };
9395
}
Collapse file

‎lib/internal/crypto/scrypt.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/scrypt.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ function check(password, salt, keylen, options) {
8383
password = getArrayBufferOrView(password, 'password');
8484
salt = getArrayBufferOrView(salt, 'salt');
8585
validateInt32(keylen, 'keylen', 0);
86+
// Coerce -0 to +0.
87+
keylen += 0;
8688

8789
let { N, r, p, maxmem } = defaults;
8890
if (options && options !== defaults) {
Collapse file

‎test/parallel/test-crypto-pbkdf2.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-pbkdf2.js
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,35 @@ for (const iterations of [-1, 0, 2147483648]) {
110110
});
111111
});
112112

113+
// `-0` keylen must not abort the process via the native binding's
114+
// IsInt32() assertion. Behavior of `keylen=0` itself varies by OpenSSL
115+
// build (bundled returns an empty buffer; some shared OpenSSL builds
116+
// throw); the requirement here is only that `-0` produces the same
117+
// outcome as `+0`.
118+
{
119+
let posError;
120+
let posResult;
121+
try {
122+
posResult = crypto.pbkdf2Sync('password', 'salt', 1, 0, 'sha256');
123+
} catch (err) {
124+
posError = err;
125+
}
126+
let negError;
127+
let negResult;
128+
try {
129+
negResult = crypto.pbkdf2Sync('password', 'salt', 1, -0, 'sha256');
130+
} catch (err) {
131+
negError = err;
132+
}
133+
if (posError !== undefined) {
134+
assert.strictEqual(negError?.message, posError.message);
135+
} else {
136+
assert.deepStrictEqual(negResult, posResult);
137+
}
138+
139+
crypto.pbkdf2('password', 'salt', 1, -0, 'sha256', common.mustCall());
140+
}
141+
113142
// Should not get FATAL ERROR with empty password and salt
114143
// https://github.com/nodejs/node/issues/8571
115144
crypto.pbkdf2('', '', 1, 32, 'sha256', common.mustSucceed());
Collapse file

‎test/parallel/test-crypto-scrypt.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-scrypt.js
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,30 @@ for (const { args, expected } of badargs) {
272272
['p', 1], ['parallelization', 1],
273273
].forEach((arg) => testParameter(...arg));
274274
}
275+
276+
// `-0` keylen must not abort the process via the native binding's
277+
// IsInt32() assertion. Assert that `-0` produces the same outcome as
278+
// `+0` (which differs by OpenSSL build).
279+
{
280+
let posError;
281+
let posResult;
282+
try {
283+
posResult = crypto.scryptSync('', '', 0);
284+
} catch (err) {
285+
posError = err;
286+
}
287+
let negError;
288+
let negResult;
289+
try {
290+
negResult = crypto.scryptSync('', '', -0);
291+
} catch (err) {
292+
negError = err;
293+
}
294+
if (posError !== undefined) {
295+
assert.strictEqual(negError?.message, posError.message);
296+
} else {
297+
assert.deepStrictEqual(negResult, posResult);
298+
}
299+
300+
crypto.scrypt('', '', -0, common.mustCall());
301+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.