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