00001 /* 00002 * Copyright (C) 2004-2007, 2009, 2014 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 1999-2001 Internet Software Consortium. 00004 * 00005 * Permission to use, copy, modify, and/or distribute this software for any 00006 * purpose with or without fee is hereby granted, provided that the above 00007 * copyright notice and this permission notice appear in all copies. 00008 * 00009 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00010 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00011 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00012 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00013 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00014 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00015 * PERFORMANCE OF THIS SOFTWARE. 00016 */ 00017 00018 /* $Id: random.h,v 1.20 2009/01/17 23:47:43 tbox Exp $ */ 00019 00020 #ifndef ISC_RANDOM_H 00021 #define ISC_RANDOM_H 1 00022 00023 #include <isc/lang.h> 00024 #include <isc/types.h> 00025 #include <isc/entropy.h> 00026 #include <isc/mem.h> 00027 #include <isc/mutex.h> 00028 00029 /*! \file isc/random.h 00030 * \brief Implements a random state pool which will let the caller return a 00031 * series of possibly non-reproducible random values. 00032 * 00033 * Note that the 00034 * strength of these numbers is not all that high, and should not be 00035 * used in cryptography functions. It is useful for jittering values 00036 * a bit here and there, such as timeouts, etc. 00037 */ 00038 00039 ISC_LANG_BEGINDECLS 00040 00041 typedef struct isc_rng isc_rng_t; 00042 /*%< 00043 * Opaque type 00044 */ 00045 00046 void 00047 isc_random_seed(isc_uint32_t seed); 00048 /*%< 00049 * Set the initial seed of the random state. 00050 */ 00051 00052 void 00053 isc_random_get(isc_uint32_t *val); 00054 /*%< 00055 * Get a random value. 00056 * 00057 * Requires: 00058 * val != NULL. 00059 */ 00060 00061 isc_uint32_t 00062 isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter); 00063 /*%< 00064 * Get a random value between (max - jitter) and (max). 00065 * This is useful for jittering timer values. 00066 */ 00067 00068 isc_result_t 00069 isc_rng_create(isc_mem_t *mctx, isc_entropy_t *entropy, isc_rng_t **rngp); 00070 /*%< 00071 * Creates and initializes a pseudo random number generator. The 00072 * returned RNG can be used to generate pseudo random numbers. 00073 * 00074 * The reference count of the returned RNG is set to 1. 00075 * 00076 * Requires: 00077 * \li mctx is a pointer to a valid memory context. 00078 * \li entropy is an optional entopy source (can be NULL) 00079 * \li rngp != NULL && *rngp == NULL is where a pointer to the RNG is 00080 * returned. 00081 * 00082 * Ensures: 00083 *\li If result is ISC_R_SUCCESS: 00084 * *rngp points to a valid RNG. 00085 * 00086 *\li If result is failure: 00087 * *rngp does not point to a valid RNG. 00088 * 00089 * Returns: 00090 *\li #ISC_R_SUCCESS Success 00091 *\li #ISC_R_NOMEMORY Resource limit: Out of Memory 00092 */ 00093 00094 void 00095 isc_rng_attach(isc_rng_t *source, isc_rng_t **targetp); 00096 /*%< 00097 * Increments a reference count on the passed RNG. 00098 * 00099 * Requires: 00100 * \li source the RNG struct to attach to (is refcount is incremented) 00101 * \li targetp != NULL && *targetp == NULL where a pointer to the 00102 * reference incremented RNG is returned. 00103 */ 00104 00105 void 00106 isc_rng_detach(isc_rng_t **rngp); 00107 /*%< 00108 * Decrements a reference count on the passed RNG. If the reference 00109 * count reaches 0, the RNG is destroyed. 00110 * 00111 * Requires: 00112 * \li rngp != NULL the RNG struct to decrement reference for 00113 */ 00114 00115 isc_uint16_t 00116 isc_rng_random(isc_rng_t *rngctx); 00117 /*%< 00118 * Returns a pseudo random 16-bit unsigned integer. 00119 */ 00120 00121 isc_uint16_t 00122 isc_rng_uniformrandom(isc_rng_t *rngctx, isc_uint16_t upper_bound); 00123 /*%< 00124 * Returns a uniformly distributed pseudo random 16-bit unsigned 00125 * integer. 00126 */ 00127 00128 ISC_LANG_ENDDECLS 00129 00130 #endif /* ISC_RANDOM_H */