Reviewers: Chris Evans, Mads Ager, vegorov,

Description:
Introduce a random entropy source which can optionally be provided at
initialization.

BUG=89462

Please review this at http://codereview.chromium.org/7395012/

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
  M     include/v8.h
  M     src/api.cc
  M     src/v8.h
  M     src/v8.cc


Index: include/v8.h
===================================================================
--- include/v8.h        (revision 8657)
+++ include/v8.h        (working copy)
@@ -2801,7 +2801,14 @@
   char** raw_data;
 };

+
 /**
+ * EntropySource is used as a callback function when v8 needs a source
+ * of cryptographically safe entropy.
+ */
+typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
+
+/**
  * Container class for static utility functions.
  */
 class V8EXPORT V8 {
@@ -3026,6 +3033,12 @@
   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: src/api.cc
===================================================================
--- src/api.cc  (revision 8657)
+++ src/api.cc  (working copy)
@@ -3933,6 +3933,11 @@
 }


+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: src/v8.cc
===================================================================
--- src/v8.cc   (revision 8657)
+++ src/v8.cc   (working copy)
@@ -50,7 +50,10 @@
 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 @@

 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 != NULL)
+      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 @@
 }


+void V8::SetEntropySource(EntropySource source) {
+  entropy_source = source;
+}
+
+
 // Used by JavaScript APIs
 uint32_t V8::Random(Isolate* isolate) {
   ASSERT(isolate == Isolate::Current());
Index: src/v8.h
===================================================================
--- src/v8.h    (revision 8657)
+++ src/v8.h    (working copy)
@@ -91,6 +91,9 @@
   static void FatalProcessOutOfMemory(const char* location,
                                       bool take_snapshot = false);

+  // Allows a cryptographically safe 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


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to