From 1f875faf388bd5ced2fc3f6626e30c24d5f7fa6d Mon Sep 17 00:00:00 2001 From: Dorian Musaj Date: Tue, 16 Jun 2020 09:49:08 +0200 Subject: [PATCH] additional param randomBytes --- nacl-fast.js | 60 ++++++++++++---------------------------------------- nacl.js | 60 ++++++++++++---------------------------------------- 2 files changed, 26 insertions(+), 94 deletions(-) diff --git a/nacl-fast.js b/nacl-fast.js index 7ea5fb58..43e36061 100644 --- a/nacl-fast.js +++ b/nacl-fast.js @@ -13,8 +13,16 @@ var gf = function(init) { return r; }; +function cleanup(arr) { + for (var i = 0; i < arr.length; i++) arr[i] = 0; +} + // Pluggable, initialized in high-level API below. -var randombytes = function(/* x, n */) { throw new Error('no PRNG'); }; +var randombytes = function(x, n , randomBytes) { + var i, v = randomBytes; + for (i = 0; i < n; i++) x[i] = v[i]; + cleanup(v); + }; var _0 = new Uint8Array(16); var _9 = new Uint8Array(32); _9[0] = 9; @@ -1378,8 +1386,8 @@ function crypto_scalarmult_base(q, n) { return crypto_scalarmult(q, n, _9); } -function crypto_box_keypair(y, x) { - randombytes(x, 32); +function crypto_box_keypair(y, x, randomBytes) { + randombytes(x, 32, randomBytes); return crypto_scalarmult_base(y, x); } @@ -2166,16 +2174,6 @@ function checkArrayTypes() { } } -function cleanup(arr) { - for (var i = 0; i < arr.length; i++) arr[i] = 0; -} - -nacl.randomBytes = function(n) { - var b = new Uint8Array(n); - randombytes(b, n); - return b; -}; - nacl.secretbox = function(msg, nonce, key) { checkArrayTypes(msg, nonce, key); checkLengths(key, nonce); @@ -2243,10 +2241,10 @@ nacl.box.open = function(msg, nonce, publicKey, secretKey) { nacl.box.open.after = nacl.secretbox.open; -nacl.box.keyPair = function() { +nacl.box.keyPair = function(randomBytes) { var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); var sk = new Uint8Array(crypto_box_SECRETKEYBYTES); - crypto_box_keypair(pk, sk); + crypto_box_keypair(pk, sk, randomBytes); return {publicKey: pk, secretKey: sk}; }; @@ -2356,36 +2354,4 @@ nacl.verify = function(x, y) { return (vn(x, 0, y, 0, x.length) === 0) ? true : false; }; -nacl.setPRNG = function(fn) { - randombytes = fn; -}; - -(function() { - // Initialize PRNG if environment provides CSPRNG. - // If not, methods calling randombytes will throw. - var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null; - if (crypto && crypto.getRandomValues) { - // Browsers. - var QUOTA = 65536; - nacl.setPRNG(function(x, n) { - var i, v = new Uint8Array(n); - for (i = 0; i < n; i += QUOTA) { - crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))); - } - for (i = 0; i < n; i++) x[i] = v[i]; - cleanup(v); - }); - } else if (typeof require !== 'undefined') { - // Node.js. - crypto = require('crypto'); - if (crypto && crypto.randomBytes) { - nacl.setPRNG(function(x, n) { - var i, v = crypto.randomBytes(n); - for (i = 0; i < n; i++) x[i] = v[i]; - cleanup(v); - }); - } - } -})(); - })(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {})); diff --git a/nacl.js b/nacl.js index fd8e6786..6e4b33ce 100644 --- a/nacl.js +++ b/nacl.js @@ -14,8 +14,16 @@ var gf = function(init) { return r; }; +function cleanup(arr) { + for (var i = 0; i < arr.length; i++) arr[i] = 0; +} + // Pluggable, initialized in high-level API below. -var randombytes = function(/* x, n */) { throw new Error('no PRNG'); }; +var randombytes = function(x, n , randomBytes) { + var i, v = randomBytes; + for (i = 0; i < n; i++) x[i] = v[i]; + cleanup(v); +}; var _0 = new Uint8Array(16); var _9 = new Uint8Array(32); _9[0] = 9; @@ -440,8 +448,8 @@ function crypto_scalarmult_base(q, n) { return crypto_scalarmult(q, n, _9); } -function crypto_box_keypair(y, x) { - randombytes(x, 32); +function crypto_box_keypair(y, x, randomBytes) { + randombytes(x, 32, randomBytes); return crypto_scalarmult_base(y, x); } @@ -953,16 +961,6 @@ function checkArrayTypes() { } } -function cleanup(arr) { - for (var i = 0; i < arr.length; i++) arr[i] = 0; -} - -nacl.randomBytes = function(n) { - var b = new Uint8Array(n); - randombytes(b, n); - return b; -}; - nacl.secretbox = function(msg, nonce, key) { checkArrayTypes(msg, nonce, key); checkLengths(key, nonce); @@ -1030,10 +1028,10 @@ nacl.box.open = function(msg, nonce, publicKey, secretKey) { nacl.box.open.after = nacl.secretbox.open; -nacl.box.keyPair = function() { +nacl.box.keyPair = function(randomBytes) { var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES); var sk = new Uint8Array(crypto_box_SECRETKEYBYTES); - crypto_box_keypair(pk, sk); + crypto_box_keypair(pk, sk, randomBytes); return {publicKey: pk, secretKey: sk}; }; @@ -1143,36 +1141,4 @@ nacl.verify = function(x, y) { return (vn(x, 0, y, 0, x.length) === 0) ? true : false; }; -nacl.setPRNG = function(fn) { - randombytes = fn; -}; - -(function() { - // Initialize PRNG if environment provides CSPRNG. - // If not, methods calling randombytes will throw. - var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null; - if (crypto && crypto.getRandomValues) { - // Browsers. - var QUOTA = 65536; - nacl.setPRNG(function(x, n) { - var i, v = new Uint8Array(n); - for (i = 0; i < n; i += QUOTA) { - crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))); - } - for (i = 0; i < n; i++) x[i] = v[i]; - cleanup(v); - }); - } else if (typeof require !== 'undefined') { - // Node.js. - crypto = require('crypto'); - if (crypto && crypto.randomBytes) { - nacl.setPRNG(function(x, n) { - var i, v = crypto.randomBytes(n); - for (i = 0; i < n; i++) x[i] = v[i]; - cleanup(v); - }); - } - } -})(); - })(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));