00001 /* 00002 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC") 00003 * Copyright (C) 2000, 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: entropy.h,v 1.35 2009/10/19 02:37:08 marka Exp $ */ 00019 00020 #ifndef ISC_ENTROPY_H 00021 #define ISC_ENTROPY_H 1 00022 00023 /***** 00024 ***** Module Info 00025 *****/ 00026 00027 /*! \file isc/entropy.h 00028 * \brief The entropy API 00029 * 00030 * \li MP: 00031 * The entropy object is locked internally. All callbacks into 00032 * application-provided functions (for setup, gathering, and 00033 * shutdown of sources) are guaranteed to be called with the 00034 * entropy API lock held. This means these functions are 00035 * not permitted to call back into the entropy API. 00036 * 00037 * \li Reliability: 00038 * No anticipated impact. 00039 * 00040 * \li Resources: 00041 * A buffer, used as an entropy pool. 00042 * 00043 * \li Security: 00044 * While this code is believed to implement good entropy gathering 00045 * and distribution, it has not been reviewed by a cryptographic 00046 * expert. 00047 * Since the added entropy is only as good as the sources used, 00048 * this module could hand out bad data and never know it. 00049 * 00050 * \li Standards: 00051 * None. 00052 */ 00053 00054 /*** 00055 *** Imports 00056 ***/ 00057 00058 #include <stdio.h> 00059 00060 #include <isc/lang.h> 00061 #include <isc/types.h> 00062 00063 /*@{*/ 00064 /*% Entropy callback function. */ 00065 typedef isc_result_t (*isc_entropystart_t)(isc_entropysource_t *source, 00066 void *arg, isc_boolean_t blocking); 00067 typedef isc_result_t (*isc_entropyget_t)(isc_entropysource_t *source, 00068 void *arg, isc_boolean_t blocking); 00069 typedef void (*isc_entropystop_t)(isc_entropysource_t *source, void *arg); 00070 /*@}*/ 00071 00072 /*** 00073 *** Flags. 00074 ***/ 00075 00076 /*! 00077 * \brief 00078 * Extract only "good" data; return failure if there is not enough 00079 * data available and there are no sources which we can poll to get 00080 * data, or those sources are empty. 00081 * 00082 * 00083 */ 00084 #define ISC_ENTROPY_GOODONLY 0x00000001U 00085 /*! 00086 * \brief 00087 * Extract as much good data as possible, but if there isn't enough 00088 * at hand, return what is available. This flag only makes sense 00089 * when used with _GOODONLY. 00090 */ 00091 #define ISC_ENTROPY_PARTIAL 0x00000002U 00092 /*! 00093 * \brief 00094 * Block the task until data is available. This is contrary to the 00095 * ISC task system, where tasks should never block. However, if 00096 * this is a special purpose application where blocking a task is 00097 * acceptable (say, an offline zone signer) this flag may be set. 00098 * This flag only makes sense when used with _GOODONLY, and will 00099 * block regardless of the setting for _PARTIAL. 00100 */ 00101 #define ISC_ENTROPY_BLOCKING 0x00000004U 00102 00103 /*! 00104 * \brief 00105 * Estimate the amount of entropy contained in the sample pool. 00106 * If this is not set, the source will be gathered and periodically 00107 * mixed into the entropy pool, but no increment in contained entropy 00108 * will be assumed. This flag only makes sense on sample sources. 00109 */ 00110 #define ISC_ENTROPYSOURCE_ESTIMATE 0x00000001U 00111 00112 /* 00113 * For use with isc_entropy_usebestsource(). 00114 */ 00115 /*! 00116 * \brief 00117 * Use the keyboard as the only entropy source. 00118 */ 00119 #define ISC_ENTROPY_KEYBOARDYES 1 00120 /*! 00121 * \brief 00122 * Never use the keyboard as an entropy source. 00123 */ 00124 #define ISC_ENTROPY_KEYBOARDNO 2 00125 /*! 00126 * \brief 00127 * Use the keyboard as an entropy source only if opening the 00128 * random device fails. 00129 */ 00130 #define ISC_ENTROPY_KEYBOARDMAYBE 3 00131 00132 ISC_LANG_BEGINDECLS 00133 00134 /*** 00135 *** Functions 00136 ***/ 00137 00138 isc_result_t 00139 isc_entropy_create(isc_mem_t *mctx, isc_entropy_t **entp); 00140 /*!< 00141 * \brief Create a new entropy object. 00142 */ 00143 00144 void 00145 isc_entropy_attach(isc_entropy_t *ent, isc_entropy_t **entp); 00146 /*!< 00147 * Attaches to an entropy object. 00148 */ 00149 00150 void 00151 isc_entropy_detach(isc_entropy_t **entp); 00152 /*!< 00153 * \brief Detaches from an entropy object. 00154 */ 00155 00156 isc_result_t 00157 isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname); 00158 /*!< 00159 * \brief Create a new entropy source from a file. 00160 * 00161 * The file is assumed to contain good randomness, and will be mixed directly 00162 * into the pool with every byte adding 8 bits of entropy. 00163 * 00164 * The file will be put into non-blocking mode, so it may be a device file, 00165 * such as /dev/random. /dev/urandom should not be used here if it can 00166 * be avoided, since it will always provide data even if it isn't good. 00167 * We will make as much pseudorandom data as we need internally if our 00168 * caller asks for it. 00169 * 00170 * If we hit end-of-file, we will stop reading from this source. Callers 00171 * who require strong random data will get failure when our pool drains. 00172 * The file will never be opened/read again once EOF is reached. 00173 */ 00174 00175 void 00176 isc_entropy_destroysource(isc_entropysource_t **sourcep); 00177 /*!< 00178 * \brief Removes an entropy source from the entropy system. 00179 */ 00180 00181 isc_result_t 00182 isc_entropy_createsamplesource(isc_entropy_t *ent, 00183 isc_entropysource_t **sourcep); 00184 /*!< 00185 * \brief Create an entropy source that consists of samples. Each sample is 00186 * added to the source via isc_entropy_addsamples(), below. 00187 */ 00188 00189 isc_result_t 00190 isc_entropy_createcallbacksource(isc_entropy_t *ent, 00191 isc_entropystart_t start, 00192 isc_entropyget_t get, 00193 isc_entropystop_t stop, 00194 void *arg, 00195 isc_entropysource_t **sourcep); 00196 /*!< 00197 * \brief Create an entropy source that is polled via a callback. 00198 * 00199 * This would 00200 * be used when keyboard input is used, or a GUI input method. It can 00201 * also be used to hook in any external entropy source. 00202 * 00203 * Samples are added via isc_entropy_addcallbacksample(), below. 00204 * _addcallbacksample() is the only function which may be called from 00205 * within an entropy API callback function. 00206 */ 00207 00208 void 00209 isc_entropy_stopcallbacksources(isc_entropy_t *ent); 00210 /*!< 00211 * \brief Call the stop functions for callback sources that have had their 00212 * start functions called. 00213 */ 00214 00215 /*@{*/ 00216 isc_result_t 00217 isc_entropy_addcallbacksample(isc_entropysource_t *source, isc_uint32_t sample, 00218 isc_uint32_t extra); 00219 isc_result_t 00220 isc_entropy_addsample(isc_entropysource_t *source, isc_uint32_t sample, 00221 isc_uint32_t extra); 00222 /*!< 00223 * \brief Add a sample to the sample source. 00224 * 00225 * The sample MUST be a timestamp 00226 * that increases over time, with the exception of wrap-around for 00227 * extremely high resolution timers which will quickly wrap-around 00228 * a 32-bit integer. 00229 * 00230 * The "extra" parameter is used only to add a bit more unpredictable 00231 * data. It is not used other than included in the hash of samples. 00232 * 00233 * When in an entropy API callback function, _addcallbacksource() must be 00234 * used. At all other times, _addsample() must be used. 00235 */ 00236 /*@}*/ 00237 00238 isc_result_t 00239 isc_entropy_getdata(isc_entropy_t *ent, void *data, unsigned int length, 00240 unsigned int *returned, unsigned int flags); 00241 /*!< 00242 * \brief Extract data from the entropy pool. This may load the pool from various 00243 * sources. 00244 * 00245 * Do this by stiring the pool and returning a part of hash as randomness. 00246 * Note that no secrets are given away here since parts of the hash are 00247 * xored together before returned. 00248 * 00249 * Honor the request from the caller to only return good data, any data, 00250 * etc. 00251 */ 00252 00253 void 00254 isc_entropy_putdata(isc_entropy_t *ent, void *data, unsigned int length, 00255 isc_uint32_t entropy); 00256 /*!< 00257 * \brief Add "length" bytes in "data" to the entropy pool, incrementing the 00258 * pool's entropy count by "entropy." 00259 * 00260 * These bytes will prime the pseudorandom portion even if no entropy is 00261 * actually added. 00262 */ 00263 00264 void 00265 isc_entropy_stats(isc_entropy_t *ent, FILE *out); 00266 /*!< 00267 * \brief Dump some (trivial) stats to the stdio stream "out". 00268 */ 00269 00270 unsigned int 00271 isc_entropy_status(isc_entropy_t *end); 00272 /* 00273 * Returns the number of bits the pool currently contains. This is just 00274 * an estimate. 00275 */ 00276 00277 isc_result_t 00278 isc_entropy_usebestsource(isc_entropy_t *ectx, isc_entropysource_t **source, 00279 const char *randomfile, int use_keyboard); 00280 /*!< 00281 * \brief Use whatever source of entropy is best. 00282 * 00283 * Notes: 00284 *\li If "randomfile" is not NULL, open it with 00285 * isc_entropy_createfilesource(). 00286 * 00287 *\li If "randomfile" is NULL and the system's random device was detected 00288 * when the program was configured and built, open that device with 00289 * isc_entropy_createfilesource(). 00290 * 00291 *\li If "use_keyboard" is #ISC_ENTROPY_KEYBOARDYES, then always open 00292 * the keyboard as an entropy source (possibly in addition to 00293 * "randomfile" or the random device). 00294 * 00295 *\li If "use_keyboard" is #ISC_ENTROPY_KEYBOARDMAYBE, open the keyboard only 00296 * if opening the random file/device fails. A message will be 00297 * printed describing the need for keyboard input. 00298 * 00299 *\li If "use_keyboard" is #ISC_ENTROPY_KEYBOARDNO, the keyboard will 00300 * never be opened. 00301 * 00302 * Returns: 00303 *\li #ISC_R_SUCCESS if at least one source of entropy could be started. 00304 * 00305 *\li #ISC_R_NOENTROPY if use_keyboard is #ISC_ENTROPY_KEYBOARDNO and 00306 * there is no random device pathname compiled into the program. 00307 * 00308 *\li A return code from isc_entropy_createfilesource() or 00309 * isc_entropy_createcallbacksource(). 00310 */ 00311 00312 ISC_LANG_ENDDECLS 00313 00314 #endif /* ISC_ENTROPY_H */