2
0

homomorphic.keypair.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.homomorphic = cryptolib.modules.homomorphic || {};
  9. cryptolib.modules.homomorphic.keypair = function(box) {
  10. 'use strict';
  11. box.homomorphic = box.homomorphic || {};
  12. box.homomorphic.keypair = {};
  13. /**
  14. * A submodule that holds ElGamal key pair functionalities.
  15. * @exports homomorphic/keypair/factory
  16. */
  17. box.homomorphic.keypair.factory = {};
  18. var converters;
  19. var mathematical;
  20. var f = function(box) {
  21. converters = new box.commons.utils.Converters();
  22. mathematical = box.commons.mathematical;
  23. };
  24. cryptolib('commons.exceptions', 'commons.utils', 'commons.mathematical', f);
  25. /** @class */
  26. box.homomorphic.keypair.factory.KeyFactory = function() {
  27. };
  28. box.homomorphic.keypair.factory.KeyFactory.prototype = {
  29. /**
  30. * @function
  31. * @returns ElGamalKeyPair
  32. */
  33. createKeyPair: function(elGamalPublicKey, elGamalPrivateKey) {
  34. return new ElGamalKeyPair(elGamalPublicKey, elGamalPrivateKey);
  35. },
  36. /**
  37. * @function
  38. * @returns ElGamalPublicKey
  39. */
  40. createPublicKey: function(
  41. serializedElGamalPublicKeyB64OrListOfGroupElements, keyGroup) {
  42. return new ElGamalPublicKey(
  43. serializedElGamalPublicKeyB64OrListOfGroupElements, keyGroup);
  44. },
  45. /**
  46. * @function
  47. * @returns ElGamalPrivateKey
  48. */
  49. createPrivateKey: function(
  50. serializedElGamalPrivateKeyB64OrListOfExponents, keyGroup) {
  51. return new ElGamalPrivateKey(
  52. serializedElGamalPrivateKeyB64OrListOfExponents, keyGroup);
  53. }
  54. };
  55. /**
  56. * Representation of an ElGamal key pair. This key pair is composed of a
  57. * private key and a public key.
  58. * @class ElGamalKeyPair
  59. * @param elGamalPublicKey
  60. * ElGamal public key.
  61. * @param elGamalPrivateKey
  62. * ElGamal private key.
  63. * @returns {ElGamalKeyPair}
  64. */
  65. function ElGamalKeyPair(elGamalPublicKey, elGamalPrivateKey) {
  66. var _elGamalPublicKey = elGamalPublicKey;
  67. var _elGamalPrivateKey = elGamalPrivateKey;
  68. this.getPublicKey = function() {
  69. return _elGamalPublicKey;
  70. };
  71. this.getPrivateKey = function() {
  72. return _elGamalPrivateKey;
  73. };
  74. }
  75. /**
  76. * Encapsulates an ElGamal public key.
  77. * <P>
  78. * This class includes the key itself (which is actually a list of
  79. * elements), as well as the mathematical group that this key belongs to.
  80. * @class ElGamalPublicKey
  81. * @param {String_or_Array} serializedElGamalPublicKeyB64OrListOfGroupElements
  82. * this parameter can be either a string containing the
  83. * serialized ElGamal public key to JSON string, or an array of
  84. * ZpGroupElement.
  85. * @param {ZpSubgroup} keyGroup
  86. * the ZpSubgroup of the public key.
  87. * @returns {ElGamalPublicKey}
  88. */
  89. function ElGamalPublicKey(
  90. serializedElGamalPublicKeyB64OrListOfGroupElements, keyGroup) {
  91. var _group;
  92. var _groupElementsArray = [];
  93. var _fromJson = function(serializedElGamalPublicKey) {
  94. var parsedPublicKey = JSON.parse(serializedElGamalPublicKey).publicKey;
  95. var p = converters.base64ToBigInteger(
  96. parsedPublicKey.zpSubgroup.p.toString());
  97. var q = converters.base64ToBigInteger(
  98. parsedPublicKey.zpSubgroup.q.toString());
  99. var generator = converters.base64ToBigInteger(
  100. parsedPublicKey.zpSubgroup.g.toString());
  101. _group = new mathematical.ZpSubgroup(generator, p, q);
  102. for (var i = 0; i < parsedPublicKey.elements.length; i++) {
  103. var elementValue =
  104. converters.base64ToBigInteger(parsedPublicKey.elements[i]);
  105. var element = new mathematical.ZpGroupElement(
  106. elementValue, _group.getP(), _group.getQ());
  107. _groupElementsArray.push(element);
  108. }
  109. };
  110. if (typeof keyGroup === 'undefined') {
  111. var serializedElGamalPublicKey = converters.base64Decode(
  112. serializedElGamalPublicKeyB64OrListOfGroupElements);
  113. _fromJson(serializedElGamalPublicKey);
  114. } else {
  115. _groupElementsArray = serializedElGamalPublicKeyB64OrListOfGroupElements;
  116. _group = keyGroup;
  117. }
  118. this.getGroup = function() {
  119. return _group;
  120. };
  121. this.getGroupElementsArray = function() {
  122. return _groupElementsArray;
  123. };
  124. }
  125. /**
  126. * Encapsulates an ElGamal private key.
  127. * <P>
  128. * This class includes the key itself (which is actually a list of
  129. * exponents), as well as the mathematical group that this key belongs to.
  130. * @class ElGamalPrivateKey
  131. * @param {String_or_Array} serializedElGamalPrivateKeyB64OrListOfExponents
  132. * this parameter can be either a string containing the
  133. * serialized to JSON string ElGamal private key
  134. * or an array of Exponents.
  135. * @param {ZpSubgroup} keyGroup
  136. * the ZpSubgroup of the private key.
  137. * @returns {ElGamalPrivateKey}
  138. */
  139. function ElGamalPrivateKey(
  140. serializedElGamalPrivateKeyB64OrListOfExponents, keyGroup) {
  141. var _group;
  142. var _exponentsArray = [];
  143. var _fromJson = function(serializedElGamalPrivateKey) {
  144. var parsedPrivateKey = JSON.parse(serializedElGamalPrivateKey).privateKey;
  145. var p = converters.base64ToBigInteger(
  146. parsedPrivateKey.zpSubgroup.p.toString());
  147. var q = converters.base64ToBigInteger(
  148. parsedPrivateKey.zpSubgroup.q.toString());
  149. var generator = converters.base64ToBigInteger(
  150. parsedPrivateKey.zpSubgroup.g.toString());
  151. _group = new mathematical.ZpSubgroup(generator, p, q);
  152. for (var i = 0; i < parsedPrivateKey.exponents.length; i++) {
  153. var exponentValue =
  154. converters.base64ToBigInteger(parsedPrivateKey.exponents[i]);
  155. var exponent = new mathematical.Exponent(_group.getQ(), exponentValue);
  156. _exponentsArray.push(exponent);
  157. }
  158. };
  159. if (typeof keyGroup === 'undefined') {
  160. var serializedElGamalPrivateKey = converters.base64Decode(
  161. serializedElGamalPrivateKeyB64OrListOfExponents);
  162. _fromJson(serializedElGamalPrivateKey);
  163. } else {
  164. _exponentsArray = serializedElGamalPrivateKeyB64OrListOfExponents;
  165. _group = keyGroup;
  166. }
  167. this.getGroup = function() {
  168. return _group;
  169. };
  170. this.getExponentsArray = function() {
  171. return _exponentsArray;
  172. };
  173. }
  174. };