]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(isaac_seed): Don't overrun the s->mm buffer.
authorJim Meyering <jim@meyering.net>
Sat, 3 Apr 1999 03:27:57 +0000 (03:27 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 3 Apr 1999 03:27:57 +0000 (03:27 +0000)
Use gethrtime if available.  Don't assume that clock_gettime succeeds.
Put most random sources first.

src/shred.c

index 198a80759f465eddd6e3a3d16f19400fde9e55db..f747a7b7f146526fc370c5ef90d7114d257bc8d9 100644 (file)
@@ -380,55 +380,79 @@ static void
 isaac_seed (struct isaac_state *s)
 {
   char *p = (char *) s->mm;
-#define MIXIN(o) (memcpy (p, (char *) &(o), sizeof (o)), p += sizeof (o))
+  char *lim = p + sizeof s->mm;
+#define MIXIN_BOUND(s) ((s) < lim - p ? (s) : lim - p)
+#define MIXIN(o) \
+    do \
+      { \
+       size_t s = MIXIN_BOUND (sizeof (o)); \
+        memcpy (p, (char *) &(o), s); \
+       p += s; \
+      } \
+    while (0)
+
+  /* Mix in bits of random information from the environment.
+     Mix the most random items first, in case lim - p is small
+     and we have to truncate.  */
 
   {
-    pid_t t = getpid ();
+    int fd = open ("/dev/urandom", O_RDONLY);
+    if (fd >= 0)
+      {
+       size_t s = MIXIN_BOUND (32);
+       read (fd, p, s);
+       p += s;
+       close (fd);
+      }
+    else
+      {
+       fd = open ("/dev/random", O_RDONLY | O_NONBLOCK);
+       if (fd >= 0)
+         {
+           /* /dev/random is more precious, so use less */
+           size_t s = MIXIN_BOUND (16);
+           read (fd, p, s);
+           p += s;
+           close (fd);
+         }
+      }
+  }
+
+#ifdef HAVE_GETHRTIME
+  {
+    hrtime_t t = gethrtime ();
     MIXIN (t);
-    t = getppid ();
+  }
+#endif
+
+#ifdef HAVE_CLOCK_GETTIME
+  {
+    struct timespec t;
+    clock_gettime (CLOCK_REALTIME, &t);
     MIXIN (t);
   }
+#endif
 
   {
-    uid_t t = getuid ();
+    time_t t = time ((time_t *) 0);
     MIXIN (t);
   }
 
   {
-    gid_t t = getgid ();
+    pid_t t = getpid ();
+    MIXIN (t);
+    t = getppid ();
     MIXIN (t);
   }
 
   {
-#ifdef HAVE_CLOCK_GETTIME              /* POSIX ns-resolution */
-    struct timespec t;
-    clock_gettime (CLOCK_REALTIME, &t);
-#else
-    time_t t = time ((time_t *) 0);
-#endif
+    uid_t t = getuid ();
     MIXIN (t);
   }
 
   {
-    int r = 0;
-    int fd = open ("/dev/urandom", O_RDONLY);
-    if (fd >= 0)
-      {
-       r = read (fd, p, 32);
-       close (fd);
-      }
-    else
-      {
-       fd = open ("/dev/random", O_RDONLY | O_NONBLOCK);
-       if (fd >= 0)
-         {
-           /* /dev/random is more precious, so use less */
-           r = read (fd, p, 16);
-           close (fd);
-         }
-      }
-    if (0 < r)
-      p += r;
+    gid_t t = getgid ();
+    MIXIN (t);
   }
 
   isaac_init (s, s->mm, sizeof (s->mm));