Skip to content

Commit 79a8fb6

Browse files
jasnelltargos
authored andcommitted
crypto: fixup scrypt regressions
Fixes a handful of regressions in scrypt support following the refactor. Fixes: #35815 PR-URL: #35821 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 8c2b179 commit 79a8fb6

8 files changed

+18
-16
lines changed

lib/internal/crypto/webcrypto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async function deriveBits(algorithm, baseKey, length) {
131131
.pbkdf2DeriveBits(algorithm, baseKey, length);
132132
case 'NODE-SCRYPT':
133133
return lazyRequire('internal/crypto/scrypt')
134-
.asyncScryptDeriveBits(algorithm, baseKey, length);
134+
.scryptDeriveBits(algorithm, baseKey, length);
135135
case 'NODE-DH':
136136
return lazyRequire('internal/crypto/diffiehellman')
137137
.asyncDeriveBitsDH(algorithm, baseKey, length);

src/crypto/crypto_scrypt.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ScryptConfig::ScryptConfig(ScryptConfig&& other) noexcept
2828
N(other.N),
2929
r(other.r),
3030
p(other.p),
31+
maxmem(other.maxmem),
3132
length(other.length) {}
3233

3334
ScryptConfig& ScryptConfig::operator=(ScryptConfig&& other) noexcept {
@@ -127,7 +128,7 @@ bool ScryptTraits::DeriveBits(
127128
ByteSource buf = ByteSource::Allocated(data, params.length);
128129
unsigned char* ptr = reinterpret_cast<unsigned char*>(data);
129130

130-
// Botht the pass and salt may be zero-length at this point
131+
// Both the pass and salt may be zero-length at this point
131132

132133
if (!EVP_PBE_scrypt(
133134
params.pass.get(),

src/crypto/crypto_util.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ ByteSource& ByteSource::operator=(ByteSource&& other) noexcept {
236236
}
237237

238238
std::unique_ptr<BackingStore> ByteSource::ReleaseToBackingStore() {
239-
CHECK_NOT_NULL(allocated_data_);
239+
// It's ok for allocated_data_ to be nullptr but
240+
// only if size_ is not zero.
241+
CHECK_IMPLIES(size_ > 0, allocated_data_ != nullptr);
240242
std::unique_ptr<BackingStore> ptr = ArrayBuffer::NewBackingStore(
241243
allocated_data_,
242244
size(),

src/node_crypto.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ void Initialize(Local<Object> target,
5858
PBKDF2Job::Initialize(env, target);
5959
Random::Initialize(env, target);
6060
RSAAlg::Initialize(env, target);
61-
ScryptJob::Initialize(env, target);
6261
SecureContext::Initialize(env, target);
6362
Sign::Initialize(env, target);
6463
SPKAC::Initialize(env, target);
6564
Timing::Initialize(env, target);
6665
Util::Initialize(env, target);
6766
Verify::Initialize(env, target);
67+
68+
#ifndef OPENSSL_NO_SCRYPT
69+
ScryptJob::Initialize(env, target);
70+
#endif
6871
}
6972

7073
} // namespace crypto

test/parallel/test-crypto-scrypt.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const assert = require('assert');
88
const crypto = require('crypto');
99

1010
const { internalBinding } = require('internal/test/binding');
11-
if (typeof internalBinding('crypto').scrypt !== 'function')
11+
if (typeof internalBinding('crypto').ScryptJob !== 'function')
1212
common.skip('no scrypt support');
1313

1414
const good = [
@@ -156,9 +156,7 @@ for (const options of good) {
156156

157157
for (const options of bad) {
158158
const expected = {
159-
code: 'ERR_CRYPTO_SCRYPT_INVALID_PARAMETER',
160-
message: 'Invalid scrypt parameter',
161-
name: 'Error',
159+
message: /Invalid scrypt param/,
162160
};
163161
assert.throws(() => crypto.scrypt('pass', 'salt', 1, options, () => {}),
164162
expected);
@@ -168,9 +166,7 @@ for (const options of bad) {
168166

169167
for (const options of toobig) {
170168
const expected = {
171-
message: new RegExp('error:[^:]+:digital envelope routines:' +
172-
'(?:EVP_PBE_scrypt|scrypt_alg):memory limit exceeded'),
173-
name: 'Error',
169+
message: /Invalid scrypt param/
174170
};
175171
assert.throws(() => crypto.scrypt('pass', 'salt', 1, options, () => {}),
176172
expected);

test/parallel/test-webcrypto-derivebits.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const { internalBinding } = require('internal/test/binding');
102102
}
103103

104104
// Test Scrypt bit derivation
105-
if (typeof internalBinding('crypto').scrypt === 'function') {
105+
if (typeof internalBinding('crypto').ScryptJob === 'function') {
106106
async function test(pass, salt, length, expected) {
107107
const ec = new TextEncoder();
108108
const key = await subtle.importKey(
@@ -111,7 +111,7 @@ if (typeof internalBinding('crypto').scrypt === 'function') {
111111
{ name: 'NODE-SCRYPT' },
112112
false, ['deriveBits']);
113113
const secret = await subtle.deriveBits({
114-
name: 'SCRYPT',
114+
name: 'NODE-SCRYPT',
115115
salt: ec.encode(salt),
116116
}, key, length);
117117
assert.strictEqual(Buffer.from(secret).toString('hex'), expected);

test/parallel/test-webcrypto-derivekey.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const { internalBinding } = require('internal/test/binding');
122122
}
123123

124124
// Test Scrypt bit derivation
125-
if (typeof internalBinding('crypto').scrypt === 'function') {
125+
if (typeof internalBinding('crypto').ScryptJob === 'function') {
126126
async function test(pass, salt, expected) {
127127
const ec = new TextEncoder();
128128
const key = await subtle.importKey(
@@ -144,7 +144,7 @@ if (typeof internalBinding('crypto').scrypt === 'function') {
144144
}
145145

146146
const kTests = [
147-
['hello', 'there', 10, 'SHA-256',
147+
['hello', 'there',
148148
'30ddda6feabaac788eb81cc38f496cd5d9a165d320c537ea05331fe720db1061']
149149
];
150150

test/sequential/test-async-wrap-getasyncid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ if (common.hasCrypto) { // eslint-disable-line node-core/crypto-check
145145
testInitialized(this, 'RandomBytesJob');
146146
}));
147147

148-
if (typeof internalBinding('crypto').scrypt === 'function') {
148+
if (typeof internalBinding('crypto').ScryptJob === 'function') {
149149
crypto.scrypt('password', 'salt', 8, common.mustCall(function() {
150150
testInitialized(this, 'ScryptJob');
151151
}));

0 commit comments

Comments
 (0)