code clean up
[oweals/gnunet.git] / src / util / crypto_random.c
index eea047ac6999fbb11665dd2bd187130c4653e878..25ba4db2a81ac94c9172c82c428590288ca7e474 100644 (file)
@@ -55,6 +55,7 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
   static unsigned int invokeCount;
 #endif
   uint32_t ret;
+  uint32_t ul;
 
   GNUNET_assert (i > 0);
 
@@ -65,8 +66,13 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
       if ((invokeCount++ % 256) == 0)
         gcry_fast_random_poll ();
 #endif
-      gcry_randomize ((unsigned char *) &ret,
-                      sizeof (uint32_t), GCRY_STRONG_RANDOM);
+      ul = UINT32_MAX - (UINT32_MAX % i);
+      do
+       {
+         gcry_randomize ((unsigned char *) &ret,
+                         sizeof (uint32_t), GCRY_STRONG_RANDOM);
+       }
+      while (ret >= ul);
       return ret % i;
     }
   else
@@ -121,12 +127,18 @@ uint64_t
 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_MAX - (UINT64_MAX % max);
+      do
+       {
+         gcry_randomize ((unsigned char *) &ret,
+                         sizeof (uint64_t), GCRY_STRONG_RANDOM);
+       }
+      while (ret >= ul);
       return ret % max;
     }
   else