Skip to content

Commit 371103d

Browse files
committed
crypto: add scrypt() and scryptSync() methods
Scrypt is a password-based key derivation function that is designed to be expensive both computationally and memory-wise in order to make brute-force attacks unrewarding. OpenSSL has had support for the scrypt algorithm since v1.1.0. Add a Node.js API modeled after `crypto.pbkdf2()` and `crypto.pbkdf2Sync()`. Changes: * Introduce helpers for copying buffers, collecting openssl errors, etc. * Add new infrastructure for offloading crypto to a worker thread. * Add a `AsyncWrap` JS class to simplify pbkdf2(), randomBytes() and scrypt(). Fixes: nodejs#8417 PR-URL: nodejs#20816 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 58176e3 commit 371103d

File tree

13 files changed

+617
-57
lines changed

13 files changed

+617
-57
lines changed

doc/api/crypto.md

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,9 +1361,9 @@ password always creates the same key. The low iteration count and
13611361
non-cryptographically secure hash algorithm allow passwords to be tested very
13621362
rapidly.
13631363

1364-
In line with OpenSSL's recommendation to use PBKDF2 instead of
1364+
In line with OpenSSL's recommendation to use a more modern algorithm instead of
13651365
[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on
1366-
their own using [`crypto.pbkdf2()`][] and to use [`crypto.createCipheriv()`][]
1366+
their own using [`crypto.scrypt()`][] and to use [`crypto.createCipheriv()`][]
13671367
to create the `Cipher` object. Users should not use ciphers with counter mode
13681368
(e.g. CTR, GCM, or CCM) in `crypto.createCipher()`. A warning is emitted when
13691369
they are used in order to avoid the risk of IV reuse that causes
@@ -1463,9 +1463,9 @@ password always creates the same key. The low iteration count and
14631463
non-cryptographically secure hash algorithm allow passwords to be tested very
14641464
rapidly.
14651465

1466-
In line with OpenSSL's recommendation to use PBKDF2 instead of
1466+
In line with OpenSSL's recommendation to use a more modern algorithm instead of
14671467
[`EVP_BytesToKey`][] it is recommended that developers derive a key and IV on
1468-
their own using [`crypto.pbkdf2()`][] and to use [`crypto.createDecipheriv()`][]
1468+
their own using [`crypto.scrypt()`][] and to use [`crypto.createDecipheriv()`][]
14691469
to create the `Decipher` object.
14701470

14711471
### crypto.createDecipheriv(algorithm, key, iv[, options])
@@ -1801,9 +1801,8 @@ The `iterations` argument must be a number set as high as possible. The
18011801
higher the number of iterations, the more secure the derived key will be,
18021802
but will take a longer amount of time to complete.
18031803

1804-
The `salt` should also be as unique as possible. It is recommended that the
1805-
salts are random and their lengths are at least 16 bytes. See
1806-
[NIST SP 800-132][] for details.
1804+
The `salt` should be as unique as possible. It is recommended that a salt is
1805+
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
18071806

18081807
Example:
18091808

@@ -1867,9 +1866,8 @@ The `iterations` argument must be a number set as high as possible. The
18671866
higher the number of iterations, the more secure the derived key will be,
18681867
but will take a longer amount of time to complete.
18691868

1870-
The `salt` should also be as unique as possible. It is recommended that the
1871-
salts are random and their lengths are at least 16 bytes. See
1872-
[NIST SP 800-132][] for details.
1869+
The `salt` should be as unique as possible. It is recommended that a salt is
1870+
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
18731871

18741872
Example:
18751873

@@ -2143,6 +2141,91 @@ threadpool request. To minimize threadpool task length variation, partition
21432141
large `randomFill` requests when doing so as part of fulfilling a client
21442142
request.
21452143

2144+
### crypto.scrypt(password, salt, keylen[, options], callback)
2145+
<!-- YAML
2146+
added: REPLACEME
2147+
-->
2148+
- `password` {string|Buffer|TypedArray}
2149+
- `salt` {string|Buffer|TypedArray}
2150+
- `keylen` {number}
2151+
- `options` {Object}
2152+
- `N` {number} CPU/memory cost parameter. Must be a power of two greater
2153+
than one. **Default:** `16384`.
2154+
- `r` {number} Block size parameter. **Default:** `8`.
2155+
- `p` {number} Parallelization parameter. **Default:** `1`.
2156+
- `maxmem` {number} Memory upper bound. It is an error when (approximately)
2157+
`128*N*r > maxmem` **Default:** `32 * 1024 * 1024`.
2158+
- `callback` {Function}
2159+
- `err` {Error}
2160+
- `derivedKey` {Buffer}
2161+
2162+
Provides an asynchronous [scrypt][] implementation. Scrypt is a password-based
2163+
key derivation function that is designed to be expensive computationally and
2164+
memory-wise in order to make brute-force attacks unrewarding.
2165+
2166+
The `salt` should be as unique as possible. It is recommended that a salt is
2167+
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
2168+
2169+
The `callback` function is called with two arguments: `err` and `derivedKey`.
2170+
`err` is an exception object when key derivation fails, otherwise `err` is
2171+
`null`. `derivedKey` is passed to the callback as a [`Buffer`][].
2172+
2173+
An exception is thrown when any of the input arguments specify invalid values
2174+
or types.
2175+
2176+
```js
2177+
const crypto = require('crypto');
2178+
// Using the factory defaults.
2179+
crypto.scrypt('secret', 'salt', 64, (err, derivedKey) => {
2180+
if (err) throw err;
2181+
console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
2182+
});
2183+
// Using a custom N parameter. Must be a power of two.
2184+
crypto.scrypt('secret', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
2185+
if (err) throw err;
2186+
console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
2187+
});
2188+
```
2189+
2190+
### crypto.scryptSync(password, salt, keylen[, options])
2191+
<!-- YAML
2192+
added: REPLACEME
2193+
-->
2194+
- `password` {string|Buffer|TypedArray}
2195+
- `salt` {string|Buffer|TypedArray}
2196+
- `keylen` {number}
2197+
- `options` {Object}
2198+
- `N` {number} CPU/memory cost parameter. Must be a power of two greater
2199+
than one. **Default:** `16384`.
2200+
- `r` {number} Block size parameter. **Default:** `8`.
2201+
- `p` {number} Parallelization parameter. **Default:** `1`.
2202+
- `maxmem` {number} Memory upper bound. It is an error when (approximately)
2203+
`128*N*r > maxmem` **Default:** `32 * 1024 * 1024`.
2204+
- Returns: {Buffer}
2205+
2206+
Provides a synchronous [scrypt][] implementation. Scrypt is a password-based
2207+
key derivation function that is designed to be expensive computationally and
2208+
memory-wise in order to make brute-force attacks unrewarding.
2209+
2210+
The `salt` should be as unique as possible. It is recommended that a salt is
2211+
random and at least 16 bytes long. See [NIST SP 800-132][] for details.
2212+
2213+
An exception is thrown when key derivation fails, otherwise the derived key is
2214+
returned as a [`Buffer`][].
2215+
2216+
An exception is thrown when any of the input arguments specify invalid values
2217+
or types.
2218+
2219+
```js
2220+
const crypto = require('crypto');
2221+
// Using the factory defaults.
2222+
const key1 = crypto.scryptSync('secret', 'salt', 64);
2223+
console.log(key1.toString('hex')); // '3745e48...08d59ae'
2224+
// Using a custom N parameter. Must be a power of two.
2225+
const key2 = crypto.scryptSync('secret', 'salt', 64, { N: 1024 });
2226+
console.log(key2.toString('hex')); // '3745e48...aa39b34'
2227+
```
2228+
21462229
### crypto.setEngine(engine[, flags])
21472230
<!-- YAML
21482231
added: v0.11.11
@@ -2650,9 +2733,9 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
26502733
[`crypto.createVerify()`]: #crypto_crypto_createverify_algorithm_options
26512734
[`crypto.getCurves()`]: #crypto_crypto_getcurves
26522735
[`crypto.getHashes()`]: #crypto_crypto_gethashes
2653-
[`crypto.pbkdf2()`]: #crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback
26542736
[`crypto.randomBytes()`]: #crypto_crypto_randombytes_size_callback
26552737
[`crypto.randomFill()`]: #crypto_crypto_randomfill_buffer_offset_size_callback
2738+
[`crypto.scrypt()`]: #crypto_crypto_scrypt_password_salt_keylen_options_callback
26562739
[`decipher.final()`]: #crypto_decipher_final_outputencoding
26572740
[`decipher.update()`]: #crypto_decipher_update_data_inputencoding_outputencoding
26582741
[`diffieHellman.setPublicKey()`]: #crypto_diffiehellman_setpublickey_publickey_encoding
@@ -2686,5 +2769,6 @@ the `crypto`, `tls`, and `https` modules and are generally specific to OpenSSL.
26862769
[RFC 3610]: https://www.rfc-editor.org/rfc/rfc3610.txt
26872770
[RFC 4055]: https://www.rfc-editor.org/rfc/rfc4055.txt
26882771
[initialization vector]: https://en.wikipedia.org/wiki/Initialization_vector
2772+
[scrypt]: https://en.wikipedia.org/wiki/Scrypt
26892773
[stream-writable-write]: stream.html#stream_writable_write_chunk_encoding_callback
26902774
[stream]: stream.html

doc/api/errors.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,18 @@ An invalid [crypto digest algorithm][] was specified.
739739
A crypto method was used on an object that was in an invalid state. For
740740
instance, calling [`cipher.getAuthTag()`][] before calling `cipher.final()`.
741741

742+
<a id="ERR_CRYPTO_SCRYPT_INVALID_PARAMETER"></a>
743+
### ERR_CRYPTO_SCRYPT_INVALID_PARAMETER
744+
745+
One or more [`crypto.scrypt()`][] or [`crypto.scryptSync()`][] parameters are
746+
outside their legal range.
747+
748+
<a id="ERR_CRYPTO_SCRYPT_NOT_SUPPORTED"></a>
749+
### ERR_CRYPTO_SCRYPT_NOT_SUPPORTED
750+
751+
Node.js was compiled without `scrypt` support. Not possible with the official
752+
release binaries but can happen with custom builds, including distro builds.
753+
742754
<a id="ERR_CRYPTO_SIGN_KEY_REQUIRED"></a>
743755
### ERR_CRYPTO_SIGN_KEY_REQUIRED
744756

@@ -1749,6 +1761,8 @@ Creation of a [`zlib`][] object failed due to incorrect configuration.
17491761
[`child_process`]: child_process.html
17501762
[`cipher.getAuthTag()`]: crypto.html#crypto_cipher_getauthtag
17511763
[`Class: assert.AssertionError`]: assert.html#assert_class_assert_assertionerror
1764+
[`crypto.scrypt()`]: crypto.html#crypto_crypto_scrypt_password_salt_keylen_options_callback
1765+
[`crypto.scryptSync()`]: crypto.html#crypto_crypto_scryptSync_password_salt_keylen_options
17521766
[`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b
17531767
[`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback
17541768
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE

lib/crypto.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const {
5252
pbkdf2,
5353
pbkdf2Sync
5454
} = require('internal/crypto/pbkdf2');
55+
const {
56+
scrypt,
57+
scryptSync
58+
} = require('internal/crypto/scrypt');
5559
const {
5660
DiffieHellman,
5761
DiffieHellmanGroup,
@@ -163,6 +167,8 @@ module.exports = exports = {
163167
randomFill,
164168
randomFillSync,
165169
rng: randomBytes,
170+
scrypt,
171+
scryptSync,
166172
setEngine,
167173
timingSafeEqual,
168174
getFips: !fipsMode ? getFipsDisabled :

lib/internal/crypto/scrypt.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict';
2+
3+
const { AsyncWrap, Providers } = process.binding('async_wrap');
4+
const { Buffer } = require('buffer');
5+
const { scrypt: _scrypt } = process.binding('crypto');
6+
const {
7+
ERR_CRYPTO_SCRYPT_INVALID_PARAMETER,
8+
ERR_CRYPTO_SCRYPT_NOT_SUPPORTED,
9+
ERR_INVALID_CALLBACK,
10+
} = require('internal/errors').codes;
11+
const {
12+
checkIsArrayBufferView,
13+
checkIsUint,
14+
getDefaultEncoding,
15+
} = require('internal/crypto/util');
16+
17+
const defaults = {
18+
N: 16384,
19+
r: 8,
20+
p: 1,
21+
maxmem: 32 << 20, // 32 MB, matches SCRYPT_MAX_MEM.
22+
};
23+
24+
function scrypt(password, salt, keylen, options, callback = defaults) {
25+
if (callback === defaults) {
26+
callback = options;
27+
options = defaults;
28+
}
29+
30+
options = check(password, salt, keylen, options);
31+
const { N, r, p, maxmem } = options;
32+
({ password, salt, keylen } = options);
33+
34+
if (typeof callback !== 'function')
35+
throw new ERR_INVALID_CALLBACK();
36+
37+
const encoding = getDefaultEncoding();
38+
const keybuf = Buffer.alloc(keylen);
39+
40+
const wrap = new AsyncWrap(Providers.SCRYPTREQUEST);
41+
wrap.ondone = (ex) => { // Retains keybuf while request is in flight.
42+
if (ex) return callback.call(wrap, ex);
43+
if (encoding === 'buffer') return callback.call(wrap, null, keybuf);
44+
callback.call(wrap, null, keybuf.toString(encoding));
45+
};
46+
47+
handleError(keybuf, password, salt, N, r, p, maxmem, wrap);
48+
}
49+
50+
function scryptSync(password, salt, keylen, options = defaults) {
51+
options = check(password, salt, keylen, options);
52+
const { N, r, p, maxmem } = options;
53+
({ password, salt, keylen } = options);
54+
const keybuf = Buffer.alloc(keylen);
55+
handleError(keybuf, password, salt, N, r, p, maxmem);
56+
const encoding = getDefaultEncoding();
57+
if (encoding === 'buffer') return keybuf;
58+
return keybuf.toString(encoding);
59+
}
60+
61+
function handleError(keybuf, password, salt, N, r, p, maxmem, wrap) {
62+
const ex = _scrypt(keybuf, password, salt, N, r, p, maxmem, wrap);
63+
64+
if (ex === undefined)
65+
return;
66+
67+
if (ex === null)
68+
throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER(); // Bad N, r, p, or maxmem.
69+
70+
throw ex; // Scrypt operation failed, exception object contains details.
71+
}
72+
73+
function check(password, salt, keylen, options, callback) {
74+
if (_scrypt === undefined)
75+
throw new ERR_CRYPTO_SCRYPT_NOT_SUPPORTED();
76+
77+
password = checkIsArrayBufferView('password', password);
78+
salt = checkIsArrayBufferView('salt', salt);
79+
keylen = checkIsUint('keylen', keylen);
80+
81+
let { N, r, p, maxmem } = defaults;
82+
if (options && options !== defaults) {
83+
if (options.hasOwnProperty('N')) N = checkIsUint('N', options.N);
84+
if (options.hasOwnProperty('r')) r = checkIsUint('r', options.r);
85+
if (options.hasOwnProperty('p')) p = checkIsUint('p', options.p);
86+
if (options.hasOwnProperty('maxmem'))
87+
maxmem = checkIsUint('maxmem', options.maxmem);
88+
if (N === 0) N = defaults.N;
89+
if (r === 0) r = defaults.r;
90+
if (p === 0) p = defaults.p;
91+
if (maxmem === 0) maxmem = defaults.maxmem;
92+
}
93+
94+
return { password, salt, keylen, N, r, p, maxmem };
95+
}
96+
97+
module.exports = { scrypt, scryptSync };

lib/internal/errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called', Error);
500500
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed', Error);
501501
E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s', TypeError);
502502
E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error);
503-
503+
E('ERR_CRYPTO_SCRYPT_INVALID_PARAMETER', 'Invalid scrypt parameter', Error);
504+
E('ERR_CRYPTO_SCRYPT_NOT_SUPPORTED', 'Scrypt algorithm not supported', Error);
504505
// Switch to TypeError. The current implementation does not seem right.
505506
E('ERR_CRYPTO_SIGN_KEY_REQUIRED', 'No key provided to sign', Error);
506507
E('ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH',

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
'lib/internal/crypto/hash.js',
9898
'lib/internal/crypto/pbkdf2.js',
9999
'lib/internal/crypto/random.js',
100+
'lib/internal/crypto/scrypt.js',
100101
'lib/internal/crypto/sig.js',
101102
'lib/internal/crypto/util.js',
102103
'lib/internal/constants.js',

src/async_wrap.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ using v8::PromiseHookType;
4545
using v8::PropertyCallbackInfo;
4646
using v8::RetainedObjectInfo;
4747
using v8::String;
48+
using v8::Uint32;
4849
using v8::Undefined;
4950
using v8::Value;
5051

@@ -133,6 +134,23 @@ RetainedObjectInfo* WrapperInfo(uint16_t class_id, Local<Value> wrapper) {
133134
// end RetainedAsyncInfo
134135

135136

137+
struct AsyncWrapObject : public AsyncWrap {
138+
static inline void New(const FunctionCallbackInfo<Value>& args) {
139+
Environment* env = Environment::GetCurrent(args);
140+
CHECK(args.IsConstructCall());
141+
CHECK(env->async_wrap_constructor_template()->HasInstance(args.This()));
142+
CHECK(args[0]->IsUint32());
143+
auto type = static_cast<ProviderType>(args[0].As<Uint32>()->Value());
144+
new AsyncWrapObject(env, args.This(), type);
145+
}
146+
147+
inline AsyncWrapObject(Environment* env, Local<Object> object,
148+
ProviderType type) : AsyncWrap(env, object, type) {}
149+
150+
inline size_t self_size() const override { return sizeof(*this); }
151+
};
152+
153+
136154
static void DestroyAsyncIdsCallback(Environment* env, void* data) {
137155
Local<Function> fn = env->async_hooks_destroy_function();
138156

@@ -569,6 +587,19 @@ void AsyncWrap::Initialize(Local<Object> target,
569587
env->set_async_hooks_destroy_function(Local<Function>());
570588
env->set_async_hooks_promise_resolve_function(Local<Function>());
571589
env->set_async_hooks_binding(target);
590+
591+
{
592+
auto class_name = FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap");
593+
auto function_template = env->NewFunctionTemplate(AsyncWrapObject::New);
594+
function_template->SetClassName(class_name);
595+
AsyncWrap::AddWrapMethods(env, function_template);
596+
auto instance_template = function_template->InstanceTemplate();
597+
instance_template->SetInternalFieldCount(1);
598+
auto function =
599+
function_template->GetFunction(env->context()).ToLocalChecked();
600+
target->Set(env->context(), class_name, function).FromJust();
601+
env->set_async_wrap_constructor_template(function_template);
602+
}
572603
}
573604

574605

src/async_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ namespace node {
7575
#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \
7676
V(PBKDF2REQUEST) \
7777
V(RANDOMBYTESREQUEST) \
78+
V(SCRYPTREQUEST) \
7879
V(TLSWRAP)
7980
#else
8081
#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ struct PackageConfig {
319319
V(async_hooks_destroy_function, v8::Function) \
320320
V(async_hooks_init_function, v8::Function) \
321321
V(async_hooks_promise_resolve_function, v8::Function) \
322+
V(async_wrap_constructor_template, v8::FunctionTemplate) \
322323
V(buffer_prototype_object, v8::Object) \
323324
V(context, v8::Context) \
324325
V(domain_callback, v8::Function) \

0 commit comments

Comments
 (0)