Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: simplify lazy loading of internal modules #45809

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ const {
const kHandle = Symbol('kHandle');
const kKeyObject = Symbol('kKeyObject');

const lazyRequireCache = {};

function lazyRequire(name) {
let ret = lazyRequireCache[name];
if (ret === undefined)
ret = lazyRequireCache[name] = require(name);
return ret;
}

let defaultEncoding = 'buffer';

function setDefaultEncoding(val) {
Expand Down Expand Up @@ -431,7 +422,6 @@ module.exports = {
validateByteSource,
validateKeyOps,
jobPromise,
lazyRequire,
validateMaxBufferLength,
bigIntArrayToUnsignedBigInt,
bigIntArrayToUnsignedInt,
Expand Down
65 changes: 32 additions & 33 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const {
getArrayBufferOrView,
getBlockSize,
hasAnyNotIn,
lazyRequire,
normalizeAlgorithm,
normalizeHashName,
validateMaxBufferLength,
Expand Down Expand Up @@ -104,7 +103,7 @@ async function generateKey(
// Fall through
case 'RSA-OAEP':
resultType = 'CryptoKeyPair';
result = await lazyRequire('internal/crypto/rsa')
result = await require('internal/crypto/rsa')
.rsaKeyGenerate(algorithm, extractable, keyUsages);
break;
case 'Ed25519':
Expand All @@ -115,19 +114,19 @@ async function generateKey(
// Fall through
case 'X448':
resultType = 'CryptoKeyPair';
result = await lazyRequire('internal/crypto/cfrg')
result = await require('internal/crypto/cfrg')
.cfrgGenerateKey(algorithm, extractable, keyUsages);
break;
case 'ECDSA':
// Fall through
case 'ECDH':
resultType = 'CryptoKeyPair';
result = await lazyRequire('internal/crypto/ec')
result = await require('internal/crypto/ec')
.ecGenerateKey(algorithm, extractable, keyUsages);
break;
case 'HMAC':
resultType = 'CryptoKey';
result = await lazyRequire('internal/crypto/mac')
result = await require('internal/crypto/mac')
.hmacGenerateKey(algorithm, extractable, keyUsages);
break;
case 'AES-CTR':
Expand All @@ -138,7 +137,7 @@ async function generateKey(
// Fall through
case 'AES-KW':
resultType = 'CryptoKey';
result = await lazyRequire('internal/crypto/aes')
result = await require('internal/crypto/aes')
.aesGenerateKey(algorithm, extractable, keyUsages);
break;
default:
Expand Down Expand Up @@ -177,13 +176,13 @@ async function deriveBits(algorithm, baseKey, length) {
case 'X448':
// Fall through
case 'ECDH':
return lazyRequire('internal/crypto/diffiehellman')
return require('internal/crypto/diffiehellman')
.ecdhDeriveBits(algorithm, baseKey, length);
case 'HKDF':
return lazyRequire('internal/crypto/hkdf')
return require('internal/crypto/hkdf')
.hkdfDeriveBits(algorithm, baseKey, length);
case 'PBKDF2':
return lazyRequire('internal/crypto/pbkdf2')
return require('internal/crypto/pbkdf2')
.pbkdf2DeriveBits(algorithm, baseKey, length);
}
throw lazyDOMException('Unrecognized name.');
Expand Down Expand Up @@ -247,15 +246,15 @@ async function deriveKey(
case 'X448':
// Fall through
case 'ECDH':
bits = await lazyRequire('internal/crypto/diffiehellman')
bits = await require('internal/crypto/diffiehellman')
.ecdhDeriveBits(algorithm, baseKey, length);
break;
case 'HKDF':
bits = await lazyRequire('internal/crypto/hkdf')
bits = await require('internal/crypto/hkdf')
.hkdfDeriveBits(algorithm, baseKey, length);
break;
case 'PBKDF2':
bits = await lazyRequire('internal/crypto/pbkdf2')
bits = await require('internal/crypto/pbkdf2')
.pbkdf2DeriveBits(algorithm, baseKey, length);
break;
default:
Expand All @@ -277,15 +276,15 @@ async function exportKeySpki(key) {
// Fall through
case 'RSA-OAEP':
if (key.type === 'public') {
return lazyRequire('internal/crypto/rsa')
return require('internal/crypto/rsa')
.rsaExportKey(key, kWebCryptoKeyFormatSPKI);
}
break;
case 'ECDSA':
// Fall through
case 'ECDH':
if (key.type === 'public') {
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecExportKey(key, kWebCryptoKeyFormatSPKI);
}
break;
Expand All @@ -297,7 +296,7 @@ async function exportKeySpki(key) {
// Fall through
case 'X448':
if (key.type === 'public') {
return lazyRequire('internal/crypto/cfrg')
return require('internal/crypto/cfrg')
.cfrgExportKey(key, kWebCryptoKeyFormatSPKI);
}
break;
Expand All @@ -316,15 +315,15 @@ async function exportKeyPkcs8(key) {
// Fall through
case 'RSA-OAEP':
if (key.type === 'private') {
return lazyRequire('internal/crypto/rsa')
return require('internal/crypto/rsa')
.rsaExportKey(key, kWebCryptoKeyFormatPKCS8);
}
break;
case 'ECDSA':
// Fall through
case 'ECDH':
if (key.type === 'private') {
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecExportKey(key, kWebCryptoKeyFormatPKCS8);
}
break;
Expand All @@ -336,7 +335,7 @@ async function exportKeyPkcs8(key) {
// Fall through
case 'X448':
if (key.type === 'private') {
return lazyRequire('internal/crypto/cfrg')
return require('internal/crypto/cfrg')
.cfrgExportKey(key, kWebCryptoKeyFormatPKCS8);
}
break;
Expand All @@ -353,7 +352,7 @@ async function exportKeyRaw(key) {
// Fall through
case 'ECDH':
if (key.type === 'public') {
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecExportKey(key, kWebCryptoKeyFormatRaw);
}
break;
Expand All @@ -365,7 +364,7 @@ async function exportKeyRaw(key) {
// Fall through
case 'X448':
if (key.type === 'public') {
return lazyRequire('internal/crypto/cfrg')
return require('internal/crypto/cfrg')
.cfrgExportKey(key, kWebCryptoKeyFormatRaw);
}
break;
Expand Down Expand Up @@ -430,7 +429,7 @@ async function exportKeyJWK(key) {
case 'AES-GCM':
// Fall through
case 'AES-KW':
jwk.alg = lazyRequire('internal/crypto/aes')
jwk.alg = require('internal/crypto/aes')
.getAlgorithmName(key.algorithm.name, key.algorithm.length);
return jwk;
case 'HMAC':
Expand Down Expand Up @@ -529,12 +528,12 @@ async function importKey(
case 'RSA-PSS':
// Fall through
case 'RSA-OAEP':
return lazyRequire('internal/crypto/rsa')
return require('internal/crypto/rsa')
.rsaImportKey(format, keyData, algorithm, extractable, keyUsages);
case 'ECDSA':
// Fall through
case 'ECDH':
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecImportKey(format, keyData, algorithm, extractable, keyUsages);
case 'Ed25519':
// Fall through
Expand All @@ -543,10 +542,10 @@ async function importKey(
case 'X25519':
// Fall through
case 'X448':
return lazyRequire('internal/crypto/cfrg')
return require('internal/crypto/cfrg')
.cfrgImportKey(format, keyData, algorithm, extractable, keyUsages);
case 'HMAC':
return lazyRequire('internal/crypto/mac')
return require('internal/crypto/mac')
.hmacImportKey(format, keyData, algorithm, extractable, keyUsages);
case 'AES-CTR':
// Fall through
Expand All @@ -555,7 +554,7 @@ async function importKey(
case 'AES-GCM':
// Fall through
case 'AES-KW':
return lazyRequire('internal/crypto/aes')
return require('internal/crypto/aes')
.aesImportKey(algorithm, format, keyData, extractable, keyUsages);
case 'HKDF':
// Fall through
Expand Down Expand Up @@ -655,19 +654,19 @@ function signVerify(algorithm, key, data, signature) {
case 'RSA-PSS':
// Fall through
case 'RSASSA-PKCS1-v1_5':
return lazyRequire('internal/crypto/rsa')
return require('internal/crypto/rsa')
.rsaSignVerify(key, data, algorithm, signature);
case 'ECDSA':
return lazyRequire('internal/crypto/ec')
return require('internal/crypto/ec')
.ecdsaSignVerify(key, data, algorithm, signature);
case 'Ed25519':
// Fall through
case 'Ed448':
// Fall through
return lazyRequire('internal/crypto/cfrg')
return require('internal/crypto/cfrg')
.eddsaSignVerify(key, data, algorithm, signature);
case 'HMAC':
return lazyRequire('internal/crypto/mac')
return require('internal/crypto/mac')
.hmacSignVerify(key, data, algorithm, signature);
}
throw lazyDOMException('Unrecognized named.', 'NotSupportedError');
Expand Down Expand Up @@ -710,18 +709,18 @@ async function cipherOrWrap(mode, algorithm, key, data, op) {

switch (algorithm.name) {
case 'RSA-OAEP':
return lazyRequire('internal/crypto/rsa')
return require('internal/crypto/rsa')
.rsaCipher(mode, key, data, algorithm);
case 'AES-CTR':
// Fall through
case 'AES-CBC':
// Fall through
case 'AES-GCM':
return lazyRequire('internal/crypto/aes')
return require('internal/crypto/aes')
.aesCipher(mode, key, data, algorithm);
case 'AES-KW':
if (op === 'wrapKey' || op === 'unwrapKey') {
return lazyRequire('internal/crypto/aes')
return require('internal/crypto/aes')
.aesCipher(mode, key, data, algorithm);
}
}
Expand Down