Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 70f198d

Browse files
bnoordhuisindutny
authored andcommitted
src: seed V8's random number generator at startup
The default entropy source is /dev/urandom on UNIX platforms, which is okay but we can do better by seeding it from OpenSSL's entropy pool. On Windows we can certainly do better; on that platform, V8 seeds the random number generator using only the current system time. Fixes #6250. NB: This is a back-port of commit 7ac2391 from the master branch that for some reason never got back-ported to the v0.10 branch. The default on UNIX platforms in v0.10 is different and arguably worse than it is with master: if no entropy source is provided, V8 3.14 calls srandom() with a xor of the PID and the current time in microseconds. That means that on systems with a coarse system clock, the initial state of the PRNG may be easily guessable. The situation on Windows is even more dire because there the PRNG is seeded with only the current time... in milliseconds.
1 parent bd547d6 commit 70f198d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/node.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,6 +3057,12 @@ int Start(int argc, char *argv[]) {
30573057
Init(argc, argv_copy);
30583058

30593059
V8::Initialize();
3060+
#if HAVE_OPENSSL
3061+
// V8 on Windows doesn't have a good source of entropy. Seed it from
3062+
// OpenSSL's pool.
3063+
V8::SetEntropySource(crypto::EntropySource);
3064+
#endif
3065+
30603066
{
30613067
Locker locker;
30623068
HandleScope handle_scope;

src/node_crypto.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ Handle<Value> ThrowCryptoTypeError(unsigned long err) {
157157
}
158158

159159

160+
bool EntropySource(unsigned char* buffer, size_t length) {
161+
// RAND_bytes() can return 0 to indicate that the entropy data is not truly
162+
// random. That's okay, it's still better than V8's stock source of entropy,
163+
// which is /dev/urandom on UNIX platforms and the current time on Windows.
164+
return RAND_bytes(buffer, length) != -1;
165+
}
166+
167+
160168
void SecureContext::Initialize(Handle<Object> target) {
161169
HandleScope scope;
162170

src/node_crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class Connection : ObjectWrap {
280280
friend class SecureContext;
281281
};
282282

283+
bool EntropySource(unsigned char* buffer, size_t length);
283284
void InitCrypto(v8::Handle<v8::Object> target);
284285

285286
} // namespace crypto

0 commit comments

Comments
 (0)