--- gen_uuid.c.orig 2006-02-19 03:05:40.000000000 -0800 +++ gen_uuid.c 2006-02-20 12:51:07.000000000 -0800 @@ -72,62 +72,24 @@ #include "uuidP.h" -#ifdef HAVE_SRANDOM -#define srand(x) srandom(x) -#define rand() random() -#endif - -static int get_random_fd(void) -{ - struct timeval tv; - static int fd = -2; - int i; - - if (fd == -2) { - gettimeofday(&tv, 0); - fd = open("/dev/urandom", O_RDONLY); - if (fd == -1) - fd = open("/dev/random", O_RDONLY | O_NONBLOCK); - srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); - } - /* Crank the random number generator a few times */ - gettimeofday(&tv, 0); - for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--) - rand(); - return fd; -} - - /* - * Generate a series of random bytes. Use /dev/urandom if possible, - * and if not, use srandom/random. + * Generate a series of random bytes, using arc4random */ static void get_random_bytes(void *buf, int nbytes) { - int i, n = nbytes, fd = get_random_fd(); - int lose_counter = 0; unsigned char *cp = (unsigned char *) buf; + u_int32_t u; + int n = nbytes / sizeof(u); - if (fd >= 0) { - while (n > 0) { - i = read(fd, cp, n); - if (i <= 0) { - if (lose_counter++ > 16) - break; - continue; - } - n -= i; - cp += i; - lose_counter = 0; - } + while (n-- > 0) { + u = arc4random(); + memcpy(cp, &u, sizeof(u)); + cp += sizeof(u); + } + if ((n = nbytes % sizeof(u)) > 0) { + u = arc4random(); + memcpy(cp, &u, n); } - - /* - * 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; } @@ -302,15 +264,9 @@ } /* - * This is the generic front-end to uuid_generate_random and - * uuid_generate_time. It uses uuid_generate_random only if - * /dev/urandom is available, since otherwise we won't have - * high-quality randomness. + * This is the generic front-end */ void uuid_generate(uuid_t out) { - if (get_random_fd() >= 0) - uuid_generate_random(out); - else - uuid_generate_time(out); + uuid_generate_random(out); }