[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r11645 - gnunet/src/util
From: |
gnunet |
Subject: |
[GNUnet-SVN] r11645 - gnunet/src/util |
Date: |
Wed, 9 Jun 2010 13:06:50 +0200 |
Author: grothoff
Date: 2010-06-09 13:06:50 +0200 (Wed, 09 Jun 2010)
New Revision: 11645
Modified:
gnunet/src/util/crypto_random.c
Log:
fixing bias
Modified: gnunet/src/util/crypto_random.c
===================================================================
--- gnunet/src/util/crypto_random.c 2010-06-09 09:18:45 UTC (rev 11644)
+++ gnunet/src/util/crypto_random.c 2010-06-09 11:06:50 UTC (rev 11645)
@@ -55,6 +55,7 @@
static unsigned int invokeCount;
#endif
uint32_t ret;
+ uint32_t ul;
GNUNET_assert (i > 0);
@@ -65,8 +66,13 @@
if ((invokeCount++ % 256) == 0)
gcry_fast_random_poll ();
#endif
- gcry_randomize ((unsigned char *) &ret,
- sizeof (uint32_t), GCRY_STRONG_RANDOM);
+ ul = ((uint32_t)-1) - (((uint32_t)-1) % i);
+ do
+ {
+ gcry_randomize ((unsigned char *) &ret,
+ sizeof (uint32_t), GCRY_STRONG_RANDOM);
+ }
+ while (ret >= ul);
return ret % i;
}
else
@@ -121,12 +127,18 @@
GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
{
uint64_t ret;
+ uint64_t ul;
GNUNET_assert (max > 0);
if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
{
- gcry_randomize ((unsigned char *) &ret,
- sizeof (uint64_t), GCRY_STRONG_RANDOM);
+ ul = ((uint64_t)-1LL) - (((uint64_t)-1LL) % max);
+ do
+ {
+ gcry_randomize ((unsigned char *) &ret,
+ sizeof (uint64_t), GCRY_STRONG_RANDOM);
+ }
+ while (ret >= ul);
return ret % max;
}
else
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r11645 - gnunet/src/util,
gnunet <=