symmetric.service.spec.js 3.8 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. describe('Symmetric service...', function() {
  9. 'use strict';
  10. describe('should be able to ..', function() {
  11. it('return a secret key for encryption', function() {
  12. cryptolib('symmetric.service', function(box) {
  13. var secretKey = box.symmetric.service.getSecretKeyForEncryption();
  14. expect(secretKey).toBeDefined();
  15. });
  16. });
  17. it('return a secret key for mac', function() {
  18. cryptolib('symmetric.service', function(box) {
  19. var secretKey = box.symmetric.service.getSecretKeyForMac();
  20. expect(secretKey).toBeDefined();
  21. });
  22. });
  23. it('encrypt and decrypt', function() {
  24. cryptolib('symmetric.service', 'commons.utils', function(box) {
  25. // generate a secret key
  26. var secretKeyB64 = box.symmetric.service.getSecretKeyForEncryption();
  27. // encrypt and decrypt data
  28. var converters = new box.commons.utils.Converters();
  29. var dataB64 = converters.base64Encode('anyString');
  30. var encryptedDataBase64 =
  31. box.symmetric.service.encrypt(secretKeyB64, dataB64);
  32. var decryptedDataBase64 =
  33. box.symmetric.service.decrypt(secretKeyB64, encryptedDataBase64);
  34. expect(dataB64 === decryptedDataBase64).toBeTruthy();
  35. });
  36. });
  37. it('compute a MAC and verify it', function() {
  38. cryptolib('symmetric.service', 'commons.utils', function(box) {
  39. // generate a secret key
  40. var secretKeyB64 = box.symmetric.service.getSecretKeyForMac();
  41. // encrypt and decrypt data
  42. var converters = new box.commons.utils.Converters();
  43. var dataB64 = converters.base64Encode('anyString');
  44. var macB64 = box.symmetric.service.getMac(secretKeyB64, [dataB64]);
  45. var verified =
  46. box.symmetric.service.verifyMac(macB64, secretKeyB64, [dataB64]);
  47. expect(verified).toBeTruthy();
  48. });
  49. });
  50. });
  51. describe('bug #90835', function() {
  52. it('if the original message contains spaces, they should be preserved',
  53. function() {
  54. cryptolib('symmetric.service', 'commons.utils', function(box) {
  55. var text = 'text with some spaces';
  56. var secretKeyB64 = box.symmetric.service.getSecretKeyForEncryption();
  57. // encrypt and decrypt data
  58. var converters = new box.commons.utils.Converters();
  59. var dataB64 = converters.base64Encode(text);
  60. var encryptedDataBase64 =
  61. box.symmetric.service.encrypt(secretKeyB64, dataB64);
  62. var decryptedDataBase64 =
  63. box.symmetric.service.decrypt(secretKeyB64, encryptedDataBase64);
  64. var decryptedData = converters.base64Decode(decryptedDataBase64);
  65. // Verify that data was successfully decrypted.
  66. expect(decryptedData).toEqual(text);
  67. });
  68. });
  69. it('if the original message contains only spaces, they should be preserved',
  70. function() {
  71. cryptolib('symmetric.service', 'commons.utils', function(box) {
  72. var text = ' ';
  73. var secretKeyB64 = box.symmetric.service.getSecretKeyForEncryption();
  74. // encrypt and decrypt data
  75. var converters = new box.commons.utils.Converters();
  76. var dataB64 = converters.base64Encode(text);
  77. var encryptedDataBase64 =
  78. box.symmetric.service.encrypt(secretKeyB64, dataB64);
  79. var decryptedDataBase64 =
  80. box.symmetric.service.decrypt(secretKeyB64, encryptedDataBase64);
  81. var decryptedData = converters.base64Decode(decryptedDataBase64);
  82. // Verify that data was successfully decrypted.
  83. expect(decryptedData).toEqual(text);
  84. });
  85. });
  86. });
  87. });