diff options
Diffstat (limited to 'src/util/crypto_random.c')
-rw-r--r-- | src/util/crypto_random.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c index b56523a46..50af9f251 100644 --- a/src/util/crypto_random.c +++ b/src/util/crypto_random.c | |||
@@ -30,6 +30,10 @@ | |||
30 | #include <gcrypt.h> | 30 | #include <gcrypt.h> |
31 | 31 | ||
32 | /** | 32 | /** |
33 | * Produce a random value. | ||
34 | * | ||
35 | * @param mode desired quality of the random number | ||
36 | * @param i the upper limit (exclusive) for the random number | ||
33 | * @return a random value in the interval [0,i[. | 37 | * @return a random value in the interval [0,i[. |
34 | */ | 38 | */ |
35 | uint32_t | 39 | uint32_t |
@@ -97,26 +101,31 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n) | |||
97 | 101 | ||
98 | /** | 102 | /** |
99 | * Random on unsigned 64-bit values. | 103 | * Random on unsigned 64-bit values. |
104 | * | ||
105 | * | ||
106 | * @param mode desired quality of the random number | ||
107 | * @param max value returned will be in range [0,max) (exclusive) | ||
108 | * @return random 64-bit number | ||
100 | */ | 109 | */ |
101 | uint64_t | 110 | uint64_t |
102 | GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, | 111 | GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, |
103 | uint64_t u) | 112 | uint64_t max) |
104 | { | 113 | { |
105 | uint64_t ret; | 114 | uint64_t ret; |
106 | 115 | ||
107 | GNUNET_assert (u > 0); | 116 | GNUNET_assert (max > 0); |
108 | if (mode == GNUNET_CRYPTO_QUALITY_STRONG) | 117 | if (mode == GNUNET_CRYPTO_QUALITY_STRONG) |
109 | { | 118 | { |
110 | gcry_randomize ((unsigned char *) &ret, | 119 | gcry_randomize ((unsigned char *) &ret, |
111 | sizeof (uint64_t), | 120 | sizeof (uint64_t), |
112 | GCRY_STRONG_RANDOM); | 121 | GCRY_STRONG_RANDOM); |
113 | return ret % u; | 122 | return ret % max; |
114 | } | 123 | } |
115 | else | 124 | else |
116 | { | 125 | { |
117 | ret = u * ((double) RANDOM () / RAND_MAX); | 126 | ret = max * ((double) RANDOM () / RAND_MAX); |
118 | if (ret >= u) | 127 | if (ret >= max) |
119 | ret = u - 1; | 128 | ret = max - 1; |
120 | return ret; | 129 | return ret; |
121 | } | 130 | } |
122 | } | 131 | } |