Skip to content

Commit

Permalink
PRNG: remove quota restriction; don't use window.
Browse files Browse the repository at this point in the history
randombytes in browsers no longer throw when output length exceeds 65536
bytes. Remove mention of this from README.

Improve detection of environment (browser or Node) by also checking
process.browser.

Replace references to window with self. Fixes #65, closes #66.
  • Loading branch information
dchest committed Feb 20, 2016
1 parent e569a0e commit 703fa18
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,6 @@ depending on the platform it runs on:
* `window.msCrypto.getRandomValues` (Internet Explorer 11)
* `crypto.randomBytes` (Node.js)

Note that browsers are required to throw `QuotaExceededError` exception if
requested `length` is more than 65536, so do not ask for more than 65536 bytes
in *one call* (multiple calls to get as many bytes as you like are okay:
browsers can generate infinite amount of random bytes without any bad
consequences).

If the platform doesn't provide a suitable PRNG, the following functions,
which require random numbers, will throw exception:

Expand Down
21 changes: 10 additions & 11 deletions nacl-fast.js
Original file line number Diff line number Diff line change
Expand Up @@ -2361,25 +2361,24 @@ nacl.setPRNG = function(fn) {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto;
if (typeof window !== 'undefined') {
// Browser.
if (window.crypto && window.crypto.getRandomValues) {
crypto = window.crypto; // Standard
} else if (window.msCrypto && window.msCrypto.getRandomValues) {
crypto = window.msCrypto; // Internet Explorer 11+
}
if (crypto) {
if (typeof process === 'undefined' || process.browser) {
// Browsers.
crypto = self.crypto || self.msCrypto;
if (crypto && crypto.getRandomValues) {
var QUOTA = 65536;
nacl.setPRNG(function(x, n) {
var i, v = new Uint8Array(n);
crypto.getRandomValues(v);
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) {
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];
Expand All @@ -2389,4 +2388,4 @@ nacl.setPRNG = function(fn) {
}
})();

})(typeof module !== 'undefined' && module.exports ? module.exports : (window.nacl = window.nacl || {}));
})(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));
4 changes: 2 additions & 2 deletions nacl-fast.min.js

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions nacl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,25 +1148,24 @@ nacl.setPRNG = function(fn) {
// Initialize PRNG if environment provides CSPRNG.
// If not, methods calling randombytes will throw.
var crypto;
if (typeof window !== 'undefined') {
// Browser.
if (window.crypto && window.crypto.getRandomValues) {
crypto = window.crypto; // Standard
} else if (window.msCrypto && window.msCrypto.getRandomValues) {
crypto = window.msCrypto; // Internet Explorer 11+
}
if (crypto) {
if (typeof process === 'undefined' || process.browser) {
// Browsers.
crypto = self.crypto || self.msCrypto;
if (crypto && crypto.getRandomValues) {
var QUOTA = 65536;
nacl.setPRNG(function(x, n) {
var i, v = new Uint8Array(n);
crypto.getRandomValues(v);
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) {
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];
Expand All @@ -1176,4 +1175,4 @@ nacl.setPRNG = function(fn) {
}
})();

})(typeof module !== 'undefined' && module.exports ? module.exports : (window.nacl = window.nacl || {}));
})(typeof module !== 'undefined' && module.exports ? module.exports : (self.nacl = self.nacl || {}));
Loading

0 comments on commit 703fa18

Please sign in to comment.