1 --- gen_uuid.c.orig 2006-02-19 03:05:40.000000000 -0800
2 +++ gen_uuid.c 2006-02-20 12:51:07.000000000 -0800
8 -#define srand(x) srandom(x)
9 -#define rand() random()
12 -static int get_random_fd(void)
19 - gettimeofday(&tv, 0);
20 - fd = open("/dev/urandom", O_RDONLY);
22 - fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
23 - srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
25 - /* Crank the random number generator a few times */
26 - gettimeofday(&tv, 0);
27 - for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
34 - * Generate a series of random bytes. Use /dev/urandom if possible,
35 - * and if not, use srandom/random.
36 + * Generate a series of random bytes, using arc4random
38 static void get_random_bytes(void *buf, int nbytes)
40 - int i, n = nbytes, fd = get_random_fd();
41 - int lose_counter = 0;
42 unsigned char *cp = (unsigned char *) buf;
44 + int n = nbytes / sizeof(u);
48 - i = read(fd, cp, n);
50 - if (lose_counter++ > 16)
60 + memcpy(cp, &u, sizeof(u));
63 + if ((n = nbytes % sizeof(u)) > 0) {
69 - * We do this all the time, but this is the only source of
70 - * randomness if /dev/random/urandom is out to lunch.
72 - for (cp = buf, i = 0; i < nbytes; i++)
73 - *cp++ ^= (rand() >> 7) & 0xFF;
81 - * This is the generic front-end to uuid_generate_random and
82 - * uuid_generate_time. It uses uuid_generate_random only if
83 - * /dev/urandom is available, since otherwise we won't have
84 - * high-quality randomness.
85 + * This is the generic front-end
87 void uuid_generate(uuid_t out)
89 - if (get_random_fd() >= 0)
90 - uuid_generate_random(out);
92 - uuid_generate_time(out);
93 + uuid_generate_random(out);