2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: entropy.c,v 1.3.2.4 2004/03/09 06:11:45 marka Exp $ */
21 * This is the system independent part of the entropy module. It is
22 * compiled via inclusion from the relevant OS source file, ie,
23 * unix/entropy.c or win32/entropy.c.
30 #include <isc/buffer.h>
31 #include <isc/entropy.h>
32 #include <isc/keyboard.h>
34 #include <isc/magic.h>
37 #include <isc/mutex.h>
38 #include <isc/platform.h>
39 #include <isc/region.h>
41 #include <isc/string.h>
46 * Much of this code is modeled after the NetBSD /dev/random implementation,
47 * written by Michael Graff <explorer@netbsd.org>.
50 #define ENTROPY_MAGIC ISC_MAGIC('E', 'n', 't', 'e')
51 #define SOURCE_MAGIC ISC_MAGIC('E', 'n', 't', 's')
53 #define VALID_ENTROPY(e) ISC_MAGIC_VALID(e, ENTROPY_MAGIC)
54 #define VALID_SOURCE(s) ISC_MAGIC_VALID(s, SOURCE_MAGIC)
57 *** "constants." Do not change these unless you _really_ know what
62 * size of entropy pool in 32-bit words. This _MUST_ be a power of 2.
64 #define RND_POOLWORDS 128
65 #define RND_POOLBYTES (RND_POOLWORDS * 4)
66 #define RND_POOLBITS (RND_POOLWORDS * 32)
69 * Number of bytes returned per hash. This must be true:
70 * threshold * 2 <= digest_size_in_bytes
72 #define RND_ENTROPY_THRESHOLD 10
73 #define THRESHOLD_BITS (RND_ENTROPY_THRESHOLD * 8)
76 * Size of the input event queue in samples.
78 #define RND_EVENTQSIZE 32
81 * The number of times we'll "reseed" for pseudorandom seeds. This is an
82 * extremely weak pseudorandom seed. If the caller is using lots of
83 * pseudorandom data and they cannot provide a stronger random source,
84 * there is little we can do other than hope they're smart enough to
85 * call _adddata() with something better than we can come up with.
87 #define RND_INITIALIZE 128
90 isc_uint32_t cursor; /* current add point in the pool */
91 isc_uint32_t entropy; /* current entropy estimate in bits */
92 isc_uint32_t pseudo; /* bits extracted in pseudorandom */
93 isc_uint32_t rotate; /* how many bits to rotate by */
94 isc_uint32_t pool[RND_POOLWORDS]; /* random pool data */
102 isc_uint32_t initialized;
103 isc_uint32_t initcount;
104 isc_entropypool_t pool;
105 unsigned int nsources;
106 isc_entropysource_t *nextsource;
107 ISC_LIST(isc_entropysource_t) sources;
111 isc_uint32_t last_time; /* last time recorded */
112 isc_uint32_t last_delta; /* last delta value */
113 isc_uint32_t last_delta2; /* last delta2 value */
114 isc_uint32_t nsamples; /* number of samples filled in */
115 isc_uint32_t *samples; /* the samples */
116 isc_uint32_t *extra; /* extra samples added in */
120 sample_queue_t samplequeue;
121 } isc_entropysamplesource_t;
124 isc_boolean_t start_called;
125 isc_entropystart_t startfunc;
126 isc_entropyget_t getfunc;
127 isc_entropystop_t stopfunc;
129 sample_queue_t samplequeue;
133 FILESOURCE_HANDLE_TYPE handle;
134 } isc_entropyfilesource_t;
136 struct isc_entropysource {
140 isc_uint32_t total; /* entropy from this source */
141 ISC_LINK(isc_entropysource_t) link;
144 isc_boolean_t warn_keyboard;
147 isc_entropysamplesource_t sample;
148 isc_entropyfilesource_t file;
149 isc_cbsource_t callback;
153 #define ENTROPY_SOURCETYPE_SAMPLE 1 /* Type is a sample source */
154 #define ENTROPY_SOURCETYPE_FILE 2 /* Type is a file source */
155 #define ENTROPY_SOURCETYPE_CALLBACK 3 /* Type is a callback source */
158 * The random pool "taps"
167 * Declarations for function provided by the system dependent sources that
171 fillpool(isc_entropy_t *, unsigned int, isc_boolean_t);
174 wait_for_sources(isc_entropy_t *);
177 destroyfilesource(isc_entropyfilesource_t *source);
181 samplequeue_release(isc_entropy_t *ent, sample_queue_t *sq) {
182 REQUIRE(sq->samples != NULL);
183 REQUIRE(sq->extra != NULL);
185 isc_mem_put(ent->mctx, sq->samples, RND_EVENTQSIZE * 4);
186 isc_mem_put(ent->mctx, sq->extra, RND_EVENTQSIZE * 4);
192 samplesource_allocate(isc_entropy_t *ent, sample_queue_t *sq) {
193 sq->samples = isc_mem_get(ent->mctx, RND_EVENTQSIZE * 4);
194 if (sq->samples == NULL)
195 return (ISC_R_NOMEMORY);
197 sq->extra = isc_mem_get(ent->mctx, RND_EVENTQSIZE * 4);
198 if (sq->extra == NULL) {
199 isc_mem_put(ent->mctx, sq->samples, RND_EVENTQSIZE * 4);
201 return (ISC_R_NOMEMORY);
206 return (ISC_R_SUCCESS);
210 * Add in entropy, even when the value we're adding in could be
214 add_entropy(isc_entropy_t *ent, isc_uint32_t entropy) {
215 /* clamp input. Yes, this must be done. */
216 entropy = ISC_MIN(entropy, RND_POOLBITS);
217 /* Add in the entropy we already have. */
218 entropy += ent->pool.entropy;
220 ent->pool.entropy = ISC_MIN(entropy, RND_POOLBITS);
224 * Decrement the amount of entropy the pool has.
227 subtract_entropy(isc_entropy_t *ent, isc_uint32_t entropy) {
228 entropy = ISC_MIN(entropy, ent->pool.entropy);
229 ent->pool.entropy -= entropy;
233 * Add in entropy, even when the value we're adding in could be
237 add_pseudo(isc_entropy_t *ent, isc_uint32_t pseudo) {
238 /* clamp input. Yes, this must be done. */
239 pseudo = ISC_MIN(pseudo, RND_POOLBITS * 8);
240 /* Add in the pseudo we already have. */
241 pseudo += ent->pool.pseudo;
243 ent->pool.pseudo = ISC_MIN(pseudo, RND_POOLBITS * 8);
247 * Decrement the amount of pseudo the pool has.
250 subtract_pseudo(isc_entropy_t *ent, isc_uint32_t pseudo) {
251 pseudo = ISC_MIN(pseudo, ent->pool.pseudo);
252 ent->pool.pseudo -= pseudo;
256 * Add one word to the pool, rotating the input as needed.
259 entropypool_add_word(isc_entropypool_t *rp, isc_uint32_t val) {
261 * Steal some values out of the pool, and xor them into the
262 * word we were given.
264 * Mix the new value into the pool using xor. This will
265 * prevent the actual values from being known to the caller
266 * since the previous values are assumed to be unknown as well.
268 val ^= rp->pool[(rp->cursor + TAP1) & (RND_POOLWORDS - 1)];
269 val ^= rp->pool[(rp->cursor + TAP2) & (RND_POOLWORDS - 1)];
270 val ^= rp->pool[(rp->cursor + TAP3) & (RND_POOLWORDS - 1)];
271 val ^= rp->pool[(rp->cursor + TAP4) & (RND_POOLWORDS - 1)];
272 val ^= rp->pool[(rp->cursor + TAP5) & (RND_POOLWORDS - 1)];
273 rp->pool[rp->cursor++] ^=
274 ((val << rp->rotate) | (val >> (32 - rp->rotate)));
277 * If we have looped around the pool, increment the rotate
278 * variable so the next value will get xored in rotated to
279 * a different position.
280 * Increment by a value that is relativly prime to the word size
281 * to try to spread the bits throughout the pool quickly when the
284 if (rp->cursor == RND_POOLWORDS) {
286 rp->rotate = (rp->rotate + 7) & 31;
291 * Add a buffer's worth of data to the pool.
293 * Requires that the lock is held on the entropy pool.
296 entropypool_adddata(isc_entropy_t *ent, void *p, unsigned int len,
297 isc_uint32_t entropy)
303 addr = (unsigned long)p;
306 if ((addr & 0x03U) != 0U) {
313 val = val << 8 | *buf++;
316 val = val << 8 | *buf++;
320 entropypool_add_word(&ent->pool, val);
323 for (; len > 3 ; len -= 4) {
324 val = *((isc_uint32_t *)buf);
326 entropypool_add_word(&ent->pool, val);
336 val = val << 8 | *buf++;
338 val = val << 8 | *buf++;
341 entropypool_add_word(&ent->pool, val);
344 add_entropy(ent, entropy);
345 subtract_pseudo(ent, entropy);
349 reseed(isc_entropy_t *ent) {
354 if (ent->initcount == 0) {
356 entropypool_adddata(ent, &pid, sizeof pid, 0);
358 entropypool_adddata(ent, &pid, sizeof pid, 0);
362 * After we've reseeded 100 times, only add new timing info every
363 * 50 requests. This will keep us from using lots and lots of
364 * CPU just to return bad pseudorandom data anyway.
366 if (ent->initcount > 100)
367 if ((ent->initcount % 50) != 0)
370 result = isc_time_now(&t);
371 if (result == ISC_R_SUCCESS) {
372 entropypool_adddata(ent, &t, sizeof t, 0);
377 static inline unsigned int
378 estimate_entropy(sample_queue_t *sq, isc_uint32_t t) {
384 * If the time counter has overflowed, calculate the real difference.
385 * If it has not, it is simplier.
387 if (t < sq->last_time)
388 delta = UINT_MAX - sq->last_time + t;
390 delta = sq->last_time - t;
396 * Calculate the second and third order differentials
398 delta2 = sq->last_delta - delta;
402 delta3 = sq->last_delta2 - delta2;
407 sq->last_delta = delta;
408 sq->last_delta2 = delta2;
411 * If any delta is 0, we got no entropy. If all are non-zero, we
412 * might have something.
414 if (delta == 0 || delta2 == 0 || delta3 == 0)
418 * We could find the smallest delta and claim we got log2(delta)
419 * bits, but for now return that we found 1 bit.
425 crunchsamples(isc_entropy_t *ent, sample_queue_t *sq) {
429 if (sq->nsamples < 6)
433 sq->last_time = sq->samples[0];
438 * Prime the values by adding in the first 4 samples in. This
439 * should completely initialize the delta calculations.
441 for (ns = 0 ; ns < 4 ; ns++)
442 (void)estimate_entropy(sq, sq->samples[ns]);
444 for (ns = 4 ; ns < sq->nsamples ; ns++)
445 added += estimate_entropy(sq, sq->samples[ns]);
447 entropypool_adddata(ent, sq->samples, sq->nsamples * 4, added);
448 entropypool_adddata(ent, sq->extra, sq->nsamples * 4, 0);
451 * Move the last 4 samples into the first 4 positions, and start
452 * adding new samples from that point.
454 for (ns = 0 ; ns < 4 ; ns++) {
455 sq->samples[ns] = sq->samples[sq->nsamples - 4 + ns];
456 sq->extra[ns] = sq->extra[sq->nsamples - 4 + ns];
465 get_from_callback(isc_entropysource_t *source, unsigned int desired,
466 isc_boolean_t blocking)
468 isc_entropy_t *ent = source->ent;
469 isc_cbsource_t *cbs = &source->sources.callback;
480 if (!cbs->start_called && cbs->startfunc != NULL) {
481 result = cbs->startfunc(source, cbs->arg, blocking);
482 if (result != ISC_R_SUCCESS)
484 cbs->start_called = ISC_TRUE;
488 result = ISC_R_SUCCESS;
489 while (desired > 0 && result == ISC_R_SUCCESS) {
490 result = cbs->getfunc(source, cbs->arg, blocking);
491 if (result == ISC_R_QUEUEFULL) {
492 got = crunchsamples(ent, &cbs->samplequeue);
494 desired -= ISC_MIN(got, desired);
495 result = ISC_R_SUCCESS;
496 } else if (result != ISC_R_SUCCESS &&
497 result != ISC_R_NOTBLOCKING)
498 source->bad = ISC_TRUE;
506 * Extract some number of bytes from the random pool, decreasing the
507 * estimate of randomness as each byte is extracted.
509 * Do this by stiring the pool and returning a part of hash as randomness.
510 * Note that no secrets are given away here since parts of the hash are
511 * xored together before returned.
513 * Honor the request from the caller to only return good data, any data,
517 isc_entropy_getdata(isc_entropy_t *ent, void *data, unsigned int length,
518 unsigned int *returned, unsigned int flags)
522 unsigned char digest[ISC_SHA1_DIGESTLENGTH];
523 isc_uint32_t remain, deltae, count, total;
525 isc_boolean_t goodonly, partial, blocking;
527 REQUIRE(VALID_ENTROPY(ent));
528 REQUIRE(data != NULL);
531 goodonly = ISC_TF((flags & ISC_ENTROPY_GOODONLY) != 0);
532 partial = ISC_TF((flags & ISC_ENTROPY_PARTIAL) != 0);
533 blocking = ISC_TF((flags & ISC_ENTROPY_BLOCKING) != 0);
535 REQUIRE(!partial || returned != NULL);
542 while (remain != 0) {
543 count = ISC_MIN(remain, RND_ENTROPY_THRESHOLD);
546 * If we are extracting good data only, make certain we
547 * have enough data in our pool for this pass. If we don't,
548 * get some, and fail if we can't, and partial returns
552 unsigned int fillcount;
554 fillcount = ISC_MAX(remain * 8, count * 8);
557 * If, however, we have at least THRESHOLD_BITS
558 * of entropy in the pool, don't block here. It is
559 * better to drain the pool once in a while and
560 * then refill it than it is to constantly keep the
563 if (ent->pool.entropy >= THRESHOLD_BITS)
564 fillpool(ent, fillcount, ISC_FALSE);
566 fillpool(ent, fillcount, blocking);
569 * Verify that we got enough entropy to do one
570 * extraction. If we didn't, bail.
572 if (ent->pool.entropy < THRESHOLD_BITS) {
580 * If we've extracted half our pool size in bits
581 * since the last refresh, try to refresh here.
583 if (ent->initialized < THRESHOLD_BITS)
584 fillpool(ent, THRESHOLD_BITS, blocking);
586 fillpool(ent, 0, ISC_FALSE);
589 * If we've not initialized with enough good random
590 * data, seed with our crappy code.
592 if (ent->initialized < THRESHOLD_BITS)
596 isc_sha1_init(&hash);
597 isc_sha1_update(&hash, (void *)(ent->pool.pool),
599 isc_sha1_final(&hash, digest);
602 * Stir the extracted data (all of it) back into the pool.
604 entropypool_adddata(ent, digest, ISC_SHA1_DIGESTLENGTH, 0);
606 for (i = 0; i < count; i++)
607 buf[i] = digest[i] ^ digest[i + RND_ENTROPY_THRESHOLD];
613 deltae = ISC_MIN(deltae, ent->pool.entropy);
615 subtract_entropy(ent, deltae);
616 add_pseudo(ent, count * 8);
620 memset(digest, 0, sizeof(digest));
622 if (returned != NULL)
623 *returned = (length - remain);
627 return (ISC_R_SUCCESS);
630 /* put the entropy we almost extracted back */
631 add_entropy(ent, total);
632 memset(data, 0, length);
633 memset(digest, 0, sizeof(digest));
634 if (returned != NULL)
639 return (ISC_R_NOENTROPY);
643 isc_entropypool_init(isc_entropypool_t *pool) {
644 pool->cursor = RND_POOLWORDS - 1;
648 memset(pool->pool, 0, RND_POOLBYTES);
652 isc_entropypool_invalidate(isc_entropypool_t *pool) {
657 memset(pool->pool, 0, RND_POOLBYTES);
661 isc_entropy_create(isc_mem_t *mctx, isc_entropy_t **entp) {
665 REQUIRE(mctx != NULL);
666 REQUIRE(entp != NULL && *entp == NULL);
668 ent = isc_mem_get(mctx, sizeof(isc_entropy_t));
670 return (ISC_R_NOMEMORY);
675 if (isc_mutex_init(&ent->lock) != ISC_R_SUCCESS) {
676 ret = ISC_R_UNEXPECTED;
681 * From here down, no failures will/can occur.
683 ISC_LIST_INIT(ent->sources);
684 ent->nextsource = NULL;
687 isc_mem_attach(mctx, &ent->mctx);
689 ent->initialized = 0;
691 ent->magic = ENTROPY_MAGIC;
693 isc_entropypool_init(&ent->pool);
696 return (ISC_R_SUCCESS);
699 isc_mem_put(mctx, ent, sizeof(isc_entropy_t));
705 * Requires "ent" be locked.
708 destroysource(isc_entropysource_t **sourcep) {
709 isc_entropysource_t *source;
717 ISC_LIST_UNLINK(ent->sources, source, link);
718 ent->nextsource = NULL;
719 REQUIRE(ent->nsources > 0);
722 switch (source->type) {
723 case ENTROPY_SOURCETYPE_FILE:
725 destroyfilesource(&source->sources.file);
727 case ENTROPY_SOURCETYPE_SAMPLE:
728 samplequeue_release(ent, &source->sources.sample.samplequeue);
730 case ENTROPY_SOURCETYPE_CALLBACK:
731 cbs = &source->sources.callback;
732 if (cbs->start_called && cbs->stopfunc != NULL) {
733 cbs->stopfunc(source, cbs->arg);
734 cbs->start_called = ISC_FALSE;
736 samplequeue_release(ent, &cbs->samplequeue);
740 memset(source, 0, sizeof(isc_entropysource_t));
742 isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
745 static inline isc_boolean_t
746 destroy_check(isc_entropy_t *ent) {
747 isc_entropysource_t *source;
752 source = ISC_LIST_HEAD(ent->sources);
753 while (source != NULL) {
754 switch (source->type) {
755 case ENTROPY_SOURCETYPE_FILE:
760 source = ISC_LIST_NEXT(source, link);
767 destroy(isc_entropy_t **entp) {
769 isc_entropysource_t *source;
772 REQUIRE(entp != NULL && *entp != NULL);
778 REQUIRE(ent->refcnt == 0);
781 * Here, detach non-sample sources.
783 source = ISC_LIST_HEAD(ent->sources);
784 while (source != NULL) {
785 switch(source->type) {
786 case ENTROPY_SOURCETYPE_FILE:
787 destroysource(&source);
790 source = ISC_LIST_HEAD(ent->sources);
794 * If there are other types of sources, we've found a bug.
796 REQUIRE(ISC_LIST_EMPTY(ent->sources));
800 isc_entropypool_invalidate(&ent->pool);
804 DESTROYLOCK(&ent->lock);
806 memset(ent, 0, sizeof(isc_entropy_t));
807 isc_mem_put(mctx, ent, sizeof(isc_entropy_t));
808 isc_mem_detach(&mctx);
812 isc_entropy_destroysource(isc_entropysource_t **sourcep) {
813 isc_entropysource_t *source;
815 isc_boolean_t killit;
817 REQUIRE(sourcep != NULL);
818 REQUIRE(VALID_SOURCE(*sourcep));
824 REQUIRE(VALID_ENTROPY(ent));
828 destroysource(&source);
830 killit = destroy_check(ent);
839 isc_entropy_createcallbacksource(isc_entropy_t *ent,
840 isc_entropystart_t start,
841 isc_entropyget_t get,
842 isc_entropystop_t stop,
844 isc_entropysource_t **sourcep)
847 isc_entropysource_t *source;
850 REQUIRE(VALID_ENTROPY(ent));
851 REQUIRE(get != NULL);
852 REQUIRE(sourcep != NULL && *sourcep == NULL);
856 source = isc_mem_get(ent->mctx, sizeof(isc_entropysource_t));
857 if (source == NULL) {
858 ret = ISC_R_NOMEMORY;
861 source->bad = ISC_FALSE;
863 cbs = &source->sources.callback;
865 ret = samplesource_allocate(ent, &cbs->samplequeue);
866 if (ret != ISC_R_SUCCESS)
869 cbs->start_called = ISC_FALSE;
870 cbs->startfunc = start;
872 cbs->stopfunc = stop;
876 * From here down, no failures can occur.
878 source->magic = SOURCE_MAGIC;
879 source->type = ENTROPY_SOURCETYPE_CALLBACK;
882 memset(source->name, 0, sizeof(source->name));
883 ISC_LINK_INIT(source, link);
886 * Hook it into the entropy system.
888 ISC_LIST_APPEND(ent->sources, source, link);
894 return (ISC_R_SUCCESS);
898 isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
906 isc_entropy_stopcallbacksources(isc_entropy_t *ent) {
907 isc_entropysource_t *source;
910 REQUIRE(VALID_ENTROPY(ent));
914 source = ISC_LIST_HEAD(ent->sources);
915 while (source != NULL) {
916 if (source->type == ENTROPY_SOURCETYPE_CALLBACK) {
917 cbs = &source->sources.callback;
918 if (cbs->start_called && cbs->stopfunc != NULL) {
919 cbs->stopfunc(source, cbs->arg);
920 cbs->start_called = ISC_FALSE;
924 source = ISC_LIST_NEXT(source, link);
931 isc_entropy_createsamplesource(isc_entropy_t *ent,
932 isc_entropysource_t **sourcep)
935 isc_entropysource_t *source;
938 REQUIRE(VALID_ENTROPY(ent));
939 REQUIRE(sourcep != NULL && *sourcep == NULL);
943 source = isc_mem_get(ent->mctx, sizeof(isc_entropysource_t));
944 if (source == NULL) {
945 ret = ISC_R_NOMEMORY;
949 sq = &source->sources.sample.samplequeue;
950 ret = samplesource_allocate(ent, sq);
951 if (ret != ISC_R_SUCCESS)
955 * From here down, no failures can occur.
957 source->magic = SOURCE_MAGIC;
958 source->type = ENTROPY_SOURCETYPE_SAMPLE;
961 memset(source->name, 0, sizeof(source->name));
962 ISC_LINK_INIT(source, link);
965 * Hook it into the entropy system.
967 ISC_LIST_APPEND(ent->sources, source, link);
973 return (ISC_R_SUCCESS);
977 isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
985 * Add a sample, and return ISC_R_SUCCESS if the queue has become full,
986 * ISC_R_NOENTROPY if it has space remaining, and ISC_R_NOMORE if the
987 * queue was full when this function was called.
990 addsample(sample_queue_t *sq, isc_uint32_t sample, isc_uint32_t extra) {
991 if (sq->nsamples >= RND_EVENTQSIZE)
992 return (ISC_R_NOMORE);
994 sq->samples[sq->nsamples] = sample;
995 sq->extra[sq->nsamples] = extra;
998 if (sq->nsamples >= RND_EVENTQSIZE)
999 return (ISC_R_QUEUEFULL);
1001 return (ISC_R_SUCCESS);
1005 isc_entropy_addsample(isc_entropysource_t *source, isc_uint32_t sample,
1010 unsigned int entropy;
1011 isc_result_t result;
1013 REQUIRE(VALID_SOURCE(source));
1019 sq = &source->sources.sample.samplequeue;
1020 result = addsample(sq, sample, extra);
1021 if (result == ISC_R_QUEUEFULL) {
1022 entropy = crunchsamples(ent, sq);
1023 add_entropy(ent, entropy);
1032 isc_entropy_addcallbacksample(isc_entropysource_t *source, isc_uint32_t sample,
1036 isc_result_t result;
1038 REQUIRE(VALID_SOURCE(source));
1039 REQUIRE(source->type == ENTROPY_SOURCETYPE_CALLBACK);
1041 sq = &source->sources.callback.samplequeue;
1042 result = addsample(sq, sample, extra);
1048 isc_entropy_putdata(isc_entropy_t *ent, void *data, unsigned int length,
1049 isc_uint32_t entropy)
1051 REQUIRE(VALID_ENTROPY(ent));
1055 entropypool_adddata(ent, data, length, entropy);
1057 if (ent->initialized < THRESHOLD_BITS)
1058 ent->initialized = THRESHOLD_BITS;
1064 dumpstats(isc_entropy_t *ent, FILE *out) {
1066 isc_msgcat_get(isc_msgcat, ISC_MSGSET_ENTROPY,
1067 ISC_MSG_ENTROPYSTATS,
1068 "Entropy pool %p: refcnt %u cursor %u,"
1069 " rotate %u entropy %u pseudo %u nsources %u"
1070 " nextsource %p initialized %u initcount %u\n"),
1072 ent->pool.cursor, ent->pool.rotate,
1073 ent->pool.entropy, ent->pool.pseudo,
1074 ent->nsources, ent->nextsource, ent->initialized,
1079 * This function ignores locking. Use at your own risk.
1082 isc_entropy_stats(isc_entropy_t *ent, FILE *out) {
1083 REQUIRE(VALID_ENTROPY(ent));
1086 dumpstats(ent, out);
1091 isc_entropy_attach(isc_entropy_t *ent, isc_entropy_t **entp) {
1092 REQUIRE(VALID_ENTROPY(ent));
1093 REQUIRE(entp != NULL && *entp == NULL);
1104 isc_entropy_detach(isc_entropy_t **entp) {
1106 isc_boolean_t killit;
1108 REQUIRE(entp != NULL && VALID_ENTROPY(*entp));
1114 REQUIRE(ent->refcnt > 0);
1117 killit = destroy_check(ent);
1126 kbdstart(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
1128 * The intent of "first" is to provide a warning message only once
1129 * during the run of a program that might try to gather keyboard
1130 * entropy multiple times.
1132 static isc_boolean_t first = ISC_TRUE;
1137 return (ISC_R_NOENTROPY);
1140 if (source->warn_keyboard)
1141 fprintf(stderr, "You must use the keyboard to create "
1142 "entropy, since your system is lacking\n"
1143 "/dev/random (or equivalent)\n\n");
1146 fprintf(stderr, "start typing:\n");
1148 return (isc_keyboard_open(&source->kbd));
1152 kbdstop(isc_entropysource_t *source, void *arg) {
1156 if (! isc_keyboard_canceled(&source->kbd))
1157 fprintf(stderr, "stop typing.\r\n");
1159 (void)isc_keyboard_close(&source->kbd, 3);
1163 kbdget(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
1164 isc_result_t result;
1166 isc_uint32_t sample;
1173 return (ISC_R_NOTBLOCKING);
1175 result = isc_keyboard_getchar(&source->kbd, &c);
1176 if (result != ISC_R_SUCCESS)
1179 result = isc_time_now(&t);
1180 if (result != ISC_R_SUCCESS)
1183 sample = isc_time_nanoseconds(&t);
1186 result = isc_entropy_addcallbacksample(source, sample, extra);
1187 if (result != ISC_R_SUCCESS) {
1188 fprintf(stderr, "\r\n");
1192 fprintf(stderr, ".");
1199 isc_entropy_usebestsource(isc_entropy_t *ectx, isc_entropysource_t **source,
1200 const char *randomfile, int use_keyboard)
1202 isc_result_t result;
1203 isc_result_t final_result = ISC_R_NOENTROPY;
1204 isc_boolean_t userfile = ISC_TRUE;
1206 REQUIRE(VALID_ENTROPY(ectx));
1207 REQUIRE(source != NULL && *source == NULL);
1208 REQUIRE(use_keyboard == ISC_ENTROPY_KEYBOARDYES ||
1209 use_keyboard == ISC_ENTROPY_KEYBOARDNO ||
1210 use_keyboard == ISC_ENTROPY_KEYBOARDMAYBE);
1212 #ifdef PATH_RANDOMDEV
1213 if (randomfile == NULL) {
1214 randomfile = PATH_RANDOMDEV;
1215 userfile = ISC_FALSE;
1219 if (randomfile != NULL && use_keyboard != ISC_ENTROPY_KEYBOARDYES) {
1220 result = isc_entropy_createfilesource(ectx, randomfile);
1221 if (result == ISC_R_SUCCESS &&
1222 use_keyboard == ISC_ENTROPY_KEYBOARDMAYBE)
1223 use_keyboard = ISC_ENTROPY_KEYBOARDNO;
1224 if (result != ISC_R_SUCCESS && userfile)
1227 final_result = result;
1230 if (use_keyboard != ISC_ENTROPY_KEYBOARDNO) {
1231 result = isc_entropy_createcallbacksource(ectx, kbdstart,
1234 if (result == ISC_R_SUCCESS)
1235 (*source)->warn_keyboard =
1236 ISC_TF(use_keyboard ==
1237 ISC_ENTROPY_KEYBOARDMAYBE);
1239 if (final_result != ISC_R_SUCCESS)
1240 final_result = result;
1244 * final_result is ISC_R_SUCCESS if at least one source of entropy
1245 * could be started, otherwise it is the error from the most recently
1246 * failed operation (or ISC_R_NOENTROPY if PATH_RANDOMDEV is not
1247 * defined and use_keyboard is ISC_ENTROPY_KEYBOARDNO).
1249 return (final_result);