2
0

cryptolib.spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. beforeEach(function() {
  9. 'use strict';
  10. cryptoPRNG.startEntropyCollection();
  11. cryptoPRNG.stopEntropyCollectionAndCreatePRNG();
  12. cryptolibPolicies = {
  13. homomorphic: {
  14. cipher: {
  15. secureRandom:
  16. {provider: Config.homomorphic.cipher.secureRandom.provider.SCYTL}
  17. }
  18. },
  19. asymmetric: {
  20. keypair: {
  21. encryption: {
  22. algorithm: Config.asymmetric.keypair.encryption.algorithm.RSA,
  23. provider: Config.asymmetric.keypair.encryption.provider.FORGE,
  24. keyLength: Config.asymmetric.keypair.encryption.keyLength.KL_2048,
  25. publicExponent: Config.asymmetric.keypair.encryption.publicExponent.F4
  26. },
  27. secureRandom:
  28. {provider: Config.asymmetric.keypair.secureRandom.provider.SCYTL}
  29. },
  30. signer: {
  31. algorithm: Config.asymmetric.signer.algorithm.RSA,
  32. provider: Config.asymmetric.signer.provider.FORGE,
  33. hash: Config.asymmetric.signer.hash.SHA256,
  34. padding: {
  35. PSS: {
  36. name: Config.asymmetric.signer.padding.PSS.name,
  37. hash: Config.asymmetric.signer.padding.PSS.hash.SHA256,
  38. mask: {
  39. MGF1: {
  40. name: Config.asymmetric.signer.padding.PSS.mask.MGF1.name,
  41. hash: Config.asymmetric.signer.padding.PSS.mask.MGF1.hash.SHA256
  42. }
  43. },
  44. saltLengthBytes:
  45. Config.asymmetric.signer.padding.PSS.saltLengthBytes.SL_32
  46. }
  47. },
  48. publicExponent: Config.asymmetric.signer.publicExponent.F4,
  49. secureRandom:
  50. {provider: Config.asymmetric.signer.secureRandom.provider.SCYTL}
  51. },
  52. cipher: {
  53. algorithm: Config.asymmetric.cipher.algorithm.RSA_KEM,
  54. secretKeyLengthBytes: Config.asymmetric.cipher.algorithm.RSA_KEM
  55. .secretKeyLengthBytes.KL_16,
  56. ivLengthBytes:
  57. Config.asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes.IVL_12,
  58. tagLengthBytes:
  59. Config.asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes.TL_16,
  60. deriver: Config.asymmetric.cipher.algorithm.RSA_KEM.deriver.name.KDF1,
  61. hash: Config.asymmetric.cipher.algorithm.RSA_KEM.deriver.messagedigest
  62. .algorithm.SHA256,
  63. symmetricCipher:
  64. Config.asymmetric.cipher.algorithm.RSA_KEM.cipher.AESGCM,
  65. provider: Config.asymmetric.cipher.provider.FORGE,
  66. secureRandom:
  67. {provider: Config.asymmetric.cipher.secureRandom.provider.SCYTL}
  68. }
  69. },
  70. primitives: {
  71. secureRandom: {provider: Config.primitives.secureRandom.provider.SCYTL},
  72. messagedigest: {
  73. algorithm: Config.primitives.messagedigest.algorithm.SHA256,
  74. provider: Config.primitives.messagedigest.provider.FORGE
  75. },
  76. derivation: {
  77. pbkdf: {
  78. provider: Config.primitives.derivation.pbkdf.provider.FORGE,
  79. hash: Config.primitives.derivation.pbkdf.hash.SHA256,
  80. saltLengthBytes:
  81. Config.primitives.derivation.pbkdf.saltLengthBytes.SL_20,
  82. keyLengthBytes:
  83. Config.primitives.derivation.pbkdf.keyLengthBytes.KL_32,
  84. iterations: Config.primitives.derivation.pbkdf.iterations.I_1
  85. }
  86. }
  87. },
  88. stores: {},
  89. symmetric: {
  90. secretkey: {
  91. encryption: {
  92. lengthBytes: Config.symmetric.secretkey.encryption.lengthBytes.SK_16
  93. },
  94. mac: {lengthBytes: Config.symmetric.secretkey.mac.lengthBytes.SK_32},
  95. secureRandom:
  96. {provider: Config.symmetric.secretkey.secureRandom.provider.SCYTL}
  97. },
  98. cipher: {
  99. provider: Config.symmetric.cipher.provider.FORGE,
  100. algorithm: {
  101. AES128_GCM: {
  102. name: Config.symmetric.cipher.algorithm.AES128_GCM.name,
  103. keyLengthBytes:
  104. Config.symmetric.cipher.algorithm.AES128_GCM.keyLengthBytes,
  105. tagLengthBytes:
  106. Config.symmetric.cipher.algorithm.AES128_GCM.tagLengthBytes
  107. }
  108. },
  109. initializationVectorLengthBytes:
  110. Config.symmetric.cipher.initializationVectorLengthBytes.IV_12,
  111. secureRandom:
  112. {provider: Config.symmetric.cipher.secureRandom.provider.SCYTL}
  113. },
  114. mac: {
  115. hash: Config.symmetric.mac.hash.SHA256,
  116. provider: Config.symmetric.mac.provider.FORGE
  117. }
  118. },
  119. proofs: {
  120. secureRandom: {provider: Config.proofs.secureRandom.provider.SCYTL},
  121. messagedigest: {
  122. algorithm: Config.proofs.messagedigest.algorithm.SHA256,
  123. provider: Config.proofs.messagedigest.provider.FORGE
  124. },
  125. charset: Config.proofs.charset.UTF8
  126. },
  127. digitalenvelope: {
  128. symmetric: {
  129. secretkey: {
  130. encryption: {
  131. lengthBytes: Config.symmetric.secretkey.encryption.lengthBytes.SK_16
  132. },
  133. mac: {lengthBytes: Config.symmetric.secretkey.mac.lengthBytes.SK_32},
  134. secureRandom: {
  135. provider: Config.symmetric.secretkey.secureRandom.provider.SCYTL
  136. }
  137. },
  138. cipher: {
  139. provider: Config.symmetric.cipher.provider.FORGE,
  140. algorithm: {
  141. AES128_GCM: {
  142. name: Config.symmetric.cipher.algorithm.AES128_GCM.name,
  143. keyLengthBytes:
  144. Config.symmetric.cipher.algorithm.AES128_GCM.keyLengthBytes,
  145. tagLengthBytes:
  146. Config.symmetric.cipher.algorithm.AES128_GCM.tagLengthBytes
  147. }
  148. },
  149. initializationVectorLengthBytes:
  150. Config.symmetric.cipher.initializationVectorLengthBytes.IV_12,
  151. secureRandom:
  152. {provider: Config.symmetric.cipher.secureRandom.provider.SCYTL}
  153. },
  154. mac: {
  155. hash: Config.symmetric.mac.hash.SHA256,
  156. provider: Config.symmetric.mac.provider.FORGE
  157. }
  158. },
  159. asymmetric: {
  160. cipher: {
  161. algorithm: Config.asymmetric.cipher.algorithm.RSA_KEM,
  162. secretKeyLengthBytes: Config.asymmetric.cipher.algorithm.RSA_KEM
  163. .secretKeyLengthBytes.KL_16,
  164. ivLengthBytes:
  165. Config.asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes.IVL_12,
  166. tagLengthBytes:
  167. Config.asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes.TL_16,
  168. deriver: Config.asymmetric.cipher.algorithm.RSA_KEM.deriver.name.KDF1,
  169. hash: Config.asymmetric.cipher.algorithm.RSA_KEM.deriver.messagedigest
  170. .algorithm.SHA256,
  171. symmetricCipher:
  172. Config.asymmetric.cipher.algorithm.RSA_KEM.cipher.AESGCM,
  173. provider: Config.asymmetric.cipher.provider.FORGE,
  174. secureRandom: {
  175. provider: Config.asymmetric.cipher.secureRandom.provider.SCYTL
  176. }
  177. }
  178. }
  179. }
  180. };
  181. });
  182. cryptolib.modules.test = cryptolib.modules.test || {};
  183. cryptolib.modules.test.test2 = cryptolib.modules.test.test2 || {};
  184. cryptolib.modules.test.test1 = function(box) {
  185. 'use strict';
  186. box.test = box.test || {};
  187. box.test.test1 = {};
  188. };
  189. cryptolib.modules.test.test2 = function(box) {
  190. 'use strict';
  191. box.test = box.test || {};
  192. box.test.test2 = {};
  193. };
  194. cryptolib.modules.test.test2.test3 = function(box) {
  195. 'use strict';
  196. box.test = box.test || {};
  197. box.test.test2 = box.test.test2 || {};
  198. box.test.test2.test3 = {};
  199. };
  200. describe('Cryptolib: ', function() {
  201. 'use strict';
  202. it('Should exist a Cryptolib sandbox', function() {
  203. cryptolib('*', function(box) {
  204. expect(box).toBeDefined();
  205. });
  206. });
  207. it('Should have a method that returns the modules names', function() {
  208. cryptolib('*', function(box) {
  209. expect(box.getModuleNames).toBeDefined();
  210. });
  211. });
  212. it('Should redefine forge global variable', function() {
  213. cryptolib('*', function(box) {
  214. expect(box.forge).toBeDefined();
  215. });
  216. });
  217. it('Should contain a utils modules', function() {
  218. cryptolib('*', function(box) {
  219. expect(box.getModuleNames()).toContain('commons');
  220. });
  221. });
  222. it('Should contain all modules', function() {
  223. cryptolib('*', function(box) {
  224. expect(box.getModuleNames()).toContain('certificates');
  225. expect(box.getModuleNames()).toContain('symmetric');
  226. expect(box.getModuleNames()).toContain('primitives');
  227. expect(box.getModuleNames()).toContain('asymmetric');
  228. expect(box.getModuleNames()).toContain('stores');
  229. expect(box.getModuleNames()).toContain('commons');
  230. });
  231. });
  232. it('should contain a test.test1 module', function() {
  233. cryptolib('test.test1', function(box) {
  234. expect(box.getModuleNames()).toContain('test');
  235. });
  236. });
  237. it('should contain a test.test2.test3 module', function() {
  238. cryptolib('test.test2.test3', function(box) {
  239. expect(box.getModuleNames()).toContain('test');
  240. });
  241. });
  242. it('should contain a test module', function() {
  243. cryptolib('test', function(box) {
  244. expect(box.getModuleNames()).toContain('test');
  245. });
  246. });
  247. it('should contain all test submodules', function() {
  248. cryptolib('test.*', function(box) {
  249. expect(box.getModuleNames()).toContain('test');
  250. });
  251. });
  252. describe('policies', function() {
  253. it('should be defined a policies objects', function() {
  254. cryptolib('test.*', function(box) {
  255. expect(box.policies).toBeDefined();
  256. });
  257. });
  258. it('should be defined a policies objects', function() {
  259. cryptolib('test.*', function(box) {
  260. expect(box.policies.asymmetric.keypair.encryption.algorithm)
  261. .toEqual(Config.asymmetric.keypair.encryption.algorithm.RSA);
  262. });
  263. });
  264. });
  265. describe('Config', function() {
  266. it('should be an immutable object. i.e. we cannot change any property',
  267. function() {
  268. cryptolib('*', function() {
  269. expect(function() {
  270. Config.asymmetric.keypair.encryption.algorithm.RSA = 'SCYTL';
  271. }).toThrow();
  272. expect(Config.asymmetric.keypair.encryption.algorithm.RSA)
  273. .toEqual('RSA');
  274. });
  275. });
  276. });
  277. describe('Cryptolib: ', function() {
  278. it('should be defined a policies objects', function() {
  279. cryptolib('test.*', function(box) {
  280. expect(box.policies.asymmetric.keypair.encryption.algorithm)
  281. .toEqual(Config.asymmetric.keypair.encryption.algorithm.RSA);
  282. });
  283. });
  284. });
  285. });