LRN: Fix automake deps to allow -j* builds again
[oweals/gnunet.git] / src / util / crypto_random.c
index 0a092cdcc35d252d29987838a933dae7a47492ef..5928b96ea7bffacdfb2e99c1ef708c00752548c5 100644 (file)
@@ -60,8 +60,9 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
 
   GNUNET_assert (i > 0);
 
-  if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
+  switch (mode)
     {
+    case GNUNET_CRYPTO_QUALITY_STRONG:
       /* see http://lists.gnupg.org/pipermail/gcrypt-devel/2004-May/000613.html */
 #ifdef gcry_fast_random_poll
       if ((invokeCount++ % 256) == 0)
@@ -75,14 +76,23 @@ GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i)
        }
       while (ret >= ul);
       return ret % i;
-    }
-  else
-    {
+    case GNUNET_CRYPTO_QUALITY_NONCE:
+      ul = UINT32_MAX - (UINT32_MAX % i);
+      do
+        {
+          gcry_create_nonce(&ret, sizeof(ret));
+        }
+      while (ret >= ul);
+      return ret % i;
+    case GNUNET_CRYPTO_QUALITY_WEAK:
       ret = i * weak_random ();
       if (ret >= i)
         ret = i - 1;
       return ret;
+    default:
+      GNUNET_assert (0);
     }
+  return 0;
 }
 
 
@@ -131,8 +141,9 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
   uint64_t ul;
 
   GNUNET_assert (max > 0);
-  if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
+  switch (mode)
     {
+    case GNUNET_CRYPTO_QUALITY_STRONG:    
       ul = UINT64_MAX - (UINT64_MAX % max);
       do
        {
@@ -141,14 +152,24 @@ GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max)
        }
       while (ret >= ul);
       return ret % max;
-    }
-  else
-    {
+    case GNUNET_CRYPTO_QUALITY_NONCE:
+      ul = UINT64_MAX - (UINT64_MAX % max);
+      do
+        {
+          gcry_create_nonce(&ret, sizeof(ret));
+        }
+      while (ret >= ul);
+
+      return ret % max;
+    case GNUNET_CRYPTO_QUALITY_WEAK:
       ret = max * weak_random ();
       if (ret >= max)
         ret = max - 1;
       return ret;
+    default:
+      GNUNET_assert (0);
     }
+  return 0;
 }
 
 /**
@@ -167,7 +188,7 @@ GNUNET_CRYPTO_random_disable_entropy_gathering ()
  * Process ID of the "find" process that we use for
  * entropy gathering.
  */
-static pid_t genproc;
+static struct GNUNET_OS_Process *genproc;
 
 /**
  * Function called by libgcrypt whenever we are
@@ -185,16 +206,17 @@ entropy_generator (void *cls,
     return;
   if (current == total)
     {
-      if (genproc != 0)
+      if (genproc != NULL)
         {
-          if (0 != PLIBC_KILL (genproc, SIGTERM))
+          if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
             GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
           GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
-          genproc = 0;
+          GNUNET_OS_process_close (genproc);
+          genproc = NULL;
         }
       return;
     }
-  if (genproc != 0)
+  if (genproc != NULL)
     {
       ret = GNUNET_OS_process_status (genproc, &type, &code);
       if (ret == GNUNET_NO)
@@ -204,10 +226,11 @@ entropy_generator (void *cls,
           GNUNET_break (0);
           return;
         }
-      if (0 != PLIBC_KILL (genproc, SIGTERM))
+      if (0 != GNUNET_OS_process_kill (genproc, SIGTERM))
         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
       GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
-      genproc = 0;
+      GNUNET_OS_process_close (genproc);
+      genproc = NULL;
     }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               _("Starting `%s' process to generate entropy\n"), "find");
@@ -222,17 +245,17 @@ entropy_generator (void *cls,
 static void
 killfind ()
 {
-  if (genproc != 0)
+  if (genproc != NULL)
     {
-      PLIBC_KILL (genproc, SIGKILL);
-      genproc = 0;
+      GNUNET_OS_process_kill (genproc, SIGKILL);
+      GNUNET_OS_process_close (genproc);
+      genproc = NULL;
     }
 }
 
 
 void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
 {
-  SRANDOM (time (NULL));
   gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
   if (!gcry_check_version (GCRYPT_VERSION))
     {
@@ -247,6 +270,7 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
 #endif
   gcry_set_progress_handler (&entropy_generator, NULL);
   atexit (&killfind);
+  SRANDOM (time (NULL) ^ GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
 }
 
 
@@ -258,3 +282,4 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini ()
 
 
 /* end of crypto_random.c */
+