2
0

homomorphic.service.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.service = function(box) {
  10. 'use strict';
  11. box.homomorphic = box.homomorphic || {};
  12. /**
  13. * A module that provides high-level ElGamal cryptographic services.
  14. *
  15. * @exports homomorphic/service
  16. */
  17. box.homomorphic.service = {};
  18. var elGamalFactory;
  19. var keyFactory;
  20. cryptolib('homomorphic.cipher', 'homomorphic.keypair', function(box) {
  21. elGamalFactory = new box.homomorphic.cipher.factory.ElGamalCipherFactory();
  22. keyFactory = new box.homomorphic.keypair.factory.KeyFactory();
  23. });
  24. /**
  25. * @function createElGamalKeyPair
  26. * @param elGamalPublicKey
  27. * {Object} an ElGamal public key.
  28. * @param elGamalPrivateKey
  29. * {Object} an ElGamal private key.
  30. *
  31. * @returns {Object} an object of ElGamalKeyPair.
  32. */
  33. box.homomorphic.service.createElGamalKeyPair = function(
  34. elGamalPublicKey, elGamalPrivateKey) {
  35. return keyFactory.createKeyPair(elGamalPublicKey, elGamalPrivateKey);
  36. };
  37. /**
  38. * @function createElGamalPublicKey
  39. * @param serializedElGamalPublicKeyB64
  40. * {String] a serialized ElGamalPublicKey, in base 64 encoded
  41. * format.
  42. *
  43. * @returns {Object} a ElGamalPublicKey, as an object.
  44. */
  45. box.homomorphic.service.createElGamalPublicKey = function(
  46. serializedElGamalPublicKeyB64) {
  47. return keyFactory.createPublicKey(serializedElGamalPublicKeyB64);
  48. };
  49. /**
  50. * @function createElGamalPrivateKey
  51. * @param serializedElGamalPrivateKeyB64
  52. * {String} a serialized ElGamalPrivateKey, in base 64 encoded
  53. * format.
  54. *
  55. * @returns {Object} a ElGamalPrivateKey, as an object.
  56. */
  57. box.homomorphic.service.createElGamalPrivateKey = function(
  58. serializedElGamalPrivateKeyB64) {
  59. return keyFactory.createPrivateKey(serializedElGamalPrivateKeyB64);
  60. };
  61. /**
  62. * @function createEncrypter
  63. * @param elGamalPublicKey
  64. * {Object} a public key, as an ElGamalPublicKey object.
  65. * @param [cryptoRandomInteger] {Object} an optional parameter to set the
  66. * source of randomness for the generation of the ElGamal encryption
  67. * exponent. If not provided, the source will be created internally.
  68. *
  69. * @returns {Object} an ElGamalEncrypter.
  70. */
  71. box.homomorphic.service.createEncrypter = function(
  72. elGamalPublicKey, cryptoRandomInteger) {
  73. return elGamalFactory.createEncrypter(
  74. elGamalPublicKey, cryptoRandomInteger);
  75. };
  76. /**
  77. * @function createDecrypter
  78. * @param elGamalPrivateKey
  79. * {Object} a private key, as an ElGamalPrivateKey object.
  80. *
  81. * @returns {Object} an ElGamalDecrypter.
  82. */
  83. box.homomorphic.service.createDecrypter = function(elGamalPrivateKey) {
  84. return elGamalFactory.createDecrypter(elGamalPrivateKey);
  85. };
  86. /**
  87. * @function createRandomDecrypter
  88. * @param elGamalPublicKey
  89. * {Object} a public key, as an ElGamalPublicKey object.
  90. * @param cryptoRandomInteger {Object} the
  91. * source of randomness for the generation of the ElGamal encryption
  92. * exponent.
  93. * @returns {Object} an ElGamalRandomDecrypter.
  94. */
  95. box.homomorphic.service.createRandomDecrypter = function(
  96. elGamalPublicKey, cryptoRandomInteger) {
  97. return elGamalFactory.createRandomDecrypter(
  98. elGamalPublicKey, cryptoRandomInteger);
  99. };
  100. };