[or-cvs] Using RAND_pseudo_bytes instead of RAND_bytes is an acciden...

Nick Mathewson nickm at seul.org
Thu Oct 6 22:18:03 UTC 2005


Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv31848/src/common

Modified Files:
	crypto.c crypto.h 
Log Message:
Using RAND_pseudo_bytes instead of RAND_bytes is an accident waiting to happen, and does not really speed us up much when we do it.  So stop doing it.

Index: crypto.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/crypto.c,v
retrieving revision 1.162
retrieving revision 1.163
diff -u -d -r1.162 -r1.163
--- crypto.c	6 Oct 2005 04:33:40 -0000	1.162
+++ crypto.c	6 Oct 2005 22:18:01 -0000	1.163
@@ -1645,24 +1645,10 @@
   return (r == 1) ? 0 : -1;
 }
 
-/** Write n bytes of pseudorandom data to <b>to</b>. Return 0 on
- * success, -1 on failure.
- */
-void
-crypto_pseudo_rand(char *to, size_t n)
-{
-  tor_assert(to);
-  if (RAND_pseudo_bytes((unsigned char*)to, n) == -1) {
-    log_fn(LOG_ERR, "RAND_pseudo_bytes failed unexpectedly.");
-    crypto_log_errors(LOG_WARN, "generating random data");
-    exit(1);
-  }
-}
-
 /** Return a pseudorandom integer, chosen uniformly from the values
  * between 0 and max-1. */
 int
-crypto_pseudo_rand_int(unsigned int max)
+crypto_rand_int(unsigned int max)
 {
   unsigned int val;
   unsigned int cutoff;
@@ -1675,7 +1661,7 @@
    */
   cutoff = UINT_MAX - (UINT_MAX%max);
   while (1) {
-    crypto_pseudo_rand((char*)&val, sizeof(val));
+    crypto_rand((char*)&val, sizeof(val));
     if (val < cutoff)
       return val % max;
   }
@@ -1689,7 +1675,7 @@
   size_t len;
   len = smartlist_len(sl);
   if (len)
-    return smartlist_get(sl,crypto_pseudo_rand_int(len));
+    return smartlist_get(sl,crypto_rand_int(len));
   return NULL; /* no elements to choose from */
 }
 

Index: crypto.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/crypto.h,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -d -r1.67 -r1.68
--- crypto.h	6 Oct 2005 04:33:40 -0000	1.67
+++ crypto.h	6 Oct 2005 22:18:01 -0000	1.68
@@ -148,8 +148,7 @@
 /* random numbers */
 int crypto_seed_rng(void);
 int crypto_rand(char *to, size_t n);
-void crypto_pseudo_rand(char *to, size_t n);
-int crypto_pseudo_rand_int(unsigned int max);
+int crypto_rand_int(unsigned int max);
 
 struct smartlist_t;
 void *smartlist_choose(const struct smartlist_t *sl);



More information about the tor-commits mailing list