Skip to content

Commit 19ad6b8

Browse files
committed
crypto: deprecate digest == null in PBKDF2
I assume that permitting digest === null was unintentional when digest === undefined was deprecated since their behavior was equivalent. The sha1 default for digest === null has somehow made it through refactoring of the PBKDF2 module multiple times, even though digest === undefined has been EOL for some time now. This change deprecates setting digest to null so we can fix the behavior in Node.js 12 or so. PR-URL: #22861 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 92fd4fc commit 19ad6b8

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

doc/api/crypto.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1786,8 +1786,8 @@ otherwise `err` will be `null`. By default, the successfully generated
17861786
`derivedKey` will be passed to the callback as a [`Buffer`][]. An error will be
17871787
thrown if any of the input arguments specify invalid values or types.
17881788

1789-
If `digest` is `null`, `'sha1'` will be used. This behavior will be deprecated
1790-
in a future version of Node.js.
1789+
If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
1790+
please specify a `digest` explicitely.
17911791

17921792
The `iterations` argument must be a number set as high as possible. The
17931793
higher the number of iterations, the more secure the derived key will be,
@@ -1852,8 +1852,8 @@ applied to derive a key of the requested byte length (`keylen`) from the
18521852
If an error occurs an `Error` will be thrown, otherwise the derived key will be
18531853
returned as a [`Buffer`][].
18541854

1855-
If `digest` is `null`, `'sha1'` will be used. This behavior will be deprecated
1856-
in a future version of Node.js.
1855+
If `digest` is `null`, `'sha1'` will be used. This behavior is deprecated,
1856+
please specify a `digest` explicitely.
18571857

18581858
The `iterations` argument must be a number set as high as possible. The
18591859
higher the number of iterations, the more secure the derived key will be,

doc/api/deprecations.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,31 @@ to the `constants` property exposed by the relevant module. For instance,
227227
### DEP0009: crypto.pbkdf2 without digest
228228
<!-- YAML
229229
changes:
230+
- version: REPLACEME
231+
pr-url: https://github.com/nodejs/node/pull/22861
232+
description: Runtime deprecation (for `digest === null`).
230233
- version: v8.0.0
231234
pr-url: https://github.com/nodejs/node/pull/11305
232-
description: End-of-Life.
235+
description: End-of-Life (for `digest === undefined`).
233236
- version: v6.12.0
234237
pr-url: https://github.com/nodejs/node/pull/10116
235238
description: A deprecation code has been assigned.
236239
- version: v6.0.0
237240
pr-url: https://github.com/nodejs/node/pull/4047
238-
description: Runtime deprecation.
241+
description: Runtime deprecation (for `digest === undefined`).
239242
-->
240243

241-
Type: End-of-Life
244+
Type: Runtime
242245

243246
Use of the [`crypto.pbkdf2()`][] API without specifying a digest was deprecated
244247
in Node.js 6.0 because the method defaulted to using the non-recommended
245248
`'SHA1'` digest. Previously, a deprecation warning was printed. Starting in
246-
Node.js 8.0.0, calling `crypto.pbkdf2()` or `crypto.pbkdf2Sync()` with an
247-
undefined `digest` will throw a `TypeError`.
249+
Node.js 8.0.0, calling `crypto.pbkdf2()` or `crypto.pbkdf2Sync()` with
250+
`digest` set to `undefined` will throw a `TypeError`.
251+
252+
Beginning in Node.js REPLACEME, calling these functions with `digest` set to
253+
`null` will print a deprecation warning to align with the behavior when `digest`
254+
is `undefined`.
248255

249256
<a id="DEP0010"></a>
250257
### DEP0010: crypto.createCredentials

lib/internal/crypto/pbkdf2.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { AsyncWrap, Providers } = internalBinding('async_wrap');
55
const { Buffer } = require('buffer');
66
const { pbkdf2: _pbkdf2 } = internalBinding('crypto');
77
const { validateUint32 } = require('internal/validators');
8+
const { deprecate } = require('internal/util');
89
const {
910
ERR_CRYPTO_INVALID_DIGEST,
1011
ERR_CRYPTO_PBKDF2_ERROR,
@@ -51,11 +52,16 @@ function pbkdf2Sync(password, salt, iterations, keylen, digest) {
5152
return keybuf.toString(encoding);
5253
}
5354

55+
const defaultDigest = deprecate(() => 'sha1',
56+
'Calling pbkdf2 or pbkdf2Sync with "digest" ' +
57+
'set to null is deprecated.',
58+
'DEP0009');
59+
5460
function check(password, salt, iterations, keylen, digest) {
5561
if (typeof digest !== 'string') {
5662
if (digest !== null)
5763
throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null'], digest);
58-
digest = 'sha1';
64+
digest = defaultDigest();
5965
}
6066

6167
password = validateArrayBufferView(password, 'password');

test/parallel/test-crypto-pbkdf2.js

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

9+
common.expectWarning(
10+
'DeprecationWarning',
11+
'Calling pbkdf2 or pbkdf2Sync with "digest" set to null is deprecated.',
12+
'DEP0009');
13+
914
//
1015
// Test PBKDF2 with RFC 6070 test vectors (except #4)
1116
//
@@ -64,7 +69,7 @@ assert.throws(
6469
);
6570

6671
assert.throws(
67-
() => crypto.pbkdf2Sync('password', 'salt', -1, 20, null),
72+
() => crypto.pbkdf2Sync('password', 'salt', -1, 20, 'sha1'),
6873
{
6974
code: 'ERR_OUT_OF_RANGE',
7075
name: 'RangeError [ERR_OUT_OF_RANGE]',

0 commit comments

Comments
 (0)