index-validation.spec.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 secureRandom = require('../lib/index');
  11. var constants = require('../lib/constants');
  12. describe('The secure random module should be able to ...', function() {
  13. var PRIVATE_KEY_PEM =
  14. '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQABAoIBAC/tn34Wf3kE9BGeGc1oFLVDaqqdVVz5/oEpeR2J7q0GnzMFYUpAhzC7WvY52cYsUPyll1Q9Jx0TUTmteo/uvKWQQFfz4nVMeS+2PoXabolBDzuWlsv/1eiRo0FOYHa/3siu8YcQN9X0DpAkpbfTmT1uoZOHZ3EuucMmOFu7vGn38Grw8bSxpR0uvTtnb8ygC+aB51y38RMyhzQQanrM8FMeAfDAy6IB0Yo7b0c50Cxa6Ax4nqn9LXyGakr5WeAMkgTIOA/GId9SZD4e5eRpq+628pOeR4O9datFltgl6r1+A4ii2VrJsDqeatGtODlX6KRKqwFHoGIa2TjgSZLuorECgYEAxeSZDOOgFsI5mB7RkRzZaQ9znJ15sgdyZiAFZAOUah4hSGdAXNAnZTlrdacduXEu3EfkpuPToX7xZSv5FRYwfBwMwCLeytlGLPjQzWejZGbo4+KqgzWb9fECDYVtDPlJ/+yLih9nt67BHweJKxYydl18rVigdVyy22X86NijSykCgYEAqKPUrXZAo+TJvmTw4tgsibJgvXBYBhmsej8mGNQw+Nyp2gV28sgm61ifIeXKS8teq+MFwGA6cHQedbsCqhMHokdhESZmlbWxhSFLihQcewBxwvrBwbaxI23yXRzwMewznZFL032PpcbqrmwFmcSSEZ3nmbvTH6ShqLW+pzDNp6MCgYBQLzdgxJ7qedqSa/JohTMG4e7rh9d2rpPJE7J7ewPZF8pOpx+qO+Gqn2COdJ+Ts2vUcAETKn9nEaPIZc/wnmQY9dioxbhWo0FPGaaphBPtq9Ez/XUv4zoFppk5V1X/isdUPsmvttf00oeIBiqrXbwmv+yz5JRn2Z7TTXjz9Ev+OQKBgQCUuoCMRzl1EgcXIqEL/0kwW6BUEqufHa9u1Ri9Vw6lvL8T6DPipMEmWK9nzuid9gtVns/ovTVtDgv7GuabplLaPQePf4WDzY11c0rSyS/hDyBFrK+LL5uEOqhAlJAGB2HyOj1clWVF+GvrTpuV5LZKUS/79pmZU7G7QCaX/0Ow7wKBgC/kDH7cmWQnWvvJ5izrx/7PogQVPOLELeUIGLu/hjsSdDKiFCxCUZ948+9NuG+DnpXDWzw//r8mPBRRGGsqFws5Aipp7yjQ3kRDCCzGelPCVhHyfmKqA+8ewXPulKS3/wIyHIvaXmsuAtTfurHtpRyzjKmCBK1Y6WQ3trIXvo7s-----END RSA PRIVATE KEY-----';
  15. var PIN_ENTROPY = 100;
  16. var randomGenerator_;
  17. var nonUint8Array_ = [1, 2, 3];
  18. var emptyString_ = '';
  19. var nonString_ = 999;
  20. var nonNumber_ = 'not a number';
  21. var nonPositiveNumber_ = 0;
  22. var nonObject_ = 0;
  23. var nonBoolean_ = 'not a boolean';
  24. beforeEach(function() {
  25. randomGenerator_ = secureRandom.newService().newRandomGenerator();
  26. });
  27. describe('create a secure random service that should be able to ..', function() {
  28. it('throw an exception when being created, using an invalid seed',
  29. function() {
  30. expect(function() {
  31. secureRandom.newService({prngSeed: null});
  32. }).toThrow();
  33. expect(function() {
  34. secureRandom.newService({prngSeed: nonUint8Array_});
  35. }).toThrow();
  36. });
  37. it('throw an exception when being created, using an invalid maximum entropy counter',
  38. function() {
  39. expect(function() {
  40. secureRandom.newService({maxEntropyCounter: null});
  41. }).toThrow();
  42. expect(function() {
  43. secureRandom.newService({maxEntropyCounter: nonNumber_});
  44. }).toThrow();
  45. expect(function() {
  46. secureRandom.newService({maxEntropyCounter: nonPositiveNumber_});
  47. }).toThrow();
  48. });
  49. it('throw an exception when being created, using an invalid hash updater interval',
  50. function() {
  51. expect(function() {
  52. secureRandom.newService({hashUpdaterInterval: null});
  53. }).toThrow();
  54. expect(function() {
  55. secureRandom.newService({hashUpdaterInterval: nonNumber_});
  56. }).toThrow();
  57. expect(function() {
  58. secureRandom.newService({hashUpdaterInterval: nonPositiveNumber_});
  59. }).toThrow();
  60. });
  61. it('throw an exception when being created, using an invalid Window object',
  62. function() {
  63. expect(function() {
  64. secureRandom.newService({windowObject: null});
  65. }).toThrow();
  66. expect(function() {
  67. secureRandom.newService({windowObject: nonObject_});
  68. }).toThrow();
  69. });
  70. it('throw an exception when being created, using an invalid Crypto API object',
  71. function() {
  72. expect(function() {
  73. secureRandom.newService({cryptoApi: null});
  74. }).toThrow();
  75. expect(function() {
  76. secureRandom.newService({cryptoApi: nonObject_});
  77. }).toThrow();
  78. });
  79. it('throw an exception when being created, using private key entropy collector data containing an invalid private key',
  80. function() {
  81. expect(function() {
  82. secureRandom.newService({privateKeyData: {pinEntropy: PIN_ENTROPY}});
  83. }).toThrow();
  84. expect(function() {
  85. secureRandom.newService({
  86. privateKeyData: {privateKey: undefined, pinEntropy: PIN_ENTROPY}
  87. });
  88. }).toThrow();
  89. expect(function() {
  90. secureRandom.newService(
  91. {privateKeyData: {privateKey: null, pinEntropy: PIN_ENTROPY}});
  92. }).toThrow();
  93. expect(function() {
  94. secureRandom.newService({
  95. privateKeyData:
  96. {privateKey: nonString_, pinEntropy: PIN_ENTROPY}
  97. });
  98. }).toThrow();
  99. expect(function() {
  100. secureRandom.newService({
  101. privateKeyData:
  102. {privateKey: emptyString_, pinEntropy: PIN_ENTROPY}
  103. });
  104. }).toThrow();
  105. });
  106. it('throw an exception when being created, using private key entropy collector data containing invalid PIN entropy',
  107. function() {
  108. expect(function() {
  109. secureRandom.newService(
  110. {privateKeyData: {privateKey: PRIVATE_KEY_PEM}});
  111. }).toThrow();
  112. expect(function() {
  113. secureRandom.newService({
  114. privateKeyData:
  115. {privateKey: PRIVATE_KEY_PEM, pinEntropy: undefined}
  116. });
  117. }).toThrow();
  118. expect(function() {
  119. secureRandom.newService({
  120. privateKeyData: {privateKey: PRIVATE_KEY_PEM, pinEntropy: null}
  121. });
  122. }).toThrow();
  123. expect(function() {
  124. secureRandom.newService({
  125. privateKeyData:
  126. {privateKey: PRIVATE_KEY_PEM, pinEntropy: nonNumber_}
  127. });
  128. }).toThrow();
  129. expect(function() {
  130. secureRandom.newService({
  131. privateKeyData: {
  132. privateKey: PRIVATE_KEY_PEM,
  133. pinEntropy: nonPositiveNumber_
  134. }
  135. });
  136. }).toThrow();
  137. });
  138. it('throw an exception when being created, usinsg an invalid noDefaultCollectors flag',
  139. function() {
  140. expect(function() {
  141. secureRandom.newService({noDefaultCollectors: null});
  142. }).toThrow();
  143. expect(function() {
  144. secureRandom.newService({noDefaultCollectors: nonBoolean_});
  145. }).toThrow();
  146. });
  147. describe('create a secure random generator that should be able to', function() {
  148. it('throw an exception when generating random bytes, using invalid input data',
  149. function() {
  150. expect(function() {
  151. randomGenerator_.nextBytes();
  152. }).toThrow();
  153. expect(function() {
  154. randomGenerator_.nextBytes(undefined);
  155. }).toThrow();
  156. expect(function() {
  157. randomGenerator_.nextBytes(null);
  158. }).toThrow();
  159. expect(function() {
  160. randomGenerator_.nextBytes(nonNumber_);
  161. }).toThrow();
  162. expect(function() {
  163. randomGenerator_.nextBytes(nonPositiveNumber_);
  164. }).toThrow();
  165. expect(function() {
  166. randomGenerator_.nextBytes(
  167. constants.SECURE_RANDOM_MAX_NUM_BYTES + 1);
  168. }).toThrow();
  169. });
  170. it('throw an exception when generating a random BigInteger by number of bits, using invalid input data',
  171. function() {
  172. expect(function() {
  173. randomGenerator_.nextBigInteger();
  174. }).toThrow();
  175. expect(function() {
  176. randomGenerator_.nextBigInteger(undefined);
  177. }).toThrow();
  178. expect(function() {
  179. randomGenerator_.nextBigInteger(null);
  180. }).toThrow();
  181. expect(function() {
  182. randomGenerator_.nextBigInteger(nonNumber_);
  183. }).toThrow();
  184. expect(function() {
  185. randomGenerator_.nextBigInteger(nonPositiveNumber_);
  186. }).toThrow();
  187. });
  188. it('throw an exception when generating a random BigInteger by number of digits, using invalid input data',
  189. function() {
  190. expect(function() {
  191. randomGenerator_.nextBigIntegerByDigits();
  192. }).toThrow();
  193. expect(function() {
  194. randomGenerator_.nextBigIntegerByDigits(undefined);
  195. }).toThrow();
  196. expect(function() {
  197. randomGenerator_.nextBigIntegerByDigits(null);
  198. }).toThrow();
  199. expect(function() {
  200. randomGenerator_.nextBigIntegerByDigits(nonNumber_);
  201. }).toThrow();
  202. expect(function() {
  203. randomGenerator_.nextBigIntegerByDigits(nonPositiveNumber_);
  204. }).toThrow();
  205. expect(function() {
  206. randomGenerator_.nextBigIntegerByDigits(
  207. constants.SECURE_RANDOM_MAX_NUM_DIGITS + 1);
  208. }).toThrow();
  209. });
  210. });
  211. });
  212. });