2
0

index-validation.spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 CommonTestData = require('./data/common-data');
  11. var ValidationTestData = require('./data/validation-data');
  12. var elGamal = require('../lib/index');
  13. var secureRandom = require('scytl-securerandom');
  14. describe('The ElGamal cryptography module should be able to ...', function() {
  15. var elGamalService_;
  16. var secureRandomGenerator_;
  17. var group_;
  18. var publicKey_;
  19. var privateKey_;
  20. var publicKeyElements_;
  21. var privateKeyExponents_;
  22. var elements_;
  23. var elementValueStrings_;
  24. var nonObject_;
  25. var emptyObject_;
  26. var nonSecureRandomService_;
  27. var nonSecureRandomGenerator_;
  28. var nonMathematicalService_;
  29. var nonBoolean_;
  30. var nonString_;
  31. var nonJsonString_;
  32. var nonArray_;
  33. var nonStringArray_;
  34. var nonMathObject_;
  35. var nonMathObjectArray_;
  36. var nonElGamalObject_;
  37. var elementsFromAnotherGroup_;
  38. var publicKeyFromAnotherGroup_;
  39. var elGamalEncrypter_;
  40. var elGamalDecrypter_;
  41. var elGamalPrngDecrypter_;
  42. var encryptedElements_;
  43. var gamma_;
  44. var phis_;
  45. var secret_;
  46. var encryptedElementsFromAnotherGroup_;
  47. beforeAll(function() {
  48. elGamalService_ = elGamal.newService();
  49. secureRandomGenerator_ = secureRandom.newService().newRandomGenerator();
  50. var commonTestData = new CommonTestData();
  51. group_ = commonTestData.getGroup();
  52. publicKey_ = commonTestData.getPublicKey();
  53. privateKey_ = commonTestData.getPrivateKey();
  54. publicKeyElements_ = commonTestData.getPublicKeyElements();
  55. privateKeyExponents_ = commonTestData.getPrivateKeyExponents();
  56. elements_ = commonTestData.getZpGroupElements();
  57. elementValueStrings_ = commonTestData.getZpGroupElementValueStrings();
  58. var validationTestData = new ValidationTestData();
  59. nonObject_ = validationTestData.getNonObject();
  60. emptyObject_ = validationTestData.getEmptyObject();
  61. nonSecureRandomService_ = validationTestData.getNonSecureRandomService();
  62. nonSecureRandomGenerator_ =
  63. validationTestData.getNonSecureRandomGenerator();
  64. nonMathematicalService_ = validationTestData.getNonMathematicalService();
  65. nonBoolean_ = validationTestData.getNonBoolean();
  66. nonString_ = validationTestData.getNonString();
  67. nonJsonString_ = validationTestData.getNonJsonString();
  68. nonArray_ = validationTestData.getNonArray();
  69. nonStringArray_ = validationTestData.getNonStringArray();
  70. nonMathObject_ = validationTestData.getNonMathObject();
  71. nonMathObjectArray_ = validationTestData.getNonMathObjectArray();
  72. nonElGamalObject_ = validationTestData.getNonElGamalObject();
  73. elementsFromAnotherGroup_ =
  74. validationTestData.getElementsFromAnotherGroup();
  75. publicKeyFromAnotherGroup_ =
  76. validationTestData.getPublicKeyFromAnotherGroup();
  77. });
  78. beforeEach(function() {
  79. elGamalEncrypter_ = elGamalService_.newEncrypter().init(publicKey_);
  80. elGamalDecrypter_ = elGamalService_.newDecrypter().init(privateKey_);
  81. elGamalPrngDecrypter_ = elGamalService_.newPrngDecrypter().init(
  82. publicKey_, secureRandomGenerator_);
  83. var encryptedElementsAndSecret =
  84. elGamalEncrypter_.encrypt(elements_, {saveSecret: true});
  85. gamma_ = encryptedElementsAndSecret.gamma;
  86. phis_ = encryptedElementsAndSecret.phis;
  87. secret_ = encryptedElementsAndSecret.secret;
  88. encryptedElements_ = elGamalService_.newEncryptedElements(
  89. encryptedElementsAndSecret.gamma, encryptedElementsAndSecret.phis);
  90. encryptedElementsFromAnotherGroup_ =
  91. elGamalService_.newEncrypter()
  92. .init(publicKeyFromAnotherGroup_)
  93. .encrypt(elementsFromAnotherGroup_);
  94. });
  95. describe('create an ElGamal cryptography service that should be able to ..', function() {
  96. it('throw an error when being created, using an invalid secure random service object',
  97. function() {
  98. expect(function() {
  99. Object.create(elGamal.newService({secureRandomService: null}));
  100. }).toThrow();
  101. expect(function() {
  102. Object.create(elGamal.newService({secureRandomService: nonObject_}));
  103. }).toThrow();
  104. expect(function() {
  105. Object.create(
  106. elGamal.newService({secureRandomService: emptyObject_}));
  107. }).toThrow();
  108. });
  109. it('throw an error when being created, using an invalid mathematical service object',
  110. function() {
  111. expect(function() {
  112. Object.create(elGamal.newService({mathematicalService: null}));
  113. }).toThrow();
  114. expect(function() {
  115. Object.create(elGamal.newService({mathematicalService: nonObject_}));
  116. }).toThrow();
  117. expect(function() {
  118. Object.create(
  119. elGamal.newService({mathematicalService: emptyObject_}));
  120. }).toThrow();
  121. });
  122. it('throw an error when creating a new ElGamalPublicKey object, using invalid input data',
  123. function() {
  124. expect(function() {
  125. elGamalService_.newPublicKey(group_);
  126. }).toThrow();
  127. expect(function() {
  128. elGamalService_.newPublicKey(undefined, publicKeyElements_);
  129. }).toThrow();
  130. expect(function() {
  131. elGamalService_.newPublicKey(null, publicKeyElements_);
  132. }).toThrow();
  133. expect(function() {
  134. elGamalService_.newPublicKey(nonMathObject_, publicKeyElements_);
  135. }).toThrow();
  136. expect(function() {
  137. elGamalService_.newPublicKey(group_, undefined);
  138. }).toThrow();
  139. expect(function() {
  140. elGamalService_.newPublicKey(group_, null);
  141. }).toThrow();
  142. expect(function() {
  143. elGamalService_.newPublicKey(group_, nonArray_);
  144. }).toThrow();
  145. expect(function() {
  146. elGamalService_.newPublicKey(group_, nonMathObjectArray_);
  147. }).toThrow();
  148. expect(function() {
  149. elGamalService_.newPublicKey(nonJsonString_);
  150. }).toThrow();
  151. });
  152. it('throw an error when creating a new ElGamalPrivateKey object, using invalid input data',
  153. function() {
  154. expect(function() {
  155. elGamalService_.newPrivateKey(undefined, privateKeyExponents_);
  156. }).toThrow();
  157. expect(function() {
  158. elGamalService_.newPrivateKey(null, privateKeyExponents_);
  159. }).toThrow();
  160. expect(function() {
  161. elGamalService_.newPrivateKey(nonObject_, privateKeyExponents_);
  162. }).toThrow();
  163. expect(function() {
  164. elGamalService_.newPrivateKey(emptyObject_, privateKeyExponents_);
  165. }).toThrow();
  166. expect(function() {
  167. elGamalService_.newPrivateKey(group_);
  168. }).toThrow();
  169. expect(function() {
  170. elGamalService_.newPrivateKey(group_, undefined);
  171. }).toThrow();
  172. expect(function() {
  173. elGamalService_.newPrivateKey(group_, null);
  174. }).toThrow();
  175. expect(function() {
  176. elGamalService_.newPrivateKey(group_, nonArray_);
  177. }).toThrow();
  178. expect(function() {
  179. elGamalService_.newPrivateKey(group_, nonMathObjectArray_);
  180. }).toThrow();
  181. expect(function() {
  182. elGamalService_.newPrivateKey(nonJsonString_);
  183. }).toThrow();
  184. });
  185. it('throw an error when creating a new ElGamalKeyPair object, using invalid input data',
  186. function() {
  187. expect(function() {
  188. elGamalService_.newKeyPair(publicKey_);
  189. }).toThrow();
  190. expect(function() {
  191. elGamalService_.newKeyPair(undefined, privateKey_);
  192. }).toThrow();
  193. expect(function() {
  194. elGamalService_.newKeyPair(null, privateKey_);
  195. }).toThrow();
  196. expect(function() {
  197. elGamalService_.newKeyPair(nonElGamalObject_, privateKey_);
  198. }).toThrow();
  199. expect(function() {
  200. elGamalService_.newKeyPair(publicKey_, undefined);
  201. }).toThrow();
  202. expect(function() {
  203. elGamalService_.newKeyPair(publicKey_, null);
  204. }).toThrow();
  205. expect(function() {
  206. elGamalService_.newKeyPair(publicKey_, nonElGamalObject_);
  207. }).toThrow();
  208. });
  209. it('throw an error when creating a new ElGamalEncryptedElements object, using invalid input data',
  210. function() {
  211. expect(function() {
  212. elGamalService_.newEncryptedElements(undefined, phis_);
  213. }).toThrow();
  214. expect(function() {
  215. elGamalService_.newEncryptedElements(null, phis_);
  216. }).toThrow();
  217. expect(function() {
  218. elGamalService_.newEncryptedElements(nonObject_, phis_);
  219. }).toThrow();
  220. expect(function() {
  221. elGamalService_.newEncryptedElements(emptyObject_, phis_);
  222. }).toThrow();
  223. expect(function() {
  224. elGamalService_.newEncryptedElements(gamma_);
  225. }).toThrow();
  226. expect(function() {
  227. elGamalService_.newEncryptedElements(gamma_, undefined);
  228. }).toThrow();
  229. expect(function() {
  230. elGamalService_.newEncryptedElements(gamma_, null);
  231. }).toThrow();
  232. expect(function() {
  233. elGamalService_.newEncryptedElements(gamma_, nonArray_);
  234. }).toThrow();
  235. expect(function() {
  236. elGamalService_.newEncryptedElements(gamma_, nonMathObjectArray_);
  237. }).toThrow();
  238. expect(function() {
  239. elGamalService_.newEncryptedElements(gamma_, phis_, null);
  240. }).toThrow();
  241. expect(function() {
  242. elGamalService_.newEncryptedElements(gamma_, phis_, nonObject_);
  243. }).toThrow();
  244. expect(function() {
  245. elGamalService_.newEncryptedElements(gamma_, phis_, emptyObject_);
  246. }).toThrow();
  247. expect(function() {
  248. elGamalService_.newEncryptedElements(nonJsonString_);
  249. }).toThrow();
  250. });
  251. describe('Create an encrypter/decrypter pair that should ..', function() {
  252. it('throw an error when being initialized, using invalid input data',
  253. function() {
  254. expect(function() {
  255. elGamalEncrypter_.init();
  256. }).toThrow();
  257. expect(function() {
  258. elGamalEncrypter_.init(undefined);
  259. }).toThrow();
  260. expect(function() {
  261. elGamalEncrypter_.init(null);
  262. }).toThrow();
  263. expect(function() {
  264. elGamalEncrypter_.init(nonElGamalObject_);
  265. }).toThrow();
  266. expect(function() {
  267. elGamalDecrypter_.init();
  268. }).toThrow();
  269. expect(function() {
  270. elGamalDecrypter_.init(undefined);
  271. }).toThrow();
  272. expect(function() {
  273. elGamalDecrypter_.init(null);
  274. }).toThrow();
  275. expect(function() {
  276. elGamalDecrypter_.init(nonElGamalObject_);
  277. }).toThrow();
  278. });
  279. it('throw an error when encrypting an array of Zp group elements, using invalid input data',
  280. function() {
  281. expect(function() {
  282. elGamalEncrypter_.encrypt();
  283. }).toThrow();
  284. expect(function() {
  285. elGamalEncrypter_.encrypt(undefined);
  286. }).toThrow();
  287. expect(function() {
  288. elGamalEncrypter_.encrypt(null);
  289. }).toThrow();
  290. expect(function() {
  291. elGamalEncrypter_.encrypt(nonArray_);
  292. }).toThrow();
  293. expect(function() {
  294. elGamalEncrypter_.encrypt(nonMathObjectArray_);
  295. }).toThrow();
  296. expect(function() {
  297. elGamalEncrypter_.encrypt(
  298. elements_, {useShortExponent: nonBoolean_});
  299. }).toThrow();
  300. expect(function() {
  301. elGamalEncrypter_.encrypt(elements_, {preComputation: null});
  302. }).toThrow();
  303. expect(function() {
  304. elGamalEncrypter_.encrypt(elements_, {preComputation: nonObject_});
  305. }).toThrow();
  306. expect(function() {
  307. elGamalEncrypter_.encrypt(
  308. elements_, {preComputation: emptyObject_});
  309. }).toThrow();
  310. expect(function() {
  311. elGamalEncrypter_.encrypt(
  312. elements_, {preComputation: nonElGamalObject_});
  313. }).toThrow();
  314. expect(function() {
  315. elGamalEncrypter_.encrypt(elementsFromAnotherGroup_);
  316. }).toThrow();
  317. });
  318. it('throw an error when encrypting an array of Zp group element strings, using invalid input data',
  319. function() {
  320. expect(function() {
  321. elGamalEncrypter_.encrypt();
  322. }).toThrow();
  323. expect(function() {
  324. elGamalEncrypter_.encrypt(undefined);
  325. }).toThrow();
  326. expect(function() {
  327. elGamalEncrypter_.encrypt(null);
  328. }).toThrow();
  329. expect(function() {
  330. elGamalEncrypter_.encrypt(nonArray_);
  331. }).toThrow();
  332. expect(function() {
  333. elGamalEncrypter_.encrypt(nonStringArray_);
  334. }).toThrow();
  335. expect(function() {
  336. elGamalEncrypter_.encrypt(elements_, {preComputation: null});
  337. }).toThrow();
  338. expect(function() {
  339. elGamalEncrypter_.encrypt(elements_, {preComputation: nonObject_});
  340. }).toThrow();
  341. expect(function() {
  342. elGamalEncrypter_.encrypt(
  343. elements_, {preComputation: emptyObject_});
  344. }).toThrow();
  345. expect(function() {
  346. elGamalEncrypter_.encrypt(
  347. elements_, {preComputation: nonElGamalObject_});
  348. }).toThrow();
  349. expect(function() {
  350. elGamalEncrypter_.encrypt(
  351. elements_, {useShortExponent: nonBoolean_});
  352. }).toThrow();
  353. expect(function() {
  354. elGamalEncrypter_.encrypt(elements_, {saveSecret: nonBoolean_});
  355. }).toThrow();
  356. });
  357. it('throw an error when pre-computing an ElGamal encryption, using invalid input data',
  358. function() {
  359. expect(function() {
  360. elGamalEncrypter_.preCompute({useShortExponent: nonBoolean_});
  361. }).toThrow();
  362. expect(function() {
  363. elGamalEncrypter_.preCompute({saveSecret: nonBoolean_});
  364. }).toThrow();
  365. });
  366. it('throw an error when decrypting an ElGamalEncryptedElements object, using invalid input data',
  367. function() {
  368. expect(function() {
  369. elGamalDecrypter_.decrypt();
  370. }).toThrow();
  371. expect(function() {
  372. elGamalDecrypter_.decrypt(undefined);
  373. }).toThrow();
  374. expect(function() {
  375. elGamalDecrypter_.decrypt(null);
  376. }).toThrow();
  377. expect(function() {
  378. elGamalDecrypter_.decrypt(nonElGamalObject_);
  379. }).toThrow();
  380. expect(function() {
  381. elGamalDecrypter_.decrypt(
  382. encryptedElements_, {confirmMembership: null});
  383. }).toThrow();
  384. expect(function() {
  385. elGamalDecrypter_.decrypt(
  386. encryptedElements_, {confirmMembership: nonBoolean_});
  387. }).toThrow();
  388. expect(function() {
  389. elGamalDecrypter_.decrypt(encryptedElementsFromAnotherGroup_);
  390. }).toThrow();
  391. });
  392. });
  393. describe('Create a PRNG decrypter that should ..', function() {
  394. it('throw an error when being initialized, using invalid input data',
  395. function() {
  396. expect(function() {
  397. elGamalPrngDecrypter_.init();
  398. }).toThrow();
  399. expect(function() {
  400. elGamalPrngDecrypter_.init(undefined);
  401. }).toThrow();
  402. expect(function() {
  403. elGamalPrngDecrypter_.init(null);
  404. }).toThrow();
  405. expect(function() {
  406. elGamalPrngDecrypter_.init(nonElGamalObject_);
  407. }).toThrow();
  408. expect(function() {
  409. elGamalPrngDecrypter_.init(publicKey_);
  410. }).toThrow();
  411. expect(function() {
  412. elGamalPrngDecrypter_.init(publicKey_, undefined);
  413. }).toThrow();
  414. expect(function() {
  415. elGamalPrngDecrypter_.init(publicKey_, null);
  416. }).toThrow();
  417. expect(function() {
  418. elGamalPrngDecrypter_.init(publicKey_, nonObject_);
  419. }).toThrow();
  420. expect(function() {
  421. elGamalPrngDecrypter_.init(publicKey_, emptyObject_);
  422. }).toThrow();
  423. });
  424. it('throw an error when decrypting an ElGamalEncryptedElements object, using invalid input data',
  425. function() {
  426. expect(function() {
  427. elGamalPrngDecrypter_.decrypt();
  428. }).toThrow();
  429. expect(function() {
  430. elGamalPrngDecrypter_.decrypt(undefined);
  431. }).toThrow();
  432. expect(function() {
  433. elGamalPrngDecrypter_.decrypt(null);
  434. }).toThrow();
  435. expect(function() {
  436. elGamalPrngDecrypter_.decrypt(nonElGamalObject_);
  437. }).toThrow();
  438. expect(function() {
  439. elGamalPrngDecrypter_.decrypt(
  440. encryptedElements_, {confirmMembership: null});
  441. }).toThrow();
  442. expect(function() {
  443. elGamalPrngDecrypter_.decrypt(
  444. encryptedElements_, {confirmMembership: nonBoolean_});
  445. }).toThrow();
  446. expect(function() {
  447. elGamalPrngDecrypter_.decrypt(encryptedElementsFromAnotherGroup_);
  448. }).toThrow();
  449. });
  450. });
  451. });
  452. });