gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r11646 - GNUnet/src/util/crypto


From: gnunet
Subject: [GNUnet-SVN] r11646 - GNUnet/src/util/crypto
Date: Wed, 9 Jun 2010 13:10:35 +0200

Author: grothoff
Date: 2010-06-09 13:10:35 +0200 (Wed, 09 Jun 2010)
New Revision: 11646

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 11:06:50 UTC (rev 11645)
+++ GNUnet/src/util/crypto/random.c     2010-06-09 11:10:35 UTC (rev 11646)
@@ -40,6 +40,7 @@
   static unsigned int invokeCount;
 #endif
   unsigned int ret;
+  unsigned int ul;
 
   GNUNET_GE_ASSERT (NULL, i > 0);
 
@@ -51,11 +52,13 @@
       if ((invokeCount++ % 256) == 0)
         gcry_fast_random_poll ();
 #endif
-      ret = rand ();            /* in case gcry_randomize fails,
-                                   we at least get a pseudo-
-                                   random number this way */
-      gcry_randomize ((unsigned char *) &ret,
-                      sizeof (unsigned int), GCRY_STRONG_RANDOM);
+     ul = ((unsigned int)-1) - (((unsigned int)-1) % i);
+      do
+       {
+         gcry_randomize ((unsigned char *) &ret,
+                         sizeof (uint32_t), GCRY_STRONG_RANDOM);
+       }
+      while (ret >= ul);
       GNUNET_unlock_gcrypt_ ();
       return ret % i;
     }
@@ -106,13 +109,19 @@
 GNUNET_random_u64 (int mode, unsigned long long u)
 {
   unsigned long long ret;
+  unsigned long long ul;
 
   GNUNET_GE_ASSERT (NULL, u > 0);
   if (mode == GNUNET_RANDOM_QUALITY_STRONG)
     {
       GNUNET_lock_gcrypt_ ();
-      gcry_randomize ((unsigned char *) &ret,
-                      sizeof (unsigned long long), GCRY_STRONG_RANDOM);
+      ul = ((unsigned long long)-1LL) - (((unsigned long long)-1LL) % u);
+      do
+       {
+         gcry_randomize ((unsigned char *) &ret,
+                         sizeof (uint64_t), GCRY_STRONG_RANDOM);
+       }
+      while (ret >= ul);
       GNUNET_unlock_gcrypt_ ();
       return ret % u;
     }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]