2
0

asymmetric.cipher.spec.js 17 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. describe('CryptoAsymmetricCipher ...', function() {
  9. 'use strict';
  10. it('should be defined', function() {
  11. cryptolib('asymmetric.cipher', function(box) {
  12. var cipherFactory =
  13. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  14. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  15. expect(rsaCipher).toBeDefined();
  16. });
  17. });
  18. describe('should be able to ..', function() {
  19. var plaintext = 'Ox2fUJq1gAbX';
  20. var privateKeyPem =
  21. '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g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Y11c0rSyS/hDyBFrK+LL5uEOqhAlJAGB2HyOj1clWVF+GvrTpuV5LZKUS/79pmZU7G7QCaX/0Ow7wKBgC/kDH7cmWQnWvvJ5izrx/7PogQVPOLELeUIGLu/hjsSdDKiFCxCUZ948+9NuG+DnpXDWzw//r8mPBRRGGsqFws5Aipp7yjQ3kRDCCzGelPCVhHyfmKqA+8ewXPulKS3/wIyHIvaXmsuAtTfurHtpRyzjKmCBK1Y6WQ3trIXvo7s-----END RSA PRIVATE KEY-----';
  22. var publicKeyPem =
  23. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----';
  24. ////////////////////
  25. //
  26. // RSA_KEM
  27. //
  28. ////////////////////
  29. it('RSA_KEM encrypt and decrypt some data - key 1024 bit - KDF1 - SHA256',
  30. function() {
  31. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  32. var publicKeyPem1024 = [
  33. '-----BEGIN PUBLIC KEY-----',
  34. 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpIzTG+VAAO9tGCrqoiwJ7e3Hz',
  35. '2IY4Nhfvlh4ZevDje7WIqGMzg0Ewf5UBhCD1BuYklxsD/urdWH5wXT1m7tD70ZqP',
  36. 'NjE4N6qnDmR38u0AND24p7WkT4AOCDG5hiRG5SDm6hp1vaLVEBRhUtdFb6z/m8og',
  37. '+6ygTIkspzesZ+RaRwIDAQAB', '-----END PUBLIC KEY-----'
  38. ].join('\n');
  39. var privateKeyPem1024 = [
  40. '-----BEGIN RSA PRIVATE KEY-----',
  41. 'MIICXAIBAAKBgQCpIzTG+VAAO9tGCrqoiwJ7e3Hz2IY4Nhfvlh4ZevDje7WIqGMz',
  42. 'g0Ewf5UBhCD1BuYklxsD/urdWH5wXT1m7tD70ZqPNjE4N6qnDmR38u0AND24p7Wk',
  43. 'T4AOCDG5hiRG5SDm6hp1vaLVEBRhUtdFb6z/m8og+6ygTIkspzesZ+RaRwIDAQAB',
  44. 'AoGAQZKko3+ExJJwMHd4Zl9+VuFFDISlhKV0Ii7Q/I/tVERh3NlrnE0GuQa9fhj7',
  45. 'rgM+tnDeyG3MIIRugKGlbIKqAlWep1jGqcLw5dUy0plppB2S93BLq4XZ406iL+KS',
  46. '3OlJhYR9J14VcMoJjN8lGY2MYX7ywx+q5gJrREu5vBS33AECQQDUkZZX7cwyVeaB',
  47. 'iDabdGpNxy88fIlRg1bRn29p0PmmmHcRZa0VaQm3EjbrVzg9xhd46IuBh60GGMYl',
  48. '4GdxiXQpAkEAy7HzJqqR+CptThAWA64hwcwWmujdjmBAJ69Scg+ZX61dpmHNiphv',
  49. 'drpbpYu7b2yPCOI1NfkLUoRSmyd5DGao7wJAVJB2lxRrH7s8sFtYHg/6GmcbS5zf',
  50. 'pCXz7ADZeedA6h3NgIZKjTH0Q3hjkMxp+2lK/TbGCQnIs5w3d+oGPQzJwQJBALaQ',
  51. '+ODYElp+FFfaHRERWlorRLt1KVa5t+aZwehPSOUzKnO8xw+Ijqa4YvneYpF8mDqb',
  52. 'HJwSae58gNllKJ5PyOsCQHbdbSFj0aClSs0AP+jVbpzINxiObn0brcZni/UI3gul',
  53. 'dO7vZNbWyr3h8QORN5HTg4ov4a3V8V0jlWboBZRJpvw=',
  54. '-----END RSA PRIVATE KEY-----'
  55. ].join('\n');
  56. var publicKey = box.forge.pki.publicKeyFromPem(publicKeyPem1024);
  57. var privateKey = box.forge.pki.privateKeyFromPem(privateKeyPem1024);
  58. // confirm that the keys have the expected modulus size
  59. expect(publicKey.n.bitLength()).toBe(1024);
  60. expect(privateKey.n.bitLength()).toBe(1024);
  61. // Create RSA cipher instance.
  62. var cipherFactory =
  63. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  64. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  65. var converters = new box.commons.utils.Converters();
  66. var dataB64 = converters.base64Encode(plaintext);
  67. // Encrypt data and Base64 encode result.
  68. var encryptedDataBase64 =
  69. rsaCipher.encrypt(publicKeyPem1024, dataB64);
  70. // Base64 decode encrypted data and decrypt data.
  71. var decryptedDataB64 =
  72. rsaCipher.decrypt(privateKeyPem1024, encryptedDataBase64);
  73. var decryptedData = converters.base64Decode(decryptedDataB64);
  74. // Verify that data was successfully decrypted.
  75. expect(decryptedData).toBe(plaintext);
  76. });
  77. });
  78. it('RSA_KEM encrypt and decrypt some data - key 2048 bit - KDF2 - SHA256',
  79. function() {
  80. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  81. // Create RSA cipher instance.
  82. var cipherFactory =
  83. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  84. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  85. var converters = new box.commons.utils.Converters();
  86. var dataB64 = converters.base64Encode(plaintext);
  87. // Encrypt data and Base64 encode result.
  88. var encryptedDataBase64 = rsaCipher.encrypt(publicKeyPem, dataB64);
  89. // Base64 decode encrypted data and decrypt data.
  90. var decryptedDataB64 =
  91. rsaCipher.decrypt(privateKeyPem, encryptedDataBase64);
  92. var decryptedData = converters.base64Decode(decryptedDataB64);
  93. // Verify that data was successfully decrypted.
  94. expect(decryptedData).toBe(plaintext);
  95. });
  96. });
  97. it('RSA_KEM encrypt and decrypt random data (512 bytes) - key 2048 bit - KDF2 - SHA256',
  98. function() {
  99. var f = function(box) {
  100. var secureRandomFactory =
  101. new box.primitives.securerandom.factory.SecureRandomFactory();
  102. var secureRandom = secureRandomFactory.getCryptoRandomBytes();
  103. var randomBytes = secureRandom.nextRandom(512);
  104. var cipherFactory =
  105. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  106. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  107. var converters = new box.commons.utils.Converters();
  108. var dataB64 = converters.base64Encode(randomBytes);
  109. // Encrypt data and Base64 encode result.
  110. var encryptedDataBase64 = rsaCipher.encrypt(publicKeyPem, dataB64);
  111. // Base64 decode encrypted data and decrypt data.
  112. var decryptedDataB64 =
  113. rsaCipher.decrypt(privateKeyPem, encryptedDataBase64);
  114. // Verify that data was successfully decrypted.
  115. expect(decryptedDataB64).toBe(dataB64);
  116. };
  117. f.policies = {
  118. primitives: {
  119. secureRandom:
  120. {provider: Config.primitives.secureRandom.provider.SCYTL},
  121. messagedigest: {
  122. algorithm: Config.primitives.messagedigest.algorithm.SHA256,
  123. provider: Config.primitives.messagedigest.provider.FORGE
  124. }
  125. },
  126. asymmetric: {
  127. cipher: {
  128. algorithm: Config.asymmetric.cipher.algorithm.RSA_KEM,
  129. secretKeyLengthBytes: Config.asymmetric.cipher.algorithm.RSA_KEM
  130. .secretKeyLengthBytes.KL_16,
  131. ivLengthBytes: Config.asymmetric.cipher.algorithm.RSA_KEM
  132. .ivLengthBytes.IVL_12,
  133. tagLengthBytes: Config.asymmetric.cipher.algorithm.RSA_KEM
  134. .tagLengthBytes.TL_16,
  135. deriver:
  136. Config.asymmetric.cipher.algorithm.RSA_KEM.deriver.name.KDF2,
  137. hash: Config.asymmetric.cipher.algorithm.RSA_KEM.deriver
  138. .messagedigest.algorithm.SHA256,
  139. symmetricCipher:
  140. Config.asymmetric.cipher.algorithm.RSA_KEM.cipher.AESGCM,
  141. provider: Config.asymmetric.cipher.provider.FORGE,
  142. secureRandom: {
  143. provider: Config.asymmetric.cipher.secureRandom.provider.SCYTL
  144. }
  145. }
  146. }
  147. };
  148. cryptolib(
  149. 'primitives.securerandom', 'asymmetric.cipher', 'commons.utils',
  150. f);
  151. });
  152. it('RSA_KEM encrypt and decrypt some data - key 2048 bit - MGF1 - SHA256',
  153. function() {
  154. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  155. // Create RSA cipher instance.
  156. var cipherFactory =
  157. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  158. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  159. var converters = new box.commons.utils.Converters();
  160. var dataB64 = converters.base64Encode(plaintext);
  161. // Encrypt data and Base64 encode result.
  162. var encryptedDataBase64 = rsaCipher.encrypt(publicKeyPem, dataB64);
  163. // Base64 decode encrypted data and decrypt data.
  164. var decryptedDataB64 =
  165. rsaCipher.decrypt(privateKeyPem, encryptedDataBase64);
  166. var decryptedData = converters.base64Decode(decryptedDataB64);
  167. // Verify that data was successfully decrypted.
  168. expect(decryptedData).toBe(plaintext);
  169. });
  170. });
  171. it('RSA_KEM encrypt null key, exception thrown', function() {
  172. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  173. // Create RSA cipher instance.
  174. var cipherFactory =
  175. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  176. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  177. var converters = new box.commons.utils.Converters();
  178. var dataB64 = converters.base64Encode(plaintext);
  179. expect(function() {
  180. rsaCipher.encrypt(null, dataB64);
  181. }).toThrow();
  182. });
  183. });
  184. it('RSA_KEM encrypt null data, exception thrown', function() {
  185. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  186. // Create RSA cipher instance.
  187. var cipherFactory =
  188. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  189. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  190. expect(function() {
  191. rsaCipher.encrypt(publicKeyPem);
  192. }).toThrow();
  193. });
  194. });
  195. it('RSA_KEM decrypt null key, exception thrown', function() {
  196. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  197. // Create RSA cipher instance.
  198. var cipherFactory =
  199. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  200. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  201. var converters = new box.commons.utils.Converters();
  202. var dataB64 = converters.base64Encode(plaintext);
  203. // Encrypt data and Base64 encode result.
  204. var encryptedDataBase64 = rsaCipher.encrypt(publicKeyPem, dataB64);
  205. expect(function() {
  206. rsaCipher.decrypt(null, encryptedDataBase64);
  207. }).toThrow();
  208. });
  209. });
  210. it('RSA_KEM decrypt null data, exception thrown', function() {
  211. cryptolib('asymmetric.cipher', 'commons.utils', function(box) {
  212. // Create RSA cipher instance.
  213. var cipherFactory =
  214. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  215. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  216. expect(function() {
  217. rsaCipher.decrypt(privateKeyPem, null);
  218. }).toThrow();
  219. });
  220. });
  221. ////////////////////
  222. //
  223. // RSA_OAEP
  224. //
  225. ////////////////////
  226. it('RSA_OAEP encrypt and decrypt some data.', function() {
  227. cryptolib('asymmetric.cipher', 'commons.utils', function() {
  228. var f = function(box) {
  229. // Create RSA cipher instance.
  230. var cipherFactory =
  231. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  232. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  233. var converters = new box.commons.utils.Converters();
  234. var dataB64 = converters.base64Encode(plaintext);
  235. // Encrypt data and Base64 encode result.
  236. var encryptedDataBase64 = rsaCipher.encrypt(publicKeyPem, dataB64);
  237. // Base64 decode encrypted data and decrypt data.
  238. var decryptedDataB64 =
  239. rsaCipher.decrypt(privateKeyPem, encryptedDataBase64);
  240. var decryptedData = converters.base64Decode(decryptedDataB64);
  241. // Verify that data was successfully decrypted.
  242. expect(decryptedData).toBe(plaintext);
  243. };
  244. f.policies = {
  245. asymmetric: {
  246. cipher: {
  247. algorithm: Config.asymmetric.cipher.algorithm.RSA_OAEP,
  248. provider: Config.asymmetric.cipher.provider.FORGE,
  249. secureRandom: {
  250. provider: Config.asymmetric.cipher.secureRandom.provider.SCYTL
  251. }
  252. }
  253. }
  254. };
  255. cryptolib('asymmetric.cipher', 'commons.utils', f);
  256. });
  257. });
  258. it('RSA_OAEP decrypt some data that was RSA encrypted using Java.',
  259. function() {
  260. cryptolib('asymmetric.cipher', 'commons.utils', function() {
  261. var f = function(box) {
  262. var asymmetricEncryptedDataBase64 =
  263. 'SuIt3oT4a98kF+7sReHLxYuVTV8GtspAUf2607aTIdnLkHrN5gYNNeXoOHuVXZtm2AneV165PsRtUpEapGtzaeWpNZS7rhnPU2JjQ26p0PkR0A6nXGX7sn7QlH8kJ/etN7wVilHNGRlM2nvgMpbIhue/X3+OQ6ROYavDfEs9XC8QCJxgD5CvkDJANzfNaybPdf4yZ+QFL90T4ZePEy/PZbIPtZMWIz8xTUkZs4SUbtfph+8tX4o+0b1lCBQlAeqegZQcycN0/JkgsQdRi5l02wrg0lDj4QMFzB9h3uBhzv3Wn4DlN9OQtHYMkPz/9MurRhHkqKJBSkBcTKawTmcA9Q==';
  264. // Create RSA cipher instance.
  265. var cipherFactory =
  266. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  267. var rsaCipher = cipherFactory.getCryptoAsymmetricCipher();
  268. var converters = new box.commons.utils.Converters();
  269. // Base64 decode encrypted data and decrypt data.
  270. var decryptedDataB64 = rsaCipher.decrypt(
  271. privateKeyPem, asymmetricEncryptedDataBase64);
  272. var decryptedData = converters.base64Decode(decryptedDataB64);
  273. // Verify that data was successfully decrypted.
  274. expect(decryptedData).toBe(plaintext);
  275. };
  276. f.policies = {
  277. asymmetric: {
  278. cipher: {
  279. algorithm: Config.asymmetric.cipher.algorithm.RSA_OAEP,
  280. provider: Config.asymmetric.cipher.provider.FORGE,
  281. secureRandom: {
  282. provider:
  283. Config.asymmetric.cipher.secureRandom.provider.SCYTL
  284. }
  285. }
  286. }
  287. };
  288. cryptolib('asymmetric.cipher', 'commons.utils', f);
  289. });
  290. });
  291. it('RSA_OAEP throw an error when the given algorithm is wrong.',
  292. function() {
  293. var f = function(box) {
  294. var cipherFactory =
  295. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  296. expect(function() {
  297. cipherFactory.getCryptoAsymmetricCipher();
  298. }).toThrow();
  299. };
  300. f.policies = {
  301. asymmetric: {
  302. cipher: {
  303. algorithm: 'WRONG',
  304. provider: 'Forge',
  305. secureRandom: {provider: 'Scytl'}
  306. }
  307. }
  308. };
  309. cryptolib('asymmetric.cipher', 'commons.utils', f);
  310. });
  311. it('RSA_OAEP throw an error when the given provider is wrong.', function() {
  312. var f = function(box) {
  313. var cipherFactory =
  314. new box.asymmetric.cipher.factory.AsymmetricCipherFactory();
  315. expect(function() {
  316. cipherFactory.getCryptoAsymmetricCipher();
  317. }).toThrow();
  318. };
  319. f.policies = {
  320. asymmetric: {
  321. cipher: {
  322. algorithm: 'CORRECT',
  323. provider: 'AAA',
  324. secureRandom: {provider: 'Scytl'}
  325. }
  326. }
  327. };
  328. cryptolib('asymmetric.cipher', 'commons.utils', f);
  329. });
  330. });
  331. });