index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 */
  9. 'use strict';
  10. var SecureRandomService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new SecureRandomService object, which encapsulates a secure
  15. * random service.
  16. *
  17. * <p>
  18. * As soon as this service is instantiated, entropy collection will start.
  19. * The first time that a random generator created by the service is used,
  20. * the entropy collection will stop and the pseudo-random number generator
  21. * (PRNG) of the service will be initialized with the collected entropy.
  22. *
  23. * @function newService
  24. * @global
  25. * @param {Object}
  26. * [options] An object containing optional arguments.
  27. * @param {Uint8Array}
  28. * [options.prngSeed=Internally generated] The seed to initialize
  29. * the PRNG.
  30. * @param {number}
  31. * [options.maxEntropyCounter=256] The amount of entropy to
  32. * accumulate before stopping collectors. It must be a positive
  33. * number.
  34. * @param {number}
  35. * [options.hashUpdaterInterval=1000] The interval (in
  36. * milliseconds) at which entropy data is to be hashed. It must be a postive
  37. * number.
  38. * @param {Object}
  39. * [options.windowObject=Built-in window object]
  40. * The <code>window</code> object provided to the collectors.
  41. * @param {Object}
  42. * [options.cryptoApi=Built-in cryptographic library]
  43. * The cryptographic library supplied to the WebKit collector (if
  44. * the latter is being used).
  45. * @param {Object}
  46. * [options.privateKeyData=Not used] The private key collector
  47. * data.
  48. * @param {string}
  49. * options.privateKeyData.privateKey The private key, in PEM
  50. * format.
  51. * @param {number}
  52. * options.privateKeyData.pinEntropy The PIN entropy of the
  53. * private key. It must be a positive number.
  54. * @param {boolean}
  55. * [options.noDefaultCollectors=false] If <code>true</code>, no
  56. * default entropy collectors are to be used.
  57. * @returns {SecureRandomService} The new SecureRandomService object.
  58. * @example <caption> How to initialize the PRNG of the service with a
  59. * chosen seed</caption>
  60. *
  61. * var secureRandom = require('scytl-securerandom');
  62. *
  63. * var secureRandomService = secureRandom.newService({prngSeed: myPrngSeed});
  64. */
  65. newService: function(options) {
  66. checkData(options);
  67. return new SecureRandomService(options);
  68. }
  69. };
  70. function checkData(options) {
  71. options = options || {};
  72. if (typeof options.prngSeed !== 'undefined') {
  73. validator.checkIsInstanceOf(
  74. options.prngSeed, Uint8Array, 'Uint8Array', 'Seed to initialize PRNG');
  75. }
  76. if (typeof options.maxEntropyCounter !== 'undefined') {
  77. validator.checkIsPositiveNumber(
  78. options.maxEntropyCounter,
  79. 'Amount of entropy to accumulate before stopping collectors');
  80. }
  81. if (typeof options.hashUpdaterInterval !== 'undefined') {
  82. validator.checkIsPositiveNumber(
  83. options.hashUpdaterInterval,
  84. 'Interval (in milliseconds) at which entropy data is to be hashed');
  85. }
  86. if (typeof options.windowObject !== 'undefined') {
  87. validator.checkIsNotNull(
  88. options.windowObject, 'Window object provided to collectors');
  89. validator.checkIsObject(
  90. options.windowObject, 'Window object provided to collectors');
  91. }
  92. if (typeof options.cryptoApi !== 'undefined') {
  93. validator.checkIsNotNull(
  94. options.cryptoApi,
  95. 'Cryptographic library supplied to WebKit collector');
  96. validator.checkIsObject(
  97. options.cryptoApi,
  98. 'Cryptographic library supplied to WebKit collector');
  99. }
  100. if (typeof options.privateKeyData !== 'undefined') {
  101. validator.checkIsNonEmptyString(
  102. options.privateKeyData.privateKey, 'private key, in PEM format');
  103. validator.checkIsPositiveNumber(
  104. options.privateKeyData.pinEntropy, 'PIN entropy of private key.');
  105. }
  106. if (typeof options.noDefaultCollectors !== 'undefined') {
  107. validator.checkIsNotNull(
  108. options.noDefaultCollectors,
  109. 'Flag to specify whether no default entropy collectors are to be used');
  110. validator.checkIsType(
  111. options.noDefaultCollectors, 'boolean',
  112. 'Flag to specify whether no default entropy collectors are to be used');
  113. }
  114. }