From 5b76a09697bfc76f5acefd65d5b37b1214d271a8 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Wed, 4 Dec 2002 01:19:28 +0000 Subject: [PATCH] 1405. [func] Use arc4random() if available. from: jakob@crt.se reviewed: marka --- CHANGES | 2 ++ acconfig.h | 5 ++++- bin/tests/genrandom.c | 8 +++++++- configure.in | 7 ++++++- lib/dns/rdataset.c | 10 +++++++++- lib/isc/random.c | 16 +++++++++++++++- 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index e834472982..2ae45c1df1 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,5 @@ +1405. [func] Use arc4random() if available. + 1404. [bug] libbind: ns_name_ntol() could overwite a zero length buffer. diff --git a/acconfig.h b/acconfig.h index b04dc0db4d..814b9fdd69 100644 --- a/acconfig.h +++ b/acconfig.h @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: acconfig.h,v 1.38 2002/10/15 04:32:49 marka Exp $ */ +/* $Id: acconfig.h,v 1.39 2002/12/04 01:19:27 marka Exp $ */ /*** *** This file is not to be included by any public header files, because @@ -76,6 +76,9 @@ /* define if gai_strerror() exists */ #undef HAVE_GAISTRERROR +/* define if arc4random() exists */ +#undef HAVE_ARC4RANDOM + /* define if pthread_setconcurrency() should be called to tell the * OS how many threads we might want to run. */ diff --git a/bin/tests/genrandom.c b/bin/tests/genrandom.c index e90cbacc65..800623ccaf 100644 --- a/bin/tests/genrandom.c +++ b/bin/tests/genrandom.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: genrandom.c,v 1.8 2001/01/09 21:41:04 bwelling Exp $ */ +/* $Id: genrandom.c,v 1.9 2002/12/04 01:19:28 marka Exp $ */ #include @@ -47,9 +47,15 @@ main(int argc, char **argv) { exit(1); } +#ifndef HAVE_ARC4RANDOM srand(0x12345678); +#endif while (bytes > 0) { +#ifndef HAVE_ARC4RANDOM unsigned short int x = (rand() & 0xFFFF); +#else + unsigned short int x = (arc4random() & 0xFFFF); +#endif unsigned char c = x & 0xFF; if (putc(c, fp) == EOF) { printf("error writing to file\n"); diff --git a/configure.in b/configure.in index 917953a495..8b99fb23be 100644 --- a/configure.in +++ b/configure.in @@ -18,7 +18,7 @@ AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)dnl esyscmd([sed "s/^/# /" COPYRIGHT])dnl AC_DIVERT_POP()dnl -AC_REVISION($Revision: 1.333 $) +AC_REVISION($Revision: 1.334 $) AC_INIT(lib/dns/name.c) AC_PREREQ(2.13) @@ -521,6 +521,11 @@ case "$use_randomdev" in ;; esac +# +# Do we have arc4random() ? +# +AC_CHECK_FUNC(arc4random, AC_DEFINE(HAVE_ARC4RANDOM)) + # # Begin pthreads checking. # diff --git a/lib/dns/rdataset.c b/lib/dns/rdataset.c index aca841ca21..2b3c0c22c4 100644 --- a/lib/dns/rdataset.c +++ b/lib/dns/rdataset.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: rdataset.c,v 1.63 2002/04/02 06:06:29 marka Exp $ */ +/* $Id: rdataset.c,v 1.64 2002/12/04 01:19:28 marka Exp $ */ #include @@ -370,7 +370,11 @@ towiresorted(dns_rdataset_t *rdataset, dns_name_t *owner_name, */ for (i = 0; i < count; i++) { dns_rdata_t rdata; +#ifndef HAVE_ARC4RANDOM choice = i + (((u_int)rand()>>3) % (count - i)); +#else + choice = i + (((u_int)arc4random()>>3) % (count - i)); +#endif rdata = shuffled[i]; shuffled[i] = shuffled[choice]; shuffled[choice] = rdata; @@ -385,7 +389,11 @@ towiresorted(dns_rdataset_t *rdataset, dns_name_t *owner_name, /* * "Cyclic" order. */ +#ifndef HAVE_ARC4RANDOM unsigned int j = (((unsigned int)rand()) >> 3) % count; +#else + unsigned int j = (((unsigned int)arc4random()) >> 3) % count; +#endif for (i = 0; i < count; i++) { if (order != NULL) sorted[j].key = (*order)(&shuffled[i], diff --git a/lib/isc/random.c b/lib/isc/random.c index 4ed13d977d..c52b9a9957 100644 --- a/lib/isc/random.c +++ b/lib/isc/random.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: random.c,v 1.15 2001/01/09 21:56:22 bwelling Exp $ */ +/* $Id: random.c,v 1.16 2002/12/04 01:19:28 marka Exp $ */ #include @@ -33,7 +33,9 @@ static isc_once_t once = ISC_ONCE_INIT; static void initialize_rand(void) { +#ifndef HAVE_ARC4RANDOM srand(time(NULL)); +#endif } static void @@ -47,7 +49,11 @@ isc_random_seed(isc_uint32_t seed) { initialize(); +#ifndef HAVE_ARC4RANDOM srand(seed); +#else + arc4random_addrandom((u_char *) &seed, sizeof(isc_uint32_t)); +#endif } void @@ -57,7 +63,11 @@ isc_random_get(isc_uint32_t *val) initialize(); +#ifndef HAVE_ARC4RANDOM *val = rand(); +#else + *val = arc4random(); +#endif } isc_uint32_t @@ -66,5 +76,9 @@ isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) { if (jitter == 0) return (max); else +#ifndef HAVE_ARC4RANDOM return (max - rand() % jitter); +#else + return (max - arc4random() % jitter); +#endif }