From 9ceb50ed0460a76b14c9f386fdf1d58dff3d5e6d Mon Sep 17 00:00:00 2001 From: Michael Graff Date: Sat, 17 Jun 2000 00:43:11 +0000 Subject: [PATCH] protect isc_entropy_stats() with a lock, and use a static dumpstats() internally when a lock is already held, for debugging. Add isc_entropy_putdata() which can be used to prime the pool. --- lib/isc/include/isc/entropy.h | 14 ++++++++++++++ lib/isc/unix/entropy.c | 35 ++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/isc/include/isc/entropy.h b/lib/isc/include/isc/entropy.h index 447b3b99c0..ba24e2766a 100644 --- a/lib/isc/include/isc/entropy.h +++ b/lib/isc/include/isc/entropy.h @@ -206,8 +206,22 @@ isc_entropy_getdata(isc_entropy_t *ent, void *data, unsigned int length, * sources. */ +void +isc_entropy_putdata(isc_entropy_t *ent, void *data, unsigned int length, + isc_uint32_t entropy); +/* + * Add "length" bytes in "data" to the entropy pool, incrementing the pool's + * entropy count by "entropy." + * + * These bytes will prime the pseudorandom portion even no entropy is actually + * added. + */ + void isc_entropy_stats(isc_entropy_t *ent, FILE *out); +/* + * Dump some (trivial) stats to the stdio stream "out". + */ ISC_LANG_ENDDECLS diff --git a/lib/isc/unix/entropy.c b/lib/isc/unix/entropy.c index 32aa11c106..23688a6cf1 100644 --- a/lib/isc/unix/entropy.c +++ b/lib/isc/unix/entropy.c @@ -278,6 +278,7 @@ entropypool_adddata(isc_entropy_t *ent, void *p, unsigned int len, } add_entropy(ent, entropy); + subtract_pseudo(ent, entropy); } static isc_uint32_t @@ -436,11 +437,6 @@ fillpool(isc_entropy_t *ent, unsigned int needed, isc_boolean_t blocking) { goto again; } - /* - * Adjust counts. - */ - subtract_pseudo(ent, added); - /* * Mark as initialized if we've added enough data. */ @@ -967,13 +963,22 @@ isc_entropy_addsample(isc_entropysource_t *source, isc_uint32_t sample, UNLOCK(&ent->lock); } -/* - * This function ignores locking. Use at your own risk. - */ void -isc_entropy_stats(isc_entropy_t *ent, FILE *out) { +isc_entropy_putdata(isc_entropy_t *ent, void *data, unsigned int length, + isc_uint32_t entropy) +{ REQUIRE(VALID_ENTROPY(ent)); + LOCK(&ent->lock); + entropypool_adddata(ent, data, length, entropy); + + if (ent->initialized < THRESHOLD_BITS) + ent->initialized = THRESHOLD_BITS; + UNLOCK(&ent->lock); +} + +static void +dumpstats(isc_entropy_t *ent, FILE *out) { fprintf(out, "Entropy pool %p: refcnt %u" " cursor %u, rotate %u entropy %u pseudo %u nsources %u" @@ -984,6 +989,18 @@ isc_entropy_stats(isc_entropy_t *ent, FILE *out) { ent->nsources, ent->nextsource, ent->initialized); } +/* + * This function ignores locking. Use at your own risk. + */ +void +isc_entropy_stats(isc_entropy_t *ent, FILE *out) { + REQUIRE(VALID_ENTROPY(ent)); + + LOCK(&ent->lock); + dumpstats(ent, out); + UNLOCK(&ent->lock); +} + void isc_entropy_attach(isc_entropy_t *ent, isc_entropy_t **entp) { REQUIRE(VALID_ENTROPY(ent));