123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /*
- * Copyright 2018 Scytl Secure Electronic Voting SA
- *
- * All rights reserved
- *
- * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
- */
- describe('Secure Random Generator: ', function() {
- 'use strict';
- it('should exist a cryptolib module', function() {
- cryptolib('primitives.securerandom', function(box) {
- expect(box.primitives.securerandom).toBeDefined();
- });
- });
- describe('CryptoScytlRandomBytes: ', function() {
- it('should generate a random array of bytes', function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var srg = secureRandomFactory.getCryptoRandomBytes();
- expect(srg.nextRandom(10)).toBeDefined();
- });
- });
- it('should throw an exception when a negative length is passed',
- function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomBytes();
- var thrownException = false;
- try {
- secureRandom.nextRandom(-1);
- } catch (error) {
- thrownException = true;
- }
- expect(thrownException).toBeTruthy();
- });
- });
- it('should throw an exception when a length that is not a number is passed',
- function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomBytes();
- var thrownException = false;
- try {
- secureRandom.nextRandom('AAA');
- } catch (error) {
- thrownException = true;
- }
- expect(thrownException).toBeTruthy();
- });
- });
- it('should throw an exception when a large length is passed', function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomBytes();
- var thrownException = false;
- try {
- secureRandom.nextRandom(4097);
- } catch (error) {
- thrownException = true;
- }
- expect(thrownException).toBeTruthy();
- });
- });
- it('throw an error when the given provider is wrong.', function() {
- var f = function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var thrownError = false;
- try {
- secureRandomFactory.getCryptoRandomBytes();
- } catch (error) {
- thrownError = true;
- }
- expect(thrownError).toBeTruthy();
- };
- f.policies = {primitives: {secureRandom: {provider: 'WRONG'}}};
- cryptolib('primitives.securerandom', f);
- });
- });
- describe('CryptoScytlRandomInteger: ', function() {
- it('should generate a random BigInteger specifying the number of bytes this number should have',
- function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomInteger();
- var n;
- for (var length = 1; length <= 100; length++) {
- n = ((new box.forge.jsbn.BigInteger('10')).pow(length))
- .subtract(box.forge.jsbn.BigInteger.ONE);
- var randomBigInteger = secureRandom.nextRandom(length);
- expect(randomBigInteger).toBeDefined();
- var greaterOrEqualThanZero = false;
- if (randomBigInteger.compareTo(forge.jsbn.BigInteger.ZERO) >= 0) {
- greaterOrEqualThanZero = true;
- }
- expect(greaterOrEqualThanZero).toBeTruthy();
- var withinRange = false;
- if (randomBigInteger.compareTo(n) <= 0) {
- withinRange = true;
- }
- expect(withinRange).toBeTruthy();
- }
- });
- });
- it('should generate a random BigInteger according to the specified bit length',
- function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomInteger();
- for (var bitLength = 1; bitLength <= 300; bitLength++) {
- var randomBigInteger = secureRandom.nextRandomByBits(bitLength);
- expect(randomBigInteger).toBeDefined();
- expect(randomBigInteger.bitLength() <= bitLength).toBeTruthy();
- }
- });
- });
- it('should throw an exception when a negative length is passed',
- function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomInteger();
- var thrownException = false;
- try {
- secureRandom.nextRandom(-1);
- } catch (error) {
- thrownException = true;
- }
- expect(thrownException).toBeTruthy();
- });
- });
- it('should throw an exception when a length that is not a number is passed',
- function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomInteger();
- var thrownException = false;
- try {
- secureRandom.nextRandom('AAA');
- } catch (error) {
- thrownException = true;
- }
- expect(thrownException).toBeTruthy();
- });
- });
- it('should throw an exception when a large length is passed', function() {
- cryptolib('primitives.securerandom', function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var secureRandom = secureRandomFactory.getCryptoRandomInteger();
- var thrownException = false;
- try {
- secureRandom.nextRandom(513);
- } catch (error) {
- thrownException = true;
- }
- expect(thrownException).toBeTruthy();
- });
- });
- it('throw an error when the given provider is wrong', function() {
- var f = function(box) {
- var secureRandomFactory =
- new box.primitives.securerandom.factory.SecureRandomFactory();
- var thrownError = false;
- try {
- secureRandomFactory.getCryptoRandomInteger();
- } catch (error) {
- thrownError = true;
- }
- expect(thrownError).toBeTruthy();
- };
- f.policies = {primitives: {secureRandom: {provider: 'WRONG'}}};
- cryptolib('primitives.securerandom', f);
- });
- });
- });
|