diff --git a/lib/jwcrypto.js b/lib/jwcrypto.js index 3a2a6e9..c709a28 100644 --- a/lib/jwcrypto.js +++ b/lib/jwcrypto.js @@ -9,9 +9,13 @@ var algs = require("./algs/index"), utils = require("./utils"), delay = utils.delay, + rng = require("./rng"), libs = require("../libs/minimal"); -var RNG = new libs.SecureRandom(); +var RNG = new rng.RNG(); + +// start autoseeding now +RNG.autoseed(); function NoSuchAlgorithmException(message) { this.message = message; @@ -137,9 +141,9 @@ exports.decrypt = function(encryptedPayload, encryptionAndMACKeys, cb) { }; -// rng +// entropy here is a string that is expected to be relatively high entropy exports.addEntropy = function(entropy) { - // do something! FIXME XXX + RNG.addEntropy(entropy); }; exports.assertion = require("./assertion"); diff --git a/lib/rng.js b/lib/rng.js new file mode 100644 index 0000000..6dad8cd --- /dev/null +++ b/lib/rng.js @@ -0,0 +1,80 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * abstract out RNG depending on client or server. + * + * auto-seeding has to be requested. + * (the seed is automatic, not the decision to auto-seed.) + * + * nextBytes takes a byteArray as input and populates it, + * because that's how the cool kids do it and so we will not bikeshed. + */ + +var utils = require("./utils"), + delay = utils.delay, + libs = require("../libs/minimal"), + sjcl = libs.sjcl; + +// detect if we have native crypto support +var crypto = null; +try { + crypto = require("crypto"); +} catch(e) {} + +// proper boolean for whether we have native support +var IS_NATIVE = !!crypto; + +function NativeRNG() { +} + +NativeRNG.prototype = { + addEntropy: function(seed_in) { + // do nothing, natively we don't care + }, + autoseed: function(cb) { + // yay, don't need to do anything + if (cb) + delay(cb)(); + }, + nextBytes: function(byteArray) { + var randomBytes = crypto.randomBytes(byteArray.length); + for (var i=0; i +