From 0d7422651b41588e60a0cad3b5d77f0802eb2a95 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 7 Nov 2020 00:26:15 +0100 Subject: [PATCH] crypto: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36012 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/crypto/aes.js | 5 +++-- lib/internal/crypto/cipher.js | 20 +++++++++++--------- lib/internal/crypto/diffiehellman.js | 9 +++++++-- lib/internal/crypto/hash.js | 5 +++-- lib/internal/crypto/sig.js | 5 +++-- lib/internal/crypto/webcrypto.js | 3 ++- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/internal/crypto/aes.js b/lib/internal/crypto/aes.js index 3ae60057c30cf8..f0814c725469e6 100644 --- a/lib/internal/crypto/aes.js +++ b/lib/internal/crypto/aes.js @@ -1,6 +1,7 @@ 'use strict'; const { + ArrayBufferPrototypeSlice, ArrayFrom, ArrayPrototypeIncludes, ArrayPrototypePush, @@ -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; diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js index 299d387d1c5e84..1b6ad0e7ea601c 100644 --- a/lib/internal/crypto/cipher.js +++ b/lib/internal/crypto/cipher.js @@ -2,6 +2,8 @@ const { ObjectSetPrototypeOf, + ReflectApply, + StringPrototypeToLowerCase, } = primordials; const { @@ -118,14 +120,14 @@ 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) { @@ -133,7 +135,7 @@ function createCipherWithIV(cipher, key, options, decipher, iv) { 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 @@ -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); @@ -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) { @@ -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); @@ -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); @@ -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; } diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index 8279f3058c449e..2a8795e378f3e0 100644 --- a/lib/internal/crypto/diffiehellman.js +++ b/lib/internal/crypto/diffiehellman.js @@ -1,6 +1,7 @@ 'use strict'; const { + ArrayBufferPrototypeSlice, FunctionPrototypeCall, MathFloor, ObjectDefineProperty, @@ -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) { @@ -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) { diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 4647e98c671ab8..6c502f52b66503 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -2,6 +2,7 @@ const { ObjectSetPrototypeOf, + ReflectApply, Symbol, } = primordials; @@ -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); @@ -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); diff --git a/lib/internal/crypto/sig.js b/lib/internal/crypto/sig.js index 27b26a87f0d6f3..783cdb33202202 100644 --- a/lib/internal/crypto/sig.js +++ b/lib/internal/crypto/sig.js @@ -2,6 +2,7 @@ const { ObjectSetPrototypeOf, + ReflectApply, } = primordials; const { @@ -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); @@ -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); diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index 81ff9ca5191004..7bd487972eb08f 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -7,6 +7,7 @@ const { ObjectDefineProperties, SafeSet, SymbolToStringTag, + StringPrototypeRepeat, } = primordials; const { @@ -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(