react-native-quick-crypto
Version:
A fast implementation of Node's `crypto` module written in C/C++ JSI
237 lines (234 loc) • 8.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateKeyPairPromise = exports.generateKeyPair = void 0;
exports.generateKeyPairSync = generateKeyPairSync;
var _ed = require("../ed");
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
var _rsa = require("../rsa");
var _ec = require("../ec");
var _dsa = require("../dsa");
var _dhKeyPair = require("../dhKeyPair");
var _slhdsa = require("../slhdsa");
var _utils = require("../utils");
var _classes = require("./classes");
var _utils2 = require("./utils");
const SLH_DSA_TYPE_TO_VARIANT = {
'slh-dsa-sha2-128s': 'SLH-DSA-SHA2-128s',
'slh-dsa-sha2-128f': 'SLH-DSA-SHA2-128f',
'slh-dsa-sha2-192s': 'SLH-DSA-SHA2-192s',
'slh-dsa-sha2-192f': 'SLH-DSA-SHA2-192f',
'slh-dsa-sha2-256s': 'SLH-DSA-SHA2-256s',
'slh-dsa-sha2-256f': 'SLH-DSA-SHA2-256f',
'slh-dsa-shake-128s': 'SLH-DSA-SHAKE-128s',
'slh-dsa-shake-128f': 'SLH-DSA-SHAKE-128f',
'slh-dsa-shake-192s': 'SLH-DSA-SHAKE-192s',
'slh-dsa-shake-192f': 'SLH-DSA-SHAKE-192f',
'slh-dsa-shake-256s': 'SLH-DSA-SHAKE-256s',
'slh-dsa-shake-256f': 'SLH-DSA-SHAKE-256f'
};
function isSlhDsaType(type) {
return type in SLH_DSA_TYPE_TO_VARIANT;
}
function slhDsaFormatKeyPairOutput(slhdsa, encoding) {
const {
publicFormat,
privateFormat,
cipher,
passphrase
} = encoding;
const publicKey = _classes.KeyObject.createKeyObject('public', slhdsa.getPublicKey(), _utils.KFormatType.DER, _utils.KeyEncoding.SPKI);
const privateKey = _classes.KeyObject.createKeyObject('private', slhdsa.getPrivateKey(), _utils.KFormatType.DER, _utils.KeyEncoding.PKCS8);
let publicKeyOutput;
let privateKeyOutput;
if (publicFormat === -1) {
publicKeyOutput = publicKey;
} else if (publicFormat === 'raw-public') {
publicKeyOutput = _reactNativeBuffer.Buffer.from(publicKey.handle.exportRawPublic());
} else {
const format = publicFormat === _utils.KFormatType.PEM ? _utils.KFormatType.PEM : _utils.KFormatType.DER;
const exported = publicKey.handle.exportKey(format, _utils.KeyEncoding.SPKI);
if (format === _utils.KFormatType.PEM) {
publicKeyOutput = _reactNativeBuffer.Buffer.from(new Uint8Array(exported)).toString('utf-8');
} else {
publicKeyOutput = exported;
}
}
if (privateFormat === -1) {
privateKeyOutput = privateKey;
} else if (privateFormat === 'raw-private') {
privateKeyOutput = _reactNativeBuffer.Buffer.from(privateKey.handle.exportRawPrivate());
} else if (privateFormat === 'raw-seed') {
privateKeyOutput = _reactNativeBuffer.Buffer.from(privateKey.handle.exportRawSeed());
} else {
const format = privateFormat === _utils.KFormatType.PEM ? _utils.KFormatType.PEM : _utils.KFormatType.DER;
const exported = privateKey.handle.exportKey(format, _utils.KeyEncoding.PKCS8, cipher, passphrase);
if (format === _utils.KFormatType.PEM) {
privateKeyOutput = _reactNativeBuffer.Buffer.from(new Uint8Array(exported)).toString('utf-8');
} else {
privateKeyOutput = exported;
}
}
return {
publicKey: publicKeyOutput,
privateKey: privateKeyOutput
};
}
function slhDsaGenerateKeyPairNodeSync(type, encoding) {
const slhdsa = new _slhdsa.SlhDsa(SLH_DSA_TYPE_TO_VARIANT[type]);
slhdsa.generateKeyPairSync();
return slhDsaFormatKeyPairOutput(slhdsa, encoding);
}
async function slhDsaGenerateKeyPairNode(type, encoding) {
const slhdsa = new _slhdsa.SlhDsa(SLH_DSA_TYPE_TO_VARIANT[type]);
await slhdsa.generateKeyPair();
return slhDsaFormatKeyPairOutput(slhdsa, encoding);
}
const generateKeyPair = (type, options, callback) => {
(0, _utils.validateFunction)(callback);
internalGenerateKeyPair(true, type, options, callback);
};
// Promisify generateKeyPair
// (attempted to use util.promisify, to no avail)
exports.generateKeyPair = generateKeyPair;
const generateKeyPairPromise = (type, options) => {
return new Promise((resolve, reject) => {
generateKeyPair(type, options, (err, publicKey, privateKey) => {
if (err) {
reject([err, undefined]);
} else {
resolve([undefined, {
publicKey,
privateKey
}]);
}
});
});
};
// generateKeyPairSync
exports.generateKeyPairPromise = generateKeyPairPromise;
function generateKeyPairSync(type, options) {
const [err, publicKey, privateKey] = internalGenerateKeyPair(false, type, options, undefined);
if (err) {
throw err;
}
return {
publicKey,
privateKey
};
}
function parseKeyPairEncoding(keyType, options = _utils.kEmptyObject) {
const {
publicKeyEncoding,
privateKeyEncoding
} = options;
let publicFormat, publicType;
if (publicKeyEncoding == null) {
publicFormat = publicType = -1;
} else if (typeof publicKeyEncoding === 'object') {
({
format: publicFormat,
type: publicType
} = (0, _utils2.parsePublicKeyEncoding)(publicKeyEncoding, keyType, 'publicKeyEncoding'));
} else {
throw new Error('Invalid argument options.publicKeyEncoding', publicKeyEncoding);
}
let privateFormat, privateType, cipher, passphrase;
if (privateKeyEncoding == null) {
privateFormat = privateType = -1;
} else if (typeof privateKeyEncoding === 'object') {
({
format: privateFormat,
type: privateType,
cipher,
passphrase
} = (0, _utils2.parsePrivateKeyEncoding)(privateKeyEncoding, keyType, 'privateKeyEncoding'));
} else {
throw new Error('Invalid argument options.privateKeyEncoding', publicKeyEncoding);
}
return {
publicFormat: publicFormat,
publicType: publicType,
privateFormat: privateFormat,
privateType: privateType,
cipher,
passphrase
};
}
function internalGenerateKeyPair(isAsync, type, options, callback) {
const encoding = parseKeyPairEncoding(type, options);
switch (type) {
case 'ed25519':
case 'ed448':
case 'x25519':
case 'x448':
return (0, _ed.ed_generateKeyPair)(isAsync, type, encoding, callback);
case 'rsa':
case 'rsa-pss':
case 'dsa':
case 'ec':
case 'dh':
break;
default:
{
if (isSlhDsaType(type)) {
break;
}
const err = new Error(`
Invalid Argument options: '${type}' scheme not supported for
generateKeyPair(). Currently not all encryption methods are supported in
this library. Check docs/implementation_coverage.md for status.
`);
return [err, undefined, undefined];
}
}
if (isAsync) {
const impl = async () => {
try {
let result;
if (type === 'rsa' || type === 'rsa-pss') {
result = await (0, _rsa.rsa_generateKeyPairNode)(type, options, encoding);
} else if (type === 'ec') {
result = await (0, _ec.ec_generateKeyPairNode)(options, encoding);
} else if (type === 'dsa') {
result = await (0, _dsa.dsa_generateKeyPairNode)(options, encoding);
} else if (type === 'dh') {
result = await (0, _dhKeyPair.dh_generateKeyPairNode)(options, encoding);
} else if (isSlhDsaType(type)) {
result = await slhDsaGenerateKeyPairNode(type, encoding);
} else {
throw new Error(`Unsupported key type: ${type}`);
}
return [undefined, result.publicKey, result.privateKey];
} catch (error) {
return [error, undefined, undefined];
}
};
impl().then(result => {
const [err, publicKey, privateKey] = result;
callback(err, publicKey, privateKey);
});
return;
}
try {
let result;
if (type === 'rsa' || type === 'rsa-pss') {
result = (0, _rsa.rsa_generateKeyPairNodeSync)(type, options, encoding);
} else if (type === 'ec') {
result = (0, _ec.ec_generateKeyPairNodeSync)(options, encoding);
} else if (type === 'dsa') {
result = (0, _dsa.dsa_generateKeyPairNodeSync)(options, encoding);
} else if (type === 'dh') {
result = (0, _dhKeyPair.dh_generateKeyPairNodeSync)(options, encoding);
} else if (isSlhDsaType(type)) {
result = slhDsaGenerateKeyPairNodeSync(type, encoding);
} else {
throw new Error(`Unsupported key type: ${type}`);
}
return [undefined, result.publicKey, result.privateKey];
} catch (error) {
return [error, undefined, undefined];
}
}
//# sourceMappingURL=generateKeyPair.js.map