diff --git a/doc/api/crypto.md b/doc/api/crypto.md
index a9a96267991b96..fa0804b86fe15c 100644
--- a/doc/api/crypto.md
+++ b/doc/api/crypto.md
@@ -1587,8 +1587,8 @@ changes:
description: The default encoding for `password` if it is a string changed
from `binary` to `utf8`.
-->
-- `password` {string}
-- `salt` {string}
+- `password` {string|Buffer|TypedArray}
+- `salt` {string|Buffer|TypedArray}
- `iterations` {number}
- `keylen` {number}
- `digest` {string}
@@ -1602,8 +1602,10 @@ applied to derive a key of the requested byte length (`keylen`) from the
`password`, `salt` and `iterations`.
The supplied `callback` function is called with two arguments: `err` and
-`derivedKey`. If an error occurs, `err` will be set; otherwise `err` will be
-null. The successfully generated `derivedKey` will be passed as a [`Buffer`][].
+`derivedKey`. If an error occurs while deriving the key, `err` will be set;
+otherwise `err` will be null. By default, the successfully generated
+`derivedKey` will be passed to the callback as a [`Buffer`][]. An error will be
+thrown if any of the input arguments specify invalid values or types.
The `iterations` argument must be a number set as high as possible. The
higher the number of iterations, the more secure the derived key will be,
@@ -1623,6 +1625,18 @@ crypto.pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
});
```
+The `crypto.DEFAULT_ENCODING` may be used to change the way the `derivedKey`
+is passed to the callback:
+
+```js
+const crypto = require('crypto');
+crypto.DEFAULT_ENCODING = 'hex';
+crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
+ if (err) throw err;
+ console.log(derivedKey); // '3745e48...aa39b34'
+});
+```
+
An array of supported digest functions can be retrieved using
[`crypto.getHashes()`][].
@@ -1643,8 +1657,8 @@ changes:
description: The default encoding for `password` if it is a string changed
from `binary` to `utf8`.
-->
-- `password` {string}
-- `salt` {string}
+- `password` {string|Buffer|TypedArray}
+- `salt` {string|Buffer|TypedArray}
- `iterations` {number}
- `keylen` {number}
- `digest` {string}
@@ -1673,6 +1687,16 @@ const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 64, 'sha512');
console.log(key.toString('hex')); // '3745e48...08d59ae'
```
+The `crypto.DEFAULT_ENCODING` may be used to change the way the `derivedKey`
+is returned:
+
+```js
+const crypto = require('crypto');
+crypto.DEFAULT_ENCODING = 'hex';
+const key = crypto.pbkdf2Sync('secret', 'salt', 100000, 512, 'sha512');
+console.log(key); // '3745e48...aa39b34'
+```
+
An array of supported digest functions can be retrieved using
[`crypto.getHashes()`][].
diff --git a/doc/api/errors.md b/doc/api/errors.md
index 5b8fb7875a7e4b..b78c6b36d634db 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -637,6 +637,11 @@ Used when the native call from `process.cpuUsage` cannot be processed properly.
Used when an invalid value for the `format` argument has been passed to the
`crypto.ECDH()` class `getPublicKey()` method.
+
+### ERR_CRYPTO_INVALID_DIGEST
+
+Used when an invalid [crypto digest algorithm][] is specified.
+
### ERR_DNS_SET_SERVERS_FAILED
@@ -1355,6 +1360,7 @@ closed.
[Node.js Error Codes]: #nodejs-error-codes
[V8's stack trace API]: https://github.com/v8/v8/wiki/Stack-Trace-API
[WHATWG URL API]: url.html#url_the_whatwg_url_api
+[crypto digest algorithm]: crypto.html#crypto_crypto_gethashes
[domains]: domain.html
[event emitter-based]: events.html#events_class_eventemitter
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
diff --git a/lib/internal/crypto/pbkdf2.js b/lib/internal/crypto/pbkdf2.js
index 5398321ece2aef..2fc211a87d7635 100644
--- a/lib/internal/crypto/pbkdf2.js
+++ b/lib/internal/crypto/pbkdf2.js
@@ -5,9 +5,13 @@ const {
getDefaultEncoding,
toBuf
} = require('internal/crypto/util');
+const { isArrayBufferView } = require('internal/util/types');
const {
PBKDF2
} = process.binding('crypto');
+const {
+ INT_MAX
+} = process.binding('constants').crypto;
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
if (typeof digest === 'function') {
@@ -34,10 +38,39 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
password = toBuf(password);
salt = toBuf(salt);
+ if (!isArrayBufferView(password)) {
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'password',
+ ['string', 'Buffer', 'TypedArray']);
+ }
+
+ if (!isArrayBufferView(salt)) {
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'salt',
+ ['string', 'Buffer', 'TypedArray']);
+ }
+
+ if (typeof iterations !== 'number')
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'iterations', 'number');
+
+ if (iterations < 0)
+ throw new errors.RangeError('ERR_OUT_OF_RANGE', 'iterations');
+
+ if (typeof keylen !== 'number')
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'keylen', 'number');
+
+ if (keylen < 0 ||
+ !Number.isFinite(keylen) ||
+ keylen > INT_MAX) {
+ throw new errors.RangeError('ERR_OUT_OF_RANGE', 'keylen');
+ }
+
const encoding = getDefaultEncoding();
- if (encoding === 'buffer')
- return PBKDF2(password, salt, iterations, keylen, digest, callback);
+ if (encoding === 'buffer') {
+ const ret = PBKDF2(password, salt, iterations, keylen, digest, callback);
+ if (ret === -1)
+ throw new errors.TypeError('ERR_CRYPTO_INVALID_DIGEST', digest);
+ return ret;
+ }
// at this point, we need to handle encodings.
if (callback) {
@@ -46,9 +79,12 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
ret = ret.toString(encoding);
callback(er, ret);
}
- PBKDF2(password, salt, iterations, keylen, digest, next);
+ if (PBKDF2(password, salt, iterations, keylen, digest, next) === -1)
+ throw new errors.TypeError('ERR_CRYPTO_INVALID_DIGEST', digest);
} else {
- var ret = PBKDF2(password, salt, iterations, keylen, digest);
+ const ret = PBKDF2(password, salt, iterations, keylen, digest);
+ if (ret === -1)
+ throw new errors.TypeError('ERR_CRYPTO_INVALID_DIGEST', digest);
return ret.toString(encoding);
}
}
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 975ae13d536661..a719570841e090 100755
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -157,6 +157,7 @@ E('ERR_CRYPTO_ECDH_INVALID_FORMAT', 'Invalid ECDH format: %s');
E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16');
E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called');
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed');
+E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s');
E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign');
E('ERR_DNS_SET_SERVERS_FAILED', (err, servers) =>
`c-ares failed to set servers: "${err}" [${servers}]`);
diff --git a/src/node_constants.cc b/src/node_constants.cc
index 787f44a1f1643e..ba33d65d1dc087 100644
--- a/src/node_constants.cc
+++ b/src/node_constants.cc
@@ -1180,6 +1180,7 @@ void DefineCryptoConstants(Local