1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2013, 2014  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id: random.c,v 1.28 2009/07/16 05:52:46 marka Exp $ */
19 
20 /*! \file */
21 
22 #include <config.h>
23 
24 #include <stdlib.h>
25 #include <time.h>		/* Required for time(). */
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 
33 #include <isc/mutex.h>
34 #include <isc/once.h>
35 #include <isc/random.h>
36 #include <isc/string.h>
37 #include <isc/util.h>
38 
39 static isc_once_t once = ISC_ONCE_INIT;
40 
41 static void
initialize_rand(void)42 initialize_rand(void)
43 {
44 #ifndef HAVE_ARC4RANDOM
45 	unsigned int pid = getpid();
46 
47 	/*
48 	 * The low bits of pid generally change faster.
49 	 * Xor them with the high bits of time which change slowly.
50 	 */
51 	pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
52 
53 	srand((unsigned)time(NULL) ^ pid);
54 #endif
55 }
56 
57 static void
initialize(void)58 initialize(void)
59 {
60 	RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
61 }
62 
63 void
isc_random_seed(isc_uint32_t seed)64 isc_random_seed(isc_uint32_t seed)
65 {
66 	initialize();
67 
68 #ifndef HAVE_ARC4RANDOM
69 	srand(seed);
70 #elif defined(HAVE_ARC4RANDOM_ADDRANDOM)
71 	arc4random_addrandom((u_char *) &seed, sizeof(isc_uint32_t));
72 #else
73 	/*
74 	 * If arcrandom() is available and no corresponding seeding
75 	 * function arc4random_addrandom() is available, no seeding is
76 	 * done on such platforms (e.g., OpenBSD 5.5). This is because
77 	 * the OS itself is supposed to seed the RNG and it is assumed
78 	 * that no explicit seeding is required.
79 	 */
80 #endif
81 }
82 
83 void
isc_random_get(isc_uint32_t * val)84 isc_random_get(isc_uint32_t *val)
85 {
86 	REQUIRE(val != NULL);
87 
88 	initialize();
89 
90 #ifndef HAVE_ARC4RANDOM
91 	/*
92 	 * rand()'s lower bits are not random.
93 	 * rand()'s upper bit is zero.
94 	 */
95 #if RAND_MAX >= 0xfffff
96 	/* We have at least 20 bits.  Use lower 16 excluding lower most 4 */
97 	*val = ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
98 #elif RAND_MAX >= 0x7fff
99 	/* We have at least 15 bits.  Use lower 10/11 excluding lower most 4 */
100 	*val = ((rand() >> 4) & 0x000007ff) | ((rand() << 7) & 0x003ff800) |
101 		((rand() << 18) & 0xffc00000);
102 #else
103 #error RAND_MAX is too small
104 #endif
105 #else
106 	*val = arc4random();
107 #endif
108 }
109 
110 isc_uint32_t
isc_random_jitter(isc_uint32_t max,isc_uint32_t jitter)111 isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
112 	isc_uint32_t rnd;
113 
114 	REQUIRE(jitter < max || (jitter == 0 && max == 0));
115 
116 	if (jitter == 0)
117 		return (max);
118 
119 	isc_random_get(&rnd);
120 	return (max - rnd % jitter);
121 }
122