Introduce a random entropy source which can optionally be provided at initialization.
authorager@chromium.org <ager@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Sun, 17 Jul 2011 09:16:28 +0000 (09:16 +0000)
committerager@chromium.org <ager@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Sun, 17 Jul 2011 09:16:28 +0000 (09:16 +0000)
BUG=89462

Review URL: http://codereview.chromium.org/7395012
Patch from Chris Neckar <cdn@chromium.org>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8666 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

include/v8.h
src/api.cc
src/v8.cc
src/v8.h

index 343be1b..ac26061 100644 (file)
@@ -2801,6 +2801,13 @@ class V8EXPORT StartupDataDecompressor {  // NOLINT
   char** raw_data;
 };
 
+
+/**
+ * EntropySource is used as a callback function when v8 needs a source
+ * of entropy.
+ */
+typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
+
 /**
  * Container class for static utility functions.
  */
@@ -3026,6 +3033,12 @@ class V8EXPORT V8 {
   static bool Initialize();
 
   /**
+   * Allows the host application to provide a callback which can be used
+   * as a source of entropy for random number generators.
+   */
+  static void SetEntropySource(EntropySource source);
+
+  /**
    * Adjusts the amount of registered external memory.  Used to give
    * V8 an indication of the amount of externally allocated memory
    * that is kept alive by JavaScript objects.  V8 uses this to decide
index 11d1922..fca7fbf 100644 (file)
@@ -3933,6 +3933,11 @@ bool v8::V8::Initialize() {
 }
 
 
+void v8::V8::SetEntropySource(EntropySource source) {
+  i::V8::SetEntropySource(source);
+}
+
+
 bool v8::V8::Dispose() {
   i::Isolate* isolate = i::Isolate::Current();
   if (!ApiCheck(isolate != NULL && isolate->IsDefaultIsolate(),
index 11af057..36f835f 100644 (file)
--- a/src/v8.cc
+++ b/src/v8.cc
@@ -50,6 +50,9 @@ bool V8::has_been_disposed_ = false;
 bool V8::has_fatal_error_ = false;
 bool V8::use_crankshaft_ = true;
 
+static Mutex* entropy_mutex = OS::CreateMutex();
+static EntropySource entropy_source;
+
 
 bool V8::Initialize(Deserializer* des) {
   InitializeOncePerProcess();
@@ -102,8 +105,14 @@ void V8::TearDown() {
 
 static void seed_random(uint32_t* state) {
   for (int i = 0; i < 2; ++i) {
-    state[i] = FLAG_random_seed;
-    while (state[i] == 0) {
+    if (FLAG_random_seed != 0) {
+      state[i] = FLAG_random_seed;
+    } else if (entropy_source != NULL) {
+      uint32_t val;
+      ScopedLock lock(entropy_mutex);
+      entropy_source(reinterpret_cast<unsigned char*>(&val), sizeof(uint32_t));
+      state[i] = val;
+    } else {
       state[i] = random();
     }
   }
@@ -124,6 +133,11 @@ static uint32_t random_base(uint32_t* state) {
 }
 
 
+void V8::SetEntropySource(EntropySource source) {
+  entropy_source = source;
+}
+
+
 // Used by JavaScript APIs
 uint32_t V8::Random(Isolate* isolate) {
   ASSERT(isolate == Isolate::Current());
index e74a60c..e565ca5 100644 (file)
--- a/src/v8.h
+++ b/src/v8.h
@@ -91,6 +91,9 @@ class V8 : public AllStatic {
   static void FatalProcessOutOfMemory(const char* location,
                                       bool take_snapshot = false);
 
+  // Allows an entropy source to be provided for use in random number
+  // generation.
+  static void SetEntropySource(EntropySource source);
   // Random number generation support. Not cryptographically safe.
   static uint32_t Random(Isolate* isolate);
   // We use random numbers internally in memory allocation and in the