From cc35e1720f7dde775e2c8246c2f9b542954e401f Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 6 Oct 2005 22:18:01 +0000 Subject: [PATCH] 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. svn:r5210 --- src/common/crypto.c | 20 +++----------------- src/common/crypto.h | 3 +-- src/or/circuitlist.c | 2 +- src/or/connection.c | 2 +- src/or/rendclient.c | 2 +- src/or/rendservice.c | 2 +- src/or/routerlist.c | 4 ++-- 7 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/common/crypto.c b/src/common/crypto.c index 120d0c1cac..16d1734ac4 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -1645,24 +1645,10 @@ crypto_rand(char *to, size_t n) return (r == 1) ? 0 : -1; } -/** Write n bytes of pseudorandom data to to. 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 @@ crypto_pseudo_rand_int(unsigned int max) */ 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 @@ smartlist_choose(const smartlist_t *sl) 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 */ } diff --git a/src/common/crypto.h b/src/common/crypto.h index f7a3fa6973..4eb57e88e4 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -148,8 +148,7 @@ void crypto_dh_free(crypto_dh_env_t *dh); /* 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); diff --git a/src/or/circuitlist.c b/src/or/circuitlist.c index 7e50762963..6d93564ebe 100644 --- a/src/or/circuitlist.c +++ b/src/or/circuitlist.c @@ -219,7 +219,7 @@ circuit_new(uint16_t p_circ_id, connection_t *p_conn) circ->package_window = CIRCWINDOW_START; circ->deliver_window = CIRCWINDOW_START; - circ->next_stream_id = crypto_pseudo_rand_int(1<<16); + circ->next_stream_id = crypto_rand_int(1<<16); circ->global_identifier = n_circuits_allocated++; circuit_add(circ); diff --git a/src/or/connection.c b/src/or/connection.c index 32eebe7d59..ae57ffb5a4 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -176,7 +176,7 @@ connection_new(int type) conn->socks_request = tor_malloc_zero(sizeof(socks_request_t)); } - conn->next_circ_id = crypto_pseudo_rand_int(1<<15); + conn->next_circ_id = crypto_rand_int(1<<15); conn->timestamp_created = now; conn->timestamp_lastread = now; diff --git a/src/or/rendclient.c b/src/or/rendclient.c index b4e8f171af..ae312d273b 100644 --- a/src/or/rendclient.c +++ b/src/or/rendclient.c @@ -460,7 +460,7 @@ rend_client_get_random_intro(const char *query) if (!entry->parsed->n_intro_points) return NULL; - i = crypto_pseudo_rand_int(entry->parsed->n_intro_points); + i = crypto_rand_int(entry->parsed->n_intro_points); if (entry->parsed->intro_point_extend_info) { return extend_info_dup(entry->parsed->intro_point_extend_info[i]); diff --git a/src/or/rendservice.c b/src/or/rendservice.c index ff94db3ee1..2b456e3358 100644 --- a/src/or/rendservice.c +++ b/src/or/rendservice.c @@ -1020,7 +1020,7 @@ rend_consider_services_upload(time_t now) service = smartlist_get(rend_service_list, i); if (!service->next_upload_time) { /* never been uploaded yet */ service->next_upload_time = - now + crypto_pseudo_rand_int(2*rendpostperiod); + now + crypto_rand_int(2*rendpostperiod); } if (service->next_upload_time < now || (service->desc_is_dirty && diff --git a/src/or/routerlist.c b/src/or/routerlist.c index 921a3db9d4..8678324a4a 100644 --- a/src/or/routerlist.c +++ b/src/or/routerlist.c @@ -757,7 +757,7 @@ routerlist_sl_choose_by_bandwidth(smartlist_t *sl) return smartlist_choose(sl); } /* Second, choose a random value from the bandwidth weights. */ - rand_bw = crypto_pseudo_rand_int(total_bw); + rand_bw = crypto_rand_int(total_bw); /* Last, count through sl until we get to the element we picked */ tmp = 0; for (i=0; ; i++) { @@ -1820,7 +1820,7 @@ update_networkstatus_client_downloads(time_t now) /* If no networkstatus was found, choose a dirserver at random as "most * recent". */ if (most_recent_idx<0) - most_recent_idx = crypto_pseudo_rand_int(n_dirservers); + most_recent_idx = crypto_rand_int(n_dirservers); /* Build a request string for all the resources we want. */ resource_len = needed * (HEX_DIGEST_LEN+1) + 6;