Skip to content

Commit

Permalink
crypto: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36012
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 authored and danielleadams committed Nov 10, 2020
1 parent 10c9ea7 commit 0d74226
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
5 changes: 3 additions & 2 deletions lib/internal/crypto/aes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayBufferPrototypeSlice,
ArrayFrom,
ArrayPrototypeIncludes,
ArrayPrototypePush,
Expand Down Expand Up @@ -182,8 +183,8 @@ function asyncAesGcmCipher(
let tag;
switch (mode) {
case kWebCryptoCipherDecrypt:
tag = data.slice(-tagByteLength);
data = data.slice(0, -tagByteLength);
tag = ArrayBufferPrototypeSlice(data, -tagByteLength);
data = ArrayBufferPrototypeSlice(data, 0, -tagByteLength);
break;
case kWebCryptoCipherEncrypt:
tag = tagByteLength;
Expand Down
20 changes: 11 additions & 9 deletions lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const {
ObjectSetPrototypeOf,
ReflectApply,
StringPrototypeToLowerCase,
} = primordials;

const {
Expand Down Expand Up @@ -118,22 +120,22 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
}
this._decoder = null;

LazyTransform.call(this, options);
ReflectApply(LazyTransform, this, [options]);
}

function createCipher(cipher, password, options, decipher) {
validateString(cipher, 'cipher');
password = getArrayBufferOrView(password, 'password');

createCipherBase.call(this, cipher, password, options, decipher);
ReflectApply(createCipherBase, this, [cipher, password, options, decipher]);
}

function createCipherWithIV(cipher, key, options, decipher, iv) {
validateString(cipher, 'cipher');
const encoding = getStringOption(options, 'encoding');
key = prepareSecretKey(key, encoding);
iv = iv === null ? null : getArrayBufferOrView(iv, 'iv');
createCipherBase.call(this, cipher, key, options, decipher, iv);
ReflectApply(createCipherBase, this, [cipher, key, options, decipher, iv]);
}

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

createCipher.call(this, cipher, password, options, true);
ReflectApply(createCipher, this, [cipher, password, options, true]);
}

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

createCipherWithIV.call(this, cipher, key, options, true, iv);
ReflectApply(createCipherWithIV, this, [cipher, key, options, true, iv]);
}

function addCipherPrototypeFunctions(constructor) {
Expand Down Expand Up @@ -271,7 +273,7 @@ function Decipher(cipher, password, options) {
if (!(this instanceof Decipher))
return new Decipher(cipher, password, options);

createCipher.call(this, cipher, password, options, false);
ReflectApply(createCipher, this, [cipher, password, options, false]);
}

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

createCipherWithIV.call(this, cipher, key, options, false, iv);
ReflectApply(createCipherWithIV, this, [cipher, key, options, false, iv]);
}

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

const ret = _getCipherInfo({}, nameOrNid, keyLength, ivLength);
if (ret !== undefined) {
if (ret.name) ret.name = ret.name.toLowerCase();
if (ret.type) ret.type = ret.type.toLowerCase();
if (ret.name) ret.name = StringPrototypeToLowerCase(ret.name);
if (ret.type) ret.type = StringPrototypeToLowerCase(ret.type);
}
return ret;
}
Expand Down
9 changes: 7 additions & 2 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayBufferPrototypeSlice,
FunctionPrototypeCall,
MathFloor,
ObjectDefineProperty,
Expand Down Expand Up @@ -475,7 +476,9 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
if (byteLength < length)
throw lazyDOMException('derived bit length is too small', 'OperationError');

return length === byteLength ? bits : bits.slice(0, length);
return length === byteLength ?
bits :
ArrayBufferPrototypeSlice(bits, 0, length);
}

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

return length === byteLength ? bits : bits.slice(0, length);
return length === byteLength ?
bits :
ArrayBufferPrototypeSlice(bits, 0, length);
}

function dhExportKey(key, format) {
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
} = primordials;

Expand Down Expand Up @@ -67,7 +68,7 @@ function Hash(algorithm, options) {
this[kState] = {
[kFinalized]: false
};
LazyTransform.call(this, options);
ReflectApply(LazyTransform, this, [options]);
}

ObjectSetPrototypeOf(Hash.prototype, LazyTransform.prototype);
Expand Down Expand Up @@ -134,7 +135,7 @@ function Hmac(hmac, key, options) {
this[kState] = {
[kFinalized]: false
};
LazyTransform.call(this, options);
ReflectApply(LazyTransform, this, [options]);
}

ObjectSetPrototypeOf(Hmac.prototype, LazyTransform.prototype);
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ObjectSetPrototypeOf,
ReflectApply,
} = primordials;

const {
Expand Down Expand Up @@ -50,7 +51,7 @@ function Sign(algorithm, options) {
this[kHandle] = new _Sign();
this[kHandle].init(algorithm);

Writable.call(this, options);
ReflectApply(Writable, this, [options]);
}

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

Writable.call(this, options);
ReflectApply(Writable, this, [options]);
}

ObjectSetPrototypeOf(Verify.prototype, Writable.prototype);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
ObjectDefineProperties,
SafeSet,
SymbolToStringTag,
StringPrototypeRepeat,
} = primordials;

const {
Expand Down Expand Up @@ -494,7 +495,7 @@ async function wrapKey(format, key, wrappingKey, algorithm) {
throw lazyDOMException('Invalid exported JWK key', 'DataError');
const ec = new TextEncoder();
const raw = JSONStringify(keyData);
keyData = ec.encode(raw + ' '.repeat(8 - (raw.length % 8)));
keyData = ec.encode(raw + StringPrototypeRepeat(' ', 8 - (raw.length % 8)));
}

return cipherOrWrap(
Expand Down

0 comments on commit 0d74226

Please sign in to comment.