primitives.securerandom.spec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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('Secure Random Generator: ', function() {
  9. 'use strict';
  10. it('should exist a cryptolib module', function() {
  11. cryptolib('primitives.securerandom', function(box) {
  12. expect(box.primitives.securerandom).toBeDefined();
  13. });
  14. });
  15. describe('CryptoScytlRandomBytes: ', function() {
  16. it('should generate a random array of bytes', function() {
  17. cryptolib('primitives.securerandom', function(box) {
  18. var secureRandomFactory =
  19. new box.primitives.securerandom.factory.SecureRandomFactory();
  20. var srg = secureRandomFactory.getCryptoRandomBytes();
  21. expect(srg.nextRandom(10)).toBeDefined();
  22. });
  23. });
  24. it('should throw an exception when a negative length is passed',
  25. function() {
  26. cryptolib('primitives.securerandom', function(box) {
  27. var secureRandomFactory =
  28. new box.primitives.securerandom.factory.SecureRandomFactory();
  29. var secureRandom = secureRandomFactory.getCryptoRandomBytes();
  30. var thrownException = false;
  31. try {
  32. secureRandom.nextRandom(-1);
  33. } catch (error) {
  34. thrownException = true;
  35. }
  36. expect(thrownException).toBeTruthy();
  37. });
  38. });
  39. it('should throw an exception when a length that is not a number is passed',
  40. function() {
  41. cryptolib('primitives.securerandom', function(box) {
  42. var secureRandomFactory =
  43. new box.primitives.securerandom.factory.SecureRandomFactory();
  44. var secureRandom = secureRandomFactory.getCryptoRandomBytes();
  45. var thrownException = false;
  46. try {
  47. secureRandom.nextRandom('AAA');
  48. } catch (error) {
  49. thrownException = true;
  50. }
  51. expect(thrownException).toBeTruthy();
  52. });
  53. });
  54. it('should throw an exception when a large length is passed', function() {
  55. cryptolib('primitives.securerandom', function(box) {
  56. var secureRandomFactory =
  57. new box.primitives.securerandom.factory.SecureRandomFactory();
  58. var secureRandom = secureRandomFactory.getCryptoRandomBytes();
  59. var thrownException = false;
  60. try {
  61. secureRandom.nextRandom(4097);
  62. } catch (error) {
  63. thrownException = true;
  64. }
  65. expect(thrownException).toBeTruthy();
  66. });
  67. });
  68. it('throw an error when the given provider is wrong.', function() {
  69. var f = function(box) {
  70. var secureRandomFactory =
  71. new box.primitives.securerandom.factory.SecureRandomFactory();
  72. var thrownError = false;
  73. try {
  74. secureRandomFactory.getCryptoRandomBytes();
  75. } catch (error) {
  76. thrownError = true;
  77. }
  78. expect(thrownError).toBeTruthy();
  79. };
  80. f.policies = {primitives: {secureRandom: {provider: 'WRONG'}}};
  81. cryptolib('primitives.securerandom', f);
  82. });
  83. });
  84. describe('CryptoScytlRandomInteger: ', function() {
  85. it('should generate a random BigInteger specifying the number of bytes this number should have',
  86. function() {
  87. cryptolib('primitives.securerandom', function(box) {
  88. var secureRandomFactory =
  89. new box.primitives.securerandom.factory.SecureRandomFactory();
  90. var secureRandom = secureRandomFactory.getCryptoRandomInteger();
  91. var n;
  92. for (var length = 1; length <= 100; length++) {
  93. n = ((new box.forge.jsbn.BigInteger('10')).pow(length))
  94. .subtract(box.forge.jsbn.BigInteger.ONE);
  95. var randomBigInteger = secureRandom.nextRandom(length);
  96. expect(randomBigInteger).toBeDefined();
  97. var greaterOrEqualThanZero = false;
  98. if (randomBigInteger.compareTo(forge.jsbn.BigInteger.ZERO) >= 0) {
  99. greaterOrEqualThanZero = true;
  100. }
  101. expect(greaterOrEqualThanZero).toBeTruthy();
  102. var withinRange = false;
  103. if (randomBigInteger.compareTo(n) <= 0) {
  104. withinRange = true;
  105. }
  106. expect(withinRange).toBeTruthy();
  107. }
  108. });
  109. });
  110. it('should generate a random BigInteger according to the specified bit length',
  111. function() {
  112. cryptolib('primitives.securerandom', function(box) {
  113. var secureRandomFactory =
  114. new box.primitives.securerandom.factory.SecureRandomFactory();
  115. var secureRandom = secureRandomFactory.getCryptoRandomInteger();
  116. for (var bitLength = 1; bitLength <= 300; bitLength++) {
  117. var randomBigInteger = secureRandom.nextRandomByBits(bitLength);
  118. expect(randomBigInteger).toBeDefined();
  119. expect(randomBigInteger.bitLength() <= bitLength).toBeTruthy();
  120. }
  121. });
  122. });
  123. it('should throw an exception when a negative length is passed',
  124. function() {
  125. cryptolib('primitives.securerandom', function(box) {
  126. var secureRandomFactory =
  127. new box.primitives.securerandom.factory.SecureRandomFactory();
  128. var secureRandom = secureRandomFactory.getCryptoRandomInteger();
  129. var thrownException = false;
  130. try {
  131. secureRandom.nextRandom(-1);
  132. } catch (error) {
  133. thrownException = true;
  134. }
  135. expect(thrownException).toBeTruthy();
  136. });
  137. });
  138. it('should throw an exception when a length that is not a number is passed',
  139. function() {
  140. cryptolib('primitives.securerandom', function(box) {
  141. var secureRandomFactory =
  142. new box.primitives.securerandom.factory.SecureRandomFactory();
  143. var secureRandom = secureRandomFactory.getCryptoRandomInteger();
  144. var thrownException = false;
  145. try {
  146. secureRandom.nextRandom('AAA');
  147. } catch (error) {
  148. thrownException = true;
  149. }
  150. expect(thrownException).toBeTruthy();
  151. });
  152. });
  153. it('should throw an exception when a large length is passed', function() {
  154. cryptolib('primitives.securerandom', function(box) {
  155. var secureRandomFactory =
  156. new box.primitives.securerandom.factory.SecureRandomFactory();
  157. var secureRandom = secureRandomFactory.getCryptoRandomInteger();
  158. var thrownException = false;
  159. try {
  160. secureRandom.nextRandom(513);
  161. } catch (error) {
  162. thrownException = true;
  163. }
  164. expect(thrownException).toBeTruthy();
  165. });
  166. });
  167. it('throw an error when the given provider is wrong', function() {
  168. var f = function(box) {
  169. var secureRandomFactory =
  170. new box.primitives.securerandom.factory.SecureRandomFactory();
  171. var thrownError = false;
  172. try {
  173. secureRandomFactory.getCryptoRandomInteger();
  174. } catch (error) {
  175. thrownError = true;
  176. }
  177. expect(thrownError).toBeTruthy();
  178. };
  179. f.policies = {primitives: {secureRandom: {provider: 'WRONG'}}};
  180. cryptolib('primitives.securerandom', f);
  181. });
  182. });
  183. });