]>
Commit | Line | Data |
---|---|---|
224c7076 A |
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 | |
3 | @@ -72,62 +72,24 @@ | |
4 | ||
5 | #include "uuidP.h" | |
6 | ||
7 | -#ifdef HAVE_SRANDOM | |
8 | -#define srand(x) srandom(x) | |
9 | -#define rand() random() | |
10 | -#endif | |
11 | - | |
12 | -static int get_random_fd(void) | |
13 | -{ | |
14 | - struct timeval tv; | |
15 | - static int fd = -2; | |
16 | - int i; | |
17 | - | |
18 | - if (fd == -2) { | |
19 | - gettimeofday(&tv, 0); | |
20 | - fd = open("/dev/urandom", O_RDONLY); | |
21 | - if (fd == -1) | |
22 | - fd = open("/dev/random", O_RDONLY | O_NONBLOCK); | |
23 | - srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec); | |
24 | - } | |
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--) | |
28 | - rand(); | |
29 | - return fd; | |
30 | -} | |
31 | - | |
32 | - | |
33 | /* | |
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 | |
37 | */ | |
38 | static void get_random_bytes(void *buf, int nbytes) | |
39 | { | |
40 | - int i, n = nbytes, fd = get_random_fd(); | |
41 | - int lose_counter = 0; | |
42 | unsigned char *cp = (unsigned char *) buf; | |
43 | + u_int32_t u; | |
44 | + int n = nbytes / sizeof(u); | |
45 | ||
46 | - if (fd >= 0) { | |
47 | - while (n > 0) { | |
48 | - i = read(fd, cp, n); | |
49 | - if (i <= 0) { | |
50 | - if (lose_counter++ > 16) | |
51 | - break; | |
52 | - continue; | |
53 | - } | |
54 | - n -= i; | |
55 | - cp += i; | |
56 | - lose_counter = 0; | |
57 | - } | |
58 | + while (n-- > 0) { | |
59 | + u = arc4random(); | |
60 | + memcpy(cp, &u, sizeof(u)); | |
61 | + cp += sizeof(u); | |
62 | + } | |
63 | + if ((n = nbytes % sizeof(u)) > 0) { | |
64 | + u = arc4random(); | |
65 | + memcpy(cp, &u, n); | |
66 | } | |
67 | - | |
68 | - /* | |
69 | - * We do this all the time, but this is the only source of | |
70 | - * randomness if /dev/random/urandom is out to lunch. | |
71 | - */ | |
72 | - for (cp = buf, i = 0; i < nbytes; i++) | |
73 | - *cp++ ^= (rand() >> 7) & 0xFF; | |
74 | return; | |
75 | } | |
76 | ||
77 | @@ -302,15 +264,9 @@ | |
78 | } | |
79 | ||
80 | /* | |
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 | |
86 | */ | |
87 | void uuid_generate(uuid_t out) | |
88 | { | |
89 | - if (get_random_fd() >= 0) | |
90 | - uuid_generate_random(out); | |
91 | - else | |
92 | - uuid_generate_time(out); | |
93 | + uuid_generate_random(out); | |
94 | } |