Revision: 8666
Author:   [email protected]
Date:     Sun Jul 17 02:16:28 2011
Log: Introduce a random entropy source which can optionally be provided at initialization.

BUG=89462

Review URL: http://codereview.chromium.org/7395012
Patch from Chris Neckar <[email protected]>.
http://code.google.com/p/v8/source/detail?r=8666

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/src/v8.cc
 /branches/bleeding_edge/src/v8.h

=======================================
--- /branches/bleeding_edge/include/v8.h        Thu Jul 14 04:00:04 2011
+++ /branches/bleeding_edge/include/v8.h        Sun Jul 17 02:16:28 2011
@@ -2801,6 +2801,13 @@
   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.
  */
@@ -3025,6 +3032,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
=======================================
--- /branches/bleeding_edge/src/api.cc  Thu Jul 14 04:00:04 2011
+++ /branches/bleeding_edge/src/api.cc  Sun Jul 17 02:16:28 2011
@@ -3931,6 +3931,11 @@
   }
   return InitializeHelper();
 }
+
+
+void v8::V8::SetEntropySource(EntropySource source) {
+  i::V8::SetEntropySource(source);
+}


 bool v8::V8::Dispose() {
=======================================
--- /branches/bleeding_edge/src/v8.cc   Mon Jul  4 04:34:29 2011
+++ /branches/bleeding_edge/src/v8.cc   Sun Jul 17 02:16:28 2011
@@ -50,6 +50,9 @@
 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 != 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();
     }
   }
@@ -122,6 +131,11 @@

   return (state[0] << 14) + (state[1] & 0x3FFFF);
 }
+
+
+void V8::SetEntropySource(EntropySource source) {
+  entropy_source = source;
+}


 // Used by JavaScript APIs
=======================================
--- /branches/bleeding_edge/src/v8.h    Wed Jun 15 23:37:49 2011
+++ /branches/bleeding_edge/src/v8.h    Sun Jul 17 02:16:28 2011
@@ -91,6 +91,9 @@
   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

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

Reply via email to