diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-06-09 11:06:50 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-06-09 11:06:50 +0000 |
commit | f334c698b9bf85bdb5861b4ed30a7b3ae4597447 (patch) | |
tree | 5b5e85c52a1d5ab5e6d9cd73d8e64bd790b710a7 | |
parent | 9cc79e527db8b9dc342225a44e446a220f1fee20 (diff) | |
download | gnunet-f334c698b9bf85bdb5861b4ed30a7b3ae4597447.tar.gz gnunet-f334c698b9bf85bdb5861b4ed30a7b3ae4597447.zip |
fixing bias
-rw-r--r-- | src/util/crypto_random.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c index eea047ac6..676416c63 100644 --- a/src/util/crypto_random.c +++ b/src/util/crypto_random.c | |||
@@ -55,6 +55,7 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i) | |||
55 | static unsigned int invokeCount; | 55 | static unsigned int invokeCount; |
56 | #endif | 56 | #endif |
57 | uint32_t ret; | 57 | uint32_t ret; |
58 | uint32_t ul; | ||
58 | 59 | ||
59 | GNUNET_assert (i > 0); | 60 | GNUNET_assert (i > 0); |
60 | 61 | ||
@@ -65,8 +66,13 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i) | |||
65 | if ((invokeCount++ % 256) == 0) | 66 | if ((invokeCount++ % 256) == 0) |
66 | gcry_fast_random_poll (); | 67 | gcry_fast_random_poll (); |
67 | #endif | 68 | #endif |
68 | gcry_randomize ((unsigned char *) &ret, | 69 | ul = ((uint32_t)-1) - (((uint32_t)-1) % i); |
69 | sizeof (uint32_t), GCRY_STRONG_RANDOM); | 70 | do |
71 | { | ||
72 | gcry_randomize ((unsigned char *) &ret, | ||
73 | sizeof (uint32_t), GCRY_STRONG_RANDOM); | ||
74 | } | ||
75 | while (ret >= ul); | ||
70 | return ret % i; | 76 | return ret % i; |
71 | } | 77 | } |
72 | else | 78 | else |
@@ -121,12 +127,18 @@ uint64_t | |||
121 | GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max) | 127 | GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max) |
122 | { | 128 | { |
123 | uint64_t ret; | 129 | uint64_t ret; |
130 | uint64_t ul; | ||
124 | 131 | ||
125 | GNUNET_assert (max > 0); | 132 | GNUNET_assert (max > 0); |
126 | if (mode == GNUNET_CRYPTO_QUALITY_STRONG) | 133 | if (mode == GNUNET_CRYPTO_QUALITY_STRONG) |
127 | { | 134 | { |
128 | gcry_randomize ((unsigned char *) &ret, | 135 | ul = ((uint64_t)-1LL) - (((uint64_t)-1LL) % max); |
129 | sizeof (uint64_t), GCRY_STRONG_RANDOM); | 136 | do |
137 | { | ||
138 | gcry_randomize ((unsigned char *) &ret, | ||
139 | sizeof (uint64_t), GCRY_STRONG_RANDOM); | ||
140 | } | ||
141 | while (ret >= ul); | ||
130 | return ret % max; | 142 | return ret % max; |
131 | } | 143 | } |
132 | else | 144 | else |