diff options
author | Nick Mathewson <nickm@torproject.org> | 2015-04-23 09:10:35 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2015-04-23 09:10:35 -0400 |
commit | 647b7d37c2b3de4a837d0a4bf810c0132624c15d (patch) | |
tree | d2619889049cdd30f62ddebffb96cf62835cb6e5 /src/common/crypto.c | |
parent | 3acee6142221839137607c9a9cbf8ca4feea5993 (diff) | |
parent | e48ad353a31f3ee376f7914563d960916ad6aecd (diff) | |
download | tor-647b7d37c2b3de4a837d0a4bf810c0132624c15d.tar.gz tor-647b7d37c2b3de4a837d0a4bf810c0132624c15d.zip |
Merge remote-tracking branch 'public/bug15745_027_03'
Diffstat (limited to 'src/common/crypto.c')
-rw-r--r-- | src/common/crypto.c | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index 1b45639a7c..e723f3d5d2 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -2317,6 +2317,25 @@ crypto_rand_int(unsigned int max) } } +/** Return a pseudorandom integer, chosen uniformly from the values <i>i</i> + * such that <b>min</b> <= <i>i</i> < <b>max</b>. + * + * <b>min</b> MUST be in range [0, <b>max</b>). + * <b>max</b> MUST be in range (min, INT_MAX]. + */ +int +crypto_rand_int_range(unsigned int min, unsigned int max) +{ + tor_assert(min < max); + tor_assert(max <= INT_MAX); + + /* The overflow is avoided here because crypto_rand_int() returns a value + * between 0 and (max - min - 1) with max being <= INT_MAX and min <= max. + * This is why we add 1 to the maximum value so we can actually get max as + * a return value. */ + return min + crypto_rand_int(max - min); +} + /** Return a pseudorandom 64-bit integer, chosen uniformly from the values * between 0 and <b>max</b>-1. */ uint64_t @@ -2379,7 +2398,7 @@ crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix, if (min_rand_len > max_rand_len) min_rand_len = max_rand_len; - randlen = min_rand_len + crypto_rand_int(max_rand_len - min_rand_len + 1); + randlen = crypto_rand_int_range(min_rand_len, max_rand_len+1); prefixlen = strlen(prefix); resultlen = prefixlen + strlen(suffix) + randlen + 16; |