Whamcloud - gitweb
gen_uuid.c (get_random_bytes): Always xor in a stream of bytes
authorTheodore Ts'o <tytso@mit.edu>
Thu, 3 Apr 2003 13:25:15 +0000 (08:25 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 3 Apr 2003 13:25:15 +0000 (08:25 -0500)
from the system PRNG (i.e., random/srandom, seeded from
the time, pid, and uid) in case /dev/random isn't doing
the right thing on a particular system.  It doesn't hurt,
and it can help, in the case of a buggy /dev/random.

lib/uuid/ChangeLog
lib/uuid/gen_uuid.c

index 7a0ec9b..fca6088 100644 (file)
@@ -1,3 +1,11 @@
+2003-04-03  Theodore Ts'o  <tytso@mit.edu>
+
+       * gen_uuid.c (get_random_bytes): Always xor in a stream of bytes
+               from the system PRNG (i.e., random/srandom, seeded from
+               the time, pid, and uid) in case /dev/random isn't doing
+               the right thing on a particular system.  It doesn't hurt,
+               and it can help, in the case of a buggy /dev/random.
+
 2003-03-14  Theodore Ts'o  <tytso@mit.edu>
 
        * Makefile.in: Add support for Apple Darwin
index 5ebc673..158b6bd 100644 (file)
@@ -74,27 +74,30 @@ static int get_random_fd(void)
  */
 static void get_random_bytes(void *buf, int nbytes)
 {
-       int i, fd = get_random_fd();
+       int i, n = nbytes, fd = get_random_fd();
        int lose_counter = 0;
-       char *cp = (char *) buf;
+       unsigned char *cp = (unsigned char *) buf;
 
        if (fd >= 0) {
-               while (nbytes > 0) {
-                       i = read(fd, cp, nbytes);
+               while (n > 0) {
+                       i = read(fd, cp, n);
                        if (i <= 0) {
                                if (lose_counter++ > 16)
                                        break;
                                continue;
                        }
-                       nbytes -= i;
+                       n -= i;
                        cp += i;
                        lose_counter = 0;
                }
        }
-
-       /* XXX put something better here if no /dev/random! */
-       for (i = 0; i < nbytes; i++)
-               *cp++ = rand() & 0xFF;
+       
+       /*
+        * We do this all the time, but this is the only source of
+        * randomness if /dev/random/urandom is out to lunch.
+        */
+       for (cp = buf, i = 0; i < nbytes; i++)
+               *cp++ ^= (rand() >> 7) & 0xFF;
        return;
 }