mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-24 20:33:31 +01:00
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:
parent
0e5b6a84eb
commit
cc35e1720f
@ -1645,24 +1645,10 @@ crypto_rand(char *to, size_t n)
|
|||||||
return (r == 1) ? 0 : -1;
|
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
|
/** Return a pseudorandom integer, chosen uniformly from the values
|
||||||
* between 0 and max-1. */
|
* between 0 and max-1. */
|
||||||
int
|
int
|
||||||
crypto_pseudo_rand_int(unsigned int max)
|
crypto_rand_int(unsigned int max)
|
||||||
{
|
{
|
||||||
unsigned int val;
|
unsigned int val;
|
||||||
unsigned int cutoff;
|
unsigned int cutoff;
|
||||||
@ -1675,7 +1661,7 @@ crypto_pseudo_rand_int(unsigned int max)
|
|||||||
*/
|
*/
|
||||||
cutoff = UINT_MAX - (UINT_MAX%max);
|
cutoff = UINT_MAX - (UINT_MAX%max);
|
||||||
while (1) {
|
while (1) {
|
||||||
crypto_pseudo_rand((char*)&val, sizeof(val));
|
crypto_rand((char*)&val, sizeof(val));
|
||||||
if (val < cutoff)
|
if (val < cutoff)
|
||||||
return val % max;
|
return val % max;
|
||||||
}
|
}
|
||||||
@ -1689,7 +1675,7 @@ smartlist_choose(const smartlist_t *sl)
|
|||||||
size_t len;
|
size_t len;
|
||||||
len = smartlist_len(sl);
|
len = smartlist_len(sl);
|
||||||
if (len)
|
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 */
|
return NULL; /* no elements to choose from */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,8 +148,7 @@ void crypto_dh_free(crypto_dh_env_t *dh);
|
|||||||
/* random numbers */
|
/* random numbers */
|
||||||
int crypto_seed_rng(void);
|
int crypto_seed_rng(void);
|
||||||
int crypto_rand(char *to, size_t n);
|
int crypto_rand(char *to, size_t n);
|
||||||
void crypto_pseudo_rand(char *to, size_t n);
|
int crypto_rand_int(unsigned int max);
|
||||||
int crypto_pseudo_rand_int(unsigned int max);
|
|
||||||
|
|
||||||
struct smartlist_t;
|
struct smartlist_t;
|
||||||
void *smartlist_choose(const struct smartlist_t *sl);
|
void *smartlist_choose(const struct smartlist_t *sl);
|
||||||
|
@ -219,7 +219,7 @@ circuit_new(uint16_t p_circ_id, connection_t *p_conn)
|
|||||||
circ->package_window = CIRCWINDOW_START;
|
circ->package_window = CIRCWINDOW_START;
|
||||||
circ->deliver_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++;
|
circ->global_identifier = n_circuits_allocated++;
|
||||||
|
|
||||||
circuit_add(circ);
|
circuit_add(circ);
|
||||||
|
@ -176,7 +176,7 @@ connection_new(int type)
|
|||||||
conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
|
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_created = now;
|
||||||
conn->timestamp_lastread = now;
|
conn->timestamp_lastread = now;
|
||||||
|
@ -460,7 +460,7 @@ rend_client_get_random_intro(const char *query)
|
|||||||
if (!entry->parsed->n_intro_points)
|
if (!entry->parsed->n_intro_points)
|
||||||
return NULL;
|
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) {
|
if (entry->parsed->intro_point_extend_info) {
|
||||||
return extend_info_dup(entry->parsed->intro_point_extend_info[i]);
|
return extend_info_dup(entry->parsed->intro_point_extend_info[i]);
|
||||||
|
@ -1020,7 +1020,7 @@ rend_consider_services_upload(time_t now)
|
|||||||
service = smartlist_get(rend_service_list, i);
|
service = smartlist_get(rend_service_list, i);
|
||||||
if (!service->next_upload_time) { /* never been uploaded yet */
|
if (!service->next_upload_time) { /* never been uploaded yet */
|
||||||
service->next_upload_time =
|
service->next_upload_time =
|
||||||
now + crypto_pseudo_rand_int(2*rendpostperiod);
|
now + crypto_rand_int(2*rendpostperiod);
|
||||||
}
|
}
|
||||||
if (service->next_upload_time < now ||
|
if (service->next_upload_time < now ||
|
||||||
(service->desc_is_dirty &&
|
(service->desc_is_dirty &&
|
||||||
|
@ -757,7 +757,7 @@ routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
|
|||||||
return smartlist_choose(sl);
|
return smartlist_choose(sl);
|
||||||
}
|
}
|
||||||
/* Second, choose a random value from the bandwidth weights. */
|
/* 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 */
|
/* Last, count through sl until we get to the element we picked */
|
||||||
tmp = 0;
|
tmp = 0;
|
||||||
for (i=0; ; i++) {
|
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
|
/* If no networkstatus was found, choose a dirserver at random as "most
|
||||||
* recent". */
|
* recent". */
|
||||||
if (most_recent_idx<0)
|
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. */
|
/* Build a request string for all the resources we want. */
|
||||||
resource_len = needed * (HEX_DIGEST_LEN+1) + 6;
|
resource_len = needed * (HEX_DIGEST_LEN+1) + 6;
|
||||||
|
Loading…
Reference in New Issue
Block a user