[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [tor/master 28/28] Make cbt_generate_sample use crypto_rand_double()



Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date: Tue, 22 Jun 2010 21:31:31 -0400
Subject: Make cbt_generate_sample use crypto_rand_double()
Commit: b111a7cd9c5e09bedf57a67f9044a2974222cd11

Possible workaround for bug 1139, if anybody cares.
---
 src/common/crypto.c   |   13 +++++++++----
 src/or/circuitbuild.c |    6 ++++--
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/common/crypto.c b/src/common/crypto.c
index 23e2a42..38fbca7 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -2065,10 +2065,15 @@ crypto_rand_double(void)
   /* We just use an unsigned int here; we don't really care about getting
    * more than 32 bits of resolution */
   unsigned int uint;
-  do {
-    crypto_rand((char*)&uint, sizeof(uint));
-  } while (uint == UINT_MAX);
-  return ((double)uint) / (double)UINT_MAX;
+  crypto_rand((char*)&uint, sizeof(uint));
+#if SIZEOF_INT == 4
+#define UINT_MAX_AS_DOUBLE 4294967296.0
+#elif SIZEOF_INT == 8
+#define UINT_MAX_AS_DOUBLE 1.8446744073709552e+19
+#else
+#error SIZEOF_INT is neither 4 nor 8
+#endif
+  return ((double)uint) / UINT_MAX_AS_DOUBLE;
 }
 
 /** Generate and return a new random hostname starting with <b>prefix</b>,
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 417d8ec..da63ff6 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -677,18 +677,20 @@ build_time_t
 circuit_build_times_generate_sample(circuit_build_times_t *cbt,
                                     double q_lo, double q_hi)
 {
-  uint64_t r = crypto_rand_uint64(UINT64_MAX-1);
+  double randval = crypto_rand_double();
   build_time_t ret;
   double u;
 
   /* Generate between [q_lo, q_hi) */
+  /*XXXX This is what nextafter is supposed to be for; we should use it on the
+   * platforms that support it. */
   q_hi -= 1.0/(INT32_MAX);
 
   tor_assert(q_lo >= 0);
   tor_assert(q_hi < 1);
   tor_assert(q_lo < q_hi);
 
-  u = q_lo + ((q_hi-q_lo)*r)/(1.0*UINT64_MAX);
+  u = q_lo + (q_hi-q_lo)*randval;
 
   tor_assert(0 <= u && u < 1.0);
   /* circuit_build_times_calculate_timeout returns <= INT32_MAX */
-- 
1.7.1