From edab294f1cc70f296dad0e5ac30e805cfccf3146 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Thu, 3 Apr 2003 08:25:15 -0500 Subject: [PATCH] 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. --- lib/uuid/ChangeLog | 8 ++++++++ lib/uuid/gen_uuid.c | 21 ++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/uuid/ChangeLog b/lib/uuid/ChangeLog index 7a0ec9b..fca6088 100644 --- a/lib/uuid/ChangeLog +++ b/lib/uuid/ChangeLog @@ -1,3 +1,11 @@ +2003-04-03 Theodore Ts'o + + * 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 * Makefile.in: Add support for Apple Darwin diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index 5ebc673..158b6bd 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -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; } -- 1.8.3.1