envelope.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /* jshint node:true */
  9. 'use strict';
  10. var codec = require('scytl-codec');
  11. module.exports = DigitalEnvelope;
  12. /**
  13. * @class DigitalEnvelope
  14. * @classdesc Encapsulates a digital envelope. This object is instantiated by
  15. * the method {@link DigitalEnvelopeService.newEnvelope} or
  16. * internally by the method {@link
  17. * DigitalEnvelopeGenerator.generate}.
  18. * @property {Uint8Array} encryptedData The symmetrically encrypted data.
  19. * @property {Uint8Array} mac The MAC of the symmetrically encrypted data.
  20. * @property {EncryptedSecretKeyPair[]} encryptedSecretKeyPairs The asymmetric
  21. * encryptions of the concatenation of the encryption secret key and
  22. * MAC secret key used to generate and open the digital envelope, each
  23. * asymmetric encryption having been generated using a different one
  24. * of the public keys used to initialize digital envelope generator.
  25. */
  26. function DigitalEnvelope(encryptedData, mac, encryptedSecretKeyPairs) {
  27. this.encryptedData = encryptedData;
  28. this.mac = mac;
  29. Object.freeze(this.encryptedSecretKeyPairs = encryptedSecretKeyPairs);
  30. Object.freeze(this);
  31. }
  32. DigitalEnvelope.prototype = {
  33. /**
  34. * Serializes this object into a JSON string representation.
  35. * <p>
  36. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  37. * corresponding serialization in the library <code>cryptoLib</code>,
  38. * implemented in Java, since the two libraries are expected to communicate
  39. * with each other via these serializations.
  40. * <p>
  41. *
  42. * @function toJson
  43. * @memberof DigitalEnvelope
  44. * @returns {string} The JSON string representation of this object.
  45. */
  46. toJson: function() {
  47. var encryptedDataB64 = codec.base64Encode(this.encryptedData);
  48. var macB64 = codec.base64Encode(this.mac);
  49. var encryptedSecretKeyPairs = this.encryptedSecretKeyPairs;
  50. var encryptedSecretKeyPairsB64 = [];
  51. var publicKeysPem_ = [];
  52. for (var i = 0; i < encryptedSecretKeyPairs.length; i++) {
  53. encryptedSecretKeyPairsB64.push(
  54. codec.base64Encode(encryptedSecretKeyPairs[i].encryptedKeyPair));
  55. publicKeysPem_[i] = encryptedSecretKeyPairs[i].publicKey;
  56. }
  57. return JSON.stringify({
  58. digitalEnvelope: {
  59. encryptedDataBase64: encryptedDataB64,
  60. macBase64: macB64,
  61. encryptedSecretKeyConcatsBase64: encryptedSecretKeyPairsB64,
  62. publicKeysPem: publicKeysPem_
  63. }
  64. });
  65. }
  66. };