random.c revision 84dc4b3e7eea3e9c8fafa5f4fd632a51ee8b356f
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004, 2005, 2007, 2009, 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Copyright (C) 1999-2003 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * copyright notice and this permission notice appear in all copies.
d36adf470fe38579fd89cb8186a88a3b21a4be14Automatic Updater * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * PERFORMANCE OF THIS SOFTWARE.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * ChaCha based random number generator derived from OpenBSD.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * The original copyright follows:
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Copyright (c) 1996, David Mazieres <dm@uun.org>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Permission to use, copy, modify, and distribute this software for any
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * purpose with or without fee is hereby granted, provided that the above
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * copyright notice and this permission notice appear in all copies.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <time.h> /* Required for time(). */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define RNG_MAGIC ISC_MAGIC('R', 'N', 'G', 'x')
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define VALID_RNG(r) ISC_MAGIC_VALID(r, RNG_MAGIC)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define CHACHA_BUFFERSIZE (16 * CHACHA_BLOCKSIZE)
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt/* ChaCha RNG state */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington isc_entropy_t *entropy; /*%< entropy source */
initialize_rand(void) {
#ifndef HAVE_ARC4RANDOM
initialize(void) {
initialize();
#ifndef HAVE_ARC4RANDOM
initialize();
#ifndef HAVE_ARC4RANDOM
if (jitter == 0)
return (max);
} rnd;
return (ISC_R_NOMEMORY);
return (result);
return (ISC_R_SUCCESS);
if (dest)
#ifndef KEYSTREAM_ONLY
size_t i, m;
static inline isc_uint16_t
sizeof(val));
0, sizeof(val));
return (val);
} rnd;
return (result);
if (r >= min)
return (r % upper_bound);