random.c File Reference

ChaCha based random number generator derived from OpenBSD. More...

#include <config.h>
#include <stdlib.h>
#include <time.h>
#include <isc/magic.h>
#include <isc/mutex.h>
#include <isc/once.h>
#include <isc/mem.h>
#include <isc/entropy.h>
#include <isc/random.h>
#include <isc/string.h>
#include <isc/util.h>
#include "chacha_private.h"

Go to the source code of this file.

Data Structures

struct  isc_rng

Defines

#define RNG_MAGIC   ISC_MAGIC('R', 'N', 'G', 'x')
#define VALID_RNG(r)   ISC_MAGIC_VALID(r, RNG_MAGIC)
#define KEYSTREAM_ONLY
#define CHACHA_KEYSIZE   32U
#define CHACHA_IVSIZE   8U
#define CHACHA_BLOCKSIZE   64
#define CHACHA_BUFFERSIZE   (16 * CHACHA_BLOCKSIZE)

Functions

static void initialize_rand (void)
static void initialize (void)
void isc_random_seed (isc_uint32_t seed)
 Set the initial seed of the random state.
void isc_random_get (isc_uint32_t *val)
 Get a random value.
isc_uint32_t isc_random_jitter (isc_uint32_t max, isc_uint32_t jitter)
 Get a random value between (max - jitter) and (max). This is useful for jittering timer values.
static void chacha_reinit (isc_rng_t *rng, isc_uint8_t *buffer, size_t n)
isc_result_t isc_rng_create (isc_mem_t *mctx, isc_entropy_t *entropy, isc_rng_t **rngp)
 Creates and initializes a pseudo random number generator. The returned RNG can be used to generate pseudo random numbers.
void isc_rng_attach (isc_rng_t *source, isc_rng_t **targetp)
 Increments a reference count on the passed RNG.
static void destroy (isc_rng_t *rng)
void isc_rng_detach (isc_rng_t **rngp)
 Decrements a reference count on the passed RNG. If the reference count reaches 0, the RNG is destroyed.
static void chacha_rekey (isc_rng_t *rng, u_char *dat, size_t datlen)
static isc_uint16_t chacha_getuint16 (isc_rng_t *rng)
static void chacha_stir (isc_rng_t *rng)
isc_uint16_t isc_rng_random (isc_rng_t *rng)
 Returns a pseudo random 16-bit unsigned integer.
isc_uint16_t isc_rng_uniformrandom (isc_rng_t *rng, isc_uint16_t upper_bound)
 Returns a uniformly distributed pseudo random 16-bit unsigned integer.

Variables

static isc_once_t once = ISC_ONCE_INIT


Detailed Description

ChaCha based random number generator derived from OpenBSD.

The original copyright follows: Copyright (c) 1996, David Mazieres <dm@uun.org> Copyright (c) 2008, Damien Miller <djm@openbsd.org> Copyright (c) 2013, Markus Friedl <markus@openbsd.org>

Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Definition in file random.c.


Define Documentation

#define RNG_MAGIC   ISC_MAGIC('R', 'N', 'G', 'x')

Definition at line 61 of file random.c.

Referenced by isc_rng_create().

#define VALID_RNG ( r   )     ISC_MAGIC_VALID(r, RNG_MAGIC)

Definition at line 62 of file random.c.

Referenced by chacha_getuint16(), chacha_rekey(), chacha_stir(), destroy(), isc_rng_attach(), isc_rng_detach(), isc_rng_random(), and isc_rng_uniformrandom().

#define KEYSTREAM_ONLY

Definition at line 64 of file random.c.

#define CHACHA_KEYSIZE   32U

Definition at line 67 of file random.c.

Referenced by chacha_reinit(), and chacha_rekey().

#define CHACHA_IVSIZE   8U

Definition at line 68 of file random.c.

Referenced by chacha_reinit(), and chacha_rekey().

#define CHACHA_BLOCKSIZE   64

Definition at line 69 of file random.c.

#define CHACHA_BUFFERSIZE   (16 * CHACHA_BLOCKSIZE)

Definition at line 70 of file random.c.

Referenced by chacha_getuint16(), chacha_rekey(), chacha_stir(), and isc_rng_create().


Function Documentation

static void initialize_rand ( void   )  [static]

Definition at line 88 of file random.c.

Referenced by initialize().

static void initialize ( void   )  [static]

Definition at line 103 of file random.c.

References initialize_rand(), isc_once_do, ISC_R_SUCCESS, once, and RUNTIME_CHECK.

void isc_random_seed ( isc_uint32_t  seed  ) 

Set the initial seed of the random state.

Definition at line 108 of file random.c.

References initialize().

void isc_random_get ( isc_uint32_t val  ) 

Get a random value.

Requires: val != NULL.

Definition at line 126 of file random.c.

References initialize(), and REQUIRE.

Referenced by ATF_TC_BODY(), chacha_stir(), control_recvmessage(), dns_acache_createentry(), entry_stale(), expirenode(), fctx_cancelquery(), followup_lookup(), generate_salt(), get_dispsocket(), insert_nodes(), isc_file_openuniquemode(), isc_file_renameunique(), isc_hash_ctxinit(), isc_pool_get(), isc_random_jitter(), isc_rng_create(), isc_taskpool_gettask(), main(), new_adbentry(), ns_client_addopt(), remove_nodes(), set_resigntime(), setup_lookup(), towiresorted(), xfrin_create(), zone_nsec3chain(), zone_resigninc(), and zone_sign().

isc_uint32_t isc_random_jitter ( isc_uint32_t  max,
isc_uint32_t  jitter 
)

Get a random value between (max - jitter) and (max). This is useful for jittering timer values.

Definition at line 151 of file random.c.

References isc_random_get(), and REQUIRE.

Referenced by dns_zone_refresh(), signwithkey(), and zone_postload().

static void chacha_reinit ( isc_rng_t rng,
isc_uint8_t buffer,
size_t  n 
) [static]

Definition at line 164 of file random.c.

References chacha_ivsetup(), CHACHA_IVSIZE, chacha_keysetup(), CHACHA_KEYSIZE, and REQUIRE.

Referenced by chacha_rekey(), and isc_rng_create().

isc_result_t isc_rng_create ( isc_mem_t mctx,
isc_entropy_t entropy,
isc_rng_t **  rngp 
)

Creates and initializes a pseudo random number generator. The returned RNG can be used to generate pseudo random numbers.

The reference count of the returned RNG is set to 1.

Requires:

Ensures: Returns:

Definition at line 175 of file random.c.

References CHACHA_BUFFERSIZE, chacha_reinit(), isc_entropy_getdata(), isc_mem_attach(), isc_mem_get, isc_mem_put, isc_mutex_init, ISC_R_NOMEMORY, ISC_R_SUCCESS, isc_random_get(), isc_entropy::magic, REQUIRE, RNG_MAGIC, and RUNTIME_CHECK.

Referenced by dns_dispatchmgr_create(), and random_test().

void isc_rng_attach ( isc_rng_t source,
isc_rng_t **  targetp 
)

Increments a reference count on the passed RNG.

Requires:

Definition at line 231 of file random.c.

References LOCK, REQUIRE, UNLOCK, and VALID_RNG.

Referenced by dispatch_allocate().

static void destroy ( isc_rng_t rng  )  [static]

Definition at line 244 of file random.c.

References isc_mem_putanddetach, isc_mutex_destroy, REQUIRE, and VALID_RNG.

void isc_rng_detach ( isc_rng_t **  rngp  ) 

Decrements a reference count on the passed RNG. If the reference count reaches 0, the RNG is destroyed.

Requires:

Definition at line 254 of file random.c.

References destroy(), INSIST, ISC_FALSE, ISC_TRUE, LOCK, REQUIRE, UNLOCK, and VALID_RNG.

Referenced by destroy_mgr(), dispatch_allocate(), dispatch_free(), and random_test().

static void chacha_rekey ( isc_rng_t rng,
u_char *  dat,
size_t  datlen 
) [static]

Definition at line 276 of file random.c.

References CHACHA_BUFFERSIZE, chacha_encrypt_bytes(), CHACHA_IVSIZE, CHACHA_KEYSIZE, chacha_reinit(), ISC_MIN, REQUIRE, and VALID_RNG.

Referenced by chacha_getuint16(), and chacha_stir().

static isc_uint16_t chacha_getuint16 ( isc_rng_t rng  )  [inline, static]

Definition at line 304 of file random.c.

References CHACHA_BUFFERSIZE, chacha_rekey(), REQUIRE, and VALID_RNG.

Referenced by isc_rng_random().

static void chacha_stir ( isc_rng_t rng  )  [static]

Definition at line 323 of file random.c.

References CHACHA_BUFFERSIZE, chacha_rekey(), isc_entropy_getdata(), ISC_R_SUCCESS, isc_random_get(), REQUIRE, RUNTIME_CHECK, and VALID_RNG.

Referenced by isc_rng_random().

isc_uint16_t isc_rng_random ( isc_rng_t rng  ) 

Returns a pseudo random 16-bit unsigned integer.

Definition at line 368 of file random.c.

References chacha_getuint16(), chacha_stir(), LOCK, REQUIRE, UNLOCK, and VALID_RNG.

Referenced by dns_dispatch_addresponse3(), isc_rng_uniformrandom(), and random_test().

isc_uint16_t isc_rng_uniformrandom ( isc_rng_t rng,
isc_uint16_t  upper_bound 
)

Returns a uniformly distributed pseudo random 16-bit unsigned integer.

Definition at line 386 of file random.c.

References isc_rng_random(), r, REQUIRE, and VALID_RNG.

Referenced by get_dispsocket(), and get_udpsocket().


Variable Documentation

isc_once_t once = ISC_ONCE_INIT [static]

Definition at line 85 of file random.c.


Generated on Tue Apr 28 17:41:18 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha