index.spec.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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, jasmine:true */
  9. 'use strict';
  10. var cryptoPolicy = require('scytl-cryptopolicy');
  11. var symmetric = require('../lib/index');
  12. var secureRandom = require('scytl-securerandom');
  13. var bitwise = require('scytl-bitwise');
  14. var codec = require('scytl-codec');
  15. describe('The symmetric cryptography module should be able to ...', function() {
  16. var DATA = 'anystring';
  17. var OTHER_DATA_1 = DATA + 'a';
  18. var OTHER_DATA_2 = DATA + 'b';
  19. var data_;
  20. var otherData1_;
  21. var otherData2_;
  22. var dataParts_;
  23. var service_;
  24. var policy_ = cryptoPolicy.newInstance();
  25. var keyGenerator_;
  26. var encryptionKey_;
  27. var macKey_;
  28. var cipher_;
  29. var macHandler_;
  30. var encryptedData_;
  31. var mac_;
  32. beforeAll(function() {
  33. data_ = codec.utf8Encode(DATA);
  34. otherData1_ = codec.utf8Encode(OTHER_DATA_1);
  35. otherData2_ = codec.utf8Encode(OTHER_DATA_2);
  36. dataParts_ = [data_, otherData1_, otherData2_];
  37. service_ = symmetric.newService();
  38. policy_ = cryptoPolicy.newInstance();
  39. keyGenerator_ = service_.newKeyGenerator();
  40. encryptionKey_ = keyGenerator_.nextEncryptionKey();
  41. macKey_ = keyGenerator_.nextMacKey();
  42. cipher_ = service_.newCipher().init(encryptionKey_);
  43. macHandler_ = service_.newMacHandler().init(macKey_);
  44. encryptedData_ = cipher_.encrypt(data_);
  45. mac_ = macHandler_.generate(data_);
  46. });
  47. describe('create a symmetric cryptography service that should be able to ..', function() {
  48. describe('create a secret key generator that should be able to', function() {
  49. it('generate a secret key for encryption', function() {
  50. expect(encryptionKey_.length)
  51. .toBe(policy_.symmetric.secretKey.encryption.lengthBytes);
  52. });
  53. it('generate a secret key for mac generation', function() {
  54. expect(macKey_.length)
  55. .toBe(policy_.symmetric.secretKey.mac.lengthBytes);
  56. });
  57. it('generate secret keys, using a specified cryptographic policy',
  58. function() {
  59. var policy = cryptoPolicy.newInstance();
  60. policy.symmetric.secretKey.encryption.lengthBytes =
  61. cryptoPolicy.options.symmetric.secretKey.encryption.lengthBytes
  62. .KL_32;
  63. policy.symmetric.secretKey.mac.lengthBytes =
  64. cryptoPolicy.options.symmetric.secretKey.mac.lengthBytes.KL_64;
  65. var service = symmetric.newService({policy: policy});
  66. var keyGenerator = service.newKeyGenerator();
  67. var encryptionKey = keyGenerator.nextEncryptionKey();
  68. expect(encryptionKey.length)
  69. .toBe(policy.symmetric.secretKey.encryption.lengthBytes);
  70. var macKey = keyGenerator.nextMacKey();
  71. expect(macKey.length)
  72. .toEqual(policy.symmetric.secretKey.mac.lengthBytes);
  73. });
  74. it('generate secret keys, using a specified secure random service object',
  75. function() {
  76. var secureRandomService = secureRandom.newService();
  77. var service =
  78. symmetric.newService({secureRandomService: secureRandomService});
  79. var keyGenerator = service.newKeyGenerator();
  80. var encryptionKey = keyGenerator.nextEncryptionKey();
  81. expect(encryptionKey.length)
  82. .toBe(policy_.symmetric.secretKey.encryption.lengthBytes);
  83. var macKey = keyGenerator.nextMacKey();
  84. expect(macKey.length)
  85. .toBe(policy_.symmetric.secretKey.mac.lengthBytes);
  86. });
  87. });
  88. describe('create a symmetric cipher that should be able to ..', function() {
  89. it('encrypt and decrypt some data', function() {
  90. var encryptedData = cipher_.encrypt(data_);
  91. var decryptedData = cipher_.decrypt(encryptedData);
  92. expect(decryptedData).toEqual(data_);
  93. });
  94. it('encrypt and decrypt some data of type string', function() {
  95. var encryptedData = cipher_.encrypt(DATA);
  96. var decryptedData = codec.utf8Decode(cipher_.decrypt(encryptedData));
  97. expect(decryptedData).toEqual(DATA);
  98. });
  99. it('encrypt and decrypt some data, using a specified cryptographic policy',
  100. function() {
  101. var policy = cryptoPolicy.newInstance();
  102. policy.symmetric.secretKey.encryption.lengthBytes =
  103. cryptoPolicy.options.symmetric.secretKey.encryption.lengthBytes
  104. .KL_32;
  105. policy.symmetric.cipher.algorithm.keyLengthBytes =
  106. cryptoPolicy.options.symmetric.cipher.algorithm.AES_GCM
  107. .keyLengthBytes.KL_32;
  108. var service = symmetric.newService({policy: policy});
  109. var key = service.newKeyGenerator().nextEncryptionKey();
  110. var cipher = service.newCipher().init(key);
  111. var encryptedData = cipher.encrypt(data_);
  112. var decryptedData = cipher.decrypt(encryptedData);
  113. expect(decryptedData).toEqual(data_);
  114. });
  115. it('encrypt and decrypt some data, using a specified secure random service object',
  116. function() {
  117. var secureRandomService = secureRandom.newService();
  118. var service =
  119. symmetric.newService({secureRandomService: secureRandomService});
  120. var keyGenerator = service.newKeyGenerator();
  121. var key = keyGenerator.nextEncryptionKey();
  122. var cipher = service.newCipher().init(key);
  123. var encryptedData = cipher.encrypt(data_);
  124. var decryptedData = cipher.decrypt(encryptedData);
  125. expect(decryptedData).toEqual(data_);
  126. });
  127. it('encrypt and decrypt data that contains characters and spaces',
  128. function() {
  129. var data = codec.utf8Encode('text with some spaces');
  130. var encryptedData = cipher_.encrypt(data);
  131. var decryptedData = cipher_.decrypt(encryptedData);
  132. expect(decryptedData).toEqual(data);
  133. });
  134. it('encrypt and decrypt data that only contains spaces', function() {
  135. var data = codec.utf8Encode(' ');
  136. var encryptedData = cipher_.encrypt(data);
  137. var decryptedData = cipher_.decrypt(encryptedData);
  138. expect(decryptedData).toEqual(data);
  139. });
  140. it('decrypt some data that was encrypted with the AES-GCM algorithm, using Java.',
  141. function() {
  142. var data_java = codec.utf8Encode('mUm9EQC8mrDw');
  143. var encryptionKey_java =
  144. codec.base64Decode('v3VagUVMuZJ+b+uFSjqGTw==');
  145. var initVector_java = codec.base64Decode('LGF26O6hR2fgUsHd');
  146. var encryptedDataJava_java =
  147. codec.base64Decode('Cvpz8/L+mTv2eFvYpxNoxt+/KJwtAOzG7hl9Dw==');
  148. var initVectorAndEncryptedData =
  149. bitwise.concatenate(initVector_java, encryptedDataJava_java);
  150. var cipher = service_.newCipher().init(encryptionKey_java);
  151. var decryptedData_java = cipher.decrypt(initVectorAndEncryptedData);
  152. expect(decryptedData_java).toEqual(data_java);
  153. });
  154. it('throw an error when initializing a cipher, using a secret key with a length that differs from that of the cipher',
  155. function() {
  156. var policy = cryptoPolicy.newInstance();
  157. policy.symmetric.secretKey.encryption.lengthBytes =
  158. cryptoPolicy.options.symmetric.secretKey.encryption.lengthBytes
  159. .KL_16;
  160. policy.symmetric.cipher.algorithm.keyLengthBytes =
  161. cryptoPolicy.options.symmetric.cipher.algorithm.AES_GCM
  162. .keyLengthBytes.KL_32;
  163. var service = symmetric.newService({policy: policy});
  164. var key = service.newKeyGenerator().nextEncryptionKey();
  165. var cipher = service.newCipher();
  166. expect(function() {
  167. cipher.init(key);
  168. }).toThrow();
  169. });
  170. it('throw an error when encrypting some data before the cipher has been initialized with a secret key',
  171. function() {
  172. var cipher = service_.newCipher();
  173. expect(function() {
  174. cipher.encrypt(data_);
  175. }).toThrow();
  176. });
  177. it('throw an error when decrypting some encrypted data before the cipher has been initialized with a secret key',
  178. function() {
  179. var cipher = service_.newCipher();
  180. expect(function() {
  181. cipher.decrypt(encryptedData_);
  182. }).toThrow();
  183. });
  184. });
  185. describe('create a MAC handler that should be able to ..', function() {
  186. it('generate and verify a MAC', function() {
  187. var mac = macHandler_.generate(data_);
  188. var verified = macHandler_.verify(mac, data_);
  189. expect(verified).toBeTruthy();
  190. });
  191. it('generate a MAC from multiple data parts', function() {
  192. for (var i = 0; i < dataParts_.length; i++) {
  193. macHandler_.update(dataParts_[i]);
  194. }
  195. var mac = macHandler_.generate();
  196. var verified = macHandler_.verify(
  197. mac, bitwise.concatenate(data_, otherData1_, otherData2_));
  198. expect(verified).toBeTruthy();
  199. for (var j = 0; j < (dataParts_.length - 1); j++) {
  200. macHandler_.update(dataParts_[j]);
  201. }
  202. mac = macHandler_.generate(otherData2_);
  203. verified = macHandler_.verify(
  204. mac, bitwise.concatenate(data_, otherData1_, otherData2_));
  205. expect(verified).toBeTruthy();
  206. });
  207. it('generate a MAC from multiple data parts, using method chaining',
  208. function() {
  209. var mac = macHandler_.update(data_)
  210. .update(otherData1_)
  211. .update(otherData2_)
  212. .generate();
  213. var verified = macHandler_.verify(
  214. mac, bitwise.concatenate(data_, otherData1_, otherData2_));
  215. expect(verified).toBeTruthy();
  216. mac = macHandler_.update(data_)
  217. .update(otherData1_)
  218. .generate(otherData2_);
  219. verified = macHandler_.verify(
  220. mac, bitwise.concatenate(data_, otherData1_, otherData2_));
  221. expect(verified).toBeTruthy();
  222. });
  223. it('verify a MAC generated from multiple data parts', function() {
  224. var mac = macHandler_.generate(
  225. bitwise.concatenate(data_, otherData1_, otherData2_));
  226. for (var i = 0; i < dataParts_.length; i++) {
  227. macHandler_.update(dataParts_[i]);
  228. }
  229. var verified = macHandler_.verify(mac);
  230. expect(verified).toBeTruthy();
  231. for (var j = 0; j < (dataParts_.length - 1); j++) {
  232. macHandler_.update(dataParts_[j]);
  233. }
  234. verified = macHandler_.verify(mac, otherData2_);
  235. expect(verified).toBeTruthy();
  236. });
  237. it('verify a MAC generated from multiple data parts, using method chaining',
  238. function() {
  239. var mac = macHandler_.generate(
  240. bitwise.concatenate(data_, otherData1_, otherData2_));
  241. var verified = macHandler_.update(data_)
  242. .update(otherData1_)
  243. .update(otherData2_)
  244. .verify(mac);
  245. expect(verified).toBeTruthy();
  246. verified = macHandler_.update(data_)
  247. .update(otherData1_)
  248. .verify(mac, otherData2_);
  249. expect(verified).toBeTruthy();
  250. });
  251. it('generate the expected MAC when using data of type string',
  252. function() {
  253. var mac1 = macHandler_.update(data_)
  254. .update(otherData1_)
  255. .generate(otherData2_);
  256. var mac2 = macHandler_.update(DATA)
  257. .update(OTHER_DATA_1)
  258. .generate(OTHER_DATA_2);
  259. expect(mac2).toEqual(mac1);
  260. });
  261. it('verify a MAC when using data of type string', function() {
  262. var mac =
  263. macHandler_.update(data_).update(otherData1_).generate(otherData2_);
  264. var verified = macHandler_.update(DATA)
  265. .update(OTHER_DATA_1)
  266. .verify(mac, OTHER_DATA_2);
  267. expect(verified).toBeTruthy();
  268. });
  269. it('be automatically reinitialized after MAC generation', function() {
  270. var mac1 = macHandler_.generate(data_);
  271. var mac2 = macHandler_.generate(data_);
  272. expect(mac1).toEqual(mac2);
  273. });
  274. it('generate a MAC, using a specified cryptographic policy', function() {
  275. var policy = cryptoPolicy.newInstance();
  276. policy.symmetric.mac.hashAlgorithm =
  277. cryptoPolicy.options.symmetric.mac.hashAlgorithm.SHA512_224;
  278. var service = symmetric.newService({policy: policy});
  279. var key = service.newKeyGenerator().nextMacKey();
  280. var MacHandler = service.newMacHandler().init(key);
  281. var mac = MacHandler.generate(data_);
  282. var verified = MacHandler.verify(mac, data_);
  283. expect(verified).toBeTruthy();
  284. expect(mac.length).toBe(28);
  285. });
  286. it('generate a MAC, using a specified secure random service object',
  287. function() {
  288. var secureRandomService = secureRandom.newService();
  289. var service =
  290. symmetric.newService({secureRandomService: secureRandomService});
  291. var key = service.newKeyGenerator().nextMacKey();
  292. var MacHandler = service.newMacHandler().init(key);
  293. var mac = MacHandler.generate(data_);
  294. var verified = MacHandler.verify(mac, data_);
  295. expect(verified).toBeTruthy();
  296. });
  297. it('throw an error when updating a MAC before the MAC handler has been initialized with a secret key',
  298. function() {
  299. var macHandler = service_.newMacHandler();
  300. expect(function() {
  301. macHandler.update(data_);
  302. }).toThrow();
  303. });
  304. it('throw an error when generating a MAC before the MAC handler has been initialized with a secret key',
  305. function() {
  306. var macHandler = service_.newMacHandler();
  307. expect(function() {
  308. macHandler.generate(data_);
  309. }).toThrow();
  310. });
  311. it('throw an error when verifying a MAC before the MAC handler has been initialized with a secret key',
  312. function() {
  313. var macHandler = service_.newMacHandler();
  314. expect(function() {
  315. macHandler.verify(mac_, data_);
  316. }).toThrow();
  317. });
  318. it('throw an error when generating a MAC without either providing data or having previously updated the MAC handler with some data',
  319. function() {
  320. var macHandler = service_.newMacHandler().init(macKey_);
  321. expect(function() {
  322. macHandler.generate();
  323. }).toThrow();
  324. });
  325. it('throw an error when verifynig a MAC without either providing data or having previously updated the MAC handler with some data',
  326. function() {
  327. var macHandler = service_.newMacHandler().init(macKey_);
  328. expect(function() {
  329. macHandler.verify(mac_);
  330. }).toThrow();
  331. });
  332. });
  333. it('throw an error when initializing a cipher, using an algorithm that is not supported by the cryptographic policy',
  334. function() {
  335. var policy = cryptoPolicy.newInstance();
  336. policy.symmetric.secretKey.encryption.lengthBytes =
  337. cryptoPolicy.options.symmetric.secretKey.encryption.lengthBytes
  338. .KL_16;
  339. policy.symmetric.cipher.algorithm.name = 'Wrong algorithm';
  340. var service = symmetric.newService({policy: policy});
  341. var key = service.newKeyGenerator().nextEncryptionKey();
  342. var cipher = service.newCipher();
  343. expect(function() {
  344. cipher.init(key);
  345. }).toThrow();
  346. });
  347. });
  348. });