xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/random.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: random.c,v 1.2 2024/08/18 20:47:14 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 1999-2003  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id: random.c,v 1.28 2009/07/16 05:52:46 marka Exp  */
21897be3a4Schristos 
22897be3a4Schristos /*! \file */
23897be3a4Schristos 
24897be3a4Schristos #include <config.h>
25897be3a4Schristos 
26897be3a4Schristos #include <stdlib.h>
27897be3a4Schristos #include <time.h>		/* Required for time(). */
28897be3a4Schristos #ifdef HAVE_SYS_TYPES_H
29897be3a4Schristos #include <sys/types.h>
30897be3a4Schristos #endif
31897be3a4Schristos #ifdef HAVE_UNISTD_H
32897be3a4Schristos #include <unistd.h>
33897be3a4Schristos #endif
34897be3a4Schristos 
35897be3a4Schristos #include <isc/mutex.h>
36897be3a4Schristos #include <isc/once.h>
37897be3a4Schristos #include <isc/random.h>
38897be3a4Schristos #include <isc/string.h>
39897be3a4Schristos #include <isc/util.h>
40897be3a4Schristos 
41897be3a4Schristos static isc_once_t once = ISC_ONCE_INIT;
42897be3a4Schristos 
43897be3a4Schristos static void
44897be3a4Schristos initialize_rand(void)
45897be3a4Schristos {
46897be3a4Schristos #ifndef HAVE_ARC4RANDOM
47897be3a4Schristos 	unsigned int pid = getpid();
48897be3a4Schristos 
49897be3a4Schristos 	/*
50897be3a4Schristos 	 * The low bits of pid generally change faster.
51897be3a4Schristos 	 * Xor them with the high bits of time which change slowly.
52897be3a4Schristos 	 */
53897be3a4Schristos 	pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
54897be3a4Schristos 
55897be3a4Schristos 	srand(time(NULL) ^ pid);
56897be3a4Schristos #endif
57897be3a4Schristos }
58897be3a4Schristos 
59897be3a4Schristos static void
60897be3a4Schristos initialize(void)
61897be3a4Schristos {
62897be3a4Schristos 	RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
63897be3a4Schristos }
64897be3a4Schristos 
65897be3a4Schristos void
66897be3a4Schristos isc_random_seed(isc_uint32_t seed)
67897be3a4Schristos {
68897be3a4Schristos 	initialize();
69897be3a4Schristos 
70897be3a4Schristos #ifndef HAVE_ARC4RANDOM
71897be3a4Schristos 	srand(seed);
72897be3a4Schristos #else
73897be3a4Schristos 	arc4random_addrandom((u_char *) &seed, sizeof(isc_uint32_t));
74897be3a4Schristos #endif
75897be3a4Schristos }
76897be3a4Schristos 
77897be3a4Schristos void
78897be3a4Schristos isc_random_get(isc_uint32_t *val)
79897be3a4Schristos {
80897be3a4Schristos 	REQUIRE(val != NULL);
81897be3a4Schristos 
82897be3a4Schristos 	initialize();
83897be3a4Schristos 
84897be3a4Schristos #ifndef HAVE_ARC4RANDOM
85897be3a4Schristos 	/*
86897be3a4Schristos 	 * rand()'s lower bits are not random.
87897be3a4Schristos 	 * rand()'s upper bit is zero.
88897be3a4Schristos 	 */
89897be3a4Schristos #if RAND_MAX >= 0xfffff
90897be3a4Schristos 	/* We have at least 20 bits.  Use lower 16 excluding lower most 4 */
91897be3a4Schristos 	*val = ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
92897be3a4Schristos #elif RAND_MAX >= 0x7fff
93897be3a4Schristos 	/* We have at least 15 bits.  Use lower 10/11 excluding lower most 4 */
94897be3a4Schristos 	*val = ((rand() >> 4) & 0x000007ff) | ((rand() << 7) & 0x003ff800) |
95897be3a4Schristos 		((rand() << 18) & 0xffc00000);
96897be3a4Schristos #else
97897be3a4Schristos #error RAND_MAX is too small
98897be3a4Schristos #endif
99897be3a4Schristos #else
100897be3a4Schristos 	*val = arc4random();
101897be3a4Schristos #endif
102897be3a4Schristos }
103897be3a4Schristos 
104897be3a4Schristos isc_uint32_t
105897be3a4Schristos isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
106897be3a4Schristos 	isc_uint32_t rnd;
107897be3a4Schristos 
108897be3a4Schristos 	REQUIRE(jitter < max || (jitter == 0 && max == 0));
109897be3a4Schristos 
110897be3a4Schristos 	if (jitter == 0)
111897be3a4Schristos 		return (max);
112897be3a4Schristos 
113897be3a4Schristos 	isc_random_get(&rnd);
114897be3a4Schristos 	return (max - rnd % jitter);
115897be3a4Schristos }
116