2
0

symmetric.cipher.spec.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 cipher ...', function() {
  9. 'use strict';
  10. var _testString = 'Ox2fUJq1gAbX';
  11. var _testString_2 = 'mUm9EQC8mrDw';
  12. var _aesSecretKeyBase64 = 'BBZcaE5El25RZZzWTLTwcA==';
  13. var _aesSecretKeyBase64_2 = 'v3VagUVMuZJ+b+uFSjqGTw==';
  14. var _symmetricEncryptedDataBase64 =
  15. 'Cvpz8/L+mTv2eFvYpxNoxt+/KJwtAOzG7hl9Dw==';
  16. var _symmetricInitVectorBase64 = 'LGF26O6hR2fgUsHd';
  17. describe('should be able to ..', function() {
  18. it('Encrypt and decrypt some data.', function() {
  19. cryptolib('symmetric.cipher', 'commons.utils', function(box) {
  20. var cipherFactory =
  21. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  22. var converters = new box.commons.utils.Converters();
  23. var aesCipher = cipherFactory.getCryptoSymmetricCipher();
  24. var dataB64 = converters.base64Encode(_testString);
  25. // Encrypt data and Base64 encode result.
  26. var initVectorAndEncryptedDataBase64 =
  27. aesCipher.encrypt(_aesSecretKeyBase64, dataB64);
  28. // Base64 decode encrypted data and decrypt data.
  29. var decryptedDataB64 = aesCipher.decrypt(
  30. _aesSecretKeyBase64, initVectorAndEncryptedDataBase64);
  31. var decryptedData = converters.base64Decode(decryptedDataB64);
  32. // Verify that data was successfully decrypted.
  33. expect(decryptedData).toBe(_testString);
  34. });
  35. });
  36. });
  37. it('AES-GCM decrypt some data that was AES-GCM encrypted using Java.',
  38. function() {
  39. cryptolib('symmetric.cipher', 'commons.utils', function(box) {
  40. var cipherFactory =
  41. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  42. var aesCipher = cipherFactory.getCryptoSymmetricCipher();
  43. var converters = new box.commons.utils.Converters();
  44. var initVectorAndEncryptedDataBase64 =
  45. _symmetricInitVectorBase64 + _symmetricEncryptedDataBase64;
  46. var decryptedDataB64 = aesCipher.decrypt(
  47. _aesSecretKeyBase64_2, initVectorAndEncryptedDataBase64);
  48. var decryptedData = converters.base64Decode(decryptedDataB64);
  49. // Verify that data was successfully decrypted.
  50. expect(decryptedData).toBe(_testString_2);
  51. });
  52. });
  53. it('throw an error when the given key does not match the required length by the cipher algorithm when encrypt',
  54. function() {
  55. cryptolib('symmetric.cipher', 'commons.utils', function(box) {
  56. var cipherFactory =
  57. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  58. var converters = new box.commons.utils.Converters();
  59. var aesCipher = cipherFactory.getCryptoSymmetricCipher();
  60. var dataB64 = converters.base64Encode(_testString);
  61. expect(function() {
  62. aesCipher.encrypt('BBZcaE5El25RzWTLTwcA==', dataB64);
  63. }).toThrow();
  64. });
  65. });
  66. it('throw an error when the given key does not match the required length by the cipher algorithm when decrypt',
  67. function() {
  68. cryptolib('symmetric.cipher', 'commons.utils', function(box) {
  69. var cipherFactory =
  70. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  71. var converters = new box.commons.utils.Converters();
  72. var aesCipher = cipherFactory.getCryptoSymmetricCipher();
  73. var dataB64 = converters.base64Encode(_testString);
  74. var initVectorAndEncryptedDataBase64 =
  75. aesCipher.encrypt(_aesSecretKeyBase64, dataB64);
  76. expect(function() {
  77. aesCipher.decrypt(
  78. 'BBZcaE5El25RzWTLTwcA==', initVectorAndEncryptedDataBase64);
  79. }).toThrow();
  80. });
  81. });
  82. it('throw an error when the given algorithm is wrong.', function() {
  83. var f = function(box) {
  84. var cipherFactory =
  85. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  86. var converters = new box.commons.utils.Converters();
  87. var aesCipher = cipherFactory.getCryptoSymmetricCipher();
  88. var dataB64 = converters.base64Encode(_testString);
  89. expect(function() {
  90. aesCipher.encrypt(_aesSecretKeyBase64, dataB64);
  91. }).toThrow();
  92. };
  93. f.policies = {
  94. symmetric: {
  95. cipher: {
  96. algorithm:
  97. {WRONG: {name: 'WRONG', keyLengthBytes: 16, tagLengthBytes: 16}},
  98. provider: 'Forge',
  99. initializationVectorLengthBytes: 12,
  100. secureRandom: {provider: 'Scytl'}
  101. }
  102. }
  103. };
  104. cryptolib('symmetric.cipher', 'commons.utils', f);
  105. });
  106. it('throw an error when the given provider is wrong.', function() {
  107. var f = function(box) {
  108. var cipherFactory =
  109. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  110. expect(function() {
  111. cipherFactory.getCryptoSymmetricCipher();
  112. }).toThrow();
  113. };
  114. f.policies = {
  115. symmetric: {
  116. cipher: {
  117. algorithm: {
  118. AES128_GCM:
  119. {name: 'AES-GCM', keyLengthBytes: 16, tagLengthBytes: 16}
  120. },
  121. provider: 'WRONG',
  122. initializationVectorLengthBytes: 12,
  123. secureRandom: {provider: 'Scytl'}
  124. }
  125. }
  126. };
  127. cryptolib('symmetric.cipher', 'commons.utils', f);
  128. });
  129. describe('bug #90835', function() {
  130. it('if the original message contains spaces, they should be preserved',
  131. function() {
  132. cryptolib('symmetric.cipher', 'commons.utils', function(box) {
  133. var text = 'text with some spaces';
  134. var cipherFactory =
  135. new box.symmetric.cipher.factory.SymmetricCipherFactory();
  136. var converters = new box.commons.utils.Converters();
  137. var aesCipher = cipherFactory.getCryptoSymmetricCipher();
  138. var dataB64 = converters.base64Encode(text);
  139. // Encrypt data and Base64 encode result.
  140. var initVectorAndEncryptedDataBase64 =
  141. aesCipher.encrypt(_aesSecretKeyBase64, dataB64);
  142. // Base64 decode encrypted data and decrypt data.
  143. var decryptedDataB64 = aesCipher.decrypt(
  144. _aesSecretKeyBase64, initVectorAndEncryptedDataBase64);
  145. var decryptedData = converters.base64Decode(decryptedDataB64);
  146. // Verify that data was successfully decrypted.
  147. expect(decryptedData).toEqual(text);
  148. });
  149. });
  150. });
  151. });