cipher.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 cryptoPolicy = require('scytl-cryptopolicy');
  11. var asymmetric = require('../lib/index');
  12. var secureRandom = require('scytl-securerandom');
  13. var codec = require('scytl-codec');
  14. var forge = require('node-forge');
  15. describe('The asymmetric cryptography service should be able to ...', function() {
  16. var PUBLIC_KEY_PEM =
  17. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----';
  18. var PRIVATE_KEY_PEM =
  19. '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y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3NlrnE0GuQa9fhj7rgM+tnDeyG3MIIRugKGlbIKqAlWep1jGqcLw5dUy0plppB2S93BLq4XZ406iL+KS3OlJhYR9J14VcMoJjN8lGY2MYX7ywx+q5gJrREu5vBS33AECQQDUkZZX7cwyVeaBiDabdGpNxy88fIlRg1bRn29p0PmmmHcRZa0VaQm3EjbrVzg9xhd46IuBh60GGMYl4GdxiXQpAkEAy7HzJqqR+CptThAWA64hwcwWmujdjmBAJ69Scg+ZX61dpmHNiphvdrpbpYu7b2yPCOI1NfkLUoRSmyd5DGao7wJAVJB2lxRrH7s8sFtYHg/6GmcbS5zfpCXz7ADZeedA6h3NgIZKjTH0Q3hjkMxp+2lK/TbGCQnIs5w3d+oGPQzJwQJBALaQ+ODYElp+FFfaHRERWlorRLt1KVa5t+aZwehPSOUzKnO8xw+Ijqa4YvneYpF8mDqbHJwSae58gNllKJ5PyOsCQHbdbSFj0aClSs0AP+jVbpzINxiObn0brcZni/UI3guldO7vZNbWyr3h8QORN5HTg4ov4a3V8V0jlWboBZRJpvw=-----END RSA PRIVATE KEY-----';
  24. var ASYMMETRIC_CIPHER_OAEP_POLICY = {
  25. algorithm: {
  26. name: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_OAEP.name,
  27. hashAlgorithm: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_OAEP
  28. .hashAlgorithm.SHA256,
  29. maskGenerator: {
  30. name: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_OAEP
  31. .maskGenerator.MGF1.name,
  32. hashAlgorithm: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_OAEP
  33. .maskGenerator.MGF1.hashAlgorithm.SHA1
  34. }
  35. }
  36. };
  37. var ASYMMETRIC_CIPHER_KEM_POLICY = {
  38. algorithm: {
  39. name: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_KEM.name,
  40. secretKeyLengthBytes: cryptoPolicy.options.asymmetric.cipher.algorithm
  41. .RSA_KEM.secretKeyLengthBytes.KL_16,
  42. ivLengthBytes: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_KEM
  43. .ivLengthBytes.IVL_12,
  44. tagLengthBytes: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_KEM
  45. .tagLengthBytes.TL_16,
  46. keyDeriver: {
  47. name: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_KEM
  48. .keyDeriver.name.KDF1,
  49. hashAlgorithm: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_KEM
  50. .keyDeriver.hashAlgorithm.SHA256,
  51. },
  52. symmetricCipher: cryptoPolicy.options.asymmetric.cipher.algorithm.RSA_KEM
  53. .symmetricCipher.AES_GCM
  54. }
  55. };
  56. var ENCRYPTED_DATA_JAVA_B64 =
  57. 'SuIt3oT4a98kF+7sReHLxYuVTV8GtspAUf2607aTIdnLkHrN5gYNNeXoOHuVXZtm2AneV165PsRtUpEapGtzaeWpNZS7rhnPU2JjQ26p0PkR0A6nXGX7sn7QlH8kJ/etN7wVilHNGRlM2nvgMpbIhue/X3+OQ6ROYavDfEs9XC8QCJxgD5CvkDJANzfNaybPdf4yZ+QFL90T4ZePEy/PZbIPtZMWIz8xTUkZs4SUbtfph+8tX4o+0b1lCBQlAeqegZQcycN0/JkgsQdRi5l02wrg0lDj4QMFzB9h3uBhzv3Wn4DlN9OQtHYMkPz/9MurRhHkqKJBSkBcTKawTmcA9Q==';
  58. var data_;
  59. var oaepPolicy_;
  60. var kemPolicy_;
  61. var asymmetricEncrypterKem_;
  62. var asymmetricDecrypterKem_;
  63. var asymmetricEncrypterOaep_;
  64. var asymmetricDecrypterOaep_;
  65. beforeAll(function() {
  66. data_ = codec.utf8Encode('Ox2fUJq1gAbX');
  67. oaepPolicy_ = cryptoPolicy.newInstance();
  68. oaepPolicy_.asymmetric.cipher = ASYMMETRIC_CIPHER_OAEP_POLICY;
  69. kemPolicy_ = cryptoPolicy.newInstance();
  70. kemPolicy_.asymmetric.cipher = ASYMMETRIC_CIPHER_KEM_POLICY;
  71. var service = asymmetric.newService({policy: kemPolicy_});
  72. asymmetricEncrypterKem_ = service.newEncrypter().init(PUBLIC_KEY_PEM);
  73. asymmetricDecrypterKem_ = service.newDecrypter().init(PRIVATE_KEY_PEM);
  74. service = asymmetric.newService({policy: oaepPolicy_});
  75. asymmetricEncrypterOaep_ = service.newEncrypter().init(PUBLIC_KEY_PEM);
  76. asymmetricDecrypterOaep_ = service.newDecrypter().init(PRIVATE_KEY_PEM);
  77. });
  78. describe(
  79. 'create an asymmetric encrypter/decrypter pair that should be able to ..',
  80. function() {
  81. it('encrypt and decrypt some data using RSA-KEM and 1024-bit keys',
  82. function() {
  83. var publicKey = forge.pki.publicKeyFromPem(PUBLIC_KEY_1024_PEM);
  84. var privateKey = forge.pki.privateKeyFromPem(PRIVATE_KEY_1024_PEM);
  85. // confirm that the keys have the expected modulus size
  86. expect(publicKey.n.bitLength()).toBe(1024);
  87. expect(privateKey.n.bitLength()).toBe(1024);
  88. var service = asymmetric.newService({policy: kemPolicy_});
  89. var asymmetricEncrypterKem =
  90. service.newEncrypter().init(PUBLIC_KEY_1024_PEM);
  91. var asymmetricDecrypterKem =
  92. service.newDecrypter().init(PRIVATE_KEY_1024_PEM);
  93. // Encrypt data.
  94. var encryptedData = asymmetricEncrypterKem.encrypt(data_);
  95. var decryptedData = asymmetricDecrypterKem.decrypt(encryptedData);
  96. // Verify that data was successfully decrypted.
  97. expect(decryptedData).toEqual(data_);
  98. });
  99. it('encrypt and decrypt some data using RSA-KEM and 2048-bit keys',
  100. function() {
  101. // Encrypt data.
  102. var encryptedData = asymmetricEncrypterKem_.encrypt(data_);
  103. // Decrypt data.
  104. var decryptedData = asymmetricDecrypterKem_.decrypt(encryptedData);
  105. // Verify that data was successfully decrypted.
  106. expect(decryptedData).toEqual(data_);
  107. });
  108. it('encrypt and decrypt some randomly generated data, 512 bytes in size, using RSA-KEM and 2048-bit keys',
  109. function() {
  110. var srs = secureRandom.newService();
  111. var randomGenerator = srs.newRandomGenerator();
  112. var data = randomGenerator.nextBytes(512);
  113. // Encrypt data.
  114. var encryptedData = asymmetricEncrypterKem_.encrypt(data);
  115. // Decrypt data.
  116. var decryptedData = asymmetricDecrypterKem_.decrypt(encryptedData);
  117. // Verify that data was successfully decrypted.
  118. expect(decryptedData).toEqual(data);
  119. });
  120. it('encrypt and decrypt some data, using RSA-OAEP', function() {
  121. // Encrypt data.
  122. var encryptedData = asymmetricEncrypterOaep_.encrypt(data_);
  123. // Decrypt data.
  124. var decryptedData = asymmetricDecrypterOaep_.decrypt(encryptedData);
  125. // Verify that data was successfully decrypted.
  126. expect(decryptedData).toEqual(data_);
  127. });
  128. it('decrypt some data that was encrypted, using RSA-OEAP with Java',
  129. function() {
  130. // Decrypt data.
  131. var decryptedData = asymmetricDecrypterOaep_.decrypt(
  132. codec.base64Decode(ENCRYPTED_DATA_JAVA_B64));
  133. // Verify that data was successfully decrypted.
  134. expect(decryptedData).toEqual(data_);
  135. });
  136. it('throw an error when being created, using the wrong cipher algorithm',
  137. function() {
  138. var policy = cryptoPolicy.newInstance();
  139. policy.asymmetric.cipher.algorithm.name = 'wrong cipher algorithm';
  140. var service = asymmetric.newService({policy: policy});
  141. expect(function() {
  142. service.newEncrypter();
  143. }).toThrow();
  144. expect(function() {
  145. service.newDecrypter();
  146. }).toThrow();
  147. });
  148. it('throw an error when being created, using RSA-KEM with the wrong key derivation function',
  149. function() {
  150. var kemPolicy = clone(kemPolicy_);
  151. kemPolicy.asymmetric.cipher.algorithm.keyDeriver.name =
  152. 'wrong RSA-KEM key derivation function';
  153. var service = asymmetric.newService({policy: kemPolicy});
  154. expect(function() {
  155. service.newEncrypter();
  156. }).toThrow();
  157. expect(function() {
  158. service.newDecrypter();
  159. }).toThrow();
  160. });
  161. it('throw an error when being created, using RSA-KEM with the wrong key derivation function hash algorithm',
  162. function() {
  163. var kemPolicy = clone(kemPolicy_);
  164. kemPolicy.asymmetric.cipher.algorithm.keyDeriver.hashAlgorithm =
  165. 'wrong RSA-KEM key derivation function hash algorithm';
  166. var service = asymmetric.newService({policy: kemPolicy});
  167. expect(function() {
  168. service.newEncrypter();
  169. }).toThrow();
  170. expect(function() {
  171. service.newDecrypter();
  172. }).toThrow();
  173. });
  174. it('throw an error when being created, using RSA-KEM with the wrong symmetric cipher algorithm',
  175. function() {
  176. var kemPolicy = clone(kemPolicy_);
  177. kemPolicy.asymmetric.cipher.algorithm.symmetricCipher =
  178. 'wrong RSA-KEM symmetric cipher algorithm';
  179. var service = asymmetric.newService({policy: kemPolicy});
  180. expect(function() {
  181. service.newEncrypter();
  182. }).toThrow();
  183. expect(function() {
  184. service.newDecrypter();
  185. }).toThrow();
  186. });
  187. it('throw an error when being created, using RSA-OAEP with the wrong hash algorithm',
  188. function() {
  189. var oaepPolicy = clone(oaepPolicy_);
  190. oaepPolicy.asymmetric.cipher.algorithm.hashAlgorithm =
  191. 'wrong RSA-OAEP hash algorithm';
  192. var service = asymmetric.newService({policy: oaepPolicy});
  193. expect(function() {
  194. service.newEncrypter();
  195. }).toThrow();
  196. expect(function() {
  197. service.newDecrypter();
  198. }).toThrow();
  199. });
  200. it('throw an error when being created, using RSA-OAEP with the wrong mask generation function',
  201. function() {
  202. var oaepPolicy = clone(oaepPolicy_);
  203. oaepPolicy.asymmetric.cipher.algorithm.maskGenerator.name =
  204. 'wrong RSA-OAEP mask generation function';
  205. var service = asymmetric.newService({policy: oaepPolicy});
  206. expect(function() {
  207. service.newEncrypter();
  208. }).toThrow();
  209. expect(function() {
  210. service.newDecrypter();
  211. }).toThrow();
  212. });
  213. it('throw an error when being created, using RSA-OAEP with the wrong mask generation function hash algorithm',
  214. function() {
  215. var oaepPolicy = clone(oaepPolicy_);
  216. oaepPolicy.asymmetric.cipher.algorithm.maskGenerator
  217. .hashAlgorithm =
  218. 'wrong RSA-OAEP mask generation function hash algorithm';
  219. var service = asymmetric.newService({policy: oaepPolicy});
  220. expect(function() {
  221. service.newEncrypter();
  222. }).toThrow();
  223. expect(function() {
  224. service.newDecrypter();
  225. }).toThrow();
  226. });
  227. });
  228. function clone(object) {
  229. return JSON.parse(JSON.stringify(object));
  230. }
  231. });