Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit dbd2508

Browse files
committed
Rename getPseudoRandomValues to getRandomValues and seed it on startup
1 parent 4d1f41f commit dbd2508

3 files changed

Lines changed: 27 additions & 23 deletions

File tree

js/core/random/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ exports.addEntropySource = sources.addEntropySource;
2626
/**
2727
* Request random bytes from the best available entropy source. This
2828
* function is asynchronous bacause it might take some time to gather
29-
* enough entropy from the source. This is similar to /dev/random
30-
* in Unix.
29+
* enough entropy from the source. This might use /dev/random as a backend
30+
* in KVM and might be extremely slow depending on the buffer size and
31+
* the amount of available entropy.
3132
*
3233
* @param {number|Uint8Array} value Number of bytes to request or buffer to fill
3334
* @param {function} cb Callback with the resulting Uint8Array buffer argument
3435
*/
35-
exports.getRandomValues = function(value, cb) {
36+
exports.getTrueRandomValues = function(value, cb) {
3637
var u8 = null;
3738
if (typeutils.isNumber(value)) {
3839
u8 = new Uint8Array(value);
@@ -43,20 +44,20 @@ exports.getRandomValues = function(value, cb) {
4344
}
4445

4546
if (!u8) {
46-
throw new Error('getRandomValues: argument 0 is not a number or Uint8Array');
47+
throw new Error('getTrueRandomValues: argument 0 is not a number or Uint8Array');
4748
}
4849

4950
if (u8.length === 0) {
50-
throw new Error('getRandomValues: buffer length must be greater than 0');
51+
throw new Error('getTrueRandomValues: buffer length must be greater than 0');
5152
}
5253

5354
if (!typeutils.isFunction(cb)) {
54-
throw new Error('getRandomValues: argument 1 is not a function');
55+
throw new Error('getTrueRandomValues: argument 1 is not a function');
5556
}
5657

5758
var defaultSource = getDefaultSource();
5859
if (!defaultSource) {
59-
throw new Error('getRandomValues: no entropy source available');
60+
throw new Error('getTrueRandomValues: no entropy source available');
6061
}
6162

6263
defaultSource.getBytes(u8, function() {
@@ -66,14 +67,12 @@ exports.getRandomValues = function(value, cb) {
6667
};
6768

6869
/**
69-
* Request random bytes from the immediately available entropy source. This
70-
* function is synchronous, but the output may contain less entropy than
71-
* getRandomValues() output. This is similar to Unix's /dev/urandom.
70+
* Request random bytes from the CSPRNG seeded with enough entropy.
7271
*
7372
* @param {number|Uint8Array} value Number of bytes to request or buffer to fill
7473
* @return {Uint8Array} Buffer filled with random data
7574
*/
76-
exports.getPseudoRandomValues = function(value) {
75+
exports.getRandomValues = function(value) {
7776
var u8 = null;
7877
if (typeutils.isNumber(value)) {
7978
u8 = new Uint8Array(value);
@@ -84,11 +83,11 @@ exports.getPseudoRandomValues = function(value) {
8483
}
8584

8685
if (!u8) {
87-
throw new Error('getPseudoRandomValues: argument 0 is not a number or Uint8Array');
86+
throw new Error('getRandomValues: argument 0 is not a number or Uint8Array');
8887
}
8988

9089
if (u8.length === 0) {
91-
throw new Error('getPseudoRandomValues: buffer length must be greater than 0');
90+
throw new Error('getRandomValues: buffer length must be greater than 0');
9291
}
9392

9493
for (var i = 0; i < u8.length; i++) {

js/core/random/sources.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
// limitations under the License.
1414

1515
'use strict';
16+
var isaac = require('./isaac-wrapper');
1617
var defaultSource = null;
1718
var availableSources = Object.create(null);
1819

1920
exports.addEntropySource = function(source) {
2021
availableSources[source.getName()] = source;
2122

23+
source.getBytes(new Uint8Array(8), function(u8) {
24+
isaac.seed(u8);
25+
console.log('[random] using entropy source', source.getName());
26+
});
27+
2228
// Set this source as the default one
2329
defaultSource = source;
24-
console.log('[random] using entropy source', defaultSource.getName());
2530
};
2631

2732
exports.getDefaultSource = function() {

js/test/unit/random/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,33 @@ test('EntropySource', function(t) {
3737
});
3838
});
3939

40-
test('getRandomValues buffer', function(t) {
40+
test('getTrueRandomValues buffer', function(t) {
4141
t.timeoutAfter(1000);
4242
var u8 = new Uint8Array([0, 0, 0]);
43-
runtime.random.getRandomValues(u8, function(u8out) {
43+
runtime.random.getTrueRandomValues(u8, function(u8out) {
4444
t.equal(u8, u8out);
4545
t.end();
4646
});
4747
});
4848

49-
test('getRandomValues length', function(t) {
49+
test('getTrueRandomValues length', function(t) {
5050
t.timeoutAfter(1000);
51-
runtime.random.getRandomValues(10, function(u8) {
51+
runtime.random.getTrueRandomValues(4, function(u8) {
5252
t.ok(u8 instanceof Uint8Array);
53-
t.equal(u8.length, 10);
53+
t.equal(u8.length, 4);
5454
t.end();
5555
});
5656
});
5757

58-
test('getPseudoRandomValues buffer', function(t) {
58+
test('getRandomValues buffer', function(t) {
5959
var u8 = new Uint8Array([0, 0, 0]);
60-
var u8out = runtime.random.getPseudoRandomValues(u8);
60+
var u8out = runtime.random.getRandomValues(u8);
6161
t.equal(u8, u8out);
6262
t.end();
6363
});
6464

65-
test('getPseudoRandomValues length', function(t) {
66-
var u8 = runtime.random.getPseudoRandomValues(10);
65+
test('getRandomValues length', function(t) {
66+
var u8 = runtime.random.getRandomValues(10);
6767
t.ok(u8 instanceof Uint8Array);
6868
t.equal(u8.length, 10);
6969
t.end();

0 commit comments

Comments
 (0)