Skip to content

Commit 0d74226

Browse files
aduh95danielleadams
authored andcommitted
crypto: refactor to use more primordials
PR-URL: #36012 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 10c9ea7 commit 0d74226

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

lib/internal/crypto/aes.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayBufferPrototypeSlice,
45
ArrayFrom,
56
ArrayPrototypeIncludes,
67
ArrayPrototypePush,
@@ -182,8 +183,8 @@ function asyncAesGcmCipher(
182183
let tag;
183184
switch (mode) {
184185
case kWebCryptoCipherDecrypt:
185-
tag = data.slice(-tagByteLength);
186-
data = data.slice(0, -tagByteLength);
186+
tag = ArrayBufferPrototypeSlice(data, -tagByteLength);
187+
data = ArrayBufferPrototypeSlice(data, 0, -tagByteLength);
187188
break;
188189
case kWebCryptoCipherEncrypt:
189190
tag = tagByteLength;

lib/internal/crypto/cipher.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const {
44
ObjectSetPrototypeOf,
5+
ReflectApply,
6+
StringPrototypeToLowerCase,
57
} = primordials;
68

79
const {
@@ -118,22 +120,22 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
118120
}
119121
this._decoder = null;
120122

121-
LazyTransform.call(this, options);
123+
ReflectApply(LazyTransform, this, [options]);
122124
}
123125

124126
function createCipher(cipher, password, options, decipher) {
125127
validateString(cipher, 'cipher');
126128
password = getArrayBufferOrView(password, 'password');
127129

128-
createCipherBase.call(this, cipher, password, options, decipher);
130+
ReflectApply(createCipherBase, this, [cipher, password, options, decipher]);
129131
}
130132

131133
function createCipherWithIV(cipher, key, options, decipher, iv) {
132134
validateString(cipher, 'cipher');
133135
const encoding = getStringOption(options, 'encoding');
134136
key = prepareSecretKey(key, encoding);
135137
iv = iv === null ? null : getArrayBufferOrView(iv, 'iv');
136-
createCipherBase.call(this, cipher, key, options, decipher, iv);
138+
ReflectApply(createCipherBase, this, [cipher, key, options, decipher, iv]);
137139
}
138140

139141
// The Cipher class is part of the legacy Node.js crypto API. It exposes
@@ -145,7 +147,7 @@ function Cipher(cipher, password, options) {
145147
if (!(this instanceof Cipher))
146148
return new Cipher(cipher, password, options);
147149

148-
createCipher.call(this, cipher, password, options, true);
150+
ReflectApply(createCipher, this, [cipher, password, options, true]);
149151
}
150152

151153
ObjectSetPrototypeOf(Cipher.prototype, LazyTransform.prototype);
@@ -241,7 +243,7 @@ function Cipheriv(cipher, key, iv, options) {
241243
if (!(this instanceof Cipheriv))
242244
return new Cipheriv(cipher, key, iv, options);
243245

244-
createCipherWithIV.call(this, cipher, key, options, true, iv);
246+
ReflectApply(createCipherWithIV, this, [cipher, key, options, true, iv]);
245247
}
246248

247249
function addCipherPrototypeFunctions(constructor) {
@@ -271,7 +273,7 @@ function Decipher(cipher, password, options) {
271273
if (!(this instanceof Decipher))
272274
return new Decipher(cipher, password, options);
273275

274-
createCipher.call(this, cipher, password, options, false);
276+
ReflectApply(createCipher, this, [cipher, password, options, false]);
275277
}
276278

277279
ObjectSetPrototypeOf(Decipher.prototype, LazyTransform.prototype);
@@ -287,7 +289,7 @@ function Decipheriv(cipher, key, iv, options) {
287289
if (!(this instanceof Decipheriv))
288290
return new Decipheriv(cipher, key, iv, options);
289291

290-
createCipherWithIV.call(this, cipher, key, options, false, iv);
292+
ReflectApply(createCipherWithIV, this, [cipher, key, options, false, iv]);
291293
}
292294

293295
ObjectSetPrototypeOf(Decipheriv.prototype, LazyTransform.prototype);
@@ -315,8 +317,8 @@ function getCipherInfo(nameOrNid, options) {
315317

316318
const ret = _getCipherInfo({}, nameOrNid, keyLength, ivLength);
317319
if (ret !== undefined) {
318-
if (ret.name) ret.name = ret.name.toLowerCase();
319-
if (ret.type) ret.type = ret.type.toLowerCase();
320+
if (ret.name) ret.name = StringPrototypeToLowerCase(ret.name);
321+
if (ret.type) ret.type = StringPrototypeToLowerCase(ret.type);
320322
}
321323
return ret;
322324
}

lib/internal/crypto/diffiehellman.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayBufferPrototypeSlice,
45
FunctionPrototypeCall,
56
MathFloor,
67
ObjectDefineProperty,
@@ -475,7 +476,9 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
475476
if (byteLength < length)
476477
throw lazyDOMException('derived bit length is too small', 'OperationError');
477478

478-
return length === byteLength ? bits : bits.slice(0, length);
479+
return length === byteLength ?
480+
bits :
481+
ArrayBufferPrototypeSlice(bits, 0, length);
479482
}
480483

481484
async function asyncDeriveBitsDH(algorithm, baseKey, length) {
@@ -528,7 +531,9 @@ async function asyncDeriveBitsDH(algorithm, baseKey, length) {
528531
if (byteLength < length)
529532
throw lazyDOMException('derived bit length is too small', 'OperationError');
530533

531-
return length === byteLength ? bits : bits.slice(0, length);
534+
return length === byteLength ?
535+
bits :
536+
ArrayBufferPrototypeSlice(bits, 0, length);
532537
}
533538

534539
function dhExportKey(key, format) {

lib/internal/crypto/hash.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ObjectSetPrototypeOf,
5+
ReflectApply,
56
Symbol,
67
} = primordials;
78

@@ -67,7 +68,7 @@ function Hash(algorithm, options) {
6768
this[kState] = {
6869
[kFinalized]: false
6970
};
70-
LazyTransform.call(this, options);
71+
ReflectApply(LazyTransform, this, [options]);
7172
}
7273

7374
ObjectSetPrototypeOf(Hash.prototype, LazyTransform.prototype);
@@ -134,7 +135,7 @@ function Hmac(hmac, key, options) {
134135
this[kState] = {
135136
[kFinalized]: false
136137
};
137-
LazyTransform.call(this, options);
138+
ReflectApply(LazyTransform, this, [options]);
138139
}
139140

140141
ObjectSetPrototypeOf(Hmac.prototype, LazyTransform.prototype);

lib/internal/crypto/sig.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ObjectSetPrototypeOf,
5+
ReflectApply,
56
} = primordials;
67

78
const {
@@ -50,7 +51,7 @@ function Sign(algorithm, options) {
5051
this[kHandle] = new _Sign();
5152
this[kHandle].init(algorithm);
5253

53-
Writable.call(this, options);
54+
ReflectApply(Writable, this, [options]);
5455
}
5556

5657
ObjectSetPrototypeOf(Sign.prototype, Writable.prototype);
@@ -164,7 +165,7 @@ function Verify(algorithm, options) {
164165
this[kHandle] = new _Verify();
165166
this[kHandle].init(algorithm);
166167

167-
Writable.call(this, options);
168+
ReflectApply(Writable, this, [options]);
168169
}
169170

170171
ObjectSetPrototypeOf(Verify.prototype, Writable.prototype);

lib/internal/crypto/webcrypto.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ObjectDefineProperties,
88
SafeSet,
99
SymbolToStringTag,
10+
StringPrototypeRepeat,
1011
} = primordials;
1112

1213
const {
@@ -494,7 +495,7 @@ async function wrapKey(format, key, wrappingKey, algorithm) {
494495
throw lazyDOMException('Invalid exported JWK key', 'DataError');
495496
const ec = new TextEncoder();
496497
const raw = JSONStringify(keyData);
497-
keyData = ec.encode(raw + ' '.repeat(8 - (raw.length % 8)));
498+
keyData = ec.encode(raw + StringPrototypeRepeat(' ', 8 - (raw.length % 8)));
498499
}
499500

500501
return cipherOrWrap(

0 commit comments

Comments
 (0)