signer.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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, mocha: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. describe('The asymmetric cryptography service should be able to ...', function() {
  15. var PUBLIC_KEY_PEM =
  16. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----';
  17. var PRIVATE_KEY_PEM =
  18. '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+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/wnmQY9dioxbhWo0FPGaaphBPtq9Ez/XUv4zoFppk5V1X/isdUPsmvttf00oeIBiqrXbwmv+yz5JRn2Z7TTXjz9Ev+OQKBgQCUuoCMRzl1EgcXIqEL/0kwW6BUEqufHa9u1Ri9Vw6lvL8T6DPipMEmWK9nzuid9gtVns/ovTVtDgv7GuabplLaPQePf4WDzY11c0rSyS/hDyBFrK+LL5uEOqhAlJAGB2HyOj1clWVF+GvrTpuV5LZKUS/79pmZU7G7QCaX/0Ow7wKBgC/kDH7cmWQnWvvJ5izrx/7PogQVPOLELeUIGLu/hjsSdDKiFCxCUZ948+9NuG+DnpXDWzw//r8mPBRRGGsqFws5Aipp7yjQ3kRDCCzGelPCVhHyfmKqA+8ewXPulKS3/wIyHIvaXmsuAtTfurHtpRyzjKmCBK1Y6WQ3trIXvo7s-----END RSA PRIVATE KEY-----';
  19. var DATA_FROM_JAVA = 'This is a string from Java.';
  20. var signer_;
  21. var sigVerifier_;
  22. var policy_;
  23. var data_;
  24. var signature_;
  25. var invalidKeyPair_;
  26. beforeAll(function() {
  27. var service = asymmetric.newService();
  28. signer_ = service.newSigner().init(PRIVATE_KEY_PEM);
  29. sigVerifier_ = service.newSignatureVerifier().init(PUBLIC_KEY_PEM);
  30. policy_ = cryptoPolicy.newInstance();
  31. data_ = codec.utf8Encode('Ox2fUJq1gAbX');
  32. signature_ = signer_.sign(data_);
  33. policy_.asymmetric.keyPair.encryption.keyLengthBits = 128;
  34. policy_.asymmetric.keyPair.encryption.publicExponent = 1;
  35. service = asymmetric.newService({policy: policy_});
  36. var keyPairGenerator = service.newKeyPairGenerator();
  37. invalidKeyPair_ = keyPairGenerator.next();
  38. });
  39. beforeEach(function() {
  40. policy_ = cryptoPolicy.newInstance();
  41. });
  42. describe('create a signer/signature verifier pair that should be able to ..', function() {
  43. it('sign some data and verify the signature, using a specified secure random service object',
  44. function() {
  45. var service = asymmetric.newService(
  46. {secureRandomService: secureRandom.newService()});
  47. var signer = service.newSigner().init(PRIVATE_KEY_PEM);
  48. var sigVerifier = service.newSignatureVerifier().init(PUBLIC_KEY_PEM);
  49. var signature = signer.sign(data_);
  50. expect(sigVerifier.verify(signature, data_)).toBeTruthy();
  51. });
  52. it('sign some data and verify the signature, using RSA and SHA256',
  53. function() {
  54. var policy = cryptoPolicy.newInstance();
  55. policy.asymmetric.signer = {
  56. algorithm: 'RSA',
  57. hashAlgorithm: 'SHA256',
  58. publicExponent: 65537,
  59. };
  60. var service = asymmetric.newService({policy: policy});
  61. var signer = service.newSigner().init(PRIVATE_KEY_PEM);
  62. var sigVerifier = service.newSignatureVerifier().init(PUBLIC_KEY_PEM);
  63. var signature = signer.sign(data_);
  64. expect(sigVerifier.verify(signature, data_)).toBeTruthy();
  65. });
  66. it('sign some data and verify the signature, using RSA, SHA256 and RSA-PSS padding',
  67. function() {
  68. var policy = cryptoPolicy.newInstance();
  69. policy.asymmetric.signer = {
  70. algorithm: 'RSA',
  71. hashAlgorithm: 'SHA256',
  72. padding: {
  73. name: 'PSS',
  74. hashAlgorithm: 'SHA256',
  75. maskGenerator: {name: 'MGF1', hashAlgorithm: 'SHA256'},
  76. saltLengthBytes: 32
  77. },
  78. publicExponent: 65537,
  79. };
  80. var service = asymmetric.newService({policy: policy});
  81. var signer = service.newSigner().init(PRIVATE_KEY_PEM);
  82. var sigVerifier = service.newSignatureVerifier().init(PUBLIC_KEY_PEM);
  83. var signature = signer.sign(data_);
  84. expect(sigVerifier.verify(signature, data_)).toBeTruthy();
  85. expect(sigVerifier.verify(signature, codec.utf8Encode('')))
  86. .toBeFalsy();
  87. });
  88. it('sign an empty string and verify the signature, using SHA256 with RSA',
  89. function() {
  90. var data = codec.utf8Encode('');
  91. var signature = signer_.sign(data);
  92. expect(sigVerifier_.verify(signature, data)).toBeTruthy();
  93. });
  94. it('verify a signature that was generated using Java', function() {
  95. // Signature generated using Java.
  96. var signatureB64 =
  97. 'KuQPmBRE/JpNQ5mvh1Y1LvW3Jr/ZYlYqOgdbgXLVZp+gjVtXHDgFZzIrC8+S+VdTs9WINjxN8aIZlbiemZ+YvXZ7fDOoJsZJRboyrFN9VGz6sWWVXAv28Xj1a2lPNlys8dO3uY0s5R77kDCvBHV4nJh0Hq2Ry+JRjRcrlPsIiDWzm9iTY7V6XzkVGCo58qWyQtloC0s73i8UM6V2nYu1oGDuHqUcvDbstElGAJrn7u4QtP2Kgzd1GSkvbSKhsjR2RniiJ7cD278i27NAEPXY+qqmWQ4c+zt3BKG4Aj4EtKa/4AyARmLC78KjZpfiGPTUzQZbMQudAMmKgSTQhx7ONg==';
  98. // Policy used to generate signature.
  99. var policy = cryptoPolicy.newInstance();
  100. policy.asymmetric.signer = {
  101. algorithm: 'RSA',
  102. hashAlgorithm: 'SHA256',
  103. publicExponent: 65537,
  104. };
  105. var service = asymmetric.newService({policy: policy});
  106. var sigVerifier = service.newSignatureVerifier().init(PUBLIC_KEY_PEM);
  107. // Verify that digital signature was successfully verified.
  108. expect(sigVerifier.verify(
  109. codec.base64Decode(signatureB64),
  110. codec.utf8Encode(DATA_FROM_JAVA)))
  111. .toBeTruthy();
  112. });
  113. it('verify a signature that was generated using Java and RSA-PSS padding',
  114. function() {
  115. // Signature generated in Java.
  116. var signatureB64 =
  117. 'JSYZ0i3Yz3L8J3bUOLOP/Kk83qkhoZ9NqBm9eUpaFoZY8gD42XHJxXyPmDYMnDFMr+J5GypfS9vheYtgn2hag8n3DcG86I80UCNuEZLEhyXpIsN4mv3rDb/xjMXlnDLWiK6MTBTDVImKfwpX6x/xw9YtcG9ADACRefH4++H9fhpg8yXUfT9OacOPHgjoW/y4hJmaKVk+IVDgLAKfToNWwVm2xU0SnFryQ9PUJeR8KHttBLR+gpZZpn+bQMcEvikw8bzAYGX4yXxGB9UgoIsz6xNyxY6P6UyJSal/JajURLuSwW7JpxjtthbyKS6SUXtH+NEg36ZpMhd8QC6EPzwrlQ==';
  118. // Policy used to generate signature.
  119. var policy = cryptoPolicy.newInstance();
  120. policy.asymmetric.signer = {
  121. algorithm: 'RSA',
  122. hashAlgorithm: 'SHA256',
  123. padding: {
  124. name: 'PSS',
  125. hashAlgorithm: 'SHA256',
  126. maskGenerator: {name: 'MGF1', hashAlgorithm: 'SHA256'},
  127. saltLengthBytes: 32
  128. },
  129. publicExponent: 65537,
  130. };
  131. var service = asymmetric.newService({policy: policy});
  132. var sigVerifier = service.newSignatureVerifier().init(PUBLIC_KEY_PEM);
  133. // Verify that digital signature was successfully verified.
  134. expect(sigVerifier.verify(
  135. codec.base64Decode(signatureB64),
  136. codec.utf8Encode(DATA_FROM_JAVA)))
  137. .toBeTruthy();
  138. });
  139. it('throw an error when being created, using the wrong signing algorithm',
  140. function() {
  141. policy_.asymmetric.signer.algorithm =
  142. 'Wrong digital signature algorithm';
  143. var service = asymmetric.newService({policy: policy_});
  144. expect(function() {
  145. service.newSigner();
  146. }).toThrow();
  147. expect(function() {
  148. service.newSignatureVerifier();
  149. }).toThrow();
  150. });
  151. it('throw an error when being created, using the wrong hash algorithm',
  152. function() {
  153. policy_.asymmetric.signer.hashAlgorithm =
  154. 'Wrong signer hash algorithm';
  155. var service = asymmetric.newService({policy: policy_});
  156. expect(function() {
  157. service.newSigner();
  158. }).toThrow();
  159. expect(function() {
  160. service.newSignatureVerifier();
  161. }).toThrow();
  162. });
  163. it('throw an error when being created, using the wrong padding name',
  164. function() {
  165. policy_.asymmetric.signer.padding.name = 'Wrong signer padding name';
  166. var service = asymmetric.newService({policy: policy_});
  167. expect(function() {
  168. service.newSigner();
  169. }).toThrow();
  170. expect(function() {
  171. service.newSignatureVerifier();
  172. }).toThrow();
  173. });
  174. it('throw an error when being created, using the wrong padding hash algorithm',
  175. function() {
  176. policy_.asymmetric.signer.padding.hashAlgorithm =
  177. 'Wrong signer padding hash algorithm';
  178. var service = asymmetric.newService({policy: policy_});
  179. expect(function() {
  180. service.newSigner();
  181. }).toThrow();
  182. expect(function() {
  183. service.newSignatureVerifier();
  184. }).toThrow();
  185. });
  186. it('throw an error when being created, using the wrong padding mask generation function',
  187. function() {
  188. policy_.asymmetric.signer.padding.maskGenerator.name =
  189. 'Wrong signer padding mask generation function';
  190. var service = asymmetric.newService({policy: policy_});
  191. expect(function() {
  192. service.newSigner();
  193. }).toThrow();
  194. expect(function() {
  195. service.newSignatureVerifier();
  196. }).toThrow();
  197. });
  198. it('throw an error when being created, using the wrong padding mask generation function hash algorithm',
  199. function() {
  200. policy_.asymmetric.signer.padding.maskGenerator.hashAlgorithm =
  201. 'Wrong signer padding mask generation function hash algorithm';
  202. var service = asymmetric.newService({policy: policy_});
  203. expect(function() {
  204. service.newSigner();
  205. }).toThrow();
  206. expect(function() {
  207. service.newSignatureVerifier();
  208. }).toThrow();
  209. });
  210. it('throw an error when signing data, using a private key with the wrong public exponent',
  211. function() {
  212. expect(function() {
  213. signer_.sign(invalidKeyPair_.getPrivateKey().getPem(), data_);
  214. }).toThrow();
  215. });
  216. it('throw an error when verifying a signature, using a public key with the wrong public exponent',
  217. function() {
  218. expect(function() {
  219. sigVerifier_.verify(
  220. signature_, invalidKeyPair_.getPublicKey().getPem(), data_);
  221. }).toThrow();
  222. });
  223. });
  224. });