2 * Copyright (C) 2004-2007, 2009, 2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.22 2010/08/10 23:48:19 tbox Exp $ */
22 * This is the system independent part of the entropy module. It is
23 * compiled via inclusion from the relevant OS source file, ie,
24 * \link unix/entropy.c unix/entropy.c \endlink or win32/entropy.c.
26 * \author Much of this code is modeled after the NetBSD /dev/random implementation,
27 * written by Michael Graff <explorer@netbsd.org>.
34 #include <isc/buffer.h>
35 #include <isc/entropy.h>
36 #include <isc/keyboard.h>
38 #include <isc/magic.h>
41 #include <isc/mutex.h>
42 #include <isc/platform.h>
43 #include <isc/region.h>
45 #include <isc/string.h>
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
66 #define RND_POOLBYTES (RND_POOLWORDS * 4)
68 #define RND_POOLBITS (RND_POOLWORDS * 32)
71 * Number of bytes returned per hash. This must be true:
72 * threshold * 2 <= digest_size_in_bytes
74 #define RND_ENTROPY_THRESHOLD 10
75 #define THRESHOLD_BITS (RND_ENTROPY_THRESHOLD * 8)
78 * Size of the input event queue in samples.
80 #define RND_EVENTQSIZE 32
83 * The number of times we'll "reseed" for pseudorandom seeds. This is an
84 * extremely weak pseudorandom seed. If the caller is using lots of
85 * pseudorandom data and they cannot provide a stronger random source,
86 * there is little we can do other than hope they're smart enough to
87 * call _adddata() with something better than we can come up with.
89 #define RND_INITIALIZE 128
93 isc_uint32_t cursor; /*%< current add point in the pool */
94 isc_uint32_t entropy; /*%< current entropy estimate in bits */
95 isc_uint32_t pseudo; /*%< bits extracted in pseudorandom */
96 isc_uint32_t rotate; /*%< how many bits to rotate by */
97 isc_uint32_t pool[RND_POOLWORDS]; /*%< random pool data */
105 isc_uint32_t initialized;
106 isc_uint32_t initcount;
107 isc_entropypool_t pool;
108 unsigned int nsources;
109 isc_entropysource_t *nextsource;
110 ISC_LIST(isc_entropysource_t) sources;
115 isc_uint32_t last_time; /*%< last time recorded */
116 isc_uint32_t last_delta; /*%< last delta value */
117 isc_uint32_t last_delta2; /*%< last delta2 value */
118 isc_uint32_t nsamples; /*%< number of samples filled in */
119 isc_uint32_t *samples; /*%< the samples */
120 isc_uint32_t *extra; /*%< extra samples added in */
124 sample_queue_t samplequeue;
125 } isc_entropysamplesource_t;
128 isc_boolean_t start_called;
129 isc_entropystart_t startfunc;
130 isc_entropyget_t getfunc;
131 isc_entropystop_t stopfunc;
133 sample_queue_t samplequeue;
137 FILESOURCE_HANDLE_TYPE handle;
138 } isc_entropyfilesource_t;
140 struct isc_entropysource {
144 isc_uint32_t total; /*%< entropy from this source */
145 ISC_LINK(isc_entropysource_t) link;
148 isc_boolean_t warn_keyboard;
151 isc_entropysamplesource_t sample;
152 isc_entropyfilesource_t file;
153 isc_cbsource_t callback;
154 isc_entropyusocketsource_t usocket;
158 #define ENTROPY_SOURCETYPE_SAMPLE 1 /*%< Type is a sample source */
159 #define ENTROPY_SOURCETYPE_FILE 2 /*%< Type is a file source */
160 #define ENTROPY_SOURCETYPE_CALLBACK 3 /*%< Type is a callback source */
161 #define ENTROPY_SOURCETYPE_USOCKET 4 /*%< Type is a Unix socket source */
165 * The random pool "taps"
176 * Declarations for function provided by the system dependent sources that
180 fillpool(isc_entropy_t *, unsigned int, isc_boolean_t);
183 wait_for_sources(isc_entropy_t *);
186 destroyfilesource(isc_entropyfilesource_t *source);
189 destroyusocketsource(isc_entropyusocketsource_t *source);
194 samplequeue_release(isc_entropy_t *ent, sample_queue_t *sq) {
195 REQUIRE(sq->samples != NULL);
196 REQUIRE(sq->extra != NULL);
198 isc_mem_put(ent->mctx, sq->samples, RND_EVENTQSIZE * 4);
199 isc_mem_put(ent->mctx, sq->extra, RND_EVENTQSIZE * 4);
205 samplesource_allocate(isc_entropy_t *ent, sample_queue_t *sq) {
206 sq->samples = isc_mem_get(ent->mctx, RND_EVENTQSIZE * 4);
207 if (sq->samples == NULL)
208 return (ISC_R_NOMEMORY);
210 sq->extra = isc_mem_get(ent->mctx, RND_EVENTQSIZE * 4);
211 if (sq->extra == NULL) {
212 isc_mem_put(ent->mctx, sq->samples, RND_EVENTQSIZE * 4);
214 return (ISC_R_NOMEMORY);
219 return (ISC_R_SUCCESS);
223 * Add in entropy, even when the value we're adding in could be
227 add_entropy(isc_entropy_t *ent, isc_uint32_t entropy) {
228 /* clamp input. Yes, this must be done. */
229 entropy = ISC_MIN(entropy, RND_POOLBITS);
230 /* Add in the entropy we already have. */
231 entropy += ent->pool.entropy;
233 ent->pool.entropy = ISC_MIN(entropy, RND_POOLBITS);
237 * Decrement the amount of entropy the pool has.
240 subtract_entropy(isc_entropy_t *ent, isc_uint32_t entropy) {
241 entropy = ISC_MIN(entropy, ent->pool.entropy);
242 ent->pool.entropy -= entropy;
246 * Add in entropy, even when the value we're adding in could be
250 add_pseudo(isc_entropy_t *ent, isc_uint32_t pseudo) {
251 /* clamp input. Yes, this must be done. */
252 pseudo = ISC_MIN(pseudo, RND_POOLBITS * 8);
253 /* Add in the pseudo we already have. */
254 pseudo += ent->pool.pseudo;
256 ent->pool.pseudo = ISC_MIN(pseudo, RND_POOLBITS * 8);
260 * Decrement the amount of pseudo the pool has.
263 subtract_pseudo(isc_entropy_t *ent, isc_uint32_t pseudo) {
264 pseudo = ISC_MIN(pseudo, ent->pool.pseudo);
265 ent->pool.pseudo -= pseudo;
269 * Add one word to the pool, rotating the input as needed.
272 entropypool_add_word(isc_entropypool_t *rp, isc_uint32_t val) {
274 * Steal some values out of the pool, and xor them into the
275 * word we were given.
277 * Mix the new value into the pool using xor. This will
278 * prevent the actual values from being known to the caller
279 * since the previous values are assumed to be unknown as well.
281 val ^= rp->pool[(rp->cursor + TAP1) & (RND_POOLWORDS - 1)];
282 val ^= rp->pool[(rp->cursor + TAP2) & (RND_POOLWORDS - 1)];
283 val ^= rp->pool[(rp->cursor + TAP3) & (RND_POOLWORDS - 1)];
284 val ^= rp->pool[(rp->cursor + TAP4) & (RND_POOLWORDS - 1)];
285 val ^= rp->pool[(rp->cursor + TAP5) & (RND_POOLWORDS - 1)];
287 rp->pool[rp->cursor++] ^= val;
289 rp->pool[rp->cursor++] ^=
290 ((val << rp->rotate) | (val >> (32 - rp->rotate)));
293 * If we have looped around the pool, increment the rotate
294 * variable so the next value will get xored in rotated to
295 * a different position.
296 * Increment by a value that is relatively prime to the word size
297 * to try to spread the bits throughout the pool quickly when the
300 if (rp->cursor == RND_POOLWORDS) {
302 rp->rotate = (rp->rotate + 7) & 31;
307 * Add a buffer's worth of data to the pool.
309 * Requires that the lock is held on the entropy pool.
312 entropypool_adddata(isc_entropy_t *ent, void *p, unsigned int len,
313 isc_uint32_t entropy)
319 addr = (unsigned long)p;
322 if ((addr & 0x03U) != 0U) {
329 val = val << 8 | *buf++;
332 val = val << 8 | *buf++;
336 entropypool_add_word(&ent->pool, val);
339 for (; len > 3; len -= 4) {
340 val = *((isc_uint32_t *)buf);
342 entropypool_add_word(&ent->pool, val);
352 val = val << 8 | *buf++;
354 val = val << 8 | *buf++;
357 entropypool_add_word(&ent->pool, val);
360 add_entropy(ent, entropy);
361 subtract_pseudo(ent, entropy);
365 reseed(isc_entropy_t *ent) {
369 if (ent->initcount == 0) {
371 entropypool_adddata(ent, &pid, sizeof(pid), 0);
373 entropypool_adddata(ent, &pid, sizeof(pid), 0);
377 * After we've reseeded 100 times, only add new timing info every
378 * 50 requests. This will keep us from using lots and lots of
379 * CPU just to return bad pseudorandom data anyway.
381 if (ent->initcount > 100)
382 if ((ent->initcount % 50) != 0)
386 entropypool_adddata(ent, &t, sizeof(t), 0);
390 static inline unsigned int
391 estimate_entropy(sample_queue_t *sq, isc_uint32_t t) {
397 * If the time counter has overflowed, calculate the real difference.
398 * If it has not, it is simpler.
400 if (t < sq->last_time)
401 delta = UINT_MAX - sq->last_time + t;
403 delta = sq->last_time - t;
409 * Calculate the second and third order differentials
411 delta2 = sq->last_delta - delta;
415 delta3 = sq->last_delta2 - delta2;
420 sq->last_delta = delta;
421 sq->last_delta2 = delta2;
424 * If any delta is 0, we got no entropy. If all are non-zero, we
425 * might have something.
427 if (delta == 0 || delta2 == 0 || delta3 == 0)
431 * We could find the smallest delta and claim we got log2(delta)
432 * bits, but for now return that we found 1 bit.
438 crunchsamples(isc_entropy_t *ent, sample_queue_t *sq) {
442 if (sq->nsamples < 6)
446 sq->last_time = sq->samples[0];
451 * Prime the values by adding in the first 4 samples in. This
452 * should completely initialize the delta calculations.
454 for (ns = 0; ns < 4; ns++)
455 (void)estimate_entropy(sq, sq->samples[ns]);
457 for (ns = 4; ns < sq->nsamples; ns++)
458 added += estimate_entropy(sq, sq->samples[ns]);
460 entropypool_adddata(ent, sq->samples, sq->nsamples * 4, added);
461 entropypool_adddata(ent, sq->extra, sq->nsamples * 4, 0);
464 * Move the last 4 samples into the first 4 positions, and start
465 * adding new samples from that point.
467 for (ns = 0; ns < 4; ns++) {
468 sq->samples[ns] = sq->samples[sq->nsamples - 4 + ns];
469 sq->extra[ns] = sq->extra[sq->nsamples - 4 + ns];
478 get_from_callback(isc_entropysource_t *source, unsigned int desired,
479 isc_boolean_t blocking)
481 isc_entropy_t *ent = source->ent;
482 isc_cbsource_t *cbs = &source->sources.callback;
493 if (!cbs->start_called && cbs->startfunc != NULL) {
494 result = cbs->startfunc(source, cbs->arg, blocking);
495 if (result != ISC_R_SUCCESS)
497 cbs->start_called = ISC_TRUE;
501 result = ISC_R_SUCCESS;
502 while (desired > 0 && result == ISC_R_SUCCESS) {
503 result = cbs->getfunc(source, cbs->arg, blocking);
504 if (result == ISC_R_QUEUEFULL) {
505 got = crunchsamples(ent, &cbs->samplequeue);
507 desired -= ISC_MIN(got, desired);
508 result = ISC_R_SUCCESS;
509 } else if (result != ISC_R_SUCCESS &&
510 result != ISC_R_NOTBLOCKING)
511 source->bad = ISC_TRUE;
519 * Extract some number of bytes from the random pool, decreasing the
520 * estimate of randomness as each byte is extracted.
522 * Do this by stiring the pool and returning a part of hash as randomness.
523 * Note that no secrets are given away here since parts of the hash are
524 * xored together before returned.
526 * Honor the request from the caller to only return good data, any data,
530 isc_entropy_getdata(isc_entropy_t *ent, void *data, unsigned int length,
531 unsigned int *returned, unsigned int flags)
535 unsigned char digest[ISC_SHA1_DIGESTLENGTH];
536 isc_uint32_t remain, deltae, count, total;
538 isc_boolean_t goodonly, partial, blocking;
540 REQUIRE(VALID_ENTROPY(ent));
541 REQUIRE(data != NULL);
544 goodonly = ISC_TF((flags & ISC_ENTROPY_GOODONLY) != 0);
545 partial = ISC_TF((flags & ISC_ENTROPY_PARTIAL) != 0);
546 blocking = ISC_TF((flags & ISC_ENTROPY_BLOCKING) != 0);
548 REQUIRE(!partial || returned != NULL);
555 while (remain != 0) {
556 count = ISC_MIN(remain, RND_ENTROPY_THRESHOLD);
559 * If we are extracting good data only, make certain we
560 * have enough data in our pool for this pass. If we don't,
561 * get some, and fail if we can't, and partial returns
565 unsigned int fillcount;
567 fillcount = ISC_MAX(remain * 8, count * 8);
570 * If, however, we have at least THRESHOLD_BITS
571 * of entropy in the pool, don't block here. It is
572 * better to drain the pool once in a while and
573 * then refill it than it is to constantly keep the
576 if (ent->pool.entropy >= THRESHOLD_BITS)
577 fillpool(ent, fillcount, ISC_FALSE);
579 fillpool(ent, fillcount, blocking);
582 * Verify that we got enough entropy to do one
583 * extraction. If we didn't, bail.
585 if (ent->pool.entropy < THRESHOLD_BITS) {
593 * If we've extracted half our pool size in bits
594 * since the last refresh, try to refresh here.
596 if (ent->initialized < THRESHOLD_BITS)
597 fillpool(ent, THRESHOLD_BITS, blocking);
599 fillpool(ent, 0, ISC_FALSE);
602 * If we've not initialized with enough good random
603 * data, seed with our crappy code.
605 if (ent->initialized < THRESHOLD_BITS)
609 isc_sha1_init(&hash);
610 isc_sha1_update(&hash, (void *)(ent->pool.pool),
612 isc_sha1_final(&hash, digest);
615 * Stir the extracted data (all of it) back into the pool.
617 entropypool_adddata(ent, digest, ISC_SHA1_DIGESTLENGTH, 0);
619 for (i = 0; i < count; i++)
620 buf[i] = digest[i] ^ digest[i + RND_ENTROPY_THRESHOLD];
626 deltae = ISC_MIN(deltae, ent->pool.entropy);
628 subtract_entropy(ent, deltae);
629 add_pseudo(ent, count * 8);
633 memset(digest, 0, sizeof(digest));
635 if (returned != NULL)
636 *returned = (length - remain);
640 return (ISC_R_SUCCESS);
643 /* put the entropy we almost extracted back */
644 add_entropy(ent, total);
645 memset(data, 0, length);
646 memset(digest, 0, sizeof(digest));
647 if (returned != NULL)
652 return (ISC_R_NOENTROPY);
656 isc_entropypool_init(isc_entropypool_t *pool) {
657 pool->cursor = RND_POOLWORDS - 1;
661 memset(pool->pool, 0, RND_POOLBYTES);
665 isc_entropypool_invalidate(isc_entropypool_t *pool) {
670 memset(pool->pool, 0, RND_POOLBYTES);
674 isc_entropy_create(isc_mem_t *mctx, isc_entropy_t **entp) {
678 REQUIRE(mctx != NULL);
679 REQUIRE(entp != NULL && *entp == NULL);
681 ent = isc_mem_get(mctx, sizeof(isc_entropy_t));
683 return (ISC_R_NOMEMORY);
688 result = isc_mutex_init(&ent->lock);
689 if (result != ISC_R_SUCCESS)
693 * From here down, no failures will/can occur.
695 ISC_LIST_INIT(ent->sources);
696 ent->nextsource = NULL;
699 isc_mem_attach(mctx, &ent->mctx);
701 ent->initialized = 0;
703 ent->magic = ENTROPY_MAGIC;
705 isc_entropypool_init(&ent->pool);
708 return (ISC_R_SUCCESS);
711 isc_mem_put(mctx, ent, sizeof(isc_entropy_t));
717 * Requires "ent" be locked.
720 destroysource(isc_entropysource_t **sourcep) {
721 isc_entropysource_t *source;
729 ISC_LIST_UNLINK(ent->sources, source, link);
730 ent->nextsource = NULL;
731 REQUIRE(ent->nsources > 0);
734 switch (source->type) {
735 case ENTROPY_SOURCETYPE_FILE:
737 destroyfilesource(&source->sources.file);
739 case ENTROPY_SOURCETYPE_USOCKET:
741 destroyusocketsource(&source->sources.usocket);
743 case ENTROPY_SOURCETYPE_SAMPLE:
744 samplequeue_release(ent, &source->sources.sample.samplequeue);
746 case ENTROPY_SOURCETYPE_CALLBACK:
747 cbs = &source->sources.callback;
748 if (cbs->start_called && cbs->stopfunc != NULL) {
749 cbs->stopfunc(source, cbs->arg);
750 cbs->start_called = ISC_FALSE;
752 samplequeue_release(ent, &cbs->samplequeue);
756 memset(source, 0, sizeof(isc_entropysource_t));
758 isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
761 static inline isc_boolean_t
762 destroy_check(isc_entropy_t *ent) {
763 isc_entropysource_t *source;
768 source = ISC_LIST_HEAD(ent->sources);
769 while (source != NULL) {
770 switch (source->type) {
771 case ENTROPY_SOURCETYPE_FILE:
772 case ENTROPY_SOURCETYPE_USOCKET:
777 source = ISC_LIST_NEXT(source, link);
784 destroy(isc_entropy_t **entp) {
786 isc_entropysource_t *source;
789 REQUIRE(entp != NULL && *entp != NULL);
795 REQUIRE(ent->refcnt == 0);
798 * Here, detach non-sample sources.
800 source = ISC_LIST_HEAD(ent->sources);
801 while (source != NULL) {
802 switch(source->type) {
803 case ENTROPY_SOURCETYPE_FILE:
804 case ENTROPY_SOURCETYPE_USOCKET:
805 destroysource(&source);
808 source = ISC_LIST_HEAD(ent->sources);
812 * If there are other types of sources, we've found a bug.
814 REQUIRE(ISC_LIST_EMPTY(ent->sources));
818 isc_entropypool_invalidate(&ent->pool);
822 DESTROYLOCK(&ent->lock);
824 memset(ent, 0, sizeof(isc_entropy_t));
825 isc_mem_put(mctx, ent, sizeof(isc_entropy_t));
826 isc_mem_detach(&mctx);
830 isc_entropy_destroysource(isc_entropysource_t **sourcep) {
831 isc_entropysource_t *source;
833 isc_boolean_t killit;
835 REQUIRE(sourcep != NULL);
836 REQUIRE(VALID_SOURCE(*sourcep));
842 REQUIRE(VALID_ENTROPY(ent));
846 destroysource(&source);
848 killit = destroy_check(ent);
857 isc_entropy_createcallbacksource(isc_entropy_t *ent,
858 isc_entropystart_t start,
859 isc_entropyget_t get,
860 isc_entropystop_t stop,
862 isc_entropysource_t **sourcep)
865 isc_entropysource_t *source;
868 REQUIRE(VALID_ENTROPY(ent));
869 REQUIRE(get != NULL);
870 REQUIRE(sourcep != NULL && *sourcep == NULL);
874 source = isc_mem_get(ent->mctx, sizeof(isc_entropysource_t));
875 if (source == NULL) {
876 result = ISC_R_NOMEMORY;
879 source->bad = ISC_FALSE;
881 cbs = &source->sources.callback;
883 result = samplesource_allocate(ent, &cbs->samplequeue);
884 if (result != ISC_R_SUCCESS)
887 cbs->start_called = ISC_FALSE;
888 cbs->startfunc = start;
890 cbs->stopfunc = stop;
894 * From here down, no failures can occur.
896 source->magic = SOURCE_MAGIC;
897 source->type = ENTROPY_SOURCETYPE_CALLBACK;
900 memset(source->name, 0, sizeof(source->name));
901 ISC_LINK_INIT(source, link);
904 * Hook it into the entropy system.
906 ISC_LIST_APPEND(ent->sources, source, link);
912 return (ISC_R_SUCCESS);
916 isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
924 isc_entropy_stopcallbacksources(isc_entropy_t *ent) {
925 isc_entropysource_t *source;
928 REQUIRE(VALID_ENTROPY(ent));
932 source = ISC_LIST_HEAD(ent->sources);
933 while (source != NULL) {
934 if (source->type == ENTROPY_SOURCETYPE_CALLBACK) {
935 cbs = &source->sources.callback;
936 if (cbs->start_called && cbs->stopfunc != NULL) {
937 cbs->stopfunc(source, cbs->arg);
938 cbs->start_called = ISC_FALSE;
942 source = ISC_LIST_NEXT(source, link);
949 isc_entropy_createsamplesource(isc_entropy_t *ent,
950 isc_entropysource_t **sourcep)
953 isc_entropysource_t *source;
956 REQUIRE(VALID_ENTROPY(ent));
957 REQUIRE(sourcep != NULL && *sourcep == NULL);
961 source = isc_mem_get(ent->mctx, sizeof(isc_entropysource_t));
962 if (source == NULL) {
963 result = ISC_R_NOMEMORY;
967 sq = &source->sources.sample.samplequeue;
968 result = samplesource_allocate(ent, sq);
969 if (result != ISC_R_SUCCESS)
973 * From here down, no failures can occur.
975 source->magic = SOURCE_MAGIC;
976 source->type = ENTROPY_SOURCETYPE_SAMPLE;
979 memset(source->name, 0, sizeof(source->name));
980 ISC_LINK_INIT(source, link);
983 * Hook it into the entropy system.
985 ISC_LIST_APPEND(ent->sources, source, link);
991 return (ISC_R_SUCCESS);
995 isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
1003 * Add a sample, and return ISC_R_SUCCESS if the queue has become full,
1004 * ISC_R_NOENTROPY if it has space remaining, and ISC_R_NOMORE if the
1005 * queue was full when this function was called.
1008 addsample(sample_queue_t *sq, isc_uint32_t sample, isc_uint32_t extra) {
1009 if (sq->nsamples >= RND_EVENTQSIZE)
1010 return (ISC_R_NOMORE);
1012 sq->samples[sq->nsamples] = sample;
1013 sq->extra[sq->nsamples] = extra;
1016 if (sq->nsamples >= RND_EVENTQSIZE)
1017 return (ISC_R_QUEUEFULL);
1019 return (ISC_R_SUCCESS);
1023 isc_entropy_addsample(isc_entropysource_t *source, isc_uint32_t sample,
1028 unsigned int entropy;
1029 isc_result_t result;
1031 REQUIRE(VALID_SOURCE(source));
1037 sq = &source->sources.sample.samplequeue;
1038 result = addsample(sq, sample, extra);
1039 if (result == ISC_R_QUEUEFULL) {
1040 entropy = crunchsamples(ent, sq);
1041 add_entropy(ent, entropy);
1050 isc_entropy_addcallbacksample(isc_entropysource_t *source, isc_uint32_t sample,
1054 isc_result_t result;
1056 REQUIRE(VALID_SOURCE(source));
1057 REQUIRE(source->type == ENTROPY_SOURCETYPE_CALLBACK);
1059 sq = &source->sources.callback.samplequeue;
1060 result = addsample(sq, sample, extra);
1066 isc_entropy_putdata(isc_entropy_t *ent, void *data, unsigned int length,
1067 isc_uint32_t entropy)
1069 REQUIRE(VALID_ENTROPY(ent));
1073 entropypool_adddata(ent, data, length, entropy);
1075 if (ent->initialized < THRESHOLD_BITS)
1076 ent->initialized = THRESHOLD_BITS;
1082 dumpstats(isc_entropy_t *ent, FILE *out) {
1084 isc_msgcat_get(isc_msgcat, ISC_MSGSET_ENTROPY,
1085 ISC_MSG_ENTROPYSTATS,
1086 "Entropy pool %p: refcnt %u cursor %u,"
1087 " rotate %u entropy %u pseudo %u nsources %u"
1088 " nextsource %p initialized %u initcount %u\n"),
1090 ent->pool.cursor, ent->pool.rotate,
1091 ent->pool.entropy, ent->pool.pseudo,
1092 ent->nsources, ent->nextsource, ent->initialized,
1097 * This function ignores locking. Use at your own risk.
1100 isc_entropy_stats(isc_entropy_t *ent, FILE *out) {
1101 REQUIRE(VALID_ENTROPY(ent));
1104 dumpstats(ent, out);
1109 isc_entropy_status(isc_entropy_t *ent) {
1110 unsigned int estimate;
1113 estimate = ent->pool.entropy;
1120 isc_entropy_attach(isc_entropy_t *ent, isc_entropy_t **entp) {
1121 REQUIRE(VALID_ENTROPY(ent));
1122 REQUIRE(entp != NULL && *entp == NULL);
1133 isc_entropy_detach(isc_entropy_t **entp) {
1135 isc_boolean_t killit;
1137 REQUIRE(entp != NULL && VALID_ENTROPY(*entp));
1143 REQUIRE(ent->refcnt > 0);
1146 killit = destroy_check(ent);
1155 kbdstart(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
1157 * The intent of "first" is to provide a warning message only once
1158 * during the run of a program that might try to gather keyboard
1159 * entropy multiple times.
1161 static isc_boolean_t first = ISC_TRUE;
1166 return (ISC_R_NOENTROPY);
1169 if (source->warn_keyboard)
1170 fprintf(stderr, "You must use the keyboard to create "
1171 "entropy, since your system is lacking\n"
1172 "/dev/random (or equivalent)\n\n");
1175 fprintf(stderr, "start typing:\n");
1177 return (isc_keyboard_open(&source->kbd));
1181 kbdstop(isc_entropysource_t *source, void *arg) {
1185 if (! isc_keyboard_canceled(&source->kbd))
1186 fprintf(stderr, "stop typing.\r\n");
1188 (void)isc_keyboard_close(&source->kbd, 3);
1192 kbdget(isc_entropysource_t *source, void *arg, isc_boolean_t blocking) {
1193 isc_result_t result;
1195 isc_uint32_t sample;
1202 return (ISC_R_NOTBLOCKING);
1204 result = isc_keyboard_getchar(&source->kbd, &c);
1205 if (result != ISC_R_SUCCESS)
1210 sample = isc_time_nanoseconds(&t);
1213 result = isc_entropy_addcallbacksample(source, sample, extra);
1214 if (result != ISC_R_SUCCESS) {
1215 fprintf(stderr, "\r\n");
1219 fprintf(stderr, ".");
1226 isc_entropy_usebestsource(isc_entropy_t *ectx, isc_entropysource_t **source,
1227 const char *randomfile, int use_keyboard)
1229 isc_result_t result;
1230 isc_result_t final_result = ISC_R_NOENTROPY;
1231 isc_boolean_t userfile = ISC_TRUE;
1233 REQUIRE(VALID_ENTROPY(ectx));
1234 REQUIRE(source != NULL && *source == NULL);
1235 REQUIRE(use_keyboard == ISC_ENTROPY_KEYBOARDYES ||
1236 use_keyboard == ISC_ENTROPY_KEYBOARDNO ||
1237 use_keyboard == ISC_ENTROPY_KEYBOARDMAYBE);
1239 #ifdef PATH_RANDOMDEV
1240 if (randomfile == NULL) {
1241 randomfile = PATH_RANDOMDEV;
1242 userfile = ISC_FALSE;
1246 if (randomfile != NULL && use_keyboard != ISC_ENTROPY_KEYBOARDYES) {
1247 result = isc_entropy_createfilesource(ectx, randomfile);
1248 if (result == ISC_R_SUCCESS &&
1249 use_keyboard == ISC_ENTROPY_KEYBOARDMAYBE)
1250 use_keyboard = ISC_ENTROPY_KEYBOARDNO;
1251 if (result != ISC_R_SUCCESS && userfile)
1254 final_result = result;
1257 if (use_keyboard != ISC_ENTROPY_KEYBOARDNO) {
1258 result = isc_entropy_createcallbacksource(ectx, kbdstart,
1261 if (result == ISC_R_SUCCESS)
1262 (*source)->warn_keyboard =
1263 ISC_TF(use_keyboard ==
1264 ISC_ENTROPY_KEYBOARDMAYBE);
1266 if (final_result != ISC_R_SUCCESS)
1267 final_result = result;
1271 * final_result is ISC_R_SUCCESS if at least one source of entropy
1272 * could be started, otherwise it is the error from the most recently
1273 * failed operation (or ISC_R_NOENTROPY if PATH_RANDOMDEV is not
1274 * defined and use_keyboard is ISC_ENTROPY_KEYBOARDNO).
1276 return (final_result);