Use /dev/urandom, as it doesn't block, which /dev/random might do. Do not
authorSebastian Wilhelmi <wilhelmi@ira.uka.de>
Thu, 19 Aug 1999 08:32:03 +0000 (08:32 +0000)
committerSebastian Wilhelmi <wilhelmi@src.gnome.org>
Thu, 19 Aug 1999 08:32:03 +0000 (08:32 +0000)
1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>

* grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
which /dev/random might do. Do not XOR the time, when getting the
seed form /dev/urandom, as this is good itself. Prevent the
initial seed from being zero, which causes the PRNG to produce
only zeros. Hints from Colin Plumb <colin@pgp.com>.

ChangeLog
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/grand.c
grand.c

index a5660c2..b865bde 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index a5660c2..b865bde 100644 (file)
@@ -1,3 +1,11 @@
+1999-07-23  Sebastian Wilhelmi  <wilhelmi@ira.uka.de>
+
+       * grand.c (g_rand_new): Use /dev/urandom, as it doesn't block,
+       which /dev/random might do. Do not XOR the time, when getting the
+       seed form /dev/urandom, as this is good itself. Prevent the
+       initial seed from being zero, which causes the PRNG to produce
+       only zeros. Hints from Colin Plumb <colin@pgp.com>.
+
 1999-08-17  Tor Lillqvist  <tml@iki.fi>
 
        * glib.h (g_trash_stack_push): Add a cast.
index 83b1bc3..47b9700 100644 (file)
@@ -75,31 +75,29 @@ g_rand_new_with_seed (guint32 seed)
 GRand* 
 g_rand_new (void)
 {
-  guint32 seed = 0;
+  guint32 seed;
   GTimeVal now;
-  static gboolean dev_random_exists = TRUE;
+  static gboolean dev_urandom_exists = TRUE;
   
-  if (dev_random_exists)
+  if (dev_urandom_exists)
     {
-      FILE* dev_random = fopen("/dev/random", "rb");
-      if (dev_random)
+      FILE* dev_urandom = fopen("/dev/urandom", "rb");
+      if (dev_urandom)
        {
-         if (fread (&seed, sizeof (seed), 1, dev_random) != 1)
+         if (fread (&seed, sizeof (seed), 1, dev_urandom) != 1)
            seed = 0;
          else
-           dev_random_exists = FALSE;
-         fclose (dev_random);
+           dev_urandom_exists = FALSE;
+         fclose (dev_urandom);
        }       
       else
-       dev_random_exists = FALSE;
+       dev_urandom_exists = FALSE;
+    }
+  if (!dev_urandom_exists)
+    {  
+      g_get_current_time (&now);
+      seed = now.tv_sec ^ now.tv_usec;
     }
-
-  /* Using /dev/random alone makes the seed computable for the
-     outside. This might pose security problems somewhere. This should
-     yield better values */
-
-  g_get_current_time (&now);
-  seed ^= now.tv_sec ^ now.tv_usec;
 
   return g_rand_new_with_seed (seed);
 }
@@ -121,6 +119,10 @@ g_rand_set_seed (GRand* rand, guint32 seed)
   /* the generator Line 25 of Table 1 in          */
   /* [KNUTH 1981, The Art of Computer Programming */
   /*    Vol. 2 (2nd Ed.), pp102]                  */
+  
+  if (seed == 0) /* This would make the PRNG procude only zeros */
+    seed = 0x6b842128; /* Just set it to another number */
+
   rand->mt[0]= seed & 0xffffffff;
   for (rand->mti=1; rand->mti<N; rand->mti++)
     rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]) & 0xffffffff;
diff --git a/grand.c b/grand.c
index 83b1bc3..47b9700 100644 (file)
--- a/grand.c
+++ b/grand.c
@@ -75,31 +75,29 @@ g_rand_new_with_seed (guint32 seed)
 GRand* 
 g_rand_new (void)
 {
-  guint32 seed = 0;
+  guint32 seed;
   GTimeVal now;
-  static gboolean dev_random_exists = TRUE;
+  static gboolean dev_urandom_exists = TRUE;
   
-  if (dev_random_exists)
+  if (dev_urandom_exists)
     {
-      FILE* dev_random = fopen("/dev/random", "rb");
-      if (dev_random)
+      FILE* dev_urandom = fopen("/dev/urandom", "rb");
+      if (dev_urandom)
        {
-         if (fread (&seed, sizeof (seed), 1, dev_random) != 1)
+         if (fread (&seed, sizeof (seed), 1, dev_urandom) != 1)
            seed = 0;
          else
-           dev_random_exists = FALSE;
-         fclose (dev_random);
+           dev_urandom_exists = FALSE;
+         fclose (dev_urandom);
        }       
       else
-       dev_random_exists = FALSE;
+       dev_urandom_exists = FALSE;
+    }
+  if (!dev_urandom_exists)
+    {  
+      g_get_current_time (&now);
+      seed = now.tv_sec ^ now.tv_usec;
     }
-
-  /* Using /dev/random alone makes the seed computable for the
-     outside. This might pose security problems somewhere. This should
-     yield better values */
-
-  g_get_current_time (&now);
-  seed ^= now.tv_sec ^ now.tv_usec;
 
   return g_rand_new_with_seed (seed);
 }
@@ -121,6 +119,10 @@ g_rand_set_seed (GRand* rand, guint32 seed)
   /* the generator Line 25 of Table 1 in          */
   /* [KNUTH 1981, The Art of Computer Programming */
   /*    Vol. 2 (2nd Ed.), pp102]                  */
+  
+  if (seed == 0) /* This would make the PRNG procude only zeros */
+    seed = 0x6b842128; /* Just set it to another number */
+
   rand->mt[0]= seed & 0xffffffff;
   for (rand->mti=1; rand->mti<N; rand->mti++)
     rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]) & 0xffffffff;