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
This commit is contained in:
Nick Mathewson 2005-10-06 22:18:01 +00:00
parent 0e5b6a84eb
commit cc35e1720f
7 changed files with 10 additions and 25 deletions

View File

@ -1645,24 +1645,10 @@ crypto_rand(char *to, size_t n)
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 @@ 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 */
}

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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]);

View File

@ -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 &&

View File

@ -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;