]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add wrappers function for libc random()
authorNick Mathewson <nickm@torproject.org>
Mon, 29 Nov 2010 20:53:33 +0000 (15:53 -0500)
committerNick Mathewson <nickm@torproject.org>
Mon, 29 Nov 2010 21:00:47 +0000 (16:00 -0500)
On windows, it's called something different.

src/common/compat.c
src/common/compat.h
src/common/crypto.c
src/or/relay.c

index 20394b4c5dcb921045ca1f335f85c73748a1b147..4d556a85e65362270ddb8a3f35fad2c35170ab41 100644 (file)
@@ -1679,6 +1679,30 @@ tor_lookup_hostname(const char *name, uint32_t *addr)
   return -1;
 }
 
+/** Initialize the insecure libc RNG. */
+void
+tor_init_weak_random(unsigned seed)
+{
+#ifdef MS_WINDOWS
+  srand(seed);
+#else
+  srandom(seed);
+#endif
+}
+
+/** Return a randomly chosen value in the range 0..TOR_RAND_MAX.  This
+ * entropy will not be cryptographically strong; do not rely on it
+ * for anything an adversary should not be able to predict. */
+long
+tor_weak_random(void)
+{
+#ifdef MS_WINDOWS
+  return rand();
+#else
+  return random();
+#endif
+}
+
 /** Hold the result of our call to <b>uname</b>. */
 static char uname_result[256];
 /** True iff uname_result is set. */
index 7d59501e2b5953fb910e085c44692bc1804c5012..449bf748f40ef93644563e3326db45d158ad0950 100644 (file)
@@ -480,6 +480,11 @@ typedef enum {
   SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED = 0x08,
 } socks5_reply_status_t;
 
+/* ===== Insecure rng */
+void tor_init_weak_random(unsigned seed);
+long tor_weak_random(void);
+#define TOR_RAND_MAX (RAND_MAX)
+
 /* ===== OS compatibility */
 const char *get_uname(void);
 
index b49547fa4debb03ac26d843f38d8fd8c4c17f956..81a432d8d4739a4474e8deede5ad909986f8a14f 100644 (file)
@@ -1935,6 +1935,14 @@ crypto_dh_free(crypto_dh_env_t *dh)
     OPENSSL_VERSION_NUMBER <= 0x00907fffl) ||   \
    (OPENSSL_VERSION_NUMBER >= 0x0090803fl))
 
+static void
+seed_weak_rng(void)
+{
+  unsigned seed;
+  crypto_rand((void*)&seed, sizeof(seed));
+  tor_init_weak_random(seed);
+}
+
 /** Seed OpenSSL's random number generator with bytes from the operating
  * system.  <b>startup</b> should be true iff we have just started Tor and
  * have not yet allocated a bunch of fds.  Return 0 on success, -1 on failure.
@@ -1985,6 +1993,7 @@ crypto_seed_rng(int startup)
   }
   RAND_seed(buf, sizeof(buf));
   memset(buf, 0, sizeof(buf));
+  seed_weak_rng();
   return 0;
 #else
   for (i = 0; filenames[i]; ++i) {
@@ -2001,6 +2010,7 @@ crypto_seed_rng(int startup)
     }
     RAND_seed(buf, (int)sizeof(buf));
     memset(buf, 0, sizeof(buf));
+    seed_weak_rng();
     return 0;
   }
 
index 8a4edb933f8b61d71f971c7dbf276f8b6f19b0b7..c64afe2dba43cd54748c14287615cd55f462638f 100644 (file)
@@ -1517,7 +1517,7 @@ circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
    * don't need cryptographic randomness here. */
   for (conn = first_conn; conn; conn = conn->next_stream) {
     num_streams++;
-    if ((random() % num_streams)==0)
+    if ((tor_weak_random() % num_streams)==0)
       chosen_stream = conn;
     /* Invariant: chosen_stream has been chosen uniformly at random from among
      * the first num_streams streams on first_conn. */