2
0

asymmetric.signer.spec.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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('Signers: ', function() {
  9. 'use strict';
  10. describe(' from the Forge provider ..', function() {
  11. describe('should be able to ..', function() {
  12. var _plaintext = 'Ox2fUJq1gAbX';
  13. var _privateKeyPem =
  14. '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoC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ow7wKBgC/kDH7cmWQnWvvJ5izrx/7PogQVPOLELeUIGLu/hjsSdDKiFCxCUZ948+9NuG+DnpXDWzw//r8mPBRRGGsqFws5Aipp7yjQ3kRDCCzGelPCVhHyfmKqA+8ewXPulKS3/wIyHIvaXmsuAtTfurHtpRyzjKmCBK1Y6WQ3trIXvo7s-----END RSA PRIVATE KEY-----';
  15. var _publicKeyPem =
  16. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----';
  17. it('generate a signature for some data and verify the signature when SHA256withRSA is used',
  18. function() {
  19. cryptolib('asymmetric.signer', 'commons.utils', function() {
  20. var f = function(box) {
  21. var signerFactory =
  22. new box.asymmetric.signer.factory.SignerFactory();
  23. var converters = new box.commons.utils.Converters();
  24. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  25. var dataB64 = converters.base64Encode(_plaintext);
  26. var arrayDataBase64 = [dataB64];
  27. var signatureBase64 =
  28. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  29. // Check whether digital signature can be verified.
  30. var verified = cryptoForgeSigner.verify(
  31. signatureBase64, _publicKeyPem, arrayDataBase64);
  32. // Verify that digital signature was successfully verified.
  33. expect(verified).toBeTruthy();
  34. };
  35. f.policies = {
  36. asymmetric: {
  37. signer: {
  38. algorithm: 'RSA',
  39. provider: 'Forge',
  40. hash: 'SHA256',
  41. publicExponent: 65537,
  42. secureRandom: {provider: 'Scytl'}
  43. }
  44. }
  45. };
  46. cryptolib('asymmetric.signer', 'commons.utils', f);
  47. });
  48. });
  49. it('generate a signature for some data and verify the signature when SHA256withRSA/PSS is used',
  50. function() {
  51. cryptolib('asymmetric.signer', 'commons.utils', function() {
  52. var f = function(box) {
  53. var signerFactory =
  54. new box.asymmetric.signer.factory.SignerFactory();
  55. var converters = new box.commons.utils.Converters();
  56. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  57. var dataB64 = converters.base64Encode(_plaintext);
  58. var arrayDataBase64 = [dataB64];
  59. var signatureBase64 =
  60. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  61. // Check whether digital signature can be verified.
  62. var verified = cryptoForgeSigner.verify(
  63. signatureBase64, _publicKeyPem, arrayDataBase64);
  64. // Verify that digital signature was successfully verified.
  65. expect(verified).toBeTruthy();
  66. };
  67. f.policies = {
  68. asymmetric: {
  69. signer: {
  70. algorithm: 'RSA',
  71. provider: 'Forge',
  72. hash: 'SHA256',
  73. padding: {
  74. PSS: {
  75. name: 'PSS',
  76. hash: 'SHA256',
  77. mask: {MGF1: {name: 'MGF1', hash: 'SHA256'}},
  78. saltLengthBytes: 32
  79. }
  80. },
  81. publicExponent: 65537,
  82. secureRandom: {provider: 'Scytl'}
  83. }
  84. }
  85. };
  86. cryptolib('asymmetric.signer', 'commons.utils', f);
  87. });
  88. });
  89. it('generate a signature for an empty data and verify the signature when SHA256withRSA is used',
  90. function() {
  91. cryptolib('asymmetric.signer', 'commons.utils', function() {
  92. var f = function(box) {
  93. var signerFactory =
  94. new box.asymmetric.signer.factory.SignerFactory();
  95. var converters = new box.commons.utils.Converters();
  96. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  97. var dataB64 = converters.base64Encode('');
  98. var arrayDataBase64 = [dataB64];
  99. var signatureBase64 =
  100. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  101. // Check whether digital signature can be verified.
  102. var verified = cryptoForgeSigner.verify(
  103. signatureBase64, _publicKeyPem, arrayDataBase64);
  104. // Verify that digital signature was successfully verified.
  105. expect(verified).toBeTruthy();
  106. };
  107. f.policies = {
  108. asymmetric: {
  109. signer: {
  110. algorithm: 'RSA',
  111. provider: 'Forge',
  112. hash: 'SHA256',
  113. publicExponent: 65537,
  114. secureRandom: {provider: 'Scytl'}
  115. }
  116. }
  117. };
  118. cryptolib('asymmetric.signer', 'commons.utils', f);
  119. });
  120. });
  121. it('detect a null element in data when signing', function() {
  122. cryptolib('asymmetric.signer', 'commons.utils', function() {
  123. var f = function(box) {
  124. var signerFactory =
  125. new box.asymmetric.signer.factory.SignerFactory();
  126. var converters = new box.commons.utils.Converters();
  127. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  128. var dataElement1 = 'dataElement1';
  129. var dataElement2 = 'dataElement2';
  130. var dataElement3 = null;
  131. var dataElement4 = 'dataElement4';
  132. var dataElement1B64 = converters.base64Encode(dataElement1);
  133. var dataElement2B64 = converters.base64Encode(dataElement2);
  134. var dataElement4B64 = converters.base64Encode(dataElement4);
  135. var arrayDataBase64 = [
  136. dataElement1B64, dataElement2B64, dataElement3, dataElement4B64
  137. ];
  138. var call = function() {
  139. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  140. };
  141. expect(call).toThrow();
  142. };
  143. f.policies = {
  144. asymmetric: {
  145. signer: {
  146. algorithm: 'RSA',
  147. provider: 'Forge',
  148. hash: 'SHA256',
  149. publicExponent: 65537,
  150. secureRandom: {provider: 'Scytl'}
  151. }
  152. }
  153. };
  154. cryptolib('asymmetric.signer', 'commons.utils', f);
  155. });
  156. });
  157. it('detect that a signature is false when SHA256withRSA is used',
  158. function() {
  159. cryptolib('asymmetric.signer', 'commons.utils', function() {
  160. var f = function(box) {
  161. var signerFactory =
  162. new box.asymmetric.signer.factory.SignerFactory();
  163. var converters = new box.commons.utils.Converters();
  164. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  165. var dataB64 = converters.base64Encode(_plaintext);
  166. var dataB64Different =
  167. converters.base64Encode('Some other string.');
  168. var arrayDataBase64 = [dataB64];
  169. var arrayDataBase64Different = [dataB64Different];
  170. var signatureBase64 =
  171. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  172. // Check whether digital signature can be verified.
  173. var verified = cryptoForgeSigner.verify(
  174. signatureBase64, _publicKeyPem, arrayDataBase64Different);
  175. // Verify that digital signature was successfully verified.
  176. expect(!verified).toBeTruthy();
  177. };
  178. f.policies = {
  179. asymmetric: {
  180. signer: {
  181. algorithm: 'RSA',
  182. provider: 'Forge',
  183. hash: 'SHA256',
  184. publicExponent: 65537,
  185. secureRandom: {provider: 'Scytl'}
  186. }
  187. }
  188. };
  189. cryptolib('asymmetric.signer', 'commons.utils', f);
  190. });
  191. });
  192. it('detect that a signature is false when SHA256withRSA/PSS is used',
  193. function() {
  194. cryptolib('asymmetric.signer', 'commons.utils', function() {
  195. var f = function(box) {
  196. var signerFactory =
  197. new box.asymmetric.signer.factory.SignerFactory();
  198. var converters = new box.commons.utils.Converters();
  199. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  200. var dataB64 = converters.base64Encode(_plaintext);
  201. var dataB64Different =
  202. converters.base64Encode('Some other string.');
  203. var arrayDataBase64 = [dataB64];
  204. var arrayDataBase64Different = [dataB64Different];
  205. var signatureBase64 =
  206. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  207. // Check whether digital signature can be verified.
  208. var verified = cryptoForgeSigner.verify(
  209. signatureBase64, _publicKeyPem, arrayDataBase64Different);
  210. // Verify that digital signature was successfully verified.
  211. expect(!verified).toBeTruthy();
  212. };
  213. f.policies = {
  214. asymmetric: {
  215. signer: {
  216. algorithm: 'RSA',
  217. provider: 'Forge',
  218. hash: 'SHA256',
  219. padding: {
  220. PSS: {
  221. name: 'PSS',
  222. hash: 'SHA256',
  223. mask: {MGF1: {name: 'MGF1', hash: 'SHA256'}},
  224. saltLengthBytes: 32
  225. }
  226. },
  227. publicExponent: 65537,
  228. secureRandom: {provider: 'Scytl'}
  229. }
  230. }
  231. };
  232. cryptolib('asymmetric.signer', 'commons.utils', f);
  233. });
  234. });
  235. it('verify a RSA signature that was generated in Java', function() {
  236. cryptolib('asymmetric.signer', 'commons.utils', function() {
  237. var f = function(box) {
  238. var signerFactory =
  239. new box.asymmetric.signer.factory.SignerFactory();
  240. var converters = new box.commons.utils.Converters();
  241. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  242. var signedData = 'This is a string from Java.';
  243. var signedDataB64 = converters.base64Encode(signedData);
  244. var arraySignedDataB64 = [signedDataB64];
  245. // this is a signature generated in Java (using RSA)
  246. var signatureFromJavaRsa =
  247. 'KuQPmBRE/JpNQ5mvh1Y1LvW3Jr/ZYlYqOgdbgXLVZp+gjVtXHDgFZzIrC8+S+VdTs9WINjxN8aIZlbiemZ+YvXZ7fDOoJsZJRboyrFN9VGz6sWWVXAv28Xj1a2lPNlys8dO3uY0s5R77kDCvBHV4nJh0Hq2Ry+JRjRcrlPsIiDWzm9iTY7V6XzkVGCo58qWyQtloC0s73i8UM6V2nYu1oGDuHqUcvDbstElGAJrn7u4QtP2Kgzd1GSkvbSKhsjR2RniiJ7cD278i27NAEPXY+qqmWQ4c+zt3BKG4Aj4EtKa/4AyARmLC78KjZpfiGPTUzQZbMQudAMmKgSTQhx7ONg==';
  248. // Check whether digital signature can be verified.
  249. var verified = cryptoForgeSigner.verify(
  250. signatureFromJavaRsa, _publicKeyPem, arraySignedDataB64);
  251. // Verify that digital signature was successfully verified.
  252. expect(verified).toBeTruthy();
  253. };
  254. f.policies = {
  255. asymmetric: {
  256. signer: {
  257. algorithm: 'RSA',
  258. provider: 'Forge',
  259. hash: 'SHA256',
  260. publicExponent: 65537,
  261. secureRandom: {provider: 'Scytl'}
  262. }
  263. }
  264. };
  265. cryptolib('asymmetric.signer', 'commons.utils', f);
  266. });
  267. });
  268. it('verify a RSA/PSS signature that was generated in Java', function() {
  269. cryptolib('asymmetric.signer', 'commons.utils', function() {
  270. var f = function(box) {
  271. var signerFactory =
  272. new box.asymmetric.signer.factory.SignerFactory();
  273. var converters = new box.commons.utils.Converters();
  274. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  275. var signedData = 'This is a string from Java.';
  276. var signedDataB64 = converters.base64Encode(signedData);
  277. var arraySignedDataB64 = [signedDataB64];
  278. // This is a signature generated in Java (using RSA/PSS)
  279. var signatureFromJavaRsa =
  280. 'JSYZ0i3Yz3L8J3bUOLOP/Kk83qkhoZ9NqBm9eUpaFoZY8gD42XHJxXyPmDYMnDFMr+J5GypfS9vheYtgn2hag8n3DcG86I80UCNuEZLEhyXpIsN4mv3rDb/xjMXlnDLWiK6MTBTDVImKfwpX6x/xw9YtcG9ADACRefH4++H9fhpg8yXUfT9OacOPHgjoW/y4hJmaKVk+IVDgLAKfToNWwVm2xU0SnFryQ9PUJeR8KHttBLR+gpZZpn+bQMcEvikw8bzAYGX4yXxGB9UgoIsz6xNyxY6P6UyJSal/JajURLuSwW7JpxjtthbyKS6SUXtH+NEg36ZpMhd8QC6EPzwrlQ==';
  281. // Check whether digital signature can be verified.
  282. var verified = cryptoForgeSigner.verify(
  283. signatureFromJavaRsa, _publicKeyPem, arraySignedDataB64);
  284. // Verify that digital signature was successfully verified.
  285. expect(verified).toBeTruthy();
  286. };
  287. f.policies = {
  288. asymmetric: {
  289. signer: {
  290. algorithm: 'RSA',
  291. provider: 'Forge',
  292. hash: 'SHA256',
  293. padding: {
  294. PSS: {
  295. name: 'PSS',
  296. hash: 'SHA256',
  297. mask: {MGF1: {name: 'MGF1', hash: 'SHA256'}},
  298. saltLengthBytes: 32
  299. }
  300. },
  301. publicExponent: 65537,
  302. secureRandom: {provider: 'Scytl'}
  303. }
  304. }
  305. };
  306. cryptolib('asymmetric.signer', 'commons.utils', f);
  307. });
  308. });
  309. it('generate a RSA digital signature for some data and verify the signature.',
  310. function() {
  311. cryptolib('asymmetric.signer', 'commons.utils', function(box) {
  312. var signerFactory =
  313. new box.asymmetric.signer.factory.SignerFactory();
  314. var converters = new box.commons.utils.Converters();
  315. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  316. var dataB64 = converters.base64Encode(_plaintext);
  317. var arrayDataBase64 = [dataB64];
  318. var signatureBase64 =
  319. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  320. // Check whether digital signature can be verified.
  321. var verified = cryptoForgeSigner.verify(
  322. signatureBase64, _publicKeyPem, arrayDataBase64);
  323. // Verify that digital signature was successfully verified.
  324. expect(verified).toBeTruthy();
  325. });
  326. });
  327. it('throw an error when the array of data is null.', function() {
  328. cryptolib('asymmetric.signer', function(box) {
  329. var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
  330. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  331. expect(function() {
  332. cryptoForgeSigner.sign(_privateKeyPem, null);
  333. }).toThrow();
  334. });
  335. });
  336. it('throw an error when the array of data contains less than one element.',
  337. function() {
  338. cryptolib('asymmetric.signer', function(box) {
  339. var signerFactory =
  340. new box.asymmetric.signer.factory.SignerFactory();
  341. var cryptoForgeSigner = signerFactory.getCryptoSigner();
  342. var arrayDataBase64 = [];
  343. expect(function() {
  344. cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
  345. }).toThrow();
  346. });
  347. });
  348. it('throw an error when the given algorithm is wrong.', function() {
  349. var f = function(box) {
  350. var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
  351. expect(function() {
  352. signerFactory.getCryptoSigner();
  353. }).toThrow();
  354. };
  355. f.policies = {
  356. asymmetric: {
  357. signer: {
  358. algorithm: 'WRONG',
  359. provider: 'Forge',
  360. secureRandom: {provider: 'Scytl'}
  361. }
  362. }
  363. };
  364. cryptolib('asymmetric.signer', f);
  365. });
  366. it('throw an error when the given provider is wrong.', function() {
  367. var f = function(box) {
  368. var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
  369. expect(function() {
  370. signerFactory.getCryptoSigner();
  371. }).toThrow();
  372. };
  373. f.policies = {
  374. asymmetric: {
  375. signer: {
  376. algorithm: 'CORRECT',
  377. provider: 'WRONG',
  378. secureRandom: {provider: 'Scytl'}
  379. }
  380. }
  381. };
  382. cryptolib('asymmetric.signer', f);
  383. });
  384. it('throw an error when the given hash algorithm is wrong.', function() {
  385. var f = function(box) {
  386. var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
  387. expect(function() {
  388. signerFactory.getCryptoSigner();
  389. }).toThrow();
  390. };
  391. f.policies = {
  392. asymmetric: {
  393. signer: {
  394. algorithm: 'RSA',
  395. hash: 'WRONG',
  396. provider: 'Forge',
  397. secureRandom: {provider: 'Scytl'}
  398. }
  399. }
  400. };
  401. cryptolib('asymmetric.signer', f);
  402. });
  403. });
  404. });
  405. });