asymmetric.keypair.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. cryptolib.modules.asymmetric = cryptolib.modules.asymmetric || {};
  9. /** @namespace asymmetric/keypair */
  10. cryptolib.modules.asymmetric.keypair = function(box) {
  11. 'use strict';
  12. box.asymmetric = box.asymmetric || {};
  13. box.asymmetric.keypair = {};
  14. /**
  15. * A module that holds certificates functionalities.
  16. *
  17. * @exports asymmetric/keypair/factory
  18. */
  19. box.asymmetric.keypair.factory = {};
  20. var policies = {
  21. encryption: {
  22. algorithm: box.policies.asymmetric.keypair.encryption.algorithm,
  23. provider: box.policies.asymmetric.keypair.encryption.provider,
  24. keyLength: box.policies.asymmetric.keypair.encryption.keyLength,
  25. publicExponent: box.policies.asymmetric.keypair.encryption.publicExponent
  26. }
  27. };
  28. var exceptions, randomFactory;
  29. var f = function(box) {
  30. exceptions = box.commons.exceptions;
  31. randomFactory =
  32. new box.primitives.securerandom.factory.SecureRandomFactory();
  33. };
  34. f.policies = {
  35. primitives: {
  36. secureRandom:
  37. {provider: box.policies.asymmetric.keypair.secureRandom.provider}
  38. }
  39. };
  40. cryptolib('commons.exceptions', 'primitives.securerandom', f);
  41. /**
  42. * A factory class for generating
  43. * {@link asymmetric.keypair.CryptoForgeKeyPairGenerator} objects.
  44. *
  45. * @class
  46. */
  47. box.asymmetric.keypair.factory.KeyPairGeneratorFactory = function() {};
  48. box.asymmetric.keypair.factory.KeyPairGeneratorFactory.prototype = {
  49. /**
  50. * @function
  51. * @returns CryptoForgeKeyPairGenerator
  52. */
  53. getEncryptionCryptoKeyPairGenerator: function() {
  54. var secureRandom;
  55. try {
  56. if (policies.encryption.provider ===
  57. Config.asymmetric.keypair.encryption.provider.FORGE) {
  58. secureRandom = randomFactory.getCryptoRandomBytes();
  59. return this.getCryptoForgeKeyPairGenerator(secureRandom);
  60. } else {
  61. throw new exceptions.CryptoLibException(
  62. 'No suitable provider was provided.');
  63. }
  64. } catch (error) {
  65. throw new exceptions.CryptoLibException(
  66. 'An encryption CryptoKeyPairGenerator could not be obtained.',
  67. error);
  68. }
  69. },
  70. getCryptoForgeKeyPairGenerator: function(secureRandom) {
  71. return new CryptoForgeKeyPairGenerator(secureRandom);
  72. }
  73. };
  74. /**
  75. * Defines an encryption key pair generator for the FORGE provider.
  76. *
  77. * @class
  78. * @memberof asymmetric/keypair
  79. */
  80. function CryptoForgeKeyPairGenerator(secureRandom) {
  81. this.secureRandom = secureRandom;
  82. }
  83. CryptoForgeKeyPairGenerator.prototype = {
  84. /**
  85. * Generates an encryption key pair.
  86. *
  87. * @returns {asymmetric/keypair.KeyPair} an encryption key pair.
  88. */
  89. generate: function() {
  90. try {
  91. if (policies.encryption.algorithm ===
  92. Config.asymmetric.keypair.encryption.algorithm.RSA) {
  93. var options = {prng: this.secureRandom};
  94. var keys = forge.pki.rsa.generateKeyPair(
  95. policies.encryption.keyLength, policies.encryption.publicExponent,
  96. options);
  97. var forgePublicKey = keys.publicKey;
  98. var forgePrivateKey = keys.privateKey;
  99. var publicKeyPem = forge.pki.publicKeyToPem(forgePublicKey);
  100. var privateKeyPem = forge.pki.privateKeyToPem(forgePrivateKey);
  101. var publicKey = new PublicKey(
  102. policies.encryption.algorithm, forgePublicKey.n, forgePublicKey.e,
  103. publicKeyPem);
  104. var privateKey = new PrivateKey(
  105. policies.encryption.algorithm, forgePrivateKey.n,
  106. forgePrivateKey.e, forgePrivateKey.d, forgePrivateKey.p,
  107. forgePrivateKey.q, forgePrivateKey.dP, forgePrivateKey.dQ,
  108. forgePrivateKey.qInv, privateKeyPem);
  109. return new KeyPair(publicKey, privateKey);
  110. } else {
  111. throw new exceptions.CryptoLibException(
  112. 'The given algorithm is not supported.');
  113. }
  114. } catch (error) {
  115. throw new exceptions.CryptoLibException(
  116. 'Encryption key pair could not be generated.', error);
  117. }
  118. }
  119. };
  120. /**
  121. * Container class for an encryption key pair.
  122. *
  123. * @class
  124. * @param publicKey
  125. * public key of the encryption key pair.
  126. * @param privateKey
  127. * private key of the encryption key pair.
  128. * @memberof asymmetric/keypair
  129. */
  130. function KeyPair(publicKey, privateKey) {
  131. /**
  132. * Retrieves the public key of the encryption key pair.
  133. *
  134. * @function
  135. * @returns PrivateKey Public key of encryption key pair.
  136. */
  137. var getPublicKey = function() {
  138. return publicKey;
  139. };
  140. /**
  141. * Retrieves the private key of the encryption key pair.
  142. *
  143. * @function
  144. * @returns PrivateKey Private key of encryption key pair.
  145. */
  146. var getPrivateKey = function() {
  147. return privateKey;
  148. };
  149. return {getPublicKey: getPublicKey, getPrivateKey: getPrivateKey};
  150. }
  151. /**
  152. * @class PrivateKey
  153. * @memberof asymmetric/keypair
  154. */
  155. function PrivateKey(algorithm, n, e, d, p, q, dP, dQ, qInv, pem) {
  156. /**
  157. * @function
  158. * @returns {string}
  159. * @memberof asymmetric/keypair.PrivateKey
  160. */
  161. var getAlgorithm = function() {
  162. return algorithm;
  163. };
  164. /**
  165. * @function
  166. * @memberof asymmetric/keypair.PrivateKey
  167. */
  168. var getModulus = function() {
  169. return n;
  170. };
  171. /**
  172. * @function
  173. * @memberof asymmetric/keypair.PrivateKey
  174. */
  175. var getPublicExponent = function() {
  176. return e;
  177. };
  178. /**
  179. * @function
  180. * @memberof asymmetric/keypair.PrivateKey
  181. */
  182. var getPrivateExponent = function() {
  183. return d;
  184. };
  185. /**
  186. * @function
  187. * @memberof asymmetric/keypair.PrivateKey
  188. */
  189. var getPrime1 = function() {
  190. return p;
  191. };
  192. /**
  193. * @function
  194. * @memberof asymmetric/keypair.PrivateKey
  195. */
  196. var getPrime2 = function() {
  197. return q;
  198. };
  199. /**
  200. * @function
  201. * @memberof asymmetric/keypair.PrivateKey
  202. */
  203. var getExponent1 = function() {
  204. return dP;
  205. };
  206. /**
  207. * @function
  208. * @memberof asymmetric/keypair.PrivateKey
  209. */
  210. var getExponent2 = function() {
  211. return dQ;
  212. };
  213. /**
  214. * @function
  215. * @memberof asymmetric/keypair.PrivateKey
  216. */
  217. var getCoefficient = function() {
  218. return qInv;
  219. };
  220. /**
  221. * @function
  222. * @memberof asymmetric/keypair.PrivateKey
  223. */
  224. var getPem = function() {
  225. return pem;
  226. };
  227. return {
  228. getAlgorithm: getAlgorithm,
  229. getModulus: getModulus,
  230. getPublicExponent: getPublicExponent,
  231. getPrivateExponent: getPrivateExponent,
  232. getPrime1: getPrime1,
  233. getPrime2: getPrime2,
  234. getExponent1: getExponent1,
  235. getExponent2: getExponent2,
  236. getCoefficient: getCoefficient,
  237. getPem: getPem
  238. };
  239. }
  240. /** @class PublicKey */
  241. function PublicKey(algorithm, n, e, pem) {
  242. /**
  243. * @function
  244. * @memberof asymmetric/keypair.PublicKey
  245. */
  246. var getAlgorithm = function() {
  247. return algorithm;
  248. };
  249. /**
  250. * @function
  251. * @memberof asymmetric/keypair.PublicKey
  252. */
  253. var getModulus = function() {
  254. return n;
  255. };
  256. /**
  257. * @function
  258. * @memberof asymmetric/keypair.PublicKey
  259. */
  260. var getPublicExponent = function() {
  261. return e;
  262. };
  263. /**
  264. * @function
  265. * @memberof asymmetric/keypair.PublicKey
  266. */
  267. var getPem = function() {
  268. return pem;
  269. };
  270. return {
  271. getAlgorithm: getAlgorithm,
  272. getModulus: getModulus,
  273. getPublicExponent: getPublicExponent,
  274. getPem: getPem
  275. };
  276. }
  277. };