arc4random.c

filter: Statementstmt 69.9% 79 / 113 Branchbran 37.9% 22 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 59.0% 101 / 171 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/BSD/arc4random.pm ↑ Summary arc4rnd_xs.c »
1 static const char __vcsid[] = "@(#) MirOS contributed arc4random.c (old)"
2 "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $"
3 ;
4  
5 /*-
6 * Arc4 random number generator for OpenBSD.
7 * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
8 *
9 * Modification and redistribution in source and binary forms is
10 * permitted provided that due credit is given to the author and the
11 * OpenBSD project by leaving this copyright notice intact.
12 */
13  
14 /*-
15 * This code is derived from section 17.1 of Applied Cryptography,
16 * second edition, which describes a stream cipher allegedly
17 * compatible with RSA Labs "RC4" cipher (the actual description of
18 * which is a trade secret). The same algorithm is used as a stream
19 * cipher called "arcfour" in Tatu Ylonen's ssh package.
20 *
21 * Here the stream cipher has been modified always to include the time
22 * when initializing the state. That makes it impossible to
23 * regenerate the same random sequence twice, so this can't be used
24 * for encryption, but will generate good random numbers.
25 *
26 * RC4 is a registered trademark of RSA Laboratories.
27 */
28  
29 /*-
30 * Modified by Robert Connolly from OpenBSD lib/libc/crypt/arc4random.c v1.11.
31 * This is arc4random(3) using urandom.
32 */
33  
34 /*-
35 * Copyright (c) 2008, 2009, 2010
36 * Thorsten Glaser <tg@mirbsd.org>
37 * This is arc4random(3) made more portable,
38 * as well as arc4random_pushb(3) for Cygwin.
39 *
40 * Provided that these terms and disclaimer and all copyright notices
41 * are retained or reproduced in an accompanying document, permission
42 * is granted to deal in this work without restriction, including un-
43 * limited rights to use, publicly perform, distribute, sell, modify,
44 * merge, give away, or sublicence.
45 *
46 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
47 * the utmost extent permitted by applicable law, neither express nor
48 * implied; without malicious intent or gross negligence. In no event
49 * may a licensor, author or contributor be held liable for indirect,
50 * direct, other damage, loss, or other issues arising in any way out
51 * of dealing in the work, even if advised of the possibility of such
52 * damage or existence of a defect, except proven that it results out
53 * of said person's immediate fault when using the work as intended.
54 */
55  
56 #include <sys/param.h>
57 #include <sys/types.h>
58 #include <sys/time.h>
59 #if defined(HAVE_SYS_SYSCTL_H) && HAVE_SYS_SYSCTL_H
60 #include <sys/sysctl.h>
61 #endif
62 #include <fcntl.h>
63 #if defined(HAVE_STDINT_H) && HAVE_STDINT_H
64 #include <stdint.h>
65 #elif defined(USE_INTTYPES)
66 #include <inttypes.h>
67 #endif
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72  
73 #if defined(__CYGWIN__) || defined(WIN32)
74 #define USE_MS_CRYPTOAPI
75 #define REDEF_USCORETYPES
76 #endif
77  
78 #ifdef USE_MS_CRYPTOAPI
79 #define WIN32_WINNT 0x400
80 #define _WIN32_WINNT 0x400
81 #include <windows.h>
82 #include <wincrypt.h>
83  
84 static uint8_t w32_buf[16*16384]; /* force reseed */
85 static uint8_t w32_hklm[80]; /* registry key (MS, Admin) */
86 static uint8_t w32_hkcu[256]; /* registry key (per user) */
87 static struct timeval w32_last; /* last time CGR was used */
88  
89 static char w32_subkey[] = "SOFTWARE\\Microsoft\\Cryptography\\RNG";
90 #endif
91  
92 #ifndef MIN
93 #define MIN(a,b) (((a)<(b))?(a):(b))
94 #endif
95 #ifndef MAX
96 #define MAX(a,b) (((a)>(b))?(a):(b))
97 #endif
98  
99 #ifdef REDEF_USCORETYPES
100 #define u_int32_t uint32_t
101 #endif
102  
103 #ifndef _PATH_URANDOM
104 #define _PATH_URANDOM "/dev/urandom"
105 #endif
106  
107 static struct arc4_stream {
108 uint8_t i;
109 uint8_t j;
110 uint8_t s[256];
111 } arc4_ctx;
112  
113 static int rs_initialized;
114 static pid_t arc4_stir_pid;
115 static int arc4_count;
116 static const char __randomdev[] = _PATH_URANDOM;
117  
118 static uint8_t arc4_getbyte(void);
119 static void stir_finish(uint8_t);
120 static void arc4_atexit(void);
121 static char arc4_writeback(uint8_t *, size_t, char);
122  
123 #ifndef arc4random_pushk
124 u_int32_t arc4random(void);
125 void arc4random_addrandom(u_char *, int);
126 void arc4random_stir(void);
127 #if defined(USE_MS_CRYPTOAPI) || defined(OPPORTUNISTIC_ROOT_PUSHB)
128 uint32_t arc4random_pushb(const void *, size_t);
129 #endif
130 #endif
131  
132 #define NEED_UNIFORM_BUF_PROTO
133 #if defined(__OpenBSD__) && defined(OpenBSD) && (OpenBSD > 200805)
134 #undef NEED_UNIFORM_BUF_PROTO
135 #elif defined(__MirBSD__) && defined(MirBSD) && (MirBSD > 0x0AA4)
136 #undef NEED_UNIFORM_BUF_PROTO
137 #endif
138  
139 #ifdef NEED_UNIFORM_BUF_PROTO
140 u_int32_t arc4random_uniform(u_int32_t);
141 void arc4random_buf(void *, size_t);
142 #endif
143  
144 static void
145 1 arc4_init(void)
146 {
147 int n;
148  
149 257 ▶ for (n = 0; n < 256; n++)
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; n < 256; n++) 256 1 257
150 256 arc4_ctx.s[n] = (uint8_t)n;
151 1 arc4_ctx.i = 0;
152 1 arc4_ctx.j = 0;
153 1 }
154  
155 static void
156 4 arc4_addrandom(const u_char *dat, size_t datlen)
157 {
158 4 size_t n = 0;
159 uint8_t si;
160  
161 4 arc4_ctx.i--;
162 1028 ▶ while (n < 256) {
Branch: 1 branch
BranchTrueFalseTotal
while (n < 256) { 1024 4 1028
163 1024 arc4_ctx.i++;
164 1024 si = arc4_ctx.s[arc4_ctx.i];
165 1024 arc4_ctx.j = (uint8_t)(arc4_ctx.j + si + dat[n++ % datlen]);
166 1024 arc4_ctx.s[arc4_ctx.i] = arc4_ctx.s[arc4_ctx.j];
167 1024 arc4_ctx.s[arc4_ctx.j] = si;
168 }
169 4 arc4_ctx.i++;
170 4 arc4_ctx.j = arc4_ctx.i;
171 4 }
172  
173 #if defined(USE_MS_CRYPTOAPI)
174 #define RNDEV_BYTES 128
175 #elif defined(__INTERIX)
176 #define RNDEV_BYTES 4 /* slow /dev/urandom */
177 #elif defined(__OpenBSD__)
178 #define RNDEV_BYTES (256 - (sizeof(struct timeval) + sizeof(pid_t)))
179 #elif defined(__CYGWIN__)
180 #define RNDEV_BYTES 64 /* /dev/urandom probably CryptoAPI */
181 #elif defined(__FreeBSD__)
182 #define RNDEV_BYTES 16 /* Yarrow has few state */
183 #elif defined(__GLIBC__)
184 #define RNDEV_BYTES 16 /* requested by maintainers */
185 #else
186 #define RNDEV_BYTES 8 /* unknown OS? */
187 #endif
188  
189 static void
190 1 arc4_stir(void)
191 {
192 int fd;
193 struct {
194 struct timeval tv;
195 pid_t pid;
196 u_int rnd[(RNDEV_BYTES + sizeof(u_int) - 1) / sizeof(u_int)];
197 } rdat;
198 1 size_t sz = 0;
199  
200 1 gettimeofday(&rdat.tv, NULL);
201 1 rdat.pid = getpid();
202 1 memcpy(rdat.rnd, __vcsid, MIN(sizeof(__vcsid), sizeof(rdat.rnd)));
203  
204 #ifdef USE_MS_CRYPTOAPI
205 if (arc4_writeback((char *)rdat.rnd, sizeof(rdat.rnd), 1))
206 goto stir_okay;
207 #endif
208  
209 /* /dev/urandom is a multithread interface, sysctl is not. */
210 /* Try to use /dev/urandom before sysctl. */
211 1 fd = open(__randomdev, O_RDONLY);
212 1 ▶ if (fd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd != -1) { 1 0 1
213 1 sz = (size_t)read(fd, rdat.rnd, sizeof(rdat.rnd));
214 1 close(fd);
215 }
216 1 ▶ if (sz > sizeof(rdat.rnd))
Branch: 1 branch
BranchTrueFalseTotal
if (sz > sizeof(rdat.rnd)) 0 1 1
217 0 sz = 0;
218 if (fd == -1 || sz != sizeof(rdat.rnd)) {
219 /* /dev/urandom failed? Maybe we're in a chroot. */
220 #if /* Linux */ defined(_LINUX_SYSCTL_H) || \
221 /* OpenBSD */ (defined(CTL_KERN) && defined(KERN_ARND))
222 int mib[3], nmib = 3;
223 size_t i = sz / sizeof(u_int), len;
224  
225 #ifdef _LINUX_SYSCTL_H
226 mib[0] = CTL_KERN;
227 mib[1] = KERN_RANDOM;
228 mib[2] = RANDOM_UUID;
229 #else
230 mib[0] = CTL_KERN;
231 mib[1] = KERN_ARND;
232 nmib = 2;
233 #endif
234  
235 while (i < sizeof(rdat.rnd) / sizeof(u_int)) {
236 len = sizeof(u_int);
237 if (sysctl(mib, nmib, &rdat.rnd[i++], &len,
238 NULL, 0) == -1) {
239 fputs("warning: no entropy source\n", stderr);
240 break;
241 }
242 }
243 #else
244 /* XXX kFreeBSD doesn't seem to have KERN_ARND or so */
245 ;
246 #endif
247 }
248  
249 #ifdef USE_MS_CRYPTOAPI
250 stir_okay:
251 #endif
252 1 fd = arc4_getbyte();
253  
254 /*
255 * Time to give up. If no entropy could be found then we will just
256 * use gettimeofday and getpid.
257 */
258 1 arc4_addrandom((u_char *)&rdat, sizeof(rdat));
259  
260 1 stir_finish(fd);
261 1 }
262  
263 static void
264 1 stir_finish(uint8_t av)
265 {
266 size_t n;
267 uint8_t tb[16];
268  
269 1 arc4_stir_pid = getpid();
270  
271 /*
272 * Discard early keystream, as per recommendations in:
273 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
274 * We discard 256 words. A long word is 4 bytes.
275 * We also discard a randomly fuzzed amount.
276 */
277 1 n = 256 * 4 + (arc4_getbyte() & 0x0FU) + (av & 0xF0U);
278 1 av &= 0x0FU;
279 1030 ▶ while (n--)
Branch: 1 branch
BranchTrueFalseTotal
while (n--) 1029 1 1030
280 1029 arc4_getbyte();
281 17 ▶ while (++n < sizeof(tb))
Branch: 1 branch
BranchTrueFalseTotal
while (++n < sizeof(tb)) 16 1 17
282 16 tb[n] = arc4_getbyte();
283 1 ▶ if (arc4_writeback(tb, sizeof(tb), 0))
Branch: 1 branch
BranchTrueFalseTotal
if (arc4_writeback(tb, sizeof(tb), 0)) 1 0 1
284 1 arc4_getbyte();
285 3 ▶ while (av--)
Branch: 1 branch
BranchTrueFalseTotal
while (av--) 2 1 3
286 2 arc4_getbyte();
287 1 arc4_count = 1600000;
288 1 }
289  
290 static uint8_t
291 1290 arc4_getbyte(void)
292 {
293 uint8_t si, sj;
294  
295 1290 arc4_ctx.i++;
296 1290 si = arc4_ctx.s[arc4_ctx.i];
297 1290 arc4_ctx.j = (uint8_t)(arc4_ctx.j + si);
298 1290 sj = arc4_ctx.s[arc4_ctx.j];
299 1290 arc4_ctx.s[arc4_ctx.i] = sj;
300 1290 arc4_ctx.s[arc4_ctx.j] = si;
301 1290 return (arc4_ctx.s[(si + sj) & 0xff]);
302 }
303  
304 static uint32_t
305 0 arc4_getword(void)
306 {
307 uint32_t val;
308 0 val = (uint32_t)arc4_getbyte() << 24;
309 0 val |= (uint32_t)arc4_getbyte() << 16;
310 0 val |= (uint32_t)arc4_getbyte() << 8;
311 0 val |= (uint32_t)arc4_getbyte();
312 0 return (val);
313 }
314  
315 void
316 1 arc4random_stir(void)
317 {
318 1 ▶ if (!rs_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rs_initialized) { 1 0 1
319 1 arc4_init();
320 1 rs_initialized = 1;
321 1 atexit(arc4_atexit);
322 }
323 1 arc4_stir();
324 1 }
325  
326 void
327 3 arc4random_addrandom(u_char *dat, int datlen)
328 {
329 3 ▶ if (!rs_initialized)
Branch: 1 branch
BranchTrueFalseTotal
if (!rs_initialized) 1 2 3
330 1 arc4random_stir();
331 3 arc4_addrandom(dat, datlen);
332 3 }
333  
334 u_int32_t
335 arc4random(void)
336 {
337 0 arc4_count -= 4;
338 0 ▶ if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid())
Branch: 3 branches
BranchTrueFalseTotal
if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid()) 0 0 0
if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid()) 0 0 0
if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid()) 0 0 0
339 0 arc4random_stir();
340 0 return arc4_getword();
341 }
342  
343 /*
344 * Returns 0 if write error; 0 if do_rd and read error;
345 * 1 if !do_rd and read error but not write error;
346 * 1 if no error occured.
347 */
348 static char
349 2 arc4_writeback(uint8_t *buf, size_t len, char do_rd)
350 {
351 #ifdef USE_MS_CRYPTOAPI
352 static char has_provider = 0;
353 static HCRYPTPROV p;
354 HKEY hKeyLM, hKeyCU;
355 DWORD ksz;
356 char rc = 6, has_rkey = 0, w32_a4b[16];
357 size_t i, j, xlen;
358 struct timeval tv;
359  
360 for (i = 0; i < sizeof(w32_a4b); ++i)
361 w32_a4b[i] = arc4_getbyte();
362 for (i = arc4_getbyte() & 15; i; --i)
363 arc4_getbyte();
364  
365 ksz = sizeof(w32_buf);
366 if ((/* read-write */ RegOpenKeyEx(HKEY_LOCAL_MACHINE, w32_subkey,
367 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKeyLM) == ERROR_SUCCESS ||
368 /* try read-only */ RegOpenKeyEx(HKEY_LOCAL_MACHINE, w32_subkey,
369 0, KEY_QUERY_VALUE, &hKeyLM) == ERROR_SUCCESS) && /* get value */
370 (RegQueryValueEx(hKeyLM, "Seed", NULL, NULL, w32_buf, &ksz) ==
371 ERROR_SUCCESS) && /* got any content? */ ksz) {
372 /* we got HKLM key, read-write or read-only */
373 has_rkey |= 1;
374 /* move content to destination */
375 memset(w32_hklm, '\0', sizeof(w32_hklm));
376 for (i = 0; i < MAX(ksz, sizeof(w32_hklm)); ++i)
377 w32_hklm[i % sizeof(w32_hklm)] ^= w32_buf[i % ksz];
378 }
379 ksz = sizeof(w32_buf);
380 if ((/* read-write */ RegCreateKeyEx(HKEY_CURRENT_USER, w32_subkey,
381 0, NULL, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, &hKeyCU,
382 NULL) == ERROR_SUCCESS || /* R/O */ RegOpenKeyEx(HKEY_CURRENT_USER,
383 w32_subkey, 0, KEY_QUERY_VALUE, &hKeyLM) == ERROR_SUCCESS) &&
384 /* get value */ (RegQueryValueEx(hKeyLM, "Seed", NULL, NULL,
385 w32_buf, &ksz) == ERROR_SUCCESS) && /* got any content? */ ksz) {
386 /* we got HKCU key, created, read-write or read-only */
387 has_rkey |= 2;
388 /* move content to destination */
389 memset(w32_hkcu, '\0', sizeof(w32_hkcu));
390 for (i = 0; i < MAX(ksz, sizeof(w32_hkcu)); ++i)
391 w32_hkcu[i % sizeof(w32_hkcu)] ^= w32_buf[i % ksz];
392 }
393  
394 if (!do_rd)
395 goto nogen_out;
396  
397 if (has_rkey && gettimeofday(&tv, NULL) == 0) {
398 /* we have registry key; rate-limit CryptGenRandom */
399 if (tv.tv_sec - w32_last.tv_sec < 128 + (arc4_getbyte() & 127))
400 goto nogen_out;
401 /* nope, more than 2-4 minutes, call it */
402 w32_last.tv_sec = tv.tv_sec;
403 }
404  
405 if (!has_provider) {
406 if (!CryptAcquireContext(&p, NULL, NULL, PROV_RSA_FULL, 0)) {
407 if ((HRESULT)GetLastError() != NTE_BAD_KEYSET)
408 goto nogen_out;
409 if (!CryptAcquireContext(&p, NULL, NULL, PROV_RSA_FULL,
410 CRYPT_NEWKEYSET))
411 goto nogen_out;
412 }
413 has_provider = 1;
414 }
415 i = 0;
416 while (i < 256)
417 w32_buf[i++] = arc4_getbyte();
418 if (!CryptGenRandom(p, sizeof(w32_buf), w32_buf)) {
419 w32_last.tv_sec = 0;
420 nogen_out:
421 rc |= 1;
422 memset(w32_buf, '\0', 256);
423 }
424 xlen = MIN(sizeof(w32_buf) - sizeof(w32_hklm) - sizeof(w32_hkcu) -
425 sizeof(w32_a4b), len);
426 j = xlen + sizeof(w32_hklm) + sizeof(w32_hkcu) + sizeof(w32_a4b);
427 for (i = 0; i < MAX(j, len); ++i)
428 w32_buf[i % j] ^= w32_hklm[i % sizeof(w32_hklm)] ^
429 w32_hkcu[i % sizeof(w32_hkcu)] ^ buf[i % len] ^
430 arc4_getbyte();
431 if (has_rkey & 1) {
432 if (RegSetValueEx(hKeyLM, "Seed", 0, REG_BINARY,
433 w32_buf, sizeof(w32_hklm)) == ERROR_SUCCESS)
434 rc &= ~2;
435 RegCloseKey(hKeyLM);
436 }
437 if (has_rkey & 2) {
438 if (RegSetValueEx(hKeyCU, "Seed", 0, REG_BINARY,
439 w32_buf + sizeof(w32_hklm), sizeof(w32_hkcu)) ==
440 ERROR_SUCCESS)
441 rc &= ~4;
442 RegCloseKey(hKeyCU);
443 }
444 for (i = 0; i < sizeof(w32_a4b); ++i)
445 w32_a4b[i] ^= w32_buf[sizeof(w32_hklm) + sizeof(w32_hkcu) + i];
446 arc4_addrandom(w32_a4b, sizeof(w32_a4b));
447  
448 i = sizeof(w32_hklm) + sizeof(w32_hkcu) + sizeof(w32_a4b);
449 while (len) {
450 j = MIN(len, xlen);
451 memcpy(buf, w32_buf + i, j);
452 buf += j;
453 len -= j;
454 }
455  
456 memset(w32_buf, '\0', sizeof(w32_buf));
457  
458 return (
459 /* read error occured */
460 (!has_rkey && (rc & 1)) ? 0 :
461 /* don't care about write errors */
462 !do_rd ? 1 :
463 /* couldn't write */
464 (rc & 6) == 6 ? 0 :
465 /* at least one RegSetValueEx succeeded */
466 1);
467 #elif defined(arc4random_pushk)
468 uint32_t num;
469  
470 num = arc4random_pushk(buf, len);
471 memcpy(buf, &num, sizeof(num));
472 return (do_rd ? 0 : 1);
473 #else
474 int fd;
475  
476 2 ▶ if ((fd = open(__randomdev, O_WRONLY)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((fd = open(__randomdev, O_WRONLY)) != -1) { 2 0 2
477 2 ▶ if (write(fd, buf, len) < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (write(fd, buf, len) < 4) 0 2 2
478 0 do_rd = 1;
479 2 close(fd);
480 }
481 2 ▶ return (do_rd || fd == -1 ? 0 : 1);
Branch: 2 branches
BranchTrueFalseTotal
return (do_rd || fd == -1 ? 0 : 1); 2 0 2
return (do_rd || fd == -1 ? 0 : 1); 2 0 2
482 #endif
483 }
484  
485 #if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk) || \
486 defined(OPPORTUNISTIC_ROOT_PUSHB)
487 uint32_t
488 arc4random_pushb(const void *src, size_t len)
489 {
490 size_t rlen;
491 union {
492 uint8_t buf[256];
493 struct {
494 struct timeval tv;
495 const void *sp, *dp;
496 size_t sz;
497 uint32_t vu;
498 } s;
499 uint32_t xbuf;
500 } idat;
501 uint32_t res = 1;
502  
503 if (!rs_initialized) {
504 arc4_init();
505 rs_initialized = 1;
506 }
507  
508 idat.s.sp = &idat;
509 idat.s.dp = src;
510 idat.s.sz = len;
511 idat.s.vu = arc4_getword();
512 gettimeofday(&idat.s.tv, NULL);
513  
514 rlen = MAX(sizeof(idat.s), len);
515 while (rlen--)
516 idat.buf[rlen % sizeof(idat.buf)] ^=
517 ((const uint8_t *)src)[rlen % len];
518 rlen = MIN(sizeof(idat), MAX(sizeof(idat.s), len));
519  
520 if (arc4_writeback((void *)&idat, rlen, 1))
521 res = 0;
522 arc4_addrandom((void *)&idat, rlen);
523 rlen = arc4_getbyte() & 1;
524 if (res)
525 res = idat.xbuf;
526 else
527 /* we got entropy from the kernel, so consider us stirred */
528 stir_finish(idat.buf[5]);
529 if (rlen)
530 (void)arc4_getbyte();
531 return (res ^ arc4_getword());
532 }
533 #endif
534  
535 static void
536 1 arc4_atexit(void)
537 {
538 struct {
539 pid_t spid;
540 int cnt;
541 uint8_t carr[240];
542 } buf;
543 1 int i = 0;
544  
545 241 ▶ while (i < 240)
Branch: 1 branch
BranchTrueFalseTotal
while (i < 240) 240 1 241
546 240 buf.carr[i++] = arc4_getbyte();
547 1 buf.spid = arc4_stir_pid;
548 1 buf.cnt = arc4_count;
549  
550 1 arc4_writeback((uint8_t *)&buf, sizeof(buf), 0);
551 1 }
552  
553 void
554 arc4random_buf(void *_buf, size_t n)
555 {
556 0 uint8_t *buf = (uint8_t *)_buf;
557  
558 0 ▶ if (!rs_initialized || arc4_stir_pid != getpid())
Branch: 2 branches
BranchTrueFalseTotal
if (!rs_initialized || arc4_stir_pid != getpid()) 0 0 0
if (!rs_initialized || arc4_stir_pid != getpid()) 0 0 0
559 0 arc4random_stir();
560 0 buf[0] = arc4_getbyte() % 3;
561 0 ▶ while (buf[0]--)
Branch: 1 branch
BranchTrueFalseTotal
while (buf[0]--) 0 0 0
562 0 (void)arc4_getbyte();
563 0 ▶ while (n--) {
Branch: 1 branch
BranchTrueFalseTotal
while (n--) { 0 0 0
564 0 ▶ if (--arc4_count <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--arc4_count <= 0) 0 0 0
565 0 arc4_stir();
566 0 buf[n] = arc4_getbyte();
567 }
568 0 }
569  
570 /*-
571 * Written by Damien Miller.
572 * With simplifications by Jinmei Tatuya.
573 */
574  
575 /*
576 * Calculate a uniformly distributed random number less than
577 * upper_bound avoiding "modulo bias".
578 *
579 * Uniformity is achieved by generating new random numbers
580 * until the one returned is outside the range
581 * [0, 2^32 % upper_bound[. This guarantees the selected
582 * random number will be inside the range
583 * [2^32 % upper_bound, 2^32[ which maps back to
584 * [0, upper_bound[ after reduction modulo upper_bound.
585 */
586 uint32_t
587 arc4random_uniform(uint32_t upper_bound)
588 {
589 uint32_t r, min;
590  
591 0 ▶ if (upper_bound < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (upper_bound < 2) 0 0 0
592 0 return (0);
593  
594 #if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL)
595 0 min = 0x100000000UL % upper_bound;
596 #else
597 /* calculate (2^32 % upper_bound) avoiding 64-bit math */
598 if (upper_bound > 0x80000000U)
599 /* 2^32 - upper_bound (only one "value area") */
600 min = 1 + ~upper_bound;
601 else
602 /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
603 min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
604 #endif
605  
606 /*
607 * This could theoretically loop forever but each retry has
608 * p > 0.5 (worst case, usually far better) of selecting a
609 * number inside the range we need, so it should rarely need
610 * to re-roll (at all).
611 */
612 0 arc4_count -= 4;
613 0 ▶ if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0)
Branch: 3 branches
BranchTrueFalseTotal
if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0) 0 0 0
if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0) 0 0 0
if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0) 0 0 0
614 0 arc4random_stir();
615 0 ▶ if (arc4_getbyte() & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (arc4_getbyte() & 1) 0 0 0
616 0 (void)arc4_getbyte();
617 do {
618 0 r = arc4_getword();
619 0 ▶ } while (r < min);
Branch: 1 branch
BranchTrueFalseTotal
} while (r < min); 0 0 0
620  
621 0 return (r % upper_bound);
622 }
« blib/lib/BSD/arc4random.pm ↑ Summary arc4rnd_xs.c »