123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /*
- * Copyright 2018 Scytl Secure Electronic Voting SA
- *
- * All rights reserved
- *
- * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
- */
- cryptolib.modules.asymmetric = cryptolib.modules.asymmetric || {};
- /** @namespace asymmetric/keypair */
- cryptolib.modules.asymmetric.keypair = function(box) {
- 'use strict';
- box.asymmetric = box.asymmetric || {};
- box.asymmetric.keypair = {};
- /**
- * A module that holds certificates functionalities.
- *
- * @exports asymmetric/keypair/factory
- */
- box.asymmetric.keypair.factory = {};
- var policies = {
- encryption: {
- algorithm: box.policies.asymmetric.keypair.encryption.algorithm,
- provider: box.policies.asymmetric.keypair.encryption.provider,
- keyLength: box.policies.asymmetric.keypair.encryption.keyLength,
- publicExponent: box.policies.asymmetric.keypair.encryption.publicExponent
- }
- };
- var exceptions, randomFactory;
- var f = function(box) {
- exceptions = box.commons.exceptions;
- randomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- };
- f.policies = {
- primitives: {
- secureRandom:
- {provider: box.policies.asymmetric.keypair.secureRandom.provider}
- }
- };
- cryptolib('commons.exceptions', 'primitives.securerandom', f);
- /**
- * A factory class for generating
- * {@link asymmetric.keypair.CryptoForgeKeyPairGenerator} objects.
- *
- * @class
- */
- box.asymmetric.keypair.factory.KeyPairGeneratorFactory = function() {};
- box.asymmetric.keypair.factory.KeyPairGeneratorFactory.prototype = {
- /**
- * @function
- * @returns CryptoForgeKeyPairGenerator
- */
- getEncryptionCryptoKeyPairGenerator: function() {
- var secureRandom;
- try {
- if (policies.encryption.provider ===
- Config.asymmetric.keypair.encryption.provider.FORGE) {
- secureRandom = randomFactory.getCryptoRandomBytes();
- return this.getCryptoForgeKeyPairGenerator(secureRandom);
- } else {
- throw new exceptions.CryptoLibException(
- 'No suitable provider was provided.');
- }
- } catch (error) {
- throw new exceptions.CryptoLibException(
- 'An encryption CryptoKeyPairGenerator could not be obtained.',
- error);
- }
- },
- getCryptoForgeKeyPairGenerator: function(secureRandom) {
- return new CryptoForgeKeyPairGenerator(secureRandom);
- }
- };
- /**
- * Defines an encryption key pair generator for the FORGE provider.
- *
- * @class
- * @memberof asymmetric/keypair
- */
- function CryptoForgeKeyPairGenerator(secureRandom) {
- this.secureRandom = secureRandom;
- }
- CryptoForgeKeyPairGenerator.prototype = {
- /**
- * Generates an encryption key pair.
- *
- * @returns {asymmetric/keypair.KeyPair} an encryption key pair.
- */
- generate: function() {
- try {
- if (policies.encryption.algorithm ===
- Config.asymmetric.keypair.encryption.algorithm.RSA) {
- var options = {prng: this.secureRandom};
- var keys = forge.pki.rsa.generateKeyPair(
- policies.encryption.keyLength, policies.encryption.publicExponent,
- options);
- var forgePublicKey = keys.publicKey;
- var forgePrivateKey = keys.privateKey;
- var publicKeyPem = forge.pki.publicKeyToPem(forgePublicKey);
- var privateKeyPem = forge.pki.privateKeyToPem(forgePrivateKey);
- var publicKey = new PublicKey(
- policies.encryption.algorithm, forgePublicKey.n, forgePublicKey.e,
- publicKeyPem);
- var privateKey = new PrivateKey(
- policies.encryption.algorithm, forgePrivateKey.n,
- forgePrivateKey.e, forgePrivateKey.d, forgePrivateKey.p,
- forgePrivateKey.q, forgePrivateKey.dP, forgePrivateKey.dQ,
- forgePrivateKey.qInv, privateKeyPem);
- return new KeyPair(publicKey, privateKey);
- } else {
- throw new exceptions.CryptoLibException(
- 'The given algorithm is not supported.');
- }
- } catch (error) {
- throw new exceptions.CryptoLibException(
- 'Encryption key pair could not be generated.', error);
- }
- }
- };
- /**
- * Container class for an encryption key pair.
- *
- * @class
- * @param publicKey
- * public key of the encryption key pair.
- * @param privateKey
- * private key of the encryption key pair.
- * @memberof asymmetric/keypair
- */
- function KeyPair(publicKey, privateKey) {
- /**
- * Retrieves the public key of the encryption key pair.
- *
- * @function
- * @returns PrivateKey Public key of encryption key pair.
- */
- var getPublicKey = function() {
- return publicKey;
- };
- /**
- * Retrieves the private key of the encryption key pair.
- *
- * @function
- * @returns PrivateKey Private key of encryption key pair.
- */
- var getPrivateKey = function() {
- return privateKey;
- };
- return {getPublicKey: getPublicKey, getPrivateKey: getPrivateKey};
- }
- /**
- * @class PrivateKey
- * @memberof asymmetric/keypair
- */
- function PrivateKey(algorithm, n, e, d, p, q, dP, dQ, qInv, pem) {
- /**
- * @function
- * @returns {string}
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getAlgorithm = function() {
- return algorithm;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getModulus = function() {
- return n;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getPublicExponent = function() {
- return e;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getPrivateExponent = function() {
- return d;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getPrime1 = function() {
- return p;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getPrime2 = function() {
- return q;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getExponent1 = function() {
- return dP;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getExponent2 = function() {
- return dQ;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getCoefficient = function() {
- return qInv;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PrivateKey
- */
- var getPem = function() {
- return pem;
- };
- return {
- getAlgorithm: getAlgorithm,
- getModulus: getModulus,
- getPublicExponent: getPublicExponent,
- getPrivateExponent: getPrivateExponent,
- getPrime1: getPrime1,
- getPrime2: getPrime2,
- getExponent1: getExponent1,
- getExponent2: getExponent2,
- getCoefficient: getCoefficient,
- getPem: getPem
- };
- }
- /** @class PublicKey */
- function PublicKey(algorithm, n, e, pem) {
- /**
- * @function
- * @memberof asymmetric/keypair.PublicKey
- */
- var getAlgorithm = function() {
- return algorithm;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PublicKey
- */
- var getModulus = function() {
- return n;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PublicKey
- */
- var getPublicExponent = function() {
- return e;
- };
- /**
- * @function
- * @memberof asymmetric/keypair.PublicKey
- */
- var getPem = function() {
- return pem;
- };
- return {
- getAlgorithm: getAlgorithm,
- getModulus: getModulus,
- getPublicExponent: getPublicExponent,
- getPem: getPem
- };
- }
- };
|