2
0

index-validation.spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 asymmetric = require('../lib/index');
  11. var codec = require('scytl-codec');
  12. var forge = require('node-forge');
  13. var BigInteger = forge.jsbn.BigInteger;
  14. describe('The asymmetric cryptography module should be able to ...', function() {
  15. var DATA = 'anystring';
  16. var PUBLIC_KEY_PEM =
  17. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVF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LPjQzWejZGbo4+KqgzWb9fECDYVtDPlJ/+yLih9nt67BHweJKxYydl18rVigdVyy22X86NijSykCgYEAqKPUrXZAo+TJvmTw4tgsibJgvXBYBhmsej8mGNQw+Nyp2gV28sgm61ifIeXKS8teq+MFwGA6cHQedbsCqhMHokdhESZmlbWxhSFLihQcewBxwvrBwbaxI23yXRzwMewznZFL032PpcbqrmwFmcSSEZ3nmbvTH6ShqLW+pzDNp6MCgYBQLzdgxJ7qedqSa/JohTMG4e7rh9d2rpPJE7J7ewPZF8pOpx+qO+Gqn2COdJ+Ts2vUcAETKn9nEaPIZc/wnmQY9dioxbhWo0FPGaaphBPtq9Ez/XUv4zoFppk5V1X/isdUPsmvttf00oeIBiqrXbwmv+yz5JRn2Z7TTXjz9Ev+OQKBgQCUuoCMRzl1EgcXIqEL/0kwW6BUEqufHa9u1Ri9Vw6lvL8T6DPipMEmWK9nzuid9gtVns/ovTVtDgv7GuabplLaPQePf4WDzY11c0rSyS/hDyBFrK+LL5uEOqhAlJAGB2HyOj1clWVF+GvrTpuV5LZKUS/79pmZU7G7QCaX/0Ow7wKBgC/kDH7cmWQnWvvJ5izrx/7PogQVPOLELeUIGLu/hjsSdDKiFCxCUZ948+9NuG+DnpXDWzw//r8mPBRRGGsqFws5Aipp7yjQ3kRDCCzGelPCVhHyfmKqA+8ewXPulKS3/wIyHIvaXmsuAtTfurHtpRyzjKmCBK1Y6WQ3trIXvo7s-----END RSA PRIVATE KEY-----';
  20. var data_;
  21. var service_;
  22. var signer_;
  23. var sigVerifier_;
  24. var encrypter_;
  25. var decrypter_;
  26. var signature_;
  27. var encryptedData_;
  28. var keyArg_;
  29. var nonObject_;
  30. var emptyObject_;
  31. var nonString_;
  32. var emptyString_;
  33. var nonUint8Array_;
  34. var nonBigInteger_;
  35. var nonPositiveBigInteger_;
  36. beforeAll(function() {
  37. data_ = codec.utf8Encode(DATA);
  38. service_ = asymmetric.newService();
  39. signer_ = service_.newSigner();
  40. sigVerifier_ = service_.newSignatureVerifier();
  41. encrypter_ = service_.newEncrypter();
  42. decrypter_ = service_.newDecrypter();
  43. keyArg_ = BigInteger.ONE;
  44. nonObject_ = 999;
  45. emptyObject_ = {};
  46. nonString_ = 999;
  47. emptyString_ = '';
  48. nonUint8Array_ = [];
  49. nonBigInteger_ = asymmetric.newService();
  50. nonPositiveBigInteger_ = BigInteger.ZERO;
  51. });
  52. beforeEach(function() {
  53. signer_.init(PRIVATE_KEY_PEM);
  54. sigVerifier_.init(PUBLIC_KEY_PEM);
  55. encrypter_.init(PUBLIC_KEY_PEM);
  56. decrypter_.init(PRIVATE_KEY_PEM);
  57. encryptedData_ = encrypter_.encrypt(data_);
  58. signature_ = signer_.sign(data_);
  59. });
  60. describe('create an asymmetric cryptography service that should be able to ..', function() {
  61. it('throw an error when being created, using invalid cryptographic policy',
  62. function() {
  63. expect(function() {
  64. Object.create(asymmetric.newService({policy: null}));
  65. }).toThrow();
  66. expect(function() {
  67. Object.create(asymmetric.newService({policy: nonObject_}));
  68. }).toThrow();
  69. expect(function() {
  70. Object.create(asymmetric.newService({policy: emptyObject_}));
  71. }).toThrow();
  72. });
  73. it('throw an error when being created, using invalid secure random service object',
  74. function() {
  75. expect(function() {
  76. Object.create(asymmetric.newService({secureRandomService: null}));
  77. }).toThrow();
  78. expect(function() {
  79. Object.create(
  80. asymmetric.newService({secureRandomService: nonObject_}));
  81. }).toThrow();
  82. expect(function() {
  83. Object.create(
  84. asymmetric.newService({secureRandomService: emptyObject_}));
  85. }).toThrow();
  86. });
  87. it('throw an error when creating a new RsaPublicKey object, using invalid input data',
  88. function() {
  89. expect(function() {
  90. service_.newRsaPublicKey();
  91. }).toThrow();
  92. expect(function() {
  93. service_.newRsaPublicKey(undefined);
  94. }).toThrow();
  95. expect(function() {
  96. service_.newRsaPublicKey(null);
  97. }).toThrow();
  98. expect(function() {
  99. service_.newRsaPublicKey(nonObject_);
  100. }).toThrow();
  101. expect(function() {
  102. service_.newRsaPublicKey(emptyObject_);
  103. }).toThrow();
  104. expect(function() {
  105. service_.newRsaPublicKey({n: undefined});
  106. }).toThrow();
  107. expect(function() {
  108. service_.newRsaPublicKey({n: null});
  109. }).toThrow();
  110. expect(function() {
  111. service_.newRsaPublicKey({n: nonBigInteger_});
  112. }).toThrow();
  113. expect(function() {
  114. service_.newRsaPublicKey({n: nonPositiveBigInteger_});
  115. }).toThrow();
  116. expect(function() {
  117. service_.newRsaPublicKey({e: undefined});
  118. }).toThrow();
  119. expect(function() {
  120. service_.newRsaPublicKey({e: null});
  121. }).toThrow();
  122. expect(function() {
  123. service_.newRsaPublicKey({e: nonBigInteger_});
  124. }).toThrow();
  125. expect(function() {
  126. service_.newRsaPublicKey(keyArg_, {e: nonPositiveBigInteger_});
  127. }).toThrow();
  128. });
  129. it('throw an error when creating a new RsaPrivateKey object, using an invalid params object, PEM string, modulus, public exponent or private exponent',
  130. function() {
  131. expect(function() {
  132. service_.newRsaPrivateKey();
  133. }).toThrow();
  134. expect(function() {
  135. service_.newRsaPrivateKey(undefined);
  136. }).toThrow();
  137. expect(function() {
  138. service_.newRsaPrivateKey(null);
  139. }).toThrow();
  140. expect(function() {
  141. service_.newRsaPrivateKey(nonObject_);
  142. }).toThrow();
  143. expect(function() {
  144. service_.newRsaPrivateKey(emptyObject_);
  145. }).toThrow();
  146. expect(function() {
  147. service_.newRsaPrivateKey({pem: emptyString_});
  148. }).toThrow();
  149. expect(function() {
  150. service_.newRsaPrivateKey({n: undefined});
  151. }).toThrow();
  152. expect(function() {
  153. service_.newRsaPrivateKey({n: null});
  154. }).toThrow();
  155. expect(function() {
  156. service_.newRsaPrivateKey({n: nonBigInteger_});
  157. }).toThrow();
  158. expect(function() {
  159. service_.newRsaPrivateKey({n: nonPositiveBigInteger_});
  160. }).toThrow();
  161. expect(function() {
  162. service_.newRsaPrivateKey({e: undefined});
  163. }).toThrow();
  164. expect(function() {
  165. service_.newRsaPrivateKey({e: null});
  166. }).toThrow();
  167. expect(function() {
  168. service_.newRsaPrivateKey({e: nonBigInteger_});
  169. }).toThrow();
  170. expect(function() {
  171. service_.newRsaPrivateKey({e: nonPositiveBigInteger_});
  172. }).toThrow();
  173. expect(function() {
  174. service_.newRsaPrivateKey({d: undefined});
  175. }).toThrow();
  176. expect(function() {
  177. service_.newRsaPrivateKey({d: null});
  178. }).toThrow();
  179. expect(function() {
  180. service_.newRsaPrivateKey({d: nonBigInteger_});
  181. }).toThrow();
  182. expect(function() {
  183. service_.newRsaPrivateKey({d: nonPositiveBigInteger_});
  184. }).toThrow();
  185. });
  186. it('throw an error when creating a new RsaPrivateKey object, using an invalid first or second prime',
  187. function() {
  188. expect(function() {
  189. service_.newRsaPrivateKey({p: undefined});
  190. }).toThrow();
  191. expect(function() {
  192. service_.newRsaPrivateKey({p: null});
  193. }).toThrow();
  194. expect(function() {
  195. service_.newRsaPrivateKey({p: nonBigInteger_});
  196. }).toThrow();
  197. expect(function() {
  198. service_.newRsaPrivateKey({p: nonPositiveBigInteger_});
  199. }).toThrow();
  200. expect(function() {
  201. service_.newRsaPrivateKey({q: undefined});
  202. }).toThrow();
  203. expect(function() {
  204. service_.newRsaPrivateKey({q: null});
  205. }).toThrow();
  206. expect(function() {
  207. service_.newRsaPrivateKey({q: nonBigInteger_});
  208. }).toThrow();
  209. expect(function() {
  210. service_.newRsaPrivateKey({q: nonPositiveBigInteger_});
  211. }).toThrow();
  212. });
  213. it('throw an error when creating a new RsaPrivateKey object, using an invalid first exponent, second exponent or coefficient',
  214. function() {
  215. expect(function() {
  216. service_.newRsaPrivateKey({dP: undefined});
  217. }).toThrow();
  218. expect(function() {
  219. service_.newRsaPrivateKey({dP: null});
  220. }).toThrow();
  221. expect(function() {
  222. service_.newRsaPrivateKey({dP: nonBigInteger_});
  223. }).toThrow();
  224. expect(function() {
  225. service_.newRsaPrivateKey({dP: nonPositiveBigInteger_});
  226. }).toThrow();
  227. expect(function() {
  228. service_.newRsaPrivateKey({dQ: undefined});
  229. }).toThrow();
  230. expect(function() {
  231. service_.newRsaPrivateKey({dQ: null});
  232. }).toThrow();
  233. expect(function() {
  234. service_.newRsaPrivateKey({dQ: nonBigInteger_});
  235. }).toThrow();
  236. expect(function() {
  237. service_.newRsaPrivateKey({dQ: nonPositiveBigInteger_});
  238. }).toThrow();
  239. expect(function() {
  240. service_.newRsaPrivateKey({qInv: undefined});
  241. }).toThrow();
  242. expect(function() {
  243. service_.newRsaPrivateKey({qInv: null});
  244. }).toThrow();
  245. expect(function() {
  246. service_.newRsaPrivateKey({qInv: nonBigInteger_});
  247. }).toThrow();
  248. expect(function() {
  249. service_.newRsaPrivateKey({qInv: nonPositiveBigInteger_});
  250. }).toThrow();
  251. });
  252. it('throw an error when creating a new KeyPair object, using invalid input data',
  253. function() {
  254. expect(function() {
  255. service_.newKeyPair();
  256. }).toThrow();
  257. expect(function() {
  258. service_.newKeyPair(undefined, PRIVATE_KEY_PEM);
  259. }).toThrow();
  260. expect(function() {
  261. service_.newKeyPair(null, PRIVATE_KEY_PEM);
  262. }).toThrow();
  263. expect(function() {
  264. service_.newKeyPair(nonString_, PRIVATE_KEY_PEM);
  265. }).toThrow();
  266. expect(function() {
  267. service_.newKeyPair(emptyString_, PRIVATE_KEY_PEM);
  268. }).toThrow();
  269. expect(function() {
  270. service_.newKeyPair(PUBLIC_KEY_PEM);
  271. }).toThrow();
  272. expect(function() {
  273. service_.newKeyPair(PUBLIC_KEY_PEM, undefined);
  274. }).toThrow();
  275. expect(function() {
  276. service_.newKeyPair(PUBLIC_KEY_PEM, null);
  277. }).toThrow();
  278. expect(function() {
  279. service_.newKeyPair(PUBLIC_KEY_PEM, nonString_);
  280. }).toThrow();
  281. expect(function() {
  282. service_.newKeyPair(PUBLIC_KEY_PEM, emptyString_);
  283. }).toThrow();
  284. });
  285. describe('create a signer that should be able to ...', function() {
  286. it('throw an error when being initialized, using invalid input data',
  287. function() {
  288. expect(function() {
  289. signer_.init();
  290. }).toThrow();
  291. expect(function() {
  292. signer_.init(undefined);
  293. }).toThrow();
  294. expect(function() {
  295. signer_.init(null);
  296. }).toThrow();
  297. expect(function() {
  298. signer_.init(nonString_);
  299. }).toThrow();
  300. expect(function() {
  301. signer_.init(emptyString_);
  302. }).toThrow();
  303. });
  304. it('throw an error when being updated, using invalid input data',
  305. function() {
  306. expect(function() {
  307. signer_.update();
  308. }).toThrow();
  309. expect(function() {
  310. signer_.update(undefined);
  311. }).toThrow();
  312. expect(function() {
  313. signer_.update(null);
  314. }).toThrow();
  315. expect(function() {
  316. signer_.update(nonUint8Array_);
  317. }).toThrow();
  318. });
  319. it('throw an error when signing, using invalid input data', function() {
  320. expect(function() {
  321. signer_.sign(null);
  322. }).toThrow();
  323. expect(function() {
  324. signer_.sign(nonUint8Array_);
  325. }).toThrow();
  326. });
  327. });
  328. describe('create a signature verifier that should be able to ...', function() {
  329. it('throw an error when being initialized, using invalid input data',
  330. function() {
  331. expect(function() {
  332. sigVerifier_.init();
  333. }).toThrow();
  334. expect(function() {
  335. sigVerifier_.init(undefined);
  336. }).toThrow();
  337. expect(function() {
  338. sigVerifier_.init(null);
  339. }).toThrow();
  340. expect(function() {
  341. sigVerifier_.init(nonString_);
  342. }).toThrow();
  343. expect(function() {
  344. sigVerifier_.init(emptyString_);
  345. }).toThrow();
  346. });
  347. it('throw an error when being updated, using invalid input data',
  348. function() {
  349. expect(function() {
  350. sigVerifier_.update();
  351. }).toThrow();
  352. expect(function() {
  353. sigVerifier_.update(undefined);
  354. }).toThrow();
  355. expect(function() {
  356. sigVerifier_.update(null);
  357. }).toThrow();
  358. expect(function() {
  359. sigVerifier_.update(nonUint8Array_);
  360. }).toThrow();
  361. });
  362. it('throw an error when verifying a signature, using invalid signature',
  363. function() {
  364. expect(function() {
  365. sigVerifier_.verify(undefined, data_);
  366. }).toThrow();
  367. expect(function() {
  368. sigVerifier_.verify(null, data_);
  369. }).toThrow();
  370. expect(function() {
  371. sigVerifier_.verify(nonUint8Array_, data_);
  372. }).toThrow();
  373. });
  374. it('throw an error when verifying a signature, using invalid data that was signed',
  375. function() {
  376. expect(function() {
  377. sigVerifier_.verify(signature_, null);
  378. }).toThrow();
  379. expect(function() {
  380. sigVerifier_.verify(signature_, nonUint8Array_);
  381. }).toThrow();
  382. });
  383. });
  384. describe('create an encrypter that should be able to ...', function() {
  385. it('throw an error when being initialized, using invalid input data',
  386. function() {
  387. expect(function() {
  388. encrypter_.init();
  389. }).toThrow();
  390. expect(function() {
  391. encrypter_.init(undefined);
  392. }).toThrow();
  393. expect(function() {
  394. encrypter_.init(null);
  395. }).toThrow();
  396. expect(function() {
  397. encrypter_.init(nonString_);
  398. }).toThrow();
  399. expect(function() {
  400. encrypter_.init(emptyString_);
  401. }).toThrow();
  402. });
  403. it('throw an error when encrypting, using invalid input data',
  404. function() {
  405. expect(function() {
  406. encrypter_.encrypt();
  407. }).toThrow();
  408. expect(function() {
  409. encrypter_.encrypt(undefined);
  410. }).toThrow();
  411. expect(function() {
  412. encrypter_.encrypt(null);
  413. }).toThrow();
  414. expect(function() {
  415. encrypter_.encrypt(nonUint8Array_);
  416. }).toThrow();
  417. });
  418. });
  419. describe('create a decrypter that should be able to ...', function() {
  420. it('throw an error when initializing, using invalid input data',
  421. function() {
  422. expect(function() {
  423. decrypter_.init();
  424. }).toThrow();
  425. expect(function() {
  426. decrypter_.init(undefined);
  427. }).toThrow();
  428. expect(function() {
  429. decrypter_.init(null);
  430. }).toThrow();
  431. expect(function() {
  432. decrypter_.init(nonString_);
  433. }).toThrow();
  434. expect(function() {
  435. decrypter_.init(emptyString_);
  436. }).toThrow();
  437. });
  438. it('throw an error when decrypting, using invalid input data',
  439. function() {
  440. expect(function() {
  441. decrypter_.decrypt();
  442. }).toThrow();
  443. expect(function() {
  444. decrypter_.decrypt(undefined);
  445. }).toThrow();
  446. expect(function() {
  447. decrypter_.decrypt(null);
  448. }).toThrow();
  449. expect(function() {
  450. decrypter_.decrypt(nonUint8Array_);
  451. }).toThrow();
  452. });
  453. });
  454. });
  455. });