Date: Sat, 7 Sep 2013 14:15:13 +0000 (UTC) From: Mark Murray <markm@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r255362 - in head: share/examples/kld share/examples/kld/random_adaptor sys/conf sys/dev/glxsb sys/dev/hifn sys/dev/random sys/dev/rndtest sys/dev/safe sys/dev/ubsec sys/kern sys/mips/c... Message-ID: <201309071415.r87EFDMv025499@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: markm Date: Sat Sep 7 14:15:13 2013 New Revision: 255362 URL: http://svnweb.freebsd.org/changeset/base/255362 Log: Bring in some behind-the-scenes development, mainly By Arthur Mesh, the rest by me. o Namespace cleanup; the Yarrow name is now restricted to where it really applies; this is in anticipation of being augmented or replaced by Fortuna in the future. Fortuna is mentioned, but behind #if logic, and is ignorable for now. o The harvest queue is pulled out into its own modules. o Entropy harvesting is emproved, both by being made more conservative, and by separating (a bit!) the sources. Available entropy crumbs are marginally improved. o Selection of sources is made clearer. With recent revelations, this will receive more work in the weeks and months to come. Submitted by: Arthur Mesh (partly) <arthurmesh@gmail.com> Added: head/share/examples/kld/random_adaptor/ - copied from r255353, projects/random_number_generator/share/examples/kld/random_adaptor/ head/sys/dev/random/pseudo_rng.c - copied unchanged from r255353, projects/random_number_generator/sys/dev/random/pseudo_rng.c head/sys/dev/random/random_harvestq.c - copied unchanged from r255353, projects/random_number_generator/sys/dev/random/random_harvestq.c head/sys/dev/random/random_harvestq.h - copied unchanged from r255353, projects/random_number_generator/sys/dev/random/random_harvestq.h Deleted: head/sys/dev/random/probe.c Modified: head/share/examples/kld/Makefile head/sys/conf/files head/sys/dev/glxsb/glxsb.c head/sys/dev/hifn/hifn7751.c head/sys/dev/random/harvest.c head/sys/dev/random/hash.c head/sys/dev/random/hash.h head/sys/dev/random/random_adaptors.c head/sys/dev/random/random_adaptors.h head/sys/dev/random/randomdev.c head/sys/dev/random/randomdev.h head/sys/dev/random/randomdev_soft.c head/sys/dev/random/randomdev_soft.h head/sys/dev/random/yarrow.c head/sys/dev/random/yarrow.h head/sys/dev/rndtest/rndtest.c head/sys/dev/safe/safe.c head/sys/dev/ubsec/ubsec.c head/sys/kern/kern_intr.c head/sys/mips/cavium/octeon_rnd.c head/sys/modules/random/Makefile head/sys/net/if_ethersubr.c head/sys/net/if_tun.c head/sys/netgraph/ng_iface.c head/sys/sys/random.h Directory Properties: head/ (props changed) head/sys/ (props changed) Modified: head/share/examples/kld/Makefile ============================================================================== --- head/share/examples/kld/Makefile Sat Sep 7 14:04:10 2013 (r255361) +++ head/share/examples/kld/Makefile Sat Sep 7 14:15:13 2013 (r255362) @@ -67,6 +67,6 @@ # $FreeBSD$ # -SUBDIR= cdev dyn_sysctl firmware khelp syscall +SUBDIR= cdev dyn_sysctl firmware khelp random_adaptor syscall .include <bsd.subdir.mk> Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/conf/files Sat Sep 7 14:15:13 2013 (r255362) @@ -2042,8 +2042,9 @@ rt2860.fw optional rt2860fw | ralfw \ clean "rt2860.fw" dev/random/harvest.c standard dev/random/hash.c optional random -dev/random/probe.c optional random +dev/random/pseudo_rng.c standard dev/random/random_adaptors.c standard +dev/random/random_harvestq.c standard dev/random/randomdev.c optional random dev/random/randomdev_soft.c optional random dev/random/yarrow.c optional random Modified: head/sys/dev/glxsb/glxsb.c ============================================================================== --- head/sys/dev/glxsb/glxsb.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/glxsb/glxsb.c Sat Sep 7 14:15:13 2013 (r255362) @@ -476,7 +476,7 @@ glxsb_rnd(void *v) if (status & SB_RNS_TRNG_VALID) { value = bus_read_4(sc->sc_sr, SB_RANDOM_NUM); /* feed with one uint32 */ - random_harvest(&value, 4, 32, 0, RANDOM_PURE); + random_harvest(&value, 4, 32/2, 0, RANDOM_PURE); } callout_reset(&sc->sc_rngco, sc->sc_rnghz, glxsb_rnd, sc); Modified: head/sys/dev/hifn/hifn7751.c ============================================================================== --- head/sys/dev/hifn/hifn7751.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/hifn/hifn7751.c Sat Sep 7 14:15:13 2013 (r255362) @@ -258,7 +258,7 @@ hifn_partname(struct hifn_softc *sc) static void default_harvest(struct rndtest_state *rsp, void *buf, u_int count) { - random_harvest(buf, count, count*NBBY, 0, RANDOM_PURE); + random_harvest(buf, count, count*NBBY/2, 0, RANDOM_PURE); } static u_int Modified: head/sys/dev/random/harvest.c ============================================================================== --- head/sys/dev/random/harvest.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/harvest.c Sat Sep 7 14:15:13 2013 (r255362) @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2000-2004 Mark R V Murray + * Copyright (c) 2000-2013 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -60,7 +60,7 @@ static int (*read_func)(void *, int) = r /* Initialise the harvester at load time */ void -random_yarrow_init_harvester(void (*reaper)(u_int64_t, const void *, u_int, +randomdev_init_harvester(void (*reaper)(u_int64_t, const void *, u_int, u_int, u_int, enum esource), int (*reader)(void *, int)) { reap_func = reaper; @@ -69,7 +69,7 @@ random_yarrow_init_harvester(void (*reap /* Deinitialise the harvester at unload time */ void -random_yarrow_deinit_harvester(void) +randomdev_deinit_harvester(void) { reap_func = NULL; read_func = read_random_phony; Modified: head/sys/dev/random/hash.c ============================================================================== --- head/sys/dev/random/hash.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/hash.c Sat Sep 7 14:15:13 2013 (r255362) @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2000-2004 Mark R V Murray + * Copyright (c) 2000-2013 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,46 +36,46 @@ __FBSDID("$FreeBSD$"); #include <dev/random/hash.h> -/* initialise the hash */ +/* Initialise the hash */ void -yarrow_hash_init(struct yarrowhash *context) +randomdev_hash_init(struct randomdev_hash *context) { SHA256_Init(&context->sha); } -/* iterate the hash */ +/* Iterate the hash */ void -yarrow_hash_iterate(struct yarrowhash *context, void *data, size_t size) +randomdev_hash_iterate(struct randomdev_hash *context, void *data, size_t size) { SHA256_Update(&context->sha, data, size); } -/* Conclude by returning the hash in the supplied /buf/ which must be +/* Conclude by returning the hash in the supplied <*buf> which must be * KEYSIZE bytes long. */ void -yarrow_hash_finish(struct yarrowhash *context, void *buf) +randomdev_hash_finish(struct randomdev_hash *context, void *buf) { SHA256_Final(buf, &context->sha); } /* Initialise the encryption routine by setting up the key schedule - * from the supplied /data/ which must be KEYSIZE bytes of binary - * data. + * from the supplied <*data> which must be KEYSIZE bytes of binary + * data. Use CBC mode for better avalanche. */ void -yarrow_encrypt_init(struct yarrowkey *context, void *data) +randomdev_encrypt_init(struct randomdev_key *context, void *data) { rijndael_cipherInit(&context->cipher, MODE_CBC, NULL); rijndael_makeKey(&context->key, DIR_ENCRYPT, KEYSIZE*8, data); } /* Encrypt the supplied data using the key schedule preset in the context. - * KEYSIZE bytes are encrypted from /d_in/ to /d_out/. + * <length> bytes are encrypted from <*d_in> to <*d_out>. <length> must be + * a multiple of BLOCKSIZE. */ void -yarrow_encrypt(struct yarrowkey *context, void *d_in, void *d_out) +randomdev_encrypt(struct randomdev_key *context, void *d_in, void *d_out, unsigned length) { - rijndael_blockEncrypt(&context->cipher, &context->key, d_in, - KEYSIZE*8, d_out); + rijndael_blockEncrypt(&context->cipher, &context->key, d_in, length*8, d_out); } Modified: head/sys/dev/random/hash.h ============================================================================== --- head/sys/dev/random/hash.h Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/hash.h Sat Sep 7 14:15:13 2013 (r255362) @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2000-2004 Mark R V Murray + * Copyright (c) 2000-2013 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,19 +26,20 @@ * $FreeBSD$ */ -#define KEYSIZE 32 /* (in bytes) 32 bytes == 256 bits */ +#define KEYSIZE 32 /* (in bytes) == 256 bits */ +#define BLOCKSIZE 16 /* (in bytes) == 128 bits */ -struct yarrowhash { /* Big! Make static! */ +struct randomdev_hash { /* Big! Make static! */ SHA256_CTX sha; }; -struct yarrowkey { /* Big! Make static! */ +struct randomdev_key { /* Big! Make static! */ keyInstance key; /* Key schedule */ cipherInstance cipher; /* Rijndael internal */ }; -void yarrow_hash_init(struct yarrowhash *); -void yarrow_hash_iterate(struct yarrowhash *, void *, size_t); -void yarrow_hash_finish(struct yarrowhash *, void *); -void yarrow_encrypt_init(struct yarrowkey *, void *); -void yarrow_encrypt(struct yarrowkey *context, void *, void *); +void randomdev_hash_init(struct randomdev_hash *); +void randomdev_hash_iterate(struct randomdev_hash *, void *, size_t); +void randomdev_hash_finish(struct randomdev_hash *, void *); +void randomdev_encrypt_init(struct randomdev_key *, void *); +void randomdev_encrypt(struct randomdev_key *context, void *, void *, unsigned); Copied: head/sys/dev/random/pseudo_rng.c (from r255353, projects/random_number_generator/sys/dev/random/pseudo_rng.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/random/pseudo_rng.c Sat Sep 7 14:15:13 2013 (r255362, copy of r255353, projects/random_number_generator/sys/dev/random/pseudo_rng.c) @@ -0,0 +1,119 @@ +/*- + * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/time.h> +#include <sys/kernel.h> +#include <sys/module.h> +#include <sys/selinfo.h> +#include <sys/systm.h> + +#include <dev/random/random_adaptors.h> +#include <dev/random/randomdev.h> + +static struct mtx pseudo_random_block_mtx; + +static int +pseudo_random_block_read(void *buf __unused, int c __unused) +{ + + mtx_lock(&pseudo_random_block_mtx); + + printf("random(4) device is blocking.\n"); + msleep(pseudo_random_block_read, &pseudo_random_block_mtx, 0, + "block", 0); + + mtx_unlock(&pseudo_random_block_mtx); + + return (0); +} + +static void +pseudo_random_block_init(void) +{ + + mtx_init(&pseudo_random_block_mtx, "sleep mtx for random_block", + NULL, MTX_DEF); +} + +static void +pseudo_random_block_deinit(void) +{ + + mtx_destroy(&pseudo_random_block_mtx); +} + +struct random_adaptor pseudo_random_block = { + .ident = "pseudo-RNG that always blocks", + .init = pseudo_random_block_init, + .deinit = pseudo_random_block_deinit, + .read = pseudo_random_block_read, + .write = (random_write_func_t *)random_null_func, + .reseed = (random_reseed_func_t *)random_null_func, + .seeded = 1, +}; + +static int +pseudo_random_panic_read(void *buf, int c) +{ + + panic("Insert a witty panic msg in here."); + + return (0); +} + +struct random_adaptor pseudo_random_panic = { + .ident = "pseudo-RNG that always panics on first read(2)", + .init = (random_init_func_t *)random_null_func, + .deinit = (random_deinit_func_t *)random_null_func, + .read = pseudo_random_panic_read, + .write = (random_write_func_t *)random_null_func, + .reseed = (random_reseed_func_t *)random_null_func, + .seeded = 1, +}; + +static int +pseudo_random_modevent(module_t mod, int type, void *unused) +{ + + switch (type) { + case MOD_LOAD: + random_adaptor_register("block", &pseudo_random_block); + EVENTHANDLER_INVOKE(random_adaptor_attach, + &pseudo_random_block); + + random_adaptor_register("panic", &pseudo_random_panic); + + return (0); + } + + return (EINVAL); +} + +RANDOM_ADAPTOR_MODULE(pseudo, pseudo_random_modevent, 1); Modified: head/sys/dev/random/random_adaptors.c ============================================================================== --- head/sys/dev/random/random_adaptors.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/random_adaptors.c Sat Sep 7 14:15:13 2013 (r255362) @@ -1,6 +1,7 @@ /*- * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org> + * Copyright (c) 2004 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,16 +31,20 @@ __FBSDID("$FreeBSD$"); #include <sys/kernel.h> #include <sys/systm.h> +#include <sys/kthread.h> #include <sys/lock.h> +#include <sys/random.h> #include <sys/selinfo.h> #include <sys/sysctl.h> #include <sys/sx.h> #include <sys/malloc.h> #include <sys/queue.h> #include <sys/libkern.h> +#include <sys/unistd.h> -#include <dev/random/random_adaptors.h> #include <dev/random/randomdev.h> +#include <dev/random/randomdev_soft.h> +#include <dev/random/random_adaptors.h> LIST_HEAD(adaptors_head, random_adaptors); static struct adaptors_head adaptors = LIST_HEAD_INITIALIZER(adaptors); @@ -57,7 +62,8 @@ random_adaptor_register(const char *name KASSERT(name != NULL && rsp != NULL, ("invalid input to %s", __func__)); - rpp = malloc(sizeof(struct random_adaptors), M_RANDOM_ADAPTORS, M_WAITOK); + rpp = malloc(sizeof(struct random_adaptors), M_RANDOM_ADAPTORS, + M_WAITOK); rpp->name = name; rpp->rsp = rsp; @@ -87,6 +93,93 @@ random_adaptor_get(const char *name) return (rsp); } +/* + * In the past, the logic of the random_adaptor selection was inverted, such + * that hardware RNGs would be chosen unless disabled. This routine is here to + * preserve that functionality to avoid folks losing their hardware RNGs by + * upgrading to newer kernel. + */ +static void +random_adaptor_choose_legacy(struct random_adaptor **adaptor) +{ + struct random_adaptor *tmp; + int enable; + + /* Then go looking for hardware */ + enable = 1; + TUNABLE_INT_FETCH("hw.nehemiah_rng_enable", &enable); + if (enable && (tmp = random_adaptor_get("nehemiah"))) + *adaptor = tmp; + + enable = 1; + TUNABLE_INT_FETCH("hw.ivy_rng_enable", &enable); + if (enable && (tmp = random_adaptor_get("rdrand"))) + *adaptor = tmp; +} + +/* + * Walk a list of registered random(4) adaptors and pick the last non-selected + * one. + * + * If none are selected, use yarrow if available. + */ +void +random_adaptor_choose(struct random_adaptor **adaptor) +{ + char rngs[128], *token, *cp; + struct random_adaptors *rpp; + + KASSERT(adaptor != NULL, ("pre-conditions failed")); + + *adaptor = NULL; + + random_adaptor_choose_legacy(adaptor); + + if (*adaptor != NULL) + return; + + if (TUNABLE_STR_FETCH("rngs_want", rngs, sizeof(rngs))) { + cp = rngs; + + while ((token = strsep(&cp, ",")) != NULL) { + if ((*adaptor = random_adaptor_get(token)) != NULL) + break; + else if (bootverbose) + printf( + "%s random adaptor is not available, skipping\n", + token); + } + } + + if (*adaptor == NULL) { + /* + * Either no RNGs are prefered via rngs_want tunable, or + * no prefered RNGs are registered. + * Fallback to Yarrow. + */ + *adaptor = random_adaptor_get("yarrow"); + + if (*adaptor == NULL) { + /* + * Yarrow doesn't seem to be available. + * Fallback to the first thing that's on the list of + * available RNGs. + */ + sx_slock(&adaptors_lock); + + rpp = LIST_FIRST(&adaptors); + if (rpp != NULL) + *adaptor = rpp->rsp; + + sx_sunlock(&adaptors_lock); + } + + if (bootverbose && *adaptor) + printf("Falling back to <%s> random adaptor", + (*adaptor)->ident); + } +} + static void random_adaptors_deinit(void *unused) { @@ -99,18 +192,28 @@ static int random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS) { struct random_adaptors *rpp; - int error; + int error, count; - error = 0; + count = error = 0; sx_slock(&adaptors_lock); - if (LIST_EMPTY(&adaptors)) - error = SYSCTL_OUT(req, "", strlen("")); + if (LIST_EMPTY(&adaptors)) { + error = SYSCTL_OUT(req, "", 0); + } else { - LIST_FOREACH(rpp, &adaptors, entries) { - if (0 != SYSCTL_OUT(req, rpp->name, strlen(rpp->name))) - break; + LIST_FOREACH(rpp, &adaptors, entries) { + + error = SYSCTL_OUT(req, ",", count++ ? 1 : 0); + + if (error) + break; + + error = SYSCTL_OUT(req, rpp->name, strlen(rpp->name)); + + if (error) + break; + } } sx_sunlock(&adaptors_lock); @@ -118,6 +221,37 @@ random_sysctl_adaptors_handler(SYSCTL_HA return (error); } +static int +random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS) +{ + struct random_adaptor *rsp; + struct random_adaptors *rpp; + const char *name; + int error; + + name = NULL; + rsp = random_get_active_adaptor(); + + if (rsp != NULL) { + sx_slock(&adaptors_lock); + + LIST_FOREACH(rpp, &adaptors, entries) { + if (rpp->rsp == rsp) + name = rpp->name; + } + + sx_sunlock(&adaptors_lock); + } + + if (rsp == NULL || name == NULL) { + error = SYSCTL_OUT(req, "", 0); + } else { + error = SYSCTL_OUT(req, name, strlen(name)); + } + + return (error); +} + static void random_adaptors_init(void *unused) { @@ -127,6 +261,11 @@ random_adaptors_init(void *unused) NULL, 0, random_sysctl_adaptors_handler, "", "Random Number Generator adaptors"); + SYSCTL_PROC(_kern_random, OID_AUTO, active_adaptor, + CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, + NULL, 0, random_sysctl_active_adaptor_handler, "", + "Active Random Number Generator Adaptor"); + sx_init(&adaptors_lock, "random_adaptors"); } Modified: head/sys/dev/random/random_adaptors.h ============================================================================== --- head/sys/dev/random/random_adaptors.h Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/random_adaptors.h Sat Sep 7 14:15:13 2013 (r255362) @@ -39,6 +39,7 @@ struct random_adaptors { struct random_adaptor *random_adaptor_get(const char *); int random_adaptor_register(const char *, struct random_adaptor *); +void random_adaptor_choose(struct random_adaptor **); /* * random_adaptor's should be registered prior to Copied: head/sys/dev/random/random_harvestq.c (from r255353, projects/random_number_generator/sys/dev/random/random_harvestq.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/random/random_harvestq.c Sat Sep 7 14:15:13 2013 (r255362, copy of r255353, projects/random_number_generator/sys/dev/random/random_harvestq.c) @@ -0,0 +1,251 @@ +/*- + * Copyright (c) 2013 Arthur Mesh + * Copyright (c) 2000-2009 Mark R V Murray + * Copyright (c) 2004 Robert N. M. Watson + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/kthread.h> +#include <sys/lock.h> +#include <sys/malloc.h> +#include <sys/mutex.h> +#include <sys/random.h> +#include <sys/sysctl.h> +#include <sys/unistd.h> + +#include <dev/random/randomdev_soft.h> + +#include "random_harvestq.h" + +#define RANDOM_FIFO_MAX 256 /* How many events to queue up */ + +MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); + +/* + * The harvest mutex protects the consistency of the entropy fifos and + * empty fifo. + */ +struct mtx harvest_mtx; + +/* Lockable FIFO queue holding entropy buffers */ +struct entropyfifo { + int count; + STAILQ_HEAD(harvestlist, harvest) head; +}; + +/* Empty entropy buffers */ +static struct entropyfifo emptyfifo; + +#define EMPTYBUFFERS 1024 + +/* Harvested entropy */ +static struct entropyfifo harvestfifo[ENTROPYSOURCE]; + +/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */ +int random_kthread_control = 0; + +static struct proc *random_kthread_proc; + +static void +random_kthread(void *arg) +{ + STAILQ_HEAD(, harvest) local_queue; + struct harvest *event = NULL; + int local_count; + enum esource source; + event_proc_f func = arg; + + STAILQ_INIT(&local_queue); + local_count = 0; + + /* Process until told to stop */ + mtx_lock_spin(&harvest_mtx); + for (; random_kthread_control >= 0;) { + + /* Cycle through all the entropy sources */ + for (source = RANDOM_START; source < ENTROPYSOURCE; source++) { + /* + * Drain entropy source records into a thread-local + * queue for processing while not holding the mutex. + */ + STAILQ_CONCAT(&local_queue, &harvestfifo[source].head); + local_count += harvestfifo[source].count; + harvestfifo[source].count = 0; + } + + /* + * Deal with events, if any, dropping the mutex as we process + * each event. Then push the events back into the empty + * fifo. + */ + if (!STAILQ_EMPTY(&local_queue)) { + mtx_unlock_spin(&harvest_mtx); + STAILQ_FOREACH(event, &local_queue, next) + func(event); + mtx_lock_spin(&harvest_mtx); + STAILQ_CONCAT(&emptyfifo.head, &local_queue); + emptyfifo.count += local_count; + local_count = 0; + } + + KASSERT(local_count == 0, ("random_kthread: local_count %d", + local_count)); + + /* + * If a queue flush was commanded, it has now happened, + * and we can mark this by resetting the command. + */ + if (random_kthread_control == 1) + random_kthread_control = 0; + + /* Work done, so don't belabour the issue */ + msleep_spin_sbt(&random_kthread_control, &harvest_mtx, + "-", SBT_1S / 10, 0, C_PREL(1)); + + } + mtx_unlock_spin(&harvest_mtx); + + random_set_wakeup_exit(&random_kthread_control); + /* NOTREACHED */ +} + +void +random_harvestq_init(event_proc_f cb) +{ + int error, i; + struct harvest *np; + enum esource e; + + /* Initialise the harvest fifos */ + STAILQ_INIT(&emptyfifo.head); + emptyfifo.count = 0; + for (i = 0; i < EMPTYBUFFERS; i++) { + np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK); + STAILQ_INSERT_TAIL(&emptyfifo.head, np, next); + } + for (e = RANDOM_START; e < ENTROPYSOURCE; e++) { + STAILQ_INIT(&harvestfifo[e].head); + harvestfifo[e].count = 0; + } + + mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN); + + + /* Start the hash/reseed thread */ + error = kproc_create(random_kthread, cb, + &random_kthread_proc, RFHIGHPID, 0, "rand_harvestq"); /* RANDOM_CSPRNG_NAME */ + + if (error != 0) + panic("Cannot create entropy maintenance thread."); +} + +void +random_harvestq_deinit(void) +{ + struct harvest *np; + enum esource e; + + /* Destroy the harvest fifos */ + while (!STAILQ_EMPTY(&emptyfifo.head)) { + np = STAILQ_FIRST(&emptyfifo.head); + STAILQ_REMOVE_HEAD(&emptyfifo.head, next); + free(np, M_ENTROPY); + } + for (e = RANDOM_START; e < ENTROPYSOURCE; e++) { + while (!STAILQ_EMPTY(&harvestfifo[e].head)) { + np = STAILQ_FIRST(&harvestfifo[e].head); + STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next); + free(np, M_ENTROPY); + } + } + + mtx_destroy(&harvest_mtx); +} + +/* + * Entropy harvesting routine. This is supposed to be fast; do + * not do anything slow in here! + */ +void +random_harvestq_internal(u_int64_t somecounter, const void *entropy, + u_int count, u_int bits, u_int frac, enum esource origin) +{ + struct harvest *event; + + KASSERT(origin >= RANDOM_START && origin <= RANDOM_PURE, + ("random_harvest_internal: origin %d invalid\n", origin)); + + /* Lockless read to avoid lock operations if fifo is full. */ + if (harvestfifo[origin].count >= RANDOM_FIFO_MAX) + return; + + mtx_lock_spin(&harvest_mtx); + + /* + * Don't make the harvest queues too big - help to prevent low-grade + * entropy swamping + */ + if (harvestfifo[origin].count < RANDOM_FIFO_MAX) { + event = STAILQ_FIRST(&emptyfifo.head); + if (event != NULL) { + /* Add the harvested data to the fifo */ + STAILQ_REMOVE_HEAD(&emptyfifo.head, next); + harvestfifo[origin].count++; + event->somecounter = somecounter; + event->size = count; + event->bits = bits; + event->frac = frac; + event->source = origin; + + /* XXXX Come back and make this dynamic! */ + count = MIN(count, HARVESTSIZE); + memcpy(event->entropy, entropy, count); + +#if 0 + { + int i; + printf("Harvest:%16jX ", event->somecounter); + for (i = 0; i < event->size; i++) + printf("%02X", event->entropy[i]); + for (; i < 16; i++) + printf(" "); + printf(" %2d 0x%2X.%03X %02X\n", event->size, event->bits, event->frac, event->source); + } +#endif + + STAILQ_INSERT_TAIL(&harvestfifo[origin].head, + event, next); + } + } + mtx_unlock_spin(&harvest_mtx); +} + Copied: head/sys/dev/random/random_harvestq.h (from r255353, projects/random_number_generator/sys/dev/random/random_harvestq.h) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/random/random_harvestq.h Sat Sep 7 14:15:13 2013 (r255362, copy of r255353, projects/random_number_generator/sys/dev/random/random_harvestq.h) @@ -0,0 +1,41 @@ +/*- + * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef __RANDOM_HARVEST_H__ +#define __RANDOM_HARVEST_H__ + +typedef void (*event_proc_f)(struct harvest *event); + +void random_harvestq_init(event_proc_f); +void random_harvestq_deinit(void); +void random_harvestq_internal(u_int64_t, const void *, + u_int, u_int, u_int, enum esource); + +extern int random_kthread_control; + +#endif /* __RANDOM_HARVEST_H__ */ Modified: head/sys/dev/random/randomdev.c ============================================================================== --- head/sys/dev/random/randomdev.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/randomdev.c Sat Sep 7 14:15:13 2013 (r255362) @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include <machine/bus.h> #include <machine/cpu.h> +#include <dev/random/random_adaptors.h> #include <dev/random/randomdev.h> #define RANDOM_MINOR 0 @@ -85,6 +86,13 @@ random_null_func(void) { } +struct random_adaptor * +random_get_active_adaptor(void) +{ + + return (random_adaptor); +} + /* ARGSUSED */ static int random_close(struct cdev *dev __unused, int flags, int fmt __unused, @@ -215,7 +223,7 @@ random_modevent(module_t mod __unused, i switch (type) { case MOD_LOAD: - random_ident_hardware(&random_adaptor); + random_adaptor_choose(&random_adaptor); if (random_adaptor == NULL) { printf( Modified: head/sys/dev/random/randomdev.h ============================================================================== --- head/sys/dev/random/randomdev.h Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/randomdev.h Sat Sep 7 14:15:13 2013 (r255362) @@ -53,3 +53,4 @@ struct random_adaptor { extern void random_ident_hardware(struct random_adaptor **); extern void random_null_func(void); +struct random_adaptor *random_get_active_adaptor(void); Modified: head/sys/dev/random/randomdev_soft.c ============================================================================== --- head/sys/dev/random/randomdev_soft.c Sat Sep 7 14:04:10 2013 (r255361) +++ head/sys/dev/random/randomdev_soft.c Sat Sep 7 14:15:13 2013 (r255362) @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2000-2009 Mark R V Murray + * Copyright (c) 2000-2013 Mark R V Murray * Copyright (c) 2004 Robert N. M. Watson * All rights reserved. * @@ -26,22 +26,24 @@ * */ +#if !defined(YARROW_RNG) && !defined(FORTUNA_RNG) +#define YARROW_RNG +#elif defined(YARROW_RNG) && defined(FORTUNA_RNG) +#error "Must define either YARROW_RNG or FORTUNA_RNG" +#endif + #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); #include <sys/param.h> #include <sys/systm.h> -#include <sys/bus.h> -#include <sys/conf.h> #include <sys/fcntl.h> #include <sys/kernel.h> -#include <sys/kthread.h> #include <sys/lock.h> #include <sys/malloc.h> #include <sys/module.h> #include <sys/mutex.h> #include <sys/poll.h> -#include <sys/proc.h> #include <sys/random.h> #include <sys/selinfo.h> #include <sys/sysctl.h> @@ -54,55 +56,51 @@ __FBSDID("$FreeBSD$"); #include <dev/random/random_adaptors.h> #include <dev/random/randomdev.h> #include <dev/random/randomdev_soft.h> +#if defined(YARROW_RNG) +#include <dev/random/yarrow.h> +#endif +#if defined(FORTUNA_RNG) +#include <dev/random/fortuna.h> +#endif + +#include "random_harvestq.h" + +static int randomdev_poll(int event, struct thread *td); +static int randomdev_block(int flag); +static void randomdev_flush_reseed(void); -#define RANDOM_FIFO_MAX 256 /* How many events to queue up */ - -static void random_kthread(void *); -static void -random_harvest_internal(u_int64_t, const void *, u_int, - u_int, u_int, enum esource); -static int random_yarrow_poll(int event,struct thread *td); -static int random_yarrow_block(int flag); -static void random_yarrow_flush_reseed(void); - -struct random_adaptor random_yarrow = { +#if defined(YARROW_RNG) +static struct random_adaptor random_context = { .ident = "Software, Yarrow", - .init = random_yarrow_init, - .deinit = random_yarrow_deinit, - .block = random_yarrow_block, + .init = randomdev_init, + .deinit = randomdev_deinit, + .block = randomdev_block, .read = random_yarrow_read, - .write = random_yarrow_write, - .poll = random_yarrow_poll, - .reseed = random_yarrow_flush_reseed, - .seeded = 1, + .write = randomdev_write, + .poll = randomdev_poll, + .reseed = randomdev_flush_reseed, + .seeded = 0, }; - -MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); - -/* - * The harvest mutex protects the consistency of the entropy fifos and - * empty fifo. - */ -struct mtx harvest_mtx; - -/* Lockable FIFO queue holding entropy buffers */ -struct entropyfifo { - int count; - STAILQ_HEAD(harvestlist, harvest) head; +#define RANDOM_MODULE_NAME yarrow +#define RANDOM_CSPRNG_NAME "yarrow" +#endif + +#if defined(FORTUNA_RNG) +static struct random_adaptor random_context = { + .ident = "Software, Fortuna", + .init = randomdev_init, + .deinit = randomdev_deinit, + .block = randomdev_block, + .read = random_fortuna_read, + .write = randomdev_write, + .poll = randomdev_poll, + .reseed = randomdev_flush_reseed, + .seeded = 0, }; +#define RANDOM_MODULE_NAME fortuna +#define RANDOM_CSPRNG_NAME "fortuna" -/* Empty entropy buffers */ -static struct entropyfifo emptyfifo; - -#define EMPTYBUFFERS 1024 - -/* Harvested entropy */ -static struct entropyfifo harvestfifo[ENTROPYSOURCE]; - -/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */ -static int random_kthread_control = 0; - -static struct proc *random_kthread_proc; *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201309071415.r87EFDMv025499>