From 9dd863d9f2bbdf77cbdda7e79d744148376061b9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 15 Sep 2016 13:36:18 -0700 Subject: [PATCH] random: require random from crypto. --- lib/crypto/crypto.js | 10 ++++++++++ lib/crypto/random.js | 23 ++++++++++++++--------- lib/crypto/schnorr.js | 4 ++-- lib/hd/mnemonic.js | 3 +-- lib/hd/private.js | 3 +-- lib/http/server.js | 3 +-- lib/mempool/mempool.js | 4 ++-- lib/net/bip150.js | 5 ++--- lib/wallet/wallet.js | 3 +-- 9 files changed, 34 insertions(+), 24 deletions(-) diff --git a/lib/crypto/crypto.js b/lib/crypto/crypto.js index d4bee3aa..aafa2962 100644 --- a/lib/crypto/crypto.js +++ b/lib/crypto/crypto.js @@ -621,4 +621,14 @@ crypto.randomBytes = random.randomBytes; * @returns {Number} */ +crypto.randomRange = random.randomRange; + +/** + * Generate a random uint32. + * Probably more cryptographically sound than + * `Math.random()`. + * @function + * @returns {Number} + */ + crypto.randomInt = random.randomInt; diff --git a/lib/crypto/random.js b/lib/crypto/random.js index ea1af3b8..6edd08e0 100644 --- a/lib/crypto/random.js +++ b/lib/crypto/random.js @@ -10,7 +10,7 @@ /* jshint worker: true */ -var random, crypto, global; +var randomBytes, crypto, global; try { crypto = require('crypto'); @@ -19,7 +19,7 @@ try { } if (crypto) { - random = function random(n) { + randomBytes = function randomBytes(n) { return crypto.randomBytes(n); }; } else { @@ -34,7 +34,7 @@ if (crypto) { crypto = global.crypto || global.msCrypto; if (crypto && crypto.getRandomValues) { - random = function random(n) { + randomBytes = function randomBytes(n) { var data = new Uint8Array(n); crypto.getRandomValues(data); return new Buffer(data.buffer); @@ -43,7 +43,7 @@ if (crypto) { // Out of luck here. Use bad randomness for now. // Possibly fall back to randy in the future: // https://github.com/deestan/randy - random = function random(n) { + randomBytes = function randomBytes(n) { var data = new Buffer(n); var i; @@ -55,8 +55,12 @@ if (crypto) { } } -function randomInt(min, max) { - var num = random(4).readUInt32LE(0, true); +function randomInt() { + return randomBytes(4).readUInt32LE(0, true); +} + +function randomRange(min, max) { + var num = randomInt(); return Math.floor((num / 0x100000000) * (max - min) + min); } @@ -64,8 +68,9 @@ function randomInt(min, max) { * Expose */ -exports = random; -exports.randomBytes = random; +exports = randomBytes; +exports.randomBytes = randomBytes; exports.randomInt = randomInt; +exports.randomRange = randomRange; -module.exports = random; +module.exports = randomBytes; diff --git a/lib/crypto/schnorr.js b/lib/crypto/schnorr.js index 81f05081..a91cf120 100644 --- a/lib/crypto/schnorr.js +++ b/lib/crypto/schnorr.js @@ -10,7 +10,7 @@ var bn = require('bn.js'); var elliptic = require('elliptic'); var Signature = require('elliptic/lib/elliptic/ec/signature'); var hmacDRBG = require('elliptic/lib/elliptic/hmac-drbg'); -var random = require('./random'); +var crypto = require('./crypto'); var curve = elliptic.ec('secp256k1').curve; var sha256 = require('./crypto').sha256; @@ -109,7 +109,7 @@ schnorr.sign = function sign(msg, key, hash, pubnonce) { throw new Error('Bad private key.'); while (!sig) { - k = new bn(random.randomBytes(32)); + k = new bn(crypto.randomBytes(32)); sig = schnorr._sign(msg, prv, k, hash, pubnonce); } diff --git a/lib/hd/mnemonic.js b/lib/hd/mnemonic.js index 2b5dc397..2573d3bc 100644 --- a/lib/hd/mnemonic.js +++ b/lib/hd/mnemonic.js @@ -9,7 +9,6 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var crypto = require('../crypto/crypto'); -var random = require('../crypto/random'); var assert = utils.assert; var constants = bcoin.constants; var BufferWriter = require('../utils/writer'); @@ -169,7 +168,7 @@ Mnemonic.prototype.toKey = function toKey(passphrase, network) { Mnemonic.prototype.getEntropy = function getEntropy() { if (!this.entropy) - this.entropy = random.randomBytes(this.bits / 8); + this.entropy = crypto.randomBytes(this.bits / 8); assert(this.bits / 8 === this.entropy.length); diff --git a/lib/hd/private.js b/lib/hd/private.js index 1cb33861..12667035 100644 --- a/lib/hd/private.js +++ b/lib/hd/private.js @@ -10,7 +10,6 @@ var bcoin = require('../env'); var utils = require('../utils/utils'); var crypto = require('../crypto/crypto'); var ec = require('../crypto/ec'); -var random = require('../crypto/random'); var assert = utils.assert; var constants = bcoin.constants; var networks = bcoin.networks; @@ -580,7 +579,7 @@ HDPrivateKey.fromKey = function fromKey(key, entropy, network) { HDPrivateKey.generate = function generate(network) { var key = ec.generatePrivateKey(); - var entropy = random.randomBytes(32); + var entropy = crypto.randomBytes(32); return HDPrivateKey.fromKey(key, entropy, network); }; diff --git a/lib/http/server.js b/lib/http/server.js index 510ca5b3..2e1dac4b 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -16,7 +16,6 @@ var http = require('./'); var HTTPBase = http.base; var utils = require('../utils/utils'); var crypto = require('../crypto/crypto'); -var random = require('../crypto/random'); var assert = utils.assert; var RPC; /*= require('./rpc'); - load lazily */ @@ -59,7 +58,7 @@ function HTTPServer(options) { this.rpc = null; if (!this.apiKey) - this.apiKey = utils.toBase58(random.randomBytes(20)); + this.apiKey = utils.toBase58(crypto.randomBytes(20)); assert(typeof this.apiKey === 'string', 'API key must be a string.'); assert(this.apiKey.length <= 200, 'API key must be under 200 bytes.'); diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 7bf10e81..185f9d17 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -14,7 +14,7 @@ var utils = require('../utils/utils'); var assert = utils.assert; var BufferWriter = require('../utils/writer'); var BufferReader = require('../utils/reader'); -var random = require('../crypto/random'); +var crypto = require('../crypto/crypto'); var VerifyError = bcoin.errors.VerifyError; var VerifyResult = utils.VerifyResult; @@ -296,7 +296,7 @@ Mempool.prototype.limitOrphans = function limitOrphans() { var i, hash; while (this.totalOrphans > constants.mempool.MAX_ORPHAN_TX) { - i = random.randomInt(0, orphans.length); + i = crypto.randomRange(0, orphans.length); hash = orphans[i]; orphans.splice(i, 1); diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 1bb2eca4..df8693cf 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -12,7 +12,6 @@ var EventEmitter = require('events').EventEmitter; var bcoin = require('../env'); var utils = require('../utils/utils'); var crypto = require('../crypto/crypto'); -var random = require('../crypto/random'); var assert = utils.assert; var constants = bcoin.constants; @@ -120,7 +119,7 @@ BIP150.prototype.reply = function reply(payload) { throw new Error('Auth failure.'); if (!this.peerIdentity) - return random.randomBytes(32); + return crypto.randomBytes(32); sig = bcoin.ec.toDER(data); msg = this.hash(this.output.sid, type, this.peerIdentity); @@ -128,7 +127,7 @@ BIP150.prototype.reply = function reply(payload) { result = bcoin.ec.verify(msg, sig, this.peerIdentity); if (!result) - return random.randomBytes(32); + return crypto.randomBytes(32); if (this.isAuthed()) { this.auth = true; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 69cfb472..3057893e 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -15,7 +15,6 @@ var crypto = require('../crypto/crypto'); var assert = utils.assert; var BufferReader = require('../utils/reader'); var BufferWriter = require('../utils/writer'); -var random = require('../crypto/random'); var TXDB = require('./txdb'); var Path = require('./path'); @@ -2381,7 +2380,7 @@ MasterKey.prototype.encrypt = function encrypt(passphrase, callback) { return callback(); data = this.key.toExtended(); - iv = random.randomBytes(16); + iv = crypto.randomBytes(16); this.stop();