symmetric.service.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.symmetric = cryptolib.modules.symmetric || {};
  9. /**
  10. * Defines the symmetric service.
  11. * @namespace symmetric/service
  12. */
  13. cryptolib.modules.symmetric.service = function(box) {
  14. 'use strict';
  15. box.symmetric = box.symmetric || {};
  16. /**
  17. * A module that provides symmetric cryptographic services.
  18. * @exports symmetric/service
  19. */
  20. box.symmetric.service = {};
  21. var cipherFactory, secretKeyFactory, macFactory;
  22. cryptolib(
  23. 'symmetric.cipher', 'symmetric.secretkey', 'symmetric.mac',
  24. 'commons.exceptions', function(box) {
  25. cipherFactory =
  26. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  27. secretKeyFactory =
  28. new box.symmetric.secretkey.factory.SecretKeyFactory();
  29. macFactory = new box.symmetric.mac.factory.MacFactory();
  30. });
  31. /**
  32. * @function getSecretKeyForEncryption
  33. * @returns {string} a secret key as a string encoded in Base64.
  34. */
  35. box.symmetric.service.getSecretKeyForEncryption = function() {
  36. var secretKeyGenerator =
  37. secretKeyFactory.getCryptoSecretKeyGeneratorForEncryption();
  38. return secretKeyGenerator.generate();
  39. };
  40. /**
  41. * @function getSecretKeyForMac
  42. * @returns {string} a MAC as a string encoded in Base64.
  43. */
  44. box.symmetric.service.getSecretKeyForMac = function() {
  45. var secretKeyGenerator =
  46. secretKeyFactory.getCryptoSecretKeyGeneratorForMac();
  47. return secretKeyGenerator.generate();
  48. };
  49. /**
  50. * @function encrypt
  51. * @returns {string} the initialization vector concatenated with the encrypted data,
  52. * as string in Base64 encoded format.
  53. */
  54. box.symmetric.service.encrypt = function(secretKeyBase64, dataBase64) {
  55. var cipher = cipherFactory.getCryptoSymmetricCipher();
  56. return cipher.encrypt(secretKeyBase64, dataBase64);
  57. };
  58. /**
  59. * @function decrypt
  60. * @returns {string} the decrypted data, as a String in Base64 encoded format.
  61. */
  62. box.symmetric.service.decrypt = function(
  63. secretKeyBase64, initVectorAndEncryptedDataBase64) {
  64. var cipher = cipherFactory.getCryptoSymmetricCipher();
  65. return cipher.decrypt(secretKeyBase64, initVectorAndEncryptedDataBase64);
  66. };
  67. /**
  68. * @function getMac
  69. * @returns {string} a string in Base64 encoded format.
  70. */
  71. box.symmetric.service.getMac = function(secretKeyBase64, arrayDataBase64) {
  72. var cryptoMac = macFactory.create();
  73. return cryptoMac.generate(secretKeyBase64, arrayDataBase64);
  74. };
  75. /**
  76. * @function verifyMac
  77. * @returns {boolean} true if the data has been verified.
  78. */
  79. box.symmetric.service.verifyMac = function(
  80. macBase64, secretKeyBase64, arrayDataBase64) {
  81. var cryptoMac = macFactory.create();
  82. return cryptoMac.verify(macBase64, secretKeyBase64, arrayDataBase64);
  83. };
  84. };