From: Theodore Ts'o Date: Thu, 3 Apr 2003 13:25:15 +0000 (-0500) Subject: gen_uuid.c (get_random_bytes): Always xor in a stream of bytes X-Git-Tag: E2FSPROGS-1_33-WIP-0414~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=edab294f1cc70f296dad0e5ac30e805cfccf3146;p=thirdparty%2Fe2fsprogs.git 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. --- diff --git a/lib/uuid/ChangeLog b/lib/uuid/ChangeLog index 7a0ec9ba6..fca6088cc 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 5ebc673b4..158b6bd7d 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; }