From f02e47dc1e9d7e175ed72ec38c6c14f6375eb3c5 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Fri, 4 Oct 2013 06:55:06 +0000 Subject: [PATCH 01/20] Snapshot. This passes the build test, but has not yet been finished or debugged. Contains: * Refactor the hardware RNG CPU instruction sources to feed into the software mixer. This is unfinished. The actual harvesting needs to be sorted out. Modified by me (see below). * Remove 'frac' parameter from random_harvest(). This was never used and adds extra code for no good reason. * Remove device write entropy harvesting. This provided a weak attack vector, was not very good at bootstrapping the device. To follow will be a replacement explicit reseed knob. * Separate out all the RANDOM_PURE sources into separate harvest entities. This adds some secuity in the case where more than one is present. * Review all the code and fix anything obviously messy or inconsistent. Address som review concerns while I'm here, like rename the pseudo-rng to 'dummy'. Submitted by: Arthur Mesh (the first item) --- .../random_adaptor/random_adaptor_example.c | 41 ++-- sys/conf/files | 7 +- sys/conf/files.amd64 | 2 - sys/conf/files.i386 | 2 - sys/dev/glxsb/glxsb.c | 2 +- sys/dev/hifn/hifn7751.c | 2 +- sys/dev/random/{pseudo_rng.c => dummy_rng.c} | 97 ++++----- sys/dev/random/harvest.c | 12 +- sys/dev/random/ivy.c | 86 ++++---- sys/dev/random/live_entropy_sources.c | 185 ++++++++++++++++++ sys/dev/random/live_entropy_sources.h | 58 ++++++ sys/dev/random/nehemiah.c | 180 +++++------------ sys/dev/random/random_adaptors.c | 91 +++------ sys/dev/random/random_harvestq.c | 36 ++-- sys/dev/random/random_harvestq.h | 2 +- sys/dev/random/randomdev.c | 78 +++----- sys/dev/random/randomdev.h | 12 +- sys/dev/random/randomdev_soft.c | 49 ++--- sys/dev/random/randomdev_soft.h | 12 +- sys/dev/random/yarrow.c | 19 +- sys/dev/rndtest/rndtest.c | 2 +- sys/dev/safe/safe.c | 2 +- sys/dev/syscons/scmouse.c | 2 +- sys/dev/syscons/syscons.c | 2 +- sys/dev/ubsec/ubsec.c | 2 +- sys/kern/kern_intr.c | 6 +- sys/mips/cavium/octeon_rnd.c | 7 +- sys/net/if_ethersubr.c | 2 +- sys/net/if_tun.c | 2 +- sys/netgraph/ng_iface.c | 2 +- sys/sys/random.h | 9 +- 31 files changed, 545 insertions(+), 466 deletions(-) rename sys/dev/random/{pseudo_rng.c => dummy_rng.c} (56%) create mode 100644 sys/dev/random/live_entropy_sources.c create mode 100644 sys/dev/random/live_entropy_sources.h diff --git a/share/examples/kld/random_adaptor/random_adaptor_example.c b/share/examples/kld/random_adaptor/random_adaptor_example.c index c0ab10a83fc..da588a87fe5 100644 --- a/share/examples/kld/random_adaptor/random_adaptor_example.c +++ b/share/examples/kld/random_adaptor/random_adaptor_example.c @@ -30,32 +30,29 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include -#include +#include #include +#include #include #include -#define RNG_NAME "example" - static int random_example_read(void *, int); struct random_adaptor random_example = { .ident = "Example RNG", - .init = (random_init_func_t *)random_null_func, - .deinit = (random_deinit_func_t *)random_null_func, + .source = RANDOM_PURE_BOGUS, /* Make sure this is in + * sys/random.h and is unique */ .read = random_example_read, - .write = (random_write_func_t *)random_null_func, - .reseed = (random_reseed_func_t *)random_null_func, - .seeded = 1, }; /* * Used under the license provided @ http://xkcd.com/221/ * http://creativecommons.org/licenses/by-nc/2.5/ */ -static u_char +static uint8_t getRandomNumber(void) { return 4; /* chosen by fair dice roll, guaranteed to be random */ @@ -64,14 +61,13 @@ getRandomNumber(void) static int random_example_read(void *buf, int c) { - u_char *b; + uint8_t *b; int count; b = buf; - for (count = 0; count < c; count++) { + for (count = 0; count < c; count++) b[count] = getRandomNumber(); - } printf("returning %d bytes of pure randomness\n", c); return (c); @@ -80,15 +76,26 @@ random_example_read(void *buf, int c) static int random_example_modevent(module_t mod, int type, void *unused) { + int error = 0; switch (type) { case MOD_LOAD: - random_adaptor_register(RNG_NAME, &random_example); - EVENTHANDLER_INVOKE(random_adaptor_attach, &random_example); - return (0); + live_entropy_source_register(&random_example); + break; + + case MOD_UNLOAD: + live_entropy_source_deregister(&random_example); + break; + + case MOD_SHUTDOWN: + break; + + default: + error = EOPNOTSUPP; + break; } - return (EINVAL); + return (error); } -RANDOM_ADAPTOR_MODULE(random_example, random_example_modevent, 1); +LIVE_ENTROPY_SRC_MODULE(live_entropy_source_example, random_example_modevent, 1); diff --git a/sys/conf/files b/sys/conf/files index 7ba7e7c0e17..6e2a358b39a 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -2043,13 +2043,14 @@ rt2860.fw optional rt2860fw | ralfw \ no-obj no-implicit-rule \ clean "rt2860.fw" dev/random/harvest.c standard -dev/random/hash.c optional random -dev/random/pseudo_rng.c standard +dev/random/dummy_rng.c standard dev/random/random_adaptors.c standard -dev/random/random_harvestq.c standard +dev/random/live_entropy_sources.c optional random +dev/random/random_harvestq.c optional random dev/random/randomdev.c optional random dev/random/randomdev_soft.c optional random dev/random/yarrow.c optional random +dev/random/hash.c optional random dev/rc/rc.c optional rc dev/re/if_re.c optional re dev/rndtest/rndtest.c optional rndtest diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64 index 9ee6d77d4ba..7ac5d720917 100644 --- a/sys/conf/files.amd64 +++ b/sys/conf/files.amd64 @@ -259,8 +259,6 @@ dev/nvme/nvme_sysctl.c optional nvme dev/nvme/nvme_test.c optional nvme dev/nvme/nvme_util.c optional nvme dev/nvram/nvram.c optional nvram isa -dev/random/ivy.c optional random rdrand_rng -dev/random/nehemiah.c optional random padlock_rng dev/qlxge/qls_dbg.c optional qlxge pci dev/qlxge/qls_dump.c optional qlxge pci dev/qlxge/qls_hw.c optional qlxge pci diff --git a/sys/conf/files.i386 b/sys/conf/files.i386 index b29d8f9213c..55017dcaa47 100644 --- a/sys/conf/files.i386 +++ b/sys/conf/files.i386 @@ -257,8 +257,6 @@ dev/nvme/nvme_test.c optional nvme dev/nvme/nvme_util.c optional nvme dev/nvram/nvram.c optional nvram isa dev/pcf/pcf_isa.c optional pcf -dev/random/ivy.c optional random rdrand_rng -dev/random/nehemiah.c optional random padlock_rng dev/sbni/if_sbni.c optional sbni dev/sbni/if_sbni_isa.c optional sbni isa dev/sbni/if_sbni_pci.c optional sbni pci diff --git a/sys/dev/glxsb/glxsb.c b/sys/dev/glxsb/glxsb.c index 52041534da3..646fe3fe7ea 100644 --- a/sys/dev/glxsb/glxsb.c +++ b/sys/dev/glxsb/glxsb.c @@ -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/2, 0, RANDOM_PURE); + random_harvest(&value, 4, 32/2, RANDOM_PURE_GLXSB); } callout_reset(&sc->sc_rngco, sc->sc_rnghz, glxsb_rnd, sc); diff --git a/sys/dev/hifn/hifn7751.c b/sys/dev/hifn/hifn7751.c index ae6c5ac6353..8954242619d 100644 --- a/sys/dev/hifn/hifn7751.c +++ b/sys/dev/hifn/hifn7751.c @@ -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/2, 0, RANDOM_PURE); + random_harvest(buf, count, count*NBBY/2, RANDOM_PURE_HIFN); } static u_int diff --git a/sys/dev/random/pseudo_rng.c b/sys/dev/random/dummy_rng.c similarity index 56% rename from sys/dev/random/pseudo_rng.c rename to sys/dev/random/dummy_rng.c index 13552942f79..eddb7292cf8 100644 --- a/sys/dev/random/pseudo_rng.c +++ b/sys/dev/random/dummy_rng.c @@ -28,87 +28,90 @@ __FBSDID("$FreeBSD$"); #include -#include +#include #include #include +#include #include #include +#include #include #include -static struct mtx pseudo_random_block_mtx; +static struct mtx dummy_random_mtx; + +/* Used to fake out unused random calls in random_adaptor */ +static void +random_null_func(void) +{ +} static int -pseudo_random_block_read(void *buf __unused, int c __unused) +dummy_random_poll(int events __unused, struct thread *td __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 int +dummy_random_block(int flag) +{ + int error = 0; + + mtx_lock(&dummy_random_mtx); + + /* Blocking logic */ + while (!error) { + if (flag & O_NONBLOCK) + error = EWOULDBLOCK; + else { + printf("random: dummy device blocking on read.\n"); + error = msleep(&dummy_random_block, + &dummy_random_mtx, + PUSER | PCATCH, "block", 0); + } + } + mtx_unlock(&dummy_random_mtx); + + return (error); +} + static void -pseudo_random_block_init(void) +dummy_random_init(void) { - mtx_init(&pseudo_random_block_mtx, "sleep mtx for random_block", + mtx_init(&dummy_random_mtx, "sleep mtx for dummy_random", NULL, MTX_DEF); } static void -pseudo_random_block_deinit(void) +dummy_random_deinit(void) { - mtx_destroy(&pseudo_random_block_mtx); + mtx_destroy(&dummy_random_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, +struct random_adaptor dummy_random = { + .ident = "Dummy entropy device that always blocks", + .init = dummy_random_init, + .deinit = dummy_random_deinit, + .block = dummy_random_block, + .poll = dummy_random_poll, + .read = (random_read_func_t *)random_null_func, .reseed = (random_reseed_func_t *)random_null_func, - .seeded = 1, + .seeded = 0, /* This device can never be seeded */ }; 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) +dummy_random_modevent(module_t mod __unused, int type, void *unused __unused) { switch (type) { case MOD_LOAD: - random_adaptor_register("block", &pseudo_random_block); + random_adaptor_register("dummy", &dummy_random); EVENTHANDLER_INVOKE(random_adaptor_attach, - &pseudo_random_block); - - random_adaptor_register("panic", &pseudo_random_panic); + &dummy_random); return (0); } @@ -116,4 +119,4 @@ pseudo_random_modevent(module_t mod, int type, void *unused) return (EINVAL); } -RANDOM_ADAPTOR_MODULE(pseudo, pseudo_random_modevent, 1); +RANDOM_ADAPTOR_MODULE(dummy, dummy_random_modevent, 1); diff --git a/sys/dev/random/harvest.c b/sys/dev/random/harvest.c index 473e429317a..9c10e8dca33 100644 --- a/sys/dev/random/harvest.c +++ b/sys/dev/random/harvest.c @@ -48,20 +48,20 @@ __FBSDID("$FreeBSD$"); static int read_random_phony(void *, int); /* Structure holding the desired entropy sources */ -struct harvest_select harvest = { 1, 1, 1, 0 }; +struct harvest_select harvest = { 1, 1, 1, 1 }; static int warned = 0; /* hold the address of the routine which is actually called if * the randomdev is loaded */ -static void (*reap_func)(u_int64_t, const void *, u_int, u_int, u_int, +static void (*reap_func)(u_int64_t, const void *, u_int, u_int, enum esource) = NULL; static int (*read_func)(void *, int) = read_random_phony; /* Initialise the harvester at load time */ void randomdev_init_harvester(void (*reaper)(u_int64_t, const void *, u_int, - u_int, u_int, enum esource), int (*reader)(void *, int)) + u_int, enum esource), int (*reader)(void *, int)) { reap_func = reaper; read_func = reader; @@ -86,12 +86,10 @@ randomdev_deinit_harvester(void) * read which can be quite expensive. */ void -random_harvest(void *entropy, u_int count, u_int bits, u_int frac, - enum esource origin) +random_harvest(void *entropy, u_int count, u_int bits, enum esource origin) { if (reap_func) - (*reap_func)(get_cyclecount(), entropy, count, bits, frac, - origin); + (*reap_func)(get_cyclecount(), entropy, count, bits, origin); } /* Userland-visible version of read_random */ diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index 07c87123acc..c8982129cc4 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -30,50 +30,44 @@ __FBSDID("$FreeBSD$"); #include -#include #include #include #include -#include +#include #include #include #include #include +#include #include #include #define RETRY_COUNT 10 -static void random_ivy_init(void); -static void random_ivy_deinit(void); static int random_ivy_read(void *, int); -struct random_adaptor random_ivy = { +struct random_hardware_source random_ivy = { .ident = "Hardware, Intel IvyBridge+ RNG", - .init = random_ivy_init, - .deinit = random_ivy_deinit, - .read = random_ivy_read, - .write = (random_write_func_t *)random_null_func, - .reseed = (random_reseed_func_t *)random_null_func, - .seeded = 1, + .source = RANDOM_PURE_RDRAND, + .read = random_ivy_read }; static inline int -ivy_rng_store(long *tmp) +ivy_rng_store(uint64_t *tmp) { #ifdef __GNUCLIKE_ASM uint32_t count; __asm __volatile( #ifdef __amd64__ - ".byte\t0x48,0x0f,0xc7,0xf0\n\t" /* rdrand %rax */ + "rdrand\t%%rax\n\t" "jnc\t1f\n\t" "movq\t%%rax,%1\n\t" "movl\t$8,%%eax\n" #else /* i386 */ - ".byte\t0x0f,0xc7,0xf0\n\t" /* rdrand %eax */ + "rdrand\t%%eax\n\t" "jnc\t1f\n\t" "movl\t%%eax,%1\n\t" "movl\t$4,%%eax\n" @@ -86,34 +80,26 @@ ivy_rng_store(long *tmp) #endif } -static void -random_ivy_init(void) -{ -} - -void -random_ivy_deinit(void) -{ -} - static int random_ivy_read(void *buf, int c) { - char *b; - long tmp; - int count, res, retry; + uint8_t *b; + int count, ret, retry; + uint64_t tmp; - for (count = c, b = buf; count > 0; count -= res, b += res) { + b = buf; + for (count = c; count > 0; count -= ret) { for (retry = 0; retry < RETRY_COUNT; retry++) { - res = ivy_rng_store(&tmp); - if (res != 0) + ret = ivy_rng_store(&tmp); + if (ret != 0) break; } - if (res == 0) + if (ret == 0) break; - if (res > count) - res = count; - memcpy(b, &tmp, res); + if (ret > count) + ret = count; + memcpy(b, &tmp, ret); + b += ret; } return (c - count); } @@ -121,25 +107,35 @@ random_ivy_read(void *buf, int c) static int rdrand_modevent(module_t mod, int type, void *unused) { + int error = 0; switch (type) { case MOD_LOAD: - if (cpu_feature2 & CPUID2_RDRAND) { - random_adaptor_register("rdrand", &random_ivy); - EVENTHANDLER_INVOKE(random_adaptor_attach, &random_ivy); - return (0); - } else { + if (cpu_feature2 & CPUID2_RDRAND) + live_entropy_source_register(&random_ivy); + else #ifndef KLD_MODULE if (bootverbose) #endif - printf( - "%s: RDRAND feature is not present on this CPU\n", + printf("%s: RDRAND is not present\n", random_ivy.ident); - return (0); - } + break; + + case MOD_UNLOAD: + if (cpu_feature2 & CPUID2_RDRAND) + live_entropy_source_deregister(&random_ivy); + break; + + case MOD_SHUTDOWN: + break; + + default: + error = EOPNOTSUPP; + break; + } - return (EINVAL); + return (error); } -RANDOM_ADAPTOR_MODULE(random_rdrand, rdrand_modevent, 1); +LIVE_ENTROPY_SRC_MODULE(random_rdrand, rdrand_modevent, 1); diff --git a/sys/dev/random/live_entropy_sources.c b/sys/dev/random/live_entropy_sources.c new file mode 100644 index 00000000000..d8eeb67d93a --- /dev/null +++ b/sys/dev/random/live_entropy_sources.c @@ -0,0 +1,185 @@ +/*- + * Copyright (c) 2013 Arthur Mesh + * Copyright (c) 2013 Mark R V Murray + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "live_entropy_sources.h" + +LIST_HEAD(les_head, live_entropy_sources); +static struct les_head sources = LIST_HEAD_INITIALIZER(sources); +static struct sx les_lock; /* need a sleepable lock */ + +#define LES_THRESHOLD 10 + +MALLOC_DEFINE(M_LIVE_ENTROPY_SRCS, "live_entropy_sources", + "Live Entropy Sources"); + +void +live_entropy_source_register(struct random_hardware_source *rsource) +{ + struct live_entropy_sources *les; + + KASSERT(rsource != NULL, ("invalid input to %s", __func__)); + + les = malloc(sizeof(struct live_entropy_sources), M_LIVE_ENTROPY_SRCS, + M_WAITOK); + les->rsource = rsource; + + sx_xlock(&les_lock); + LIST_INSERT_HEAD(&sources, les, entries); + sx_xunlock(&les_lock); +} + +void +live_entropy_source_deregister(struct random_hardware_source *rsource) +{ + struct live_entropy_sources *les; + + KASSERT(rsource != NULL, ("invalid input to %s", __func__)); + + sx_xlock(&les_lock); + LIST_FOREACH(les, &sources, entries) { + if (les->rsource == rsource) { + LIST_REMOVE(les, entries); + free(les, M_LIVE_ENTROPY_SRCS); + break; + } + } + sx_xunlock(&les_lock); +} + +static int +live_entropy_source_handler(SYSCTL_HANDLER_ARGS) +{ + struct live_entropy_sources *les; + int error, count; + + count = error = 0; + + sx_slock(&les_lock); + + if (LIST_EMPTY(&sources)) + error = SYSCTL_OUT(req, "", 0); + else { + LIST_FOREACH(les, &sources, entries) { + + error = SYSCTL_OUT(req, ",", count++ ? 1 : 0); + if (error) + break; + + error = SYSCTL_OUT(req, les->rsource->ident, strlen(les->rsource->ident)); + if (error) + break; + } + } + + sx_sunlock(&les_lock); + + return (error); +} + +static void +live_entropy_sources_init(void *unused) +{ + + SYSCTL_PROC(_kern_random, OID_AUTO, live_entropy_sources, + CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, + NULL, 0, live_entropy_source_handler, "", + "List of Active Live Entropy Sources"); + + sx_init(&les_lock, "live_entropy_sources"); +} + +/* + * Run through all "live" sources reading entropy for the given + * number of rounds, which should be a multiple of the number + * of entropy accumulation pools in use; 2 for Yarrow and 32 + * for Fortuna. + */ +void +live_entropy_sources_feed(int rounds) +{ + struct live_entropy_sources *les; + uint8_t buf[HARVESTSIZE]; + int i, n; + + sx_slock(&les_lock); + + /* + * Walk over all of live entropy sources, and feed their output + * to the system-wide RNG. + */ + LIST_FOREACH(les, &sources, entries) { + + for (i = 0; i < rounds; i++) { + /* + * This should be quick, since it's a live entropy + * source. + */ + n = les->rsource->read(buf, sizeof(buf)); + /* FIXME: Whine loudly if this didn't work. */ + + /* + * FIXME: Cannot harvest this stuff into the queue; + * the poor thing will choke to death! + */ + random_harvest(buf, n, 0, les->rsource->source); + } + + } + + sx_sunlock(&les_lock); +} + +static void +live_entropy_sources_deinit(void *unused) +{ + + sx_destroy(&les_lock); +} + +SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, + live_entropy_sources_init, NULL); +SYSUNINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, + live_entropy_sources_deinit, NULL); diff --git a/sys/dev/random/live_entropy_sources.h b/sys/dev/random/live_entropy_sources.h new file mode 100644 index 00000000000..807972f2ddc --- /dev/null +++ b/sys/dev/random/live_entropy_sources.h @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2013 Arthur Mesh + * Copyright (c) 2013 Mark R V Murray + * 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 __LIVE_ENTROPY_SOURCES__ +#define __LIVE_ENTROPY_SOURCES__ + +/* + * Live entropy source is a source of entropy that can provide + * specified or approximate amount of entropy immediately upon request or within + * an acceptable amount of time. + */ +struct live_entropy_sources { + LIST_ENTRY(live_entropy_sources) entries; /* list of providers */ + struct random_hardware_source *rsource; /* associated random adaptor */ +}; + +void live_entropy_source_register(struct random_hardware_source *); +void live_entropy_source_deregister(struct random_hardware_source *); +void live_entropy_sources_feed(int); + +#define LIVE_ENTROPY_SRC_MODULE(name, modevent, ver) \ + static moduledata_t name##_mod = { \ + #name, \ + modevent, \ + 0 \ + }; \ + DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, \ + SI_ORDER_SECOND); \ + MODULE_VERSION(name, ver); \ + MODULE_DEPEND(name, random, 1, 1, 1); + +#endif /* __LIVE_ENTROPY_SOURCES__ */ diff --git a/sys/dev/random/nehemiah.c b/sys/dev/random/nehemiah.c index 1b4416eb19c..8f1ad9eeebc 100644 --- a/sys/dev/random/nehemiah.c +++ b/sys/dev/random/nehemiah.c @@ -1,6 +1,5 @@ /*- - * Copyright (c) 2013 David E. O'Brien - * Copyright (c) 2004 Mark R V Murray + * Copyright (c) 2013 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,209 +29,126 @@ __FBSDID("$FreeBSD$"); #include -#include +#include #include -#include #include +#include #include #include -#include #include #include #include +#include #include #include -#define RANDOM_BLOCK_SIZE 256 -#define CIPHER_BLOCK_SIZE 16 - static void random_nehemiah_init(void); static void random_nehemiah_deinit(void); static int random_nehemiah_read(void *, int); -struct random_adaptor random_nehemiah = { - .ident = "Hardware, VIA Nehemiah", - .init = random_nehemiah_init, - .deinit = random_nehemiah_deinit, - .read = random_nehemiah_read, - .write = (random_write_func_t *)random_null_func, - .reseed = (random_reseed_func_t *)random_null_func, - .seeded = 1, +struct random_hardware_source random_nehemiah = { + .ident = "Hardware, VIA Nehemiah Padlock RNG", + .source = RANDOM_PURE_NEHEMIAH, + .read = random_nehemiah_read }; -union VIA_ACE_CW { - uint64_t raw; - struct { - u_int round_count : 4; - u_int algorithm_type : 3; - u_int key_generation_type : 1; - u_int intermediate : 1; - u_int decrypt : 1; - u_int key_size : 2; - u_int filler0 : 20; - u_int filler1 : 32; - u_int filler2 : 32; - u_int filler3 : 32; - } field; -}; - -/* The extra 7 is to allow an 8-byte write on the last byte of the - * arrays. The ACE wants the AES data 16-byte/128-bit aligned, and - * it _always_ writes n*64 bits. The RNG does not care about alignment, - * and it always writes n*32 bits or n*64 bits. - */ -static uint8_t key[CIPHER_BLOCK_SIZE+7] __aligned(16); -static uint8_t iv[CIPHER_BLOCK_SIZE+7] __aligned(16); -static uint8_t in[RANDOM_BLOCK_SIZE+7] __aligned(16); -static uint8_t out[RANDOM_BLOCK_SIZE+7] __aligned(16); - -static union VIA_ACE_CW acw __aligned(16); +/* This H/W RNG never stores more than 8 bytes in one go */ static struct fpu_kern_ctx *fpu_ctx_save; -static struct mtx random_nehemiah_mtx; - /* ARGSUSED */ static __inline size_t VIA_RNG_store(void *buf) { -#ifdef __GNUCLIKE_ASM uint32_t retval = 0; uint32_t rate = 0; - /* The .byte line is really VIA C3 "xstore" instruction */ +#ifdef __GNUCLIKE_ASM __asm __volatile( - "movl $0,%%edx \n\t" - ".byte 0x0f, 0xa7, 0xc0" + "movl $0,%%edx\n\t" + "xstore" : "=a" (retval), "+d" (rate), "+D" (buf) : : "memory" ); +#endif if (rate == 0) return (retval&0x1f); -#endif return (0); } -/* ARGSUSED */ -static __inline void -VIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv) -{ -#ifdef __GNUCLIKE_ASM - /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */ - __asm __volatile( - "pushf \n\t" - "popf \n\t" - "rep \n\t" - ".byte 0x0f, 0xa7, 0xc8" - : "+a" (iv), "+c" (count), "+D" (out), "+S" (in) - : "b" (key), "d" (cw) - : "cc", "memory" - ); -#endif -} - static void random_nehemiah_init(void) { - acw.raw = 0ULL; - acw.field.round_count = 12; - mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF); fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL); } -void +static void random_nehemiah_deinit(void) { fpu_kern_free_ctx(fpu_ctx_save); - mtx_destroy(&random_nehemiah_mtx); } static int random_nehemiah_read(void *buf, int c) { - int i, error; + uint8_t *b; size_t count, ret; - uint8_t *p; + uint64_t tmp; - mtx_lock(&random_nehemiah_mtx); - error = fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL); - if (error != 0) { - mtx_unlock(&random_nehemiah_mtx); - return (0); + if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) { + b = buf; + for (count = c; count > 0; count -= ret) { + ret = MIN(VIA_RNG_store(&tmp), count); + memcpy(b, &tmp, ret); + b += ret; + } + fpu_kern_leave(curthread, fpu_ctx_save); } + else + c = 0; - /* Get a random AES key */ - count = 0; - p = key; - do { - ret = VIA_RNG_store(p); - p += ret; - count += ret; - } while (count < CIPHER_BLOCK_SIZE); - - /* Get a random AES IV */ - count = 0; - p = iv; - do { - ret = VIA_RNG_store(p); - p += ret; - count += ret; - } while (count < CIPHER_BLOCK_SIZE); - - /* Get a block of random bytes */ - count = 0; - p = in; - do { - ret = VIA_RNG_store(p); - p += ret; - count += ret; - } while (count < RANDOM_BLOCK_SIZE); - - /* This is a Davies-Meyer hash of the most paranoid variety; the - * key, IV and the data are all read directly from the hardware RNG. - * All of these are used precisely once. - */ - VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE, - key, &acw, iv); - for (i = 0; i < RANDOM_BLOCK_SIZE; i++) - out[i] ^= in[i]; - - c = MIN(RANDOM_BLOCK_SIZE, c); - memcpy(buf, out, (size_t)c); - - fpu_kern_leave(curthread, fpu_ctx_save); - mtx_unlock(&random_nehemiah_mtx); return (c); } static int nehemiah_modevent(module_t mod, int type, void *unused) { + int error = 0; switch (type) { case MOD_LOAD: if (via_feature_rng & VIA_HAS_RNG) { - random_adaptor_register("nehemiah", &random_nehemiah); - EVENTHANDLER_INVOKE(random_adaptor_attach, - &random_nehemiah); - return (0); - } else { + live_entropy_source_register(&random_nehemiah); + random_nehemiah_init(); + } else #ifndef KLD_MODULE if (bootverbose) #endif - printf( - "%s: VIA RNG feature is not present on this CPU\n", + printf("%s: VIA Padlock RNG not present\n", random_nehemiah.ident); - return (0); - } + break; + + case MOD_UNLOAD: + if (via_feature_rng & VIA_HAS_RNG) + random_nehemiah_deinit(); + live_entropy_source_deregister(&random_nehemiah); + break; + + case MOD_SHUTDOWN: + break; + + default: + error = EOPNOTSUPP; + break; + } - return (EINVAL); + return (error); } -RANDOM_ADAPTOR_MODULE(nehemiah, nehemiah_modevent, 1); +LIVE_ENTROPY_SRC_MODULE(nehemiah, nehemiah_modevent, 1); diff --git a/sys/dev/random/random_adaptors.c b/sys/dev/random/random_adaptors.c index 43f55f22fe4..5ea77c27181 100644 --- a/sys/dev/random/random_adaptors.c +++ b/sys/dev/random/random_adaptors.c @@ -1,7 +1,7 @@ /*- * Copyright (c) 2013 Arthur Mesh * Copyright (c) 2013 David E. O'Brien - * Copyright (c) 2004 Mark R V Murray + * Copyright (c) 2013 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,17 +29,17 @@ #include __FBSDID("$FreeBSD$"); -#include #include +#include #include +#include #include -#include -#include -#include -#include #include #include -#include +#include +#include +#include +#include #include #include @@ -95,30 +95,6 @@ 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. @@ -134,47 +110,29 @@ random_adaptor_choose(struct random_adaptor **adaptor) 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) { + 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); - } + 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. + * Fallback to the first thing that's on the list of + * available RNGs. */ - *adaptor = random_adaptor_get("yarrow"); + sx_slock(&adaptors_lock); - 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; - rpp = LIST_FIRST(&adaptors); - if (rpp != NULL) - *adaptor = rpp->rsp; - - sx_sunlock(&adaptors_lock); - } + sx_sunlock(&adaptors_lock); if (bootverbose && *adaptor) printf("Falling back to <%s> random adaptor\n", @@ -200,19 +158,16 @@ random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS) sx_slock(&adaptors_lock); - if (LIST_EMPTY(&adaptors)) { + if (LIST_EMPTY(&adaptors)) error = SYSCTL_OUT(req, "", 0); - } else { - + else { 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; } @@ -237,19 +192,17 @@ random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS) if (rsp != NULL) { sx_slock(&adaptors_lock); - LIST_FOREACH(rpp, &adaptors, entries) { + LIST_FOREACH(rpp, &adaptors, entries) if (rpp->rsp == rsp) name = rpp->name; - } sx_sunlock(&adaptors_lock); } - if (rsp == NULL || name == NULL) { + if (rsp == NULL || name == NULL) error = SYSCTL_OUT(req, "", 0); - } else { + else error = SYSCTL_OUT(req, name, strlen(name)); - } return (error); } diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index fc87cde0068..4f8f34157a1 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -1,6 +1,6 @@ /*- + * Copyright (c) 2000-2013 Mark R V Murray * Copyright (c) 2013 Arthur Mesh - * Copyright (c) 2000-2009 Mark R V Murray * Copyright (c) 2004 Robert N. M. Watson * All rights reserved. * @@ -46,7 +46,7 @@ __FBSDID("$FreeBSD$"); #include "random_harvestq.h" -#define RANDOM_FIFO_MAX 256 /* How many events to queue up */ +#define RANDOM_FIFO_MAX 1024 /* How many events to queue up */ MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); @@ -65,8 +65,6 @@ struct entropyfifo { /* Empty entropy buffers */ static struct entropyfifo emptyfifo; -#define EMPTYBUFFERS 1024 - /* Harvested entropy */ static struct entropyfifo harvestfifo[ENTROPYSOURCE]; @@ -103,9 +101,8 @@ random_kthread(void *arg) } /* - * Deal with events, if any, dropping the mutex as we process - * each event. Then push the events back into the empty - * fifo. + * Deal with events, if any. + * Then transfer the used events back into the empty fifo. */ if (!STAILQ_EMPTY(&local_queue)) { mtx_unlock_spin(&harvest_mtx); @@ -120,16 +117,29 @@ random_kthread(void *arg) KASSERT(local_count == 0, ("random_kthread: local_count %d", local_count)); + /* + * Do Hardware/fast RNG source processing here. + */ +#if 0 + while (hardware_source) { + event = hardware_source->read(); + func(event); + hardware_source++; + /* Throttle somehow? */ + } +#endif + /* * 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)); + "-", SBT_1S/10, 0, C_PREL(1)); } mtx_unlock_spin(&harvest_mtx); @@ -148,7 +158,7 @@ random_harvestq_init(event_proc_f cb) /* Initialise the harvest fifos */ STAILQ_INIT(&emptyfifo.head); emptyfifo.count = 0; - for (i = 0; i < EMPTYBUFFERS; i++) { + for (i = 0; i < RANDOM_FIFO_MAX; i++) { np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK); STAILQ_INSERT_TAIL(&emptyfifo.head, np, next); } @@ -197,11 +207,11 @@ random_harvestq_deinit(void) */ void random_harvestq_internal(u_int64_t somecounter, const void *entropy, - u_int count, u_int bits, u_int frac, enum esource origin) + u_int count, u_int bits, enum esource origin) { struct harvest *event; - KASSERT(origin >= RANDOM_START && origin <= RANDOM_PURE, + KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("random_harvest_internal: origin %d invalid\n", origin)); /* Lockless read to avoid lock operations if fifo is full. */ @@ -223,7 +233,6 @@ random_harvestq_internal(u_int64_t somecounter, const void *entropy, event->somecounter = somecounter; event->size = count; event->bits = bits; - event->frac = frac; event->source = origin; /* XXXX Come back and make this dynamic! */ @@ -238,7 +247,7 @@ random_harvestq_internal(u_int64_t somecounter, const void *entropy, 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); + printf(" %2d %2d %02X\n", event->size, event->bits, event->source); } #endif @@ -248,4 +257,3 @@ random_harvestq_internal(u_int64_t somecounter, const void *entropy, } mtx_unlock_spin(&harvest_mtx); } - diff --git a/sys/dev/random/random_harvestq.h b/sys/dev/random/random_harvestq.h index 9766c7728c4..11ccfc46e00 100644 --- a/sys/dev/random/random_harvestq.h +++ b/sys/dev/random/random_harvestq.h @@ -34,7 +34,7 @@ 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); + u_int, u_int, enum esource); extern int random_kthread_control; diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index 2d24dd56dd5..990324e14d5 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -1,6 +1,6 @@ /*- + * Copyright (c) 2000-2013 Mark R V Murray * Copyright (c) 2013 Arthur Mesh - * Copyright (c) 2000-2004 Mark R V Murray * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -52,11 +53,11 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #define RANDOM_MINOR 0 -static d_close_t random_close; static d_read_t random_read; static d_write_t random_write; static d_ioctl_t random_ioctl; @@ -64,7 +65,6 @@ static d_poll_t random_poll; static struct cdevsw random_cdevsw = { .d_version = D_VERSION, - .d_close = random_close, .d_read = random_read, .d_write = random_write, .d_ioctl = random_ioctl, @@ -72,34 +72,9 @@ static struct cdevsw random_cdevsw = { .d_name = "random", }; -static eventhandler_tag attach_tag; -static int random_inited; - /* For use with make_dev(9)/destroy_dev(9). */ static struct cdev *random_dev; -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, - struct thread *td) -{ - if ((flags & FWRITE) && (priv_check(td, PRIV_RANDOM_RESEED) == 0) - && (securelevel_gt(td->td_ucred, 0) == 0)) { - (*random_adaptor->reseed)(); - random_adaptor->seeded = 1; - arc4rand(NULL, 0, 1); /* Reseed arc4random as well. */ - } - - return (0); -} - /* ARGSUSED */ static int random_read(struct cdev *dev __unused, struct uio *uio, int flag) @@ -107,6 +82,10 @@ random_read(struct cdev *dev __unused, struct uio *uio, int flag) int c, error = 0; void *random_buf; + /* XXX: Harvest some entropy from live entropy sources, if available */ + live_entropy_sources_feed(65); /* 65 is meaningless -- + need to decide appropriate value */ + /* Blocking logic */ if (!random_adaptor->seeded) error = (*random_adaptor->block)(flag); @@ -121,6 +100,9 @@ random_read(struct cdev *dev __unused, struct uio *uio, int flag) c = (*random_adaptor->read)(random_buf, c); error = uiomove(random_buf, c, uio); } + /* Finished reading; let the source know so it can do some + * optional housekeeping */ + (*random_adaptor->read)(NULL, 0); free(random_buf, M_TEMP); @@ -133,22 +115,16 @@ random_read(struct cdev *dev __unused, struct uio *uio, int flag) static int random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused) { - int c, error = 0; - void *random_buf; - random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); + /* We used to allow this to insert userland entropy. + * We don't any more because (1) this so-called entropy + * is usually lousy and (b) its vaguely possible to + * mess with entropy harvesting by overdoing a write. + * Now we just ignore input like /dev/null does. + */ + uio->uio_resid = 0; - while (uio->uio_resid > 0) { - c = MIN((int)uio->uio_resid, PAGE_SIZE); - error = uiomove(random_buf, c, uio); - if (error) - break; - (*random_adaptor->write)(random_buf, c); - } - - free(random_buf, M_TEMP); - - return (error); + return (0); } /* ARGSUSED */ @@ -179,7 +155,7 @@ random_poll(struct cdev *dev __unused, int events, struct thread *td) if (random_adaptor->seeded) revents = events & (POLLIN | POLLRDNORM); else - revents = (*random_adaptor->poll) (events,td); + revents = (*random_adaptor->poll)(events, td); } return (revents); } @@ -187,6 +163,8 @@ random_poll(struct cdev *dev __unused, int events, struct thread *td) static void random_initialize(void *p, struct random_adaptor *s) { + static int random_inited = 0; + if (random_inited) { printf("random: <%s> already initialized\n", random_adaptor->ident); @@ -199,9 +177,10 @@ random_initialize(void *p, struct random_adaptor *s) printf("random: <%s> initialized\n", s->ident); + /* Use an appropriately evil mode for those who are concerned + * with daemons */ random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw, RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random"); - make_dev_alias(random_dev, "urandom"); /* XXX Deprecated */ /* mark random(4) as initialized, to avoid being called again */ random_inited = 1; @@ -211,6 +190,7 @@ random_initialize(void *p, struct random_adaptor *s) static int random_modevent(module_t mod __unused, int type, void *data __unused) { + static eventhandler_tag attach_tag = NULL; int error = 0; switch (type) { @@ -218,13 +198,12 @@ random_modevent(module_t mod __unused, int type, void *data __unused) random_adaptor_choose(&random_adaptor); if (random_adaptor == NULL) { - printf( - "random: No random adaptor attached, postponing initialization\n"); + printf("random: No random adaptor attached, " + "postponing initialization\n"); attach_tag = EVENTHANDLER_REGISTER(random_adaptor_attach, random_initialize, NULL, EVENTHANDLER_PRI_ANY); - } else { + } else random_initialize(NULL, random_adaptor); - } break; @@ -234,10 +213,9 @@ random_modevent(module_t mod __unused, int type, void *data __unused) destroy_dev(random_dev); } /* Unregister the event handler */ - if (attach_tag != NULL) { + if (attach_tag != NULL) EVENTHANDLER_DEREGISTER(random_adaptor_attach, attach_tag); - } break; diff --git a/sys/dev/random/randomdev.h b/sys/dev/random/randomdev.h index aeb47531ae7..a0e002f89ae 100644 --- a/sys/dev/random/randomdev.h +++ b/sys/dev/random/randomdev.h @@ -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 @@ -34,7 +34,6 @@ typedef void random_init_func_t(void); typedef void random_deinit_func_t(void); typedef int random_block_func_t(int); typedef int random_read_func_t(void *, int); -typedef void random_write_func_t(void *, int); typedef int random_poll_func_t(int, struct thread *); typedef void random_reseed_func_t(void); @@ -46,11 +45,12 @@ struct random_adaptor { random_deinit_func_t *deinit; random_block_func_t *block; random_read_func_t *read; - random_write_func_t *write; random_poll_func_t *poll; random_reseed_func_t *reseed; }; -extern void random_ident_hardware(struct random_adaptor **); -extern void random_null_func(void); -struct random_adaptor *random_get_active_adaptor(void); +struct random_hardware_source { + const char *ident; + enum esource source; + random_read_func_t *read; +}; diff --git a/sys/dev/random/randomdev_soft.c b/sys/dev/random/randomdev_soft.c index 61afb1b76a7..80f46df5871 100644 --- a/sys/dev/random/randomdev_soft.c +++ b/sys/dev/random/randomdev_soft.c @@ -76,10 +76,9 @@ static struct random_adaptor random_context = { .deinit = randomdev_deinit, .block = randomdev_block, .read = random_yarrow_read, - .write = randomdev_write, .poll = randomdev_poll, .reseed = randomdev_flush_reseed, - .seeded = 1, + .seeded = 0, /* This will be seeded during entropy processing */ }; #define RANDOM_MODULE_NAME yarrow #define RANDOM_CSPRNG_NAME "yarrow" @@ -92,14 +91,12 @@ static struct random_adaptor random_context = { .deinit = randomdev_deinit, .block = randomdev_block, .read = random_fortuna_read, - .write = randomdev_write, .poll = randomdev_poll, .reseed = randomdev_flush_reseed, - .seeded = 1, + .seeded = 0, /* This will be excplicitly seeded at startup when secured */ }; #define RANDOM_MODULE_NAME fortuna #define RANDOM_CSPRNG_NAME "fortuna" - #endif /* List for the dynamic sysctls */ @@ -111,7 +108,7 @@ random_check_boolean(SYSCTL_HANDLER_ARGS) { if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0) *(u_int *)(oidp->oid_arg1) = 1; - return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); + return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req)); } void @@ -134,7 +131,7 @@ randomdev_init(void) SYSCTL_ADD_PROC(&random_clist, SYSCTL_CHILDREN(random_sys_o), OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW, - &random_context.seeded, 1, random_check_boolean, "I", + &random_context.seeded, 0, random_check_boolean, "I", "Seeded State"); random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist, @@ -155,12 +152,12 @@ randomdev_init(void) SYSCTL_ADD_PROC(&random_clist, SYSCTL_CHILDREN(random_sys_harvest_o), OID_AUTO, "interrupt", CTLTYPE_INT | CTLFLAG_RW, - &harvest.interrupt, 0, random_check_boolean, "I", + &harvest.interrupt, 1, random_check_boolean, "I", "Harvest IRQ entropy"); SYSCTL_ADD_PROC(&random_clist, SYSCTL_CHILDREN(random_sys_harvest_o), OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW, - &harvest.swi, 0, random_check_boolean, "I", + &harvest.swi, 1, random_check_boolean, "I", "Harvest SWI entropy"); random_harvestq_init(random_process_event); @@ -192,33 +189,16 @@ randomdev_deinit(void) sysctl_ctx_free(&random_clist); } -void -randomdev_write(void *buf, int count) -{ - int i; - u_int chunk; - - /* - * Break the input up into HARVESTSIZE chunks. The writer has too - * much control here, so "estimate" the entropy as zero. - */ - for (i = 0; i < count; i += HARVESTSIZE) { - chunk = HARVESTSIZE; - if (i + chunk >= count) - chunk = (u_int)(count - i); - random_harvestq_internal(get_cyclecount(), (char *)buf + i, - chunk, 0, 0, RANDOM_WRITE); - } -} - void randomdev_unblock(void) { if (!random_context.seeded) { - random_context.seeded = 1; selwakeuppri(&random_context.rsel, PUSER); wakeup(&random_context); + printf("random: unblocking device.\n"); + random_context.seeded = 1; } + /* Do arc4random(9) a favour while we are about it. */ (void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE, ARC4_ENTR_HAVE); } @@ -227,6 +207,7 @@ static int randomdev_poll(int events, struct thread *td) { int revents = 0; + mtx_lock(&random_reseed_mtx); if (random_context.seeded) @@ -235,7 +216,7 @@ randomdev_poll(int events, struct thread *td) selrecord(td, &random_context.rsel); mtx_unlock(&random_reseed_mtx); - return revents; + return (revents); } static int @@ -250,7 +231,7 @@ randomdev_block(int flag) if (flag & O_NONBLOCK) error = EWOULDBLOCK; else { - printf("Entropy device is blocking.\n"); + printf("random: blocking on read.\n"); error = msleep(&random_context, &random_reseed_mtx, PUSER | PCATCH, "block", 0); @@ -258,7 +239,7 @@ randomdev_block(int flag) } mtx_unlock(&random_reseed_mtx); - return error; + return (error); } /* Helper routine to perform explicit reseeds */ @@ -271,15 +252,17 @@ randomdev_flush_reseed(void) pause("-", hz / 10); #if defined(YARROW_RNG) + /* This ultimately calls randomdev_unblock() */ random_yarrow_reseed(); #endif #if defined(FORTUNA_RNG) + /* This ultimately calls randomdev_unblock() */ random_fortuna_reseed(); #endif } static int -randomdev_modevent(module_t mod, int type, void *unused) +randomdev_modevent(module_t mod __unused, int type, void *unused __unused) { switch (type) { diff --git a/sys/dev/random/randomdev_soft.h b/sys/dev/random/randomdev_soft.h index c92a5a8e072..7ee9053ec3b 100644 --- a/sys/dev/random/randomdev_soft.h +++ b/sys/dev/random/randomdev_soft.h @@ -49,18 +49,16 @@ MALLOC_DECLARE(M_ENTROPY); struct harvest { uintmax_t somecounter; /* fast counter for clock jitter */ uint8_t entropy[HARVESTSIZE]; /* the harvested entropy */ - u_int size, bits, frac; /* stats about the entropy */ - enum esource source; /* stats about the entropy */ + u_int size, bits; /* stats about the entropy */ + enum esource source; /* origin of the entropy */ STAILQ_ENTRY(harvest) next; /* next item on the list */ }; void randomdev_init(void); void randomdev_deinit(void); -void randomdev_write(void *, int); - void randomdev_init_harvester(void (*)(u_int64_t, const void *, u_int, - u_int, u_int, enum esource), int (*)(void *, int)); + u_int, enum esource), int (*)(void *, int)); void randomdev_deinit_harvester(void); void random_set_wakeup_exit(void *); @@ -80,6 +78,6 @@ random_check_uint_##name(SYSCTL_HANDLER_ARGS) \ else if (*(u_int *)(oidp->oid_arg1) > (max)) \ *(u_int *)(oidp->oid_arg1) = (max); \ } \ - return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, \ - req); \ + return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, \ + req)); \ } diff --git a/sys/dev/random/yarrow.c b/sys/dev/random/yarrow.c index 21913f924f6..635e126da6f 100644 --- a/sys/dev/random/yarrow.c +++ b/sys/dev/random/yarrow.c @@ -67,8 +67,6 @@ static struct random_state { struct pool { struct source { u_int bits; /* estimated bits of entropy */ - u_int frac; /* fractional bits of entropy - (given as 1024/n) */ } source[ENTROPYSOURCE]; u_int thresh; /* pool reseed threshhold */ struct randomdev_hash hash; /* accumulated entropy */ @@ -122,9 +120,7 @@ random_process_event(struct harvest *event) sizeof(event->entropy)); randomdev_hash_iterate(&random_state.pool[pl].hash, &event->somecounter, sizeof(event->somecounter)); - source->frac += event->frac; - source->bits += event->bits + (source->frac >> 12); /* bits + frac/0x1000 */ - source->frac &= 0xFFF; /* Keep the fractional bits */ + source->bits += event->bits; /* Count the over-threshold sources in each pool */ for (pl = 0; pl < 2; pl++) { @@ -286,12 +282,9 @@ reseed(u_int fastslow) /* 5. Reset entropy estimate accumulators to zero */ - for (i = 0; i <= fastslow; i++) { - for (j = RANDOM_START; j < ENTROPYSOURCE; j++) { + for (i = 0; i <= fastslow; i++) + for (j = RANDOM_START; j < ENTROPYSOURCE; j++) random_state.pool[i].source[j].bits = 0; - random_state.pool[i].source[j].frac = 0; - } - } /* 6. Wipe memory of intermediate values */ @@ -320,6 +313,10 @@ random_yarrow_read(void *buf, int count) int i; int retval; + /* Check for final read request */ + if (buf == NULL && count == 0) + return (0); + /* The reseed task must not be jumped on */ mtx_lock(&random_reseed_mtx); @@ -362,7 +359,7 @@ random_yarrow_read(void *buf, int count) } } mtx_unlock(&random_reseed_mtx); - return retval; + return (retval); } static void diff --git a/sys/dev/rndtest/rndtest.c b/sys/dev/rndtest/rndtest.c index 3c42972c36f..e4fd3b0b2cb 100644 --- a/sys/dev/rndtest/rndtest.c +++ b/sys/dev/rndtest/rndtest.c @@ -152,7 +152,7 @@ rndtest_harvest(struct rndtest_state *rsp, void *buf, u_int len) for (len /= sizeof (u_int32_t); len; len--) add_true_randomness(*p++); #else - random_harvest(buf, len, len*NBBY/2, 0, RANDOM_PURE); + random_harvest(buf, len, len*NBBY/2, RANDOM_PURE_RNDTEST); #endif } } diff --git a/sys/dev/safe/safe.c b/sys/dev/safe/safe.c index 721b9f4b2b6..a3590efbeb0 100644 --- a/sys/dev/safe/safe.c +++ b/sys/dev/safe/safe.c @@ -211,7 +211,7 @@ safe_partname(struct safe_softc *sc) static void default_harvest(struct rndtest_state *rsp, void *buf, u_int count) { - random_harvest(buf, count, count*NBBY/2, 0, RANDOM_PURE); + random_harvest(buf, count, count*NBBY/2, RANDOM_PURE_SAFE); } #endif /* SAFE_NO_RNG */ diff --git a/sys/dev/syscons/scmouse.c b/sys/dev/syscons/scmouse.c index 21e6d0830d6..4dbd1a77873 100644 --- a/sys/dev/syscons/scmouse.c +++ b/sys/dev/syscons/scmouse.c @@ -666,7 +666,7 @@ sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) mouse = (mouse_info_t*)data; - random_harvest(mouse, sizeof(mouse_info_t), 2, 0, RANDOM_MOUSE); + random_harvest(mouse, sizeof(mouse_info_t), 2, RANDOM_MOUSE); if (cmd == OLD_CONS_MOUSECTL) { static u_char swapb[] = { 0, 4, 2, 6, 1, 5, 3, 7 }; diff --git a/sys/dev/syscons/syscons.c b/sys/dev/syscons/syscons.c index f09973c49a1..b44dd537105 100644 --- a/sys/dev/syscons/syscons.c +++ b/sys/dev/syscons/syscons.c @@ -3400,7 +3400,7 @@ next_code: sc_touch_scrn_saver(); if (!(flags & SCGETC_CN)) - random_harvest(&c, sizeof(c), 1, 0, RANDOM_KEYBOARD); + random_harvest(&c, sizeof(c), 1, RANDOM_KEYBOARD); if (scp->kbd_mode != K_XLATE) return KEYCHAR(c); diff --git a/sys/dev/ubsec/ubsec.c b/sys/dev/ubsec/ubsec.c index 0e1b7cb5df6..6c06b71820b 100644 --- a/sys/dev/ubsec/ubsec.c +++ b/sys/dev/ubsec/ubsec.c @@ -259,7 +259,7 @@ ubsec_partname(struct ubsec_softc *sc) static void default_harvest(struct rndtest_state *rsp, void *buf, u_int count) { - random_harvest(buf, count, count*NBBY/2, 0, RANDOM_PURE); + random_harvest(buf, count, count*NBBY/2, RANDOM_PURE_UBSEC); } static int diff --git a/sys/kern/kern_intr.c b/sys/kern/kern_intr.c index f4b04c3f71b..e4f1c823c88 100644 --- a/sys/kern/kern_intr.c +++ b/sys/kern/kern_intr.c @@ -901,7 +901,7 @@ intr_event_schedule_thread(struct intr_event *ie) p->p_pid, td->td_name); entropy.event = (uintptr_t)ie; entropy.td = ctd; - random_harvest(&entropy, sizeof(entropy), 2, 0, + random_harvest(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT); } @@ -1055,7 +1055,7 @@ intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it) p->p_pid, td->td_name); entropy.event = (uintptr_t)ie; entropy.td = ctd; - random_harvest(&entropy, sizeof(entropy), 2, 0, + random_harvest(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT); } @@ -1146,7 +1146,7 @@ swi_sched(void *cookie, int flags) curproc->p_pid, curthread->td_name); entropy.event = (uintptr_t)ih; entropy.td = curthread; - random_harvest(&entropy, sizeof(entropy), 1, 0, + random_harvest(&entropy, sizeof(entropy), 1, RANDOM_SWI); } diff --git a/sys/mips/cavium/octeon_rnd.c b/sys/mips/cavium/octeon_rnd.c index 298f06aee1d..27a707810e4 100644 --- a/sys/mips/cavium/octeon_rnd.c +++ b/sys/mips/cavium/octeon_rnd.c @@ -41,11 +41,6 @@ __FBSDID("$FreeBSD$"); #include #include -/* - * XXX - * random_harvest(9) says to call it with no more than 16 bytes, but at least - * safe(4) seems to violate that rule. - */ #define OCTEON_RND_WORDS 2 struct octeon_rnd_softc { @@ -131,7 +126,7 @@ octeon_rnd_harvest(void *arg) for (i = 0; i < OCTEON_RND_WORDS; i++) sc->sc_entropy[i] = cvmx_rng_get_random64(); random_harvest(sc->sc_entropy, sizeof sc->sc_entropy, - (sizeof(sc->sc_entropy)*8)/2, 0, RANDOM_PURE); + (sizeof(sc->sc_entropy)*8)/2, RANDOM_PURE_OCTEON); callout_reset(&sc->sc_callout, hz * 5, octeon_rnd_harvest, sc); } diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c index 638b3647f6b..cc5d8b5d6ff 100644 --- a/sys/net/if_ethersubr.c +++ b/sys/net/if_ethersubr.c @@ -639,7 +639,7 @@ ether_input_internal(struct ifnet *ifp, struct mbuf *m) } if (harvest.ethernet) - random_harvest(&(m->m_data), 12, 3, 0, RANDOM_NET_ETHER); + random_harvest(&(m->m_data), 12, 3, RANDOM_NET_ETHER); ether_demux(ifp, m); CURVNET_RESTORE(); diff --git a/sys/net/if_tun.c b/sys/net/if_tun.c index f36e7ada3d2..6a73d7cfbcd 100644 --- a/sys/net/if_tun.c +++ b/sys/net/if_tun.c @@ -918,7 +918,7 @@ tunwrite(struct cdev *dev, struct uio *uio, int flag) return (EAFNOSUPPORT); } if (harvest.point_to_point) - random_harvest(&(m->m_data), 12, 3, 0, RANDOM_NET_TUN); + random_harvest(&(m->m_data), 12, 3, RANDOM_NET_TUN); ifp->if_ibytes += m->m_pkthdr.len; ifp->if_ipackets++; CURVNET_SET(ifp->if_vnet); diff --git a/sys/netgraph/ng_iface.c b/sys/netgraph/ng_iface.c index 72fc16201df..12ee3a5cd4a 100644 --- a/sys/netgraph/ng_iface.c +++ b/sys/netgraph/ng_iface.c @@ -775,7 +775,7 @@ ng_iface_rcvdata(hook_p hook, item_p item) return (EAFNOSUPPORT); } if (harvest.point_to_point) - random_harvest(&(m->m_data), 12, 3, 0, RANDOM_NET_NG); + random_harvest(&(m->m_data), 12, 3, RANDOM_NET_NG); M_SETFIB(m, ifp->if_fib); netisr_dispatch(isr, m); return (0); diff --git a/sys/sys/random.h b/sys/sys/random.h index 2f86c82e854..efaa58793a8 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -47,10 +47,17 @@ enum esource { RANDOM_NET_NG, RANDOM_INTERRUPT, RANDOM_SWI, + RANDOM_PURE_OCTEON, + RANDOM_PURE_SAFE, + RANDOM_PURE_GLXSB, + RANDOM_PURE_UBSEC, + RANDOM_PURE_HIFN, + RANDOM_PURE_RDRAND, + RANDOM_PURE_NEHEMIAH, RANDOM_PURE, ENTROPYSOURCE }; -void random_harvest(void *, u_int, u_int, u_int, enum esource); +void random_harvest(void *, u_int, u_int, enum esource); /* Allow the sysadmin to select the broad category of * entropy types to harvest From 095ed2c9f3c7ccbed16363d600ec6ad1c25a8804 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Sun, 6 Oct 2013 09:55:28 +0000 Subject: [PATCH 02/20] SNAPSHOT. Simplify the malloc pools; We only need one for this device. Simplify the harvest queue. Marginally improve the entropy pool hashing, making it a bit faster in the process. Connect up the hardware "live" source harvesting. This is simplistic for now, and will need to be made rate-adaptive. All of the above passes a compile test but needs to be debugged. --- sys/dev/random/dummy_rng.c | 1 + sys/dev/random/ivy.c | 5 +- sys/dev/random/live_entropy_sources.c | 36 +++++---- sys/dev/random/live_entropy_sources.h | 2 +- sys/dev/random/nehemiah.c | 5 +- sys/dev/random/random_adaptors.c | 5 +- sys/dev/random/random_harvestq.c | 108 +++++++++++--------------- sys/dev/random/randomdev.c | 16 ++-- sys/dev/random/randomdev.h | 2 + sys/dev/random/randomdev_soft.c | 4 +- sys/dev/random/randomdev_soft.h | 2 - sys/dev/random/yarrow.c | 23 ++++-- 12 files changed, 110 insertions(+), 99 deletions(-) diff --git a/sys/dev/random/dummy_rng.c b/sys/dev/random/dummy_rng.c index eddb7292cf8..a609da5d6c9 100644 --- a/sys/dev/random/dummy_rng.c +++ b/sys/dev/random/dummy_rng.c @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index c8982129cc4..e1a42c4973c 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -32,6 +32,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -40,9 +41,11 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include +#include #include #include -#include #define RETRY_COUNT 10 diff --git a/sys/dev/random/live_entropy_sources.c b/sys/dev/random/live_entropy_sources.c index d8eeb67d93a..d29c94ad8a5 100644 --- a/sys/dev/random/live_entropy_sources.c +++ b/sys/dev/random/live_entropy_sources.c @@ -40,9 +40,12 @@ __FBSDID("$FreeBSD$"); #include #include +#include + #include #include #include +#include #include "live_entropy_sources.h" @@ -52,9 +55,6 @@ static struct sx les_lock; /* need a sleepable lock */ #define LES_THRESHOLD 10 -MALLOC_DEFINE(M_LIVE_ENTROPY_SRCS, "live_entropy_sources", - "Live Entropy Sources"); - void live_entropy_source_register(struct random_hardware_source *rsource) { @@ -62,8 +62,7 @@ live_entropy_source_register(struct random_hardware_source *rsource) KASSERT(rsource != NULL, ("invalid input to %s", __func__)); - les = malloc(sizeof(struct live_entropy_sources), M_LIVE_ENTROPY_SRCS, - M_WAITOK); + les = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK); les->rsource = rsource; sx_xlock(&les_lock); @@ -82,7 +81,7 @@ live_entropy_source_deregister(struct random_hardware_source *rsource) LIST_FOREACH(les, &sources, entries) { if (les->rsource == rsource) { LIST_REMOVE(les, entries); - free(les, M_LIVE_ENTROPY_SRCS); + free(les, M_ENTROPY); break; } } @@ -136,12 +135,16 @@ live_entropy_sources_init(void *unused) * number of rounds, which should be a multiple of the number * of entropy accumulation pools in use; 2 for Yarrow and 32 * for Fortuna. + * + * BEWARE!!! + * This function runs inside the RNG thread! Don't do anything silly! */ void -live_entropy_sources_feed(int rounds) +live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) { + static struct harvest event; + static uint8_t buf[HARVESTSIZE]; struct live_entropy_sources *les; - uint8_t buf[HARVESTSIZE]; int i, n; sx_slock(&les_lock); @@ -157,14 +160,19 @@ live_entropy_sources_feed(int rounds) * This should be quick, since it's a live entropy * source. */ - n = les->rsource->read(buf, sizeof(buf)); /* FIXME: Whine loudly if this didn't work. */ + n = les->rsource->read(buf, sizeof(buf)); + n = MIN(n, HARVESTSIZE); + + event.somecounter = get_cyclecount(); + event.size = n; + event.bits = (n*8)/2; + event.source = les->rsource->source; + memcpy(event.entropy, buf, n); + + /* Do the actual entropy insertion */ + entropy_processor(&event); - /* - * FIXME: Cannot harvest this stuff into the queue; - * the poor thing will choke to death! - */ - random_harvest(buf, n, 0, les->rsource->source); } } diff --git a/sys/dev/random/live_entropy_sources.h b/sys/dev/random/live_entropy_sources.h index 807972f2ddc..ae74cc9ee13 100644 --- a/sys/dev/random/live_entropy_sources.h +++ b/sys/dev/random/live_entropy_sources.h @@ -42,7 +42,7 @@ struct live_entropy_sources { void live_entropy_source_register(struct random_hardware_source *); void live_entropy_source_deregister(struct random_hardware_source *); -void live_entropy_sources_feed(int); +void live_entropy_sources_feed(int, event_proc_f); #define LIVE_ENTROPY_SRC_MODULE(name, modevent, ver) \ static moduledata_t name##_mod = { \ diff --git a/sys/dev/random/nehemiah.c b/sys/dev/random/nehemiah.c index 8f1ad9eeebc..4db55f81889 100644 --- a/sys/dev/random/nehemiah.c +++ b/sys/dev/random/nehemiah.c @@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -40,9 +41,11 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include +#include #include #include -#include static void random_nehemiah_init(void); static void random_nehemiah_deinit(void); diff --git a/sys/dev/random/random_adaptors.c b/sys/dev/random/random_adaptors.c index 5ea77c27181..3d582146226 100644 --- a/sys/dev/random/random_adaptors.c +++ b/sys/dev/random/random_adaptors.c @@ -55,8 +55,6 @@ static struct sysctl_ctx_list random_clist; struct random_adaptor *random_adaptor; -MALLOC_DEFINE(M_RANDOM_ADAPTORS, "random_adaptors", "Random adaptors buffers"); - int random_adaptor_register(const char *name, struct random_adaptor *rsp) { @@ -64,8 +62,7 @@ random_adaptor_register(const char *name, struct random_adaptor *rsp) 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_ENTROPY, M_WAITOK); rpp->name = name; rpp->rsp = rsp; diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 4f8f34157a1..6ae29fd5f1b 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -39,17 +39,17 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include +#include #include - -#include "random_harvestq.h" +#include +#include #define RANDOM_FIFO_MAX 1024 /* 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. @@ -66,7 +66,7 @@ struct entropyfifo { static struct entropyfifo emptyfifo; /* Harvested entropy */ -static struct entropyfifo harvestfifo[ENTROPYSOURCE]; +static struct entropyfifo harvestfifo; /* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */ int random_kthread_control = 0; @@ -79,8 +79,7 @@ random_kthread(void *arg) STAILQ_HEAD(, harvest) local_queue; struct harvest *event = NULL; int local_count; - enum esource source; - event_proc_f func = arg; + event_proc_f entropy_processor = arg; STAILQ_INIT(&local_queue); local_count = 0; @@ -89,16 +88,14 @@ random_kthread(void *arg) 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; - } + /* + * Grab all the entropy events. + * Drain entropy source records into a thread-local + * queue for processing while not holding the mutex. + */ + STAILQ_CONCAT(&local_queue, &harvestfifo.head); + local_count += harvestfifo.count; + harvestfifo.count = 0; /* * Deal with events, if any. @@ -107,7 +104,7 @@ random_kthread(void *arg) if (!STAILQ_EMPTY(&local_queue)) { mtx_unlock_spin(&harvest_mtx); STAILQ_FOREACH(event, &local_queue, next) - func(event); + entropy_processor(event); mtx_lock_spin(&harvest_mtx); STAILQ_CONCAT(&emptyfifo.head, &local_queue); emptyfifo.count += local_count; @@ -118,16 +115,10 @@ random_kthread(void *arg) local_count)); /* - * Do Hardware/fast RNG source processing here. + * Do only one round of the hardware sources for now. + * Later we'll need to make it rate-adaptive. */ -#if 0 - while (hardware_source) { - event = hardware_source->read(); - func(event); - hardware_source++; - /* Throttle somehow? */ - } -#endif + live_entropy_sources_feed(1, entropy_processor); /* * If a queue flush was commanded, it has now happened, @@ -153,23 +144,23 @@ random_harvestq_init(event_proc_f cb) { int error, i; struct harvest *np; - enum esource e; /* Initialise the harvest fifos */ + + /* Contains the currently unused event structs. */ STAILQ_INIT(&emptyfifo.head); - emptyfifo.count = 0; for (i = 0; i < RANDOM_FIFO_MAX; 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; - } + emptyfifo.count = RANDOM_FIFO_MAX; + + /* Will contain the queued-up events. */ + STAILQ_INIT(&harvestfifo.head); + harvestfifo.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 */ @@ -182,7 +173,6 @@ void random_harvestq_deinit(void) { struct harvest *np; - enum esource e; /* Destroy the harvest fifos */ while (!STAILQ_EMPTY(&emptyfifo.head)) { @@ -190,20 +180,26 @@ random_harvestq_deinit(void) 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); - } + emptyfifo.count = 0; + while (!STAILQ_EMPTY(&harvestfifo.head)) { + np = STAILQ_FIRST(&harvestfifo.head); + STAILQ_REMOVE_HEAD(&harvestfifo.head, next); + free(np, M_ENTROPY); } + harvestfifo.count = 0; mtx_destroy(&harvest_mtx); } /* - * Entropy harvesting routine. This is supposed to be fast; do - * not do anything slow in here! + * Entropy harvesting routine. + * This is supposed to be fast; do not do anything slow in here! + * + * It is also illegal (and morally reprehensible) to insert any + * high-rate data here. "High-rate" is define as a data source + * that will usually cause lots of failures of the "Lockless read" + * check a few lines below. This includes the "always-on" sources + * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources. */ void random_harvestq_internal(u_int64_t somecounter, const void *entropy, @@ -215,21 +211,21 @@ random_harvestq_internal(u_int64_t somecounter, const void *entropy, ("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) + if (harvestfifo.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 + * On't overfill the harvest queue; this could steal all + * our memory. */ - if (harvestfifo[origin].count < RANDOM_FIFO_MAX) { + if (harvestfifo.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++; + emptyfifo.count--; event->somecounter = somecounter; event->size = count; event->bits = bits; @@ -239,21 +235,11 @@ random_harvestq_internal(u_int64_t somecounter, const void *entropy, 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 %2d %02X\n", event->size, event->bits, event->source); - } -#endif - - STAILQ_INSERT_TAIL(&harvestfifo[origin].head, + STAILQ_INSERT_TAIL(&harvestfifo.head, event, next); + harvestfifo.count++; } } + mtx_unlock_spin(&harvest_mtx); } diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index 990324e14d5..f37da70c126 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -52,9 +52,11 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include #include +#include +#include +#include +#include #define RANDOM_MINOR 0 @@ -72,6 +74,8 @@ static struct cdevsw random_cdevsw = { .d_name = "random", }; +MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); + /* For use with make_dev(9)/destroy_dev(9). */ static struct cdev *random_dev; @@ -82,10 +86,6 @@ random_read(struct cdev *dev __unused, struct uio *uio, int flag) int c, error = 0; void *random_buf; - /* XXX: Harvest some entropy from live entropy sources, if available */ - live_entropy_sources_feed(65); /* 65 is meaningless -- - need to decide appropriate value */ - /* Blocking logic */ if (!random_adaptor->seeded) error = (*random_adaptor->block)(flag); @@ -93,7 +93,7 @@ random_read(struct cdev *dev __unused, struct uio *uio, int flag) /* The actual read */ if (!error) { - random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); + random_buf = (void *)malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); while (uio->uio_resid > 0 && !error) { c = MIN(uio->uio_resid, PAGE_SIZE); @@ -104,7 +104,7 @@ random_read(struct cdev *dev __unused, struct uio *uio, int flag) * optional housekeeping */ (*random_adaptor->read)(NULL, 0); - free(random_buf, M_TEMP); + free(random_buf, M_ENTROPY); } diff --git a/sys/dev/random/randomdev.h b/sys/dev/random/randomdev.h index a0e002f89ae..3afa94331f7 100644 --- a/sys/dev/random/randomdev.h +++ b/sys/dev/random/randomdev.h @@ -30,6 +30,8 @@ * and non algorithm-specific for the entropy processor */ +MALLOC_DECLARE(M_ENTROPY); + typedef void random_init_func_t(void); typedef void random_deinit_func_t(void); typedef int random_block_func_t(int); diff --git a/sys/dev/random/randomdev_soft.c b/sys/dev/random/randomdev_soft.c index 80f46df5871..8f8b849fead 100644 --- a/sys/dev/random/randomdev_soft.c +++ b/sys/dev/random/randomdev_soft.c @@ -53,9 +53,10 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include #include +#include +#include #if defined(YARROW_RNG) #include #endif @@ -63,7 +64,6 @@ __FBSDID("$FreeBSD$"); #include #endif -#include "random_harvestq.h" static int randomdev_poll(int event, struct thread *td); static int randomdev_block(int flag); diff --git a/sys/dev/random/randomdev_soft.h b/sys/dev/random/randomdev_soft.h index 7ee9053ec3b..7a2b62e8e76 100644 --- a/sys/dev/random/randomdev_soft.h +++ b/sys/dev/random/randomdev_soft.h @@ -41,8 +41,6 @@ #define HARVESTSIZE 16 /* max size of each harvested entropy unit */ -MALLOC_DECLARE(M_ENTROPY); - /* These are used to queue harvested packets of entropy. The entropy * buffer size is pretty arbitrary. */ diff --git a/sys/dev/random/yarrow.c b/sys/dev/random/yarrow.c index 635e126da6f..ab26eefb17d 100644 --- a/sys/dev/random/yarrow.c +++ b/sys/dev/random/yarrow.c @@ -97,6 +97,7 @@ clear_counter(void) /* 128-bit C = C + 1 */ /* Nothing to see here, folks, just an ugly mess. */ +/* TODO: Make a Galois counter instead? */ static void increment_counter(void) { @@ -113,13 +114,25 @@ random_process_event(struct harvest *event) struct source *source; enum esource src; - /* Unpack the event into the appropriate source accumulator */ +#if 1 + /* Do this better with DTrace */ + { + 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 %2d %02X\n", event->size, event->bits, event->source); + } +#endif + + /* Accumulate the event into the appropriate pool */ pl = random_state.which; source = &random_state.pool[pl].source[event->source]; - randomdev_hash_iterate(&random_state.pool[pl].hash, event->entropy, - sizeof(event->entropy)); - randomdev_hash_iterate(&random_state.pool[pl].hash, &event->somecounter, - sizeof(event->somecounter)); + randomdev_hash_iterate(&random_state.pool[pl].hash, event, + sizeof(*event)); source->bits += event->bits; /* Count the over-threshold sources in each pool */ From eee1352e67e6e70935c04c895eb648ec4ce28dcb Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Sun, 6 Oct 2013 12:35:29 +0000 Subject: [PATCH 03/20] Don't use the "real" assembler mnemonics; older compilers may not understand them (like when building CURRENT on 9.x). Submitted by: Konstantin Belousov --- sys/dev/random/ivy.c | 4 ++-- sys/dev/random/nehemiah.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index e1a42c4973c..f9a98327459 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -65,12 +65,12 @@ ivy_rng_store(uint64_t *tmp) __asm __volatile( #ifdef __amd64__ - "rdrand\t%%rax\n\t" + ".byte\t0x48,0x0f,0xc7,0xf0\n\t" /* rdrand %rax */ "jnc\t1f\n\t" "movq\t%%rax,%1\n\t" "movl\t$8,%%eax\n" #else /* i386 */ - "rdrand\t%%eax\n\t" + ".byte\t0x0f,0xc7,0xf0\n\t" /* rdrand %eax */ "jnc\t1f\n\t" "movl\t%%eax,%1\n\t" "movl\t$4,%%eax\n" diff --git a/sys/dev/random/nehemiah.c b/sys/dev/random/nehemiah.c index 4db55f81889..133fe9da08e 100644 --- a/sys/dev/random/nehemiah.c +++ b/sys/dev/random/nehemiah.c @@ -71,7 +71,7 @@ VIA_RNG_store(void *buf) #ifdef __GNUCLIKE_ASM __asm __volatile( "movl $0,%%edx\n\t" - "xstore" + ".byte 0x0f, 0xa7, 0xc0" /* xstore */ : "=a" (retval), "+d" (rate), "+D" (buf) : : "memory" From ad1f3311961af3d5ce6c37b32db5bf65d8ffbf35 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Sun, 6 Oct 2013 12:40:32 +0000 Subject: [PATCH 04/20] Debug run. This now works, except that the "live" sources haven't been tested. With all sources turned on, this unlocks itself in a couple of seconds! That is no my box, and there is no guarantee that this will be the case everywhere. * Cut debug prints. * Use the same locks/mutexes all the way through. * Be a tad more conservative about entropy estimates. --- sys/dev/random/live_entropy_sources.c | 22 +++++++--------------- sys/dev/random/random_harvestq.c | 2 +- sys/dev/random/random_harvestq.h | 1 + sys/dev/random/yarrow.c | 6 +++++- sys/net/if_ethersubr.c | 2 +- sys/net/if_tun.c | 2 +- sys/netgraph/ng_iface.c | 2 +- 7 files changed, 17 insertions(+), 20 deletions(-) diff --git a/sys/dev/random/live_entropy_sources.c b/sys/dev/random/live_entropy_sources.c index d29c94ad8a5..7d5ee9bd03b 100644 --- a/sys/dev/random/live_entropy_sources.c +++ b/sys/dev/random/live_entropy_sources.c @@ -51,7 +51,6 @@ __FBSDID("$FreeBSD$"); LIST_HEAD(les_head, live_entropy_sources); static struct les_head sources = LIST_HEAD_INITIALIZER(sources); -static struct sx les_lock; /* need a sleepable lock */ #define LES_THRESHOLD 10 @@ -65,9 +64,9 @@ live_entropy_source_register(struct random_hardware_source *rsource) les = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK); les->rsource = rsource; - sx_xlock(&les_lock); + mtx_lock_spin(&harvest_mtx); LIST_INSERT_HEAD(&sources, les, entries); - sx_xunlock(&les_lock); + mtx_unlock_spin(&harvest_mtx); } void @@ -77,7 +76,7 @@ live_entropy_source_deregister(struct random_hardware_source *rsource) KASSERT(rsource != NULL, ("invalid input to %s", __func__)); - sx_xlock(&les_lock); + mtx_lock_spin(&harvest_mtx); LIST_FOREACH(les, &sources, entries) { if (les->rsource == rsource) { LIST_REMOVE(les, entries); @@ -85,7 +84,7 @@ live_entropy_source_deregister(struct random_hardware_source *rsource) break; } } - sx_xunlock(&les_lock); + mtx_unlock_spin(&harvest_mtx); } static int @@ -96,7 +95,7 @@ live_entropy_source_handler(SYSCTL_HANDLER_ARGS) count = error = 0; - sx_slock(&les_lock); + mtx_lock_spin(&harvest_mtx); if (LIST_EMPTY(&sources)) error = SYSCTL_OUT(req, "", 0); @@ -113,7 +112,7 @@ live_entropy_source_handler(SYSCTL_HANDLER_ARGS) } } - sx_sunlock(&les_lock); + mtx_unlock_spin(&harvest_mtx); return (error); } @@ -126,8 +125,6 @@ live_entropy_sources_init(void *unused) CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, live_entropy_source_handler, "", "List of Active Live Entropy Sources"); - - sx_init(&les_lock, "live_entropy_sources"); } /* @@ -138,6 +135,7 @@ live_entropy_sources_init(void *unused) * * BEWARE!!! * This function runs inside the RNG thread! Don't do anything silly! + * The harvest_mtx mutex is held; you may count on that. */ void live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) @@ -147,8 +145,6 @@ live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) struct live_entropy_sources *les; int i, n; - sx_slock(&les_lock); - /* * Walk over all of live entropy sources, and feed their output * to the system-wide RNG. @@ -176,15 +172,11 @@ live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) } } - - sx_sunlock(&les_lock); } static void live_entropy_sources_deinit(void *unused) { - - sx_destroy(&les_lock); } SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 6ae29fd5f1b..4e9d711fc4b 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -52,7 +52,7 @@ __FBSDID("$FreeBSD$"); /* * The harvest mutex protects the consistency of the entropy fifos and - * empty fifo. + * empty fifo and other associated structures. */ struct mtx harvest_mtx; diff --git a/sys/dev/random/random_harvestq.h b/sys/dev/random/random_harvestq.h index 11ccfc46e00..f4e9bb188a9 100644 --- a/sys/dev/random/random_harvestq.h +++ b/sys/dev/random/random_harvestq.h @@ -37,5 +37,6 @@ void random_harvestq_internal(u_int64_t, const void *, u_int, u_int, enum esource); extern int random_kthread_control; +extern struct mtx harvest_mtx; #endif /* __RANDOM_HARVEST_H__ */ diff --git a/sys/dev/random/yarrow.c b/sys/dev/random/yarrow.c index ab26eefb17d..6d2ea3b48fd 100644 --- a/sys/dev/random/yarrow.c +++ b/sys/dev/random/yarrow.c @@ -114,7 +114,7 @@ random_process_event(struct harvest *event) struct source *source; enum esource src; -#if 1 +#if 0 /* Do this better with DTrace */ { int i; @@ -243,6 +243,10 @@ reseed(u_int fastslow) u_int i; enum esource j; +#if 0 + printf("Yarrow: %s reseed\n", fastslow == FAST ? "fast" : "slow"); +#endif + /* The reseed task must not be jumped on */ mtx_lock(&random_reseed_mtx); diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c index cc5d8b5d6ff..f1a16665464 100644 --- a/sys/net/if_ethersubr.c +++ b/sys/net/if_ethersubr.c @@ -639,7 +639,7 @@ ether_input_internal(struct ifnet *ifp, struct mbuf *m) } if (harvest.ethernet) - random_harvest(&(m->m_data), 12, 3, RANDOM_NET_ETHER); + random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER); ether_demux(ifp, m); CURVNET_RESTORE(); diff --git a/sys/net/if_tun.c b/sys/net/if_tun.c index 6a73d7cfbcd..262d6d2f9a6 100644 --- a/sys/net/if_tun.c +++ b/sys/net/if_tun.c @@ -918,7 +918,7 @@ tunwrite(struct cdev *dev, struct uio *uio, int flag) return (EAFNOSUPPORT); } if (harvest.point_to_point) - random_harvest(&(m->m_data), 12, 3, RANDOM_NET_TUN); + random_harvest(&(m->m_data), 12, 2, RANDOM_NET_TUN); ifp->if_ibytes += m->m_pkthdr.len; ifp->if_ipackets++; CURVNET_SET(ifp->if_vnet); diff --git a/sys/netgraph/ng_iface.c b/sys/netgraph/ng_iface.c index 12ee3a5cd4a..6c18d2a1cc1 100644 --- a/sys/netgraph/ng_iface.c +++ b/sys/netgraph/ng_iface.c @@ -775,7 +775,7 @@ ng_iface_rcvdata(hook_p hook, item_p item) return (EAFNOSUPPORT); } if (harvest.point_to_point) - random_harvest(&(m->m_data), 12, 3, RANDOM_NET_NG); + random_harvest(&(m->m_data), 12, 2, RANDOM_NET_NG); M_SETFIB(m, ifp->if_fib); netisr_dispatch(isr, m); return (0); From 241b55d182e02d05b4d2cf962b372690ef3d733a Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Sun, 6 Oct 2013 12:43:42 +0000 Subject: [PATCH 05/20] As userland writing to /dev/random is no more, remove the "better than nothing" bootstrap mode. Add SWI harvesting to the mix. My box seeds Yarrow by itself in a few seconds! YMMV; more to follow. --- etc/defaults/rc.conf | 1 + etc/rc.d/initrandom | 42 +++++++----------------------------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/etc/defaults/rc.conf b/etc/defaults/rc.conf index 80f279dbfcc..e748dc09736 100644 --- a/etc/defaults/rc.conf +++ b/etc/defaults/rc.conf @@ -651,6 +651,7 @@ entropy_save_num="8" # Number of entropy cache files to save. harvest_interrupt="YES" # Entropy device harvests interrupt randomness harvest_ethernet="YES" # Entropy device harvests ethernet randomness harvest_p_to_p="YES" # Entropy device harvests point-to-point randomness +harvest_swi="YES" # Entropy device harvests internal SWI randomness dmesg_enable="YES" # Save dmesg(8) to /var/run/dmesg.boot watchdogd_enable="NO" # Start the software watchdog daemon watchdogd_flags="" # Flags to watchdogd (if enabled) diff --git a/etc/rc.d/initrandom b/etc/rc.d/initrandom index 47838737806..907668b191c 100755 --- a/etc/rc.d/initrandom +++ b/etc/rc.d/initrandom @@ -14,26 +14,6 @@ name="initrandom" start_cmd="initrandom_start" stop_cmd=":" -feed_dev_random() -{ - if [ -f "${1}" -a -r "${1}" -a -s "${1}" ]; then - cat "${1}" | dd of=/dev/random bs=8k 2>/dev/null - fi -} - -better_than_nothing() -{ - # XXX temporary until we can improve the entropy - # harvesting rate. - # Entropy below is not great, but better than nothing. - # This unblocks the generator at startup - # Note: commands are ordered to cause the most variance across reboots. - ( kenv; dmesg; df -ib; ps -fauxww; date; sysctl -a ) \ - | dd of=/dev/random bs=8k 2>/dev/null - /sbin/sha256 -q `sysctl -n kern.bootfile` \ - | dd of=/dev/random bs=8k 2>/dev/null -} - initrandom_start() { soft_random_generator=`sysctl kern.random 2>/dev/null` @@ -63,23 +43,15 @@ initrandom_start() else ${SYSCTL} kern.random.sys.harvest.point_to_point=0 >/dev/null fi + + if checkyesno harvest_swi; then + ${SYSCTL} kern.random.sys.harvest.swi=1 >/dev/null + echo -n ' swi' + else + ${SYSCTL} kern.random.sys.harvest.swi=0 >/dev/null + fi fi - # First pass at reseeding /dev/random. - # - case ${entropy_file} in - [Nn][Oo] | '') - ;; - *) - if [ -w /dev/random ]; then - feed_dev_random "${entropy_file}" - fi - ;; - esac - - better_than_nothing - - echo -n ' kickstart' fi echo '.' From 1f25c4ec6d1d9a8fceabd5dbfc2594cb30fe0338 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Sun, 6 Oct 2013 13:01:42 +0000 Subject: [PATCH 06/20] Fix up the man page for random(4). This mainly removes no-longer-relevant details about HW RNGs, reseeding explicitly and user-supplied entropy. --- share/man/man4/random.4 | 118 ++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 65 deletions(-) diff --git a/share/man/man4/random.4 b/share/man/man4/random.4 index 5e73bc0aa6b..4ad5b3996c8 100644 --- a/share/man/man4/random.4 +++ b/share/man/man4/random.4 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2001 Mark R V Murray. All rights reserved. +.\" Copyright (c) 2001-2013 Mark R V Murray. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -43,35 +43,48 @@ The device will probe for certain hardware entropy sources, and use these in preference to the fallback, which is a generator implemented in software. -If the kernel environment MIB's -.Va hw.nehemiah_rng_enable -or -.Va hw.ivy_rng_enable -are set to -.Dq Li 0 , -the associated hardware entropy source will be ignored. .Pp -If the device is using -the software generator, -writing data to +The software generator will start in an +.Em unseeded +state, and will block reads until +it is (re)seeded. +This may cause trouble at system boot +when keys and the like +are generated from +/dev/random +so steps should be taken to ensure a +reseed as soon as possible. +The +.Xr sysctl 8 +controlling the +.Em seeded +status (see below) may be used +if security is not an issue +or for convenience +during setup or development. +.Pp +This initial seeding +of random number generators +is a bootstrapping problem +that needs very careful attention. +In some cases, +it may be difficult +to find enough randomness +to seed a random number generator +until a system is fully operational, +but the system requires random numbers +to become fully operational. +It is (or more accurately should be) +critically important that the .Nm -would perturb the internal state. -This perturbation of the internal state -is the only userland method of introducing -extra entropy into the device. -If the writer has superuser privilege, -then closing the device after writing -will make the software generator reseed itself. -This can be used for extra security, -as it immediately introduces any/all new entropy -into the PRNG. -The hardware generators will generate -sufficient quantities of entropy, -and will therefore ignore user-supplied input. -The software -.Nm -device may be controlled with -.Xr sysctl 8 . +device is seeded +before the first time it is used. +In the case where a dummy or "blocking-only" +device is used, +it is the responsibility +of the system architect +to ensure that no blocking reads +hold up critical processes. .Pp To see the current settings of the software .Nm @@ -81,22 +94,20 @@ device, use the command line: .Pp which results in something like: .Bd -literal -offset indent -kern.random.adaptors: yarrow +kern.random.adaptors: yarrow,dummy +kern.random.active_adaptor: yarrow +kern.random.yarrow.gengateinterval: 10 +kern.random.yarrow.bins: 10 +kern.random.yarrow.fastthresh: 96 +kern.random.yarrow.slowthresh: 128 +kern.random.yarrow.slowoverthresh: 2 kern.random.sys.seeded: 1 kern.random.sys.harvest.ethernet: 1 kern.random.sys.harvest.point_to_point: 1 kern.random.sys.harvest.interrupt: 1 -kern.random.sys.harvest.swi: 0 -kern.random.yarrow.gengateinterval: 10 -kern.random.yarrow.bins: 10 -kern.random.yarrow.fastthresh: 192 -kern.random.yarrow.slowthresh: 256 -kern.random.yarrow.slowoverthresh: 2 +kern.random.sys.harvest.swi: 1 .Ed .Pp -(These would not be seen if a -hardware generator is present.) -.Pp Other than .Dl kern.random.adaptors all settings are read/write. @@ -107,9 +118,10 @@ variable indicates whether or not the .Nm device is in an acceptably secure state as a result of reseeding. -If set to 0, the device will block (on read) until the next reseed -(which can be from an explicit write, -or as a result of entropy harvesting). +If set to 0, +the device will block (on read) +until the next reseed +as a result of entropy harvesting. A reseed will set the value to 1 (non-blocking). .Pp The @@ -276,19 +288,6 @@ the generator produce independent sequences. However, the guessability or reproducibility of the sequence is unimportant, unlike the previous cases. .Pp -One final consideration for the seeding of random number generators -is a bootstrapping problem. -In some cases, it may be difficult to find enough randomness to -seed a random number generator until a system is fully operational, -but the system requires random numbers to become fully operational. -There is no substitute for careful thought here, -but the -.Fx -.Nm -device, -which is based on the Yarrow system, -should be of some help in this area. -.Pp .Fx does also provide the traditional .Xr rand 3 @@ -325,17 +324,6 @@ and is an implementation of the .Em Yarrow algorithm by Bruce Schneier, .Em et al . -The only hardware implementations -currently are for the -.Tn VIA C3 Nehemiah -(stepping 3 or greater) -CPU -and the -.Tn Intel -.Dq Bull Mountain -.Em RdRand -instruction and underlying random number generator (RNG). -More will be added in the future. .Pp The author gratefully acknowledges significant assistance from VIA Technologies, Inc. From 1a3c1f06dd19d727c83fa85b5c26c629d71e9b04 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Sun, 6 Oct 2013 22:45:02 +0000 Subject: [PATCH 07/20] Snapshot. Looking pretty good; this mostly works now. New code includes: * Read cached entropy at startup, both from files and from loader(8) preloaded entropy. Failures are soft, but announced. Untested. * Use EVENTHANDLER to do above just before we go multiuser. Untested. --- sys/boot/forth/loader.conf | 11 ++++ sys/conf/files | 1 + sys/conf/files.amd64 | 2 + sys/conf/files.i386 | 2 + sys/dev/random/ivy.c | 15 +++++ sys/dev/random/live_entropy_sources.c | 37 +++++++---- sys/dev/random/live_entropy_sources.h | 2 + sys/dev/random/random_harvestq.c | 57 +++++++++++++++++ sys/dev/random/randomdev_soft.c | 2 + sys/dev/random/rwfile.c | 91 +++++++++++++++++++++++++++ sys/dev/random/rwfile.h | 30 +++++++++ sys/kern/init_main.c | 2 + sys/sys/eventhandler.h | 4 ++ sys/sys/random.h | 3 +- 14 files changed, 244 insertions(+), 15 deletions(-) create mode 100644 sys/dev/random/rwfile.c create mode 100644 sys/dev/random/rwfile.h diff --git a/sys/boot/forth/loader.conf b/sys/boot/forth/loader.conf index 76f40c4df9a..0757b5f4b4b 100644 --- a/sys/boot/forth/loader.conf +++ b/sys/boot/forth/loader.conf @@ -38,6 +38,17 @@ bitmap_name="splash.bmp" # Set this to the name of the file bitmap_type="splash_image_data" # and place it on the module_path +############################################################## +### Random number generator configuration ################### +############################################################## + +entropy_cache_load="NO" # Set this to YES to load entropy at boot time +entropy_cache_name="/boot/entropy" # Set this to the name of the file +entropy_cache_type="/boot/entropy" +#kern.random.sys.seeded="0" # Set this to 1 to start /dev/random + # without waiting for a (re)seed. + + ############################################################## ### Loader settings ######################################## ############################################################## diff --git a/sys/conf/files b/sys/conf/files index 39fddd9e36c..f3e298c48f1 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -2051,6 +2051,7 @@ dev/random/randomdev.c optional random dev/random/randomdev_soft.c optional random dev/random/yarrow.c optional random dev/random/hash.c optional random +dev/random/rwfile.c optional random dev/rc/rc.c optional rc dev/re/if_re.c optional re dev/rndtest/rndtest.c optional rndtest diff --git a/sys/conf/files.amd64 b/sys/conf/files.amd64 index 7ac5d720917..1914c48de9b 100644 --- a/sys/conf/files.amd64 +++ b/sys/conf/files.amd64 @@ -259,6 +259,8 @@ dev/nvme/nvme_sysctl.c optional nvme dev/nvme/nvme_test.c optional nvme dev/nvme/nvme_util.c optional nvme dev/nvram/nvram.c optional nvram isa +dev/random/ivy.c optional rdrand_rng +dev/random/nehemiah.c optional padlock_rng dev/qlxge/qls_dbg.c optional qlxge pci dev/qlxge/qls_dump.c optional qlxge pci dev/qlxge/qls_hw.c optional qlxge pci diff --git a/sys/conf/files.i386 b/sys/conf/files.i386 index 55017dcaa47..e2596590a04 100644 --- a/sys/conf/files.i386 +++ b/sys/conf/files.i386 @@ -257,6 +257,8 @@ dev/nvme/nvme_test.c optional nvme dev/nvme/nvme_util.c optional nvme dev/nvram/nvram.c optional nvram isa dev/pcf/pcf_isa.c optional pcf +dev/random/ivy.c optional rdrand_rng +dev/random/nehemiah.c optional padlock_rng dev/sbni/if_sbni.c optional sbni dev/sbni/if_sbni_isa.c optional sbni isa dev/sbni/if_sbni_pci.c optional sbni pci diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index f9a98327459..8ca291e52a7 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -57,6 +57,15 @@ struct random_hardware_source random_ivy = { .read = random_ivy_read }; +#if 1 +static inline int +ivy_rng_store(uint64_t *tmp) +{ + *tmp = 0xF001FACE; + + return (sizeof(uint64_t)); +} +#else static inline int ivy_rng_store(uint64_t *tmp) { @@ -82,6 +91,7 @@ ivy_rng_store(uint64_t *tmp) return (0); #endif } +#endif static int random_ivy_read(void *buf, int c) @@ -114,6 +124,10 @@ rdrand_modevent(module_t mod, int type, void *unused) switch (type) { case MOD_LOAD: +#if 1 + live_entropy_source_register(&random_ivy); + printf("%s: CRAP RDRAND is present\n", random_ivy.ident); +#else if (cpu_feature2 & CPUID2_RDRAND) live_entropy_source_register(&random_ivy); else @@ -122,6 +136,7 @@ rdrand_modevent(module_t mod, int type, void *unused) #endif printf("%s: RDRAND is not present\n", random_ivy.ident); +#endif break; case MOD_UNLOAD: diff --git a/sys/dev/random/live_entropy_sources.c b/sys/dev/random/live_entropy_sources.c index 7d5ee9bd03b..d9af30b732d 100644 --- a/sys/dev/random/live_entropy_sources.c +++ b/sys/dev/random/live_entropy_sources.c @@ -52,7 +52,11 @@ __FBSDID("$FreeBSD$"); LIST_HEAD(les_head, live_entropy_sources); static struct les_head sources = LIST_HEAD_INITIALIZER(sources); -#define LES_THRESHOLD 10 +/* + * The harvest mutex protects the consistency of the entropy fifos and + * empty fifo and other associated structures. + */ +struct mtx live_mtx; void live_entropy_source_register(struct random_hardware_source *rsource) @@ -64,27 +68,27 @@ live_entropy_source_register(struct random_hardware_source *rsource) les = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK); les->rsource = rsource; - mtx_lock_spin(&harvest_mtx); + mtx_lock(&live_mtx); LIST_INSERT_HEAD(&sources, les, entries); - mtx_unlock_spin(&harvest_mtx); + mtx_unlock(&live_mtx); } void live_entropy_source_deregister(struct random_hardware_source *rsource) { - struct live_entropy_sources *les; + struct live_entropy_sources *les = NULL; KASSERT(rsource != NULL, ("invalid input to %s", __func__)); - mtx_lock_spin(&harvest_mtx); - LIST_FOREACH(les, &sources, entries) { + mtx_lock(&live_mtx); + LIST_FOREACH(les, &sources, entries) if (les->rsource == rsource) { LIST_REMOVE(les, entries); - free(les, M_ENTROPY); break; } - } - mtx_unlock_spin(&harvest_mtx); + mtx_unlock(&live_mtx); + if (les != NULL) + free(les, M_ENTROPY); } static int @@ -95,7 +99,7 @@ live_entropy_source_handler(SYSCTL_HANDLER_ARGS) count = error = 0; - mtx_lock_spin(&harvest_mtx); + mtx_lock(&live_mtx); if (LIST_EMPTY(&sources)) error = SYSCTL_OUT(req, "", 0); @@ -112,7 +116,7 @@ live_entropy_source_handler(SYSCTL_HANDLER_ARGS) } } - mtx_unlock_spin(&harvest_mtx); + mtx_unlock(&live_mtx); return (error); } @@ -125,6 +129,8 @@ live_entropy_sources_init(void *unused) CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, live_entropy_source_handler, "", "List of Active Live Entropy Sources"); + + mtx_init(&live_mtx, "live entropy source mutex", NULL, MTX_DEF); } /* @@ -135,7 +141,7 @@ live_entropy_sources_init(void *unused) * * BEWARE!!! * This function runs inside the RNG thread! Don't do anything silly! - * The harvest_mtx mutex is held; you may count on that. + * Remember that we are NOT holding harvest_mtx on entry! */ void live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) @@ -145,6 +151,8 @@ live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) struct live_entropy_sources *les; int i, n; + mtx_lock(&live_mtx); + /* * Walk over all of live entropy sources, and feed their output * to the system-wide RNG. @@ -168,15 +176,18 @@ live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) /* Do the actual entropy insertion */ entropy_processor(&event); - } } + + mtx_unlock(&live_mtx); } static void live_entropy_sources_deinit(void *unused) { + + mtx_destroy(&live_mtx); } SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, diff --git a/sys/dev/random/live_entropy_sources.h b/sys/dev/random/live_entropy_sources.h index ae74cc9ee13..febb24ae22f 100644 --- a/sys/dev/random/live_entropy_sources.h +++ b/sys/dev/random/live_entropy_sources.h @@ -40,6 +40,8 @@ struct live_entropy_sources { struct random_hardware_source *rsource; /* associated random adaptor */ }; +extern struct mtx live_mtx; + void live_entropy_source_register(struct random_hardware_source *); void live_entropy_source_deregister(struct random_hardware_source *); void live_entropy_sources_feed(int, event_proc_f); diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 4e9d711fc4b..04c59b15110 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -33,8 +33,10 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include +#include #include #include #include @@ -43,10 +45,13 @@ __FBSDID("$FreeBSD$"); #include #include +#include + #include #include #include #include +#include #define RANDOM_FIFO_MAX 1024 /* How many events to queue up */ @@ -73,6 +78,56 @@ int random_kthread_control = 0; static struct proc *random_kthread_proc; +static const char *entropy_files[] = { + "/entropy", + "/var/db/entropy", + "/boot/entropy", /* Yeah, Yeah. I know this is loaded by + * loader(8), but not always, and it doesn't + * hurt to do this again. + */ + NULL +}; + +/* Deal with entropy cached externally if this is present. + */ +static void +random_harvestq_cache(void *arg __unused) +{ + const char **entropy_file; + uint8_t *keyfile, *data; + size_t size, i; + int error; + + /* Get stuff that may have been preloaded by loader(8) */ + keyfile = preload_search_by_type("/boot/entropy"); + if (keyfile != NULL) { + data = preload_fetch_addr(keyfile); + size = preload_fetch_size(keyfile); + if (data != NULL && size != 0) { + for (i = 0U; i < size; i += 16) + random_harvestq_internal(get_cyclecount(), data + i, 16, (16*8)/4, RANDOM_CACHED); + printf("random: read %zu bytes from preloaded cache\n", size); + bzero(data, size); + } + else + printf("random: no preloaded entropy cache available\n"); + } + data = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); + for (entropy_file = entropy_files; *entropy_file; entropy_file++) { + error = randomdev_read_file(*entropy_file, data); + if (error == 0) { + for (i = 0U; i < PAGE_SIZE; i += 16) + random_harvestq_internal(get_cyclecount(), data + i, 16, (16*8)/4, RANDOM_CACHED); + printf("random: read %d bytes from '%s'\n", PAGE_SIZE, *entropy_file); + } + else + printf("random: entropy cache '%s' not present or unreadable; error = %d\n", *entropy_file, error); + } + bzero(data, PAGE_SIZE); + free(data, M_ENTROPY); +} +EVENTHANDLER_DEFINE(multiuser, random_harvestq_cache, NULL, 0); + static void random_kthread(void *arg) { @@ -118,7 +173,9 @@ random_kthread(void *arg) * Do only one round of the hardware sources for now. * Later we'll need to make it rate-adaptive. */ + mtx_unlock_spin(&harvest_mtx); live_entropy_sources_feed(1, entropy_processor); + mtx_lock_spin(&harvest_mtx); /* * If a queue flush was commanded, it has now happened, diff --git a/sys/dev/random/randomdev_soft.c b/sys/dev/random/randomdev_soft.c index 8f8b849fead..352d0ae789e 100644 --- a/sys/dev/random/randomdev_soft.c +++ b/sys/dev/random/randomdev_soft.c @@ -99,6 +99,8 @@ static struct random_adaptor random_context = { #define RANDOM_CSPRNG_NAME "fortuna" #endif +TUNABLE_INT("kern.random.sys.seeded", &random_context.seeded); + /* List for the dynamic sysctls */ static struct sysctl_ctx_list random_clist; diff --git a/sys/dev/random/rwfile.c b/sys/dev/random/rwfile.c new file mode 100644 index 00000000000..2c563c8c8f3 --- /dev/null +++ b/sys/dev/random/rwfile.c @@ -0,0 +1,91 @@ +/*- + * Copyright (c) 2013 Mark R V Murray + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include + +int +randomdev_read_file(const char *filename, void *buf) +{ + struct nameidata nd; + struct thread* td = curthread; + int error; + ssize_t resid; + int flags; + + NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td); + flags = FREAD; + error = vn_open(&nd, &flags, 0, NULL); + if (error == 0) { + NDFREE(&nd, NDF_ONLY_PNBUF); + if (nd.ni_vp->v_type != VREG) + error = ENOEXEC; + else + error = vn_rdwr(UIO_READ, nd.ni_vp, buf, PAGE_SIZE, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); + + VOP_UNLOCK(nd.ni_vp, 0); + vn_close(nd.ni_vp, FREAD, td->td_ucred, td); + } + + return (error); +} + +int +randomdev_write_file(const char *filename, void *buf) +{ + struct nameidata nd; + struct thread* td = curthread; + int error; + ssize_t resid; + int flags; + + NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td); + flags = FWRITE | O_CREAT | O_TRUNC; + error = vn_open(&nd, &flags, 0, NULL); + if (error == 0) { + NDFREE(&nd, NDF_ONLY_PNBUF); + if (nd.ni_vp->v_type != VREG) + error = ENOEXEC; + else + error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, PAGE_SIZE, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); + + VOP_UNLOCK(nd.ni_vp, 0); + vn_close(nd.ni_vp, FREAD, td->td_ucred, td); + } + + return (error); +} diff --git a/sys/dev/random/rwfile.h b/sys/dev/random/rwfile.h new file mode 100644 index 00000000000..b1fcff81d18 --- /dev/null +++ b/sys/dev/random/rwfile.h @@ -0,0 +1,30 @@ +/*- + * Copyright (c) 2013 Mark R V Murray + * 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$ + */ + +int randomdev_read_file(const char *filename, void *buf); +int randomdev_write_file(const char *filename, void *buf); diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index ed343b60308..b366f69278d 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -847,6 +847,8 @@ kick_init(const void *udata __unused) { struct thread *td; + EVENTHANDLER_INVOKE(multiuser); + td = FIRST_THREAD_IN_PROC(initproc); thread_lock(td); TD_SET_CAN_RUN(td); diff --git a/sys/sys/eventhandler.h b/sys/sys/eventhandler.h index f1d06b56ae3..ee19073c3a3 100644 --- a/sys/sys/eventhandler.h +++ b/sys/sys/eventhandler.h @@ -192,6 +192,10 @@ EVENTHANDLER_DECLARE(vm_lowmem, vm_lowmem_handler_t); typedef void (*mountroot_handler_t)(void *); EVENTHANDLER_DECLARE(mountroot, mountroot_handler_t); +/* Going multiuser (starting pid 1) event */ +typedef void (*multiuser_handler_t)(void *); +EVENTHANDLER_DECLARE(multiuser, multiuser_handler_t); + /* File system mount events */ struct mount; struct vnode; diff --git a/sys/sys/random.h b/sys/sys/random.h index efaa58793a8..1ccad3af239 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -39,7 +39,7 @@ int read_random(void *, int); */ enum esource { RANDOM_START = 0, - RANDOM_WRITE = 0, + RANDOM_CACHED = 0, RANDOM_KEYBOARD, RANDOM_MOUSE, RANDOM_NET_TUN, @@ -54,7 +54,6 @@ enum esource { RANDOM_PURE_HIFN, RANDOM_PURE_RDRAND, RANDOM_PURE_NEHEMIAH, - RANDOM_PURE, ENTROPYSOURCE }; void random_harvest(void *, u_int, u_int, enum esource); From 6e818c871f7df35c9d645223ae724dd5562f8ceb Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Tue, 8 Oct 2013 06:54:52 +0000 Subject: [PATCH 08/20] Debugging. My attempt at EVENTHANDLER(multiuser) was a failure; use EVENTHANDLER(mountroot) instead. This means we can't count on /var being present, so something will need to be done about harvesting /var/db/entropy/... . Some policy now needs to be sorted out, and a pre-sync cache needs to be written, but apart from that we are now ready to go. Over to review. --- sys/dev/random/random_harvestq.c | 38 ++++++++++++++++++++------------ sys/dev/random/rwfile.c | 9 ++++---- sys/dev/random/rwfile.h | 4 ++-- sys/kern/init_main.c | 2 -- sys/sys/eventhandler.h | 4 ---- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 04c59b15110..e5e2c5aa49a 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -25,7 +25,6 @@ * (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 @@ -46,6 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include @@ -80,21 +80,18 @@ static struct proc *random_kthread_proc; static const char *entropy_files[] = { "/entropy", - "/var/db/entropy", - "/boot/entropy", /* Yeah, Yeah. I know this is loaded by - * loader(8), but not always, and it doesn't - * hurt to do this again. - */ NULL }; /* Deal with entropy cached externally if this is present. + * Lots of policy may eventually arrive in this function. + * Called after / is mounted. */ static void random_harvestq_cache(void *arg __unused) { const char **entropy_file; - uint8_t *keyfile, *data; + uint8_t *keyfile, *data, *zbuf; size_t size, i; int error; @@ -104,21 +101,34 @@ random_harvestq_cache(void *arg __unused) data = preload_fetch_addr(keyfile); size = preload_fetch_size(keyfile); if (data != NULL && size != 0) { - for (i = 0U; i < size; i += 16) - random_harvestq_internal(get_cyclecount(), data + i, 16, (16*8)/4, RANDOM_CACHED); + for (i = 0; i < size; i += 16) + random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED); printf("random: read %zu bytes from preloaded cache\n", size); bzero(data, size); } else printf("random: no preloaded entropy cache available\n"); } + + /* Read and attempt to overwrite the entropy cache files. + * If the file exists, can be read and then overwritten,i + * then use it. Ignore it otherwise, but print out what is + * going on. + */ data = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); + zbuf = __DECONST(void *, zero_region); for (entropy_file = entropy_files; *entropy_file; entropy_file++) { - error = randomdev_read_file(*entropy_file, data); + error = randomdev_read_file(*entropy_file, data, PAGE_SIZE); if (error == 0) { - for (i = 0U; i < PAGE_SIZE; i += 16) - random_harvestq_internal(get_cyclecount(), data + i, 16, (16*8)/4, RANDOM_CACHED); - printf("random: read %d bytes from '%s'\n", PAGE_SIZE, *entropy_file); + printf("random: entropy cache '%s' provides %d bytes\n", *entropy_file, PAGE_SIZE); + error = randomdev_write_file(*entropy_file, zbuf, PAGE_SIZE); + if (error == 0) { + printf("random: entropy cache '%s' contents used and successfully overwritten\n", *entropy_file); + for (i = 0; i < PAGE_SIZE; i += 16) + random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED); + } + else + printf("random: entropy cache '%s' not overwritten and therefore not used; error = %d\n", *entropy_file, error); } else printf("random: entropy cache '%s' not present or unreadable; error = %d\n", *entropy_file, error); @@ -126,7 +136,7 @@ random_harvestq_cache(void *arg __unused) bzero(data, PAGE_SIZE); free(data, M_ENTROPY); } -EVENTHANDLER_DEFINE(multiuser, random_harvestq_cache, NULL, 0); +EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0); static void random_kthread(void *arg) diff --git a/sys/dev/random/rwfile.c b/sys/dev/random/rwfile.c index 2c563c8c8f3..85afcb03325 100644 --- a/sys/dev/random/rwfile.c +++ b/sys/dev/random/rwfile.c @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$"); #include int -randomdev_read_file(const char *filename, void *buf) +randomdev_read_file(const char *filename, void *buf, size_t length) { struct nameidata nd; struct thread* td = curthread; @@ -55,8 +55,7 @@ randomdev_read_file(const char *filename, void *buf) if (nd.ni_vp->v_type != VREG) error = ENOEXEC; else - error = vn_rdwr(UIO_READ, nd.ni_vp, buf, PAGE_SIZE, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); - + error = vn_rdwr(UIO_READ, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); VOP_UNLOCK(nd.ni_vp, 0); vn_close(nd.ni_vp, FREAD, td->td_ucred, td); } @@ -65,7 +64,7 @@ randomdev_read_file(const char *filename, void *buf) } int -randomdev_write_file(const char *filename, void *buf) +randomdev_write_file(const char *filename, void *buf, size_t length) { struct nameidata nd; struct thread* td = curthread; @@ -81,7 +80,7 @@ randomdev_write_file(const char *filename, void *buf) if (nd.ni_vp->v_type != VREG) error = ENOEXEC; else - error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, PAGE_SIZE, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); + error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); VOP_UNLOCK(nd.ni_vp, 0); vn_close(nd.ni_vp, FREAD, td->td_ucred, td); diff --git a/sys/dev/random/rwfile.h b/sys/dev/random/rwfile.h index b1fcff81d18..39aba04151d 100644 --- a/sys/dev/random/rwfile.h +++ b/sys/dev/random/rwfile.h @@ -26,5 +26,5 @@ * $FreeBSD$ */ -int randomdev_read_file(const char *filename, void *buf); -int randomdev_write_file(const char *filename, void *buf); +int randomdev_read_file(const char *filename, void *buf, size_t); +int randomdev_write_file(const char *filename, void *buf, size_t); diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index b366f69278d..ed343b60308 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -847,8 +847,6 @@ kick_init(const void *udata __unused) { struct thread *td; - EVENTHANDLER_INVOKE(multiuser); - td = FIRST_THREAD_IN_PROC(initproc); thread_lock(td); TD_SET_CAN_RUN(td); diff --git a/sys/sys/eventhandler.h b/sys/sys/eventhandler.h index ee19073c3a3..f1d06b56ae3 100644 --- a/sys/sys/eventhandler.h +++ b/sys/sys/eventhandler.h @@ -192,10 +192,6 @@ EVENTHANDLER_DECLARE(vm_lowmem, vm_lowmem_handler_t); typedef void (*mountroot_handler_t)(void *); EVENTHANDLER_DECLARE(mountroot, mountroot_handler_t); -/* Going multiuser (starting pid 1) event */ -typedef void (*multiuser_handler_t)(void *); -EVENTHANDLER_DECLARE(multiuser, multiuser_handler_t); - /* File system mount events */ struct mount; struct vnode; From db3fcaf97086f96ddb4299115efe887680475e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Tue, 8 Oct 2013 11:05:26 +0000 Subject: [PATCH 09/20] Add YARROW_RNG and FORTUNA_RNG to sys/conf/options. Add a SYSINIT that forces a reseed during proc0 setup, which happens fairly late in the boot process. Add a RANDOM_DEBUG option which enables some debugging printf()s. Add a new RANDOM_ATTACH entropy source which harvests entropy from the get_cyclecount() delta across each call to a device attach method. --- sys/conf/options | 5 +++++ sys/dev/random/random_adaptors.c | 14 ++++++++++++++ sys/dev/random/randomdev_soft.c | 5 +++++ sys/dev/random/yarrow.c | 14 ++++++++++++++ sys/kern/subr_bus.c | 16 ++++++++++++++++ sys/modules/random/Makefile | 2 +- sys/sys/random.h | 1 + 7 files changed, 56 insertions(+), 1 deletion(-) diff --git a/sys/conf/options b/sys/conf/options index 2c0a183594f..9e244762ac3 100644 --- a/sys/conf/options +++ b/sys/conf/options @@ -904,3 +904,8 @@ RACCT opt_global.h # Resource Limits RCTL opt_global.h + +# Random number generator(s) +YARROW_RNG opt_random.h +FORTUNA_RNG opt_random.h +RANDOM_DEBUG opt_random.h diff --git a/sys/dev/random/random_adaptors.c b/sys/dev/random/random_adaptors.c index 3d582146226..8cbebbce4c8 100644 --- a/sys/dev/random/random_adaptors.c +++ b/sys/dev/random/random_adaptors.c @@ -227,3 +227,17 @@ SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, random_adaptors_init, NULL); SYSUNINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, random_adaptors_deinit, NULL); + +static void +random_adaptors_reseed(void *unused) +{ + + (void)unused; + if (random_adaptor != NULL) { + (*random_adaptor->reseed)(); + random_adaptor->seeded = 1; + } + arc4rand(NULL, 0, 1); +} +SYSINIT(random_reseed, SI_SUB_INTRINSIC_POST, SI_ORDER_SECOND, + random_adaptors_reseed, NULL); diff --git a/sys/dev/random/randomdev_soft.c b/sys/dev/random/randomdev_soft.c index 352d0ae789e..9c3aa5366c6 100644 --- a/sys/dev/random/randomdev_soft.c +++ b/sys/dev/random/randomdev_soft.c @@ -26,11 +26,16 @@ * */ +#include "opt_random.h" + #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 +#if defined(FORTUNA_RNG) +#error "Fortuna is not yet implemented" +#endif #include __FBSDID("$FreeBSD$"); diff --git a/sys/dev/random/yarrow.c b/sys/dev/random/yarrow.c index 6d2ea3b48fd..1cfa37372ed 100644 --- a/sys/dev/random/yarrow.c +++ b/sys/dev/random/yarrow.c @@ -28,6 +28,8 @@ #include __FBSDID("$FreeBSD$"); +#include "opt_random.h" + #include #include #include @@ -398,5 +400,17 @@ generator_gate(void) void random_yarrow_reseed(void) { +#ifdef RANDOM_DEBUG + int i; + + printf("%s(): fast:", __func__); + for (i = RANDOM_START; i < ENTROPYSOURCE; ++i) + printf(" %d", random_state.pool[FAST].source[i].bits); + printf("\n"); + printf("%s(): slow:", __func__); + for (i = RANDOM_START; i < ENTROPYSOURCE; ++i) + printf(" %d", random_state.pool[SLOW].source[i].bits); + printf("\n"); +#endif reseed(SLOW); } diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index b3b1852cada..5c4c3297f14 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -28,6 +28,7 @@ __FBSDID("$FreeBSD$"); #include "opt_bus.h" +#include "opt_random.h" #include #include @@ -44,6 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -55,6 +57,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include @@ -2766,6 +2769,7 @@ device_probe_and_attach(device_t dev) int device_attach(device_t dev) { + uint64_t attachtime; int error; if (resource_disabled(dev->driver->name, dev->unit)) { @@ -2778,6 +2782,7 @@ device_attach(device_t dev) device_sysctl_init(dev); if (!device_is_quiet(dev)) device_print_child(dev->parent, dev); + attachtime = get_cyclecount(); dev->state = DS_ATTACHING; if ((error = DEVICE_ATTACH(dev)) != 0) { printf("device_attach: %s%d attach returned %d\n", @@ -2790,6 +2795,17 @@ device_attach(device_t dev) dev->state = DS_NOTPRESENT; return (error); } + attachtime = get_cyclecount() - attachtime; + /* + * 4 bits per device is a reasonable value for desktop and server + * hardware with good get_cyclecount() implementations, but may + * need to be adjusted on other platforms. + */ +#ifdef RANDOM_DEBUG + printf("%s(): feeding %d bit(s) of entropy from %s%d\n", + __func__, 4, dev->driver->name, dev->unit); +#endif + random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH); device_sysctl_update(dev); if (dev->busy) dev->state = DS_BUSY; diff --git a/sys/modules/random/Makefile b/sys/modules/random/Makefile index 60b62afde1d..a0c6077de88 100644 --- a/sys/modules/random/Makefile +++ b/sys/modules/random/Makefile @@ -12,7 +12,7 @@ SRCS+= ivy.c .endif SRCS+= randomdev_soft.c yarrow.c hash.c SRCS+= rijndael-alg-fst.c rijndael-api-fst.c sha2.c -SRCS+= bus_if.h device_if.h vnode_if.h opt_cpu.h +SRCS+= bus_if.h device_if.h vnode_if.h opt_cpu.h opt_random.h CFLAGS+= -I${.CURDIR}/../.. diff --git a/sys/sys/random.h b/sys/sys/random.h index 1ccad3af239..68bb2627373 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -46,6 +46,7 @@ enum esource { RANDOM_NET_ETHER, RANDOM_NET_NG, RANDOM_INTERRUPT, + RANDOM_ATTACH, RANDOM_SWI, RANDOM_PURE_OCTEON, RANDOM_PURE_SAFE, From 623f70165fdf2fcc0770954a266fdcd27ec53384 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Tue, 8 Oct 2013 18:48:11 +0000 Subject: [PATCH 10/20] Time to eat crow for me. I replaced the sx_* locks that Arthur used with regular mutexes; this turned out the be the wrong thing to do as the locks need to be sleepable. Revert this folly. Submitted by: Arthur Mesh (In original diff) --- sys/dev/random/live_entropy_sources.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/sys/dev/random/live_entropy_sources.c b/sys/dev/random/live_entropy_sources.c index d9af30b732d..d406ebd2086 100644 --- a/sys/dev/random/live_entropy_sources.c +++ b/sys/dev/random/live_entropy_sources.c @@ -53,10 +53,9 @@ LIST_HEAD(les_head, live_entropy_sources); static struct les_head sources = LIST_HEAD_INITIALIZER(sources); /* - * The harvest mutex protects the consistency of the entropy fifos and - * empty fifo and other associated structures. + * The live_lock protects the consistency of the "struct les_head sources" */ -struct mtx live_mtx; +static struct sx les_lock; /* need a sleepable lock */ void live_entropy_source_register(struct random_hardware_source *rsource) @@ -68,9 +67,9 @@ live_entropy_source_register(struct random_hardware_source *rsource) les = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK); les->rsource = rsource; - mtx_lock(&live_mtx); + sx_xlock(&les_lock); LIST_INSERT_HEAD(&sources, les, entries); - mtx_unlock(&live_mtx); + sx_xunlock(&les_lock); } void @@ -80,13 +79,13 @@ live_entropy_source_deregister(struct random_hardware_source *rsource) KASSERT(rsource != NULL, ("invalid input to %s", __func__)); - mtx_lock(&live_mtx); + sx_xlock(&les_lock); LIST_FOREACH(les, &sources, entries) if (les->rsource == rsource) { LIST_REMOVE(les, entries); break; } - mtx_unlock(&live_mtx); + sx_xunlock(&les_lock); if (les != NULL) free(les, M_ENTROPY); } @@ -99,7 +98,7 @@ live_entropy_source_handler(SYSCTL_HANDLER_ARGS) count = error = 0; - mtx_lock(&live_mtx); + sx_slock(&les_lock); if (LIST_EMPTY(&sources)) error = SYSCTL_OUT(req, "", 0); @@ -116,7 +115,7 @@ live_entropy_source_handler(SYSCTL_HANDLER_ARGS) } } - mtx_unlock(&live_mtx); + sx_sunlock(&les_lock); return (error); } @@ -130,7 +129,7 @@ live_entropy_sources_init(void *unused) NULL, 0, live_entropy_source_handler, "", "List of Active Live Entropy Sources"); - mtx_init(&live_mtx, "live entropy source mutex", NULL, MTX_DEF); + sx_init(&les_lock, "live_entropy_sources"); } /* @@ -151,7 +150,7 @@ live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) struct live_entropy_sources *les; int i, n; - mtx_lock(&live_mtx); + sx_slock(&les_lock); /* * Walk over all of live entropy sources, and feed their output @@ -180,14 +179,14 @@ live_entropy_sources_feed(int rounds, event_proc_f entropy_processor) } - mtx_unlock(&live_mtx); + sx_sunlock(&les_lock); } static void live_entropy_sources_deinit(void *unused) { - mtx_destroy(&live_mtx); + sx_destroy(&les_lock); } SYSINIT(random_adaptors, SI_SUB_DRIVERS, SI_ORDER_FIRST, From 8cc20a46b758a9d030209f9dd7d5f5c821b46786 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Tue, 8 Oct 2013 18:57:46 +0000 Subject: [PATCH 11/20] Make a mildly sylistic change to the order of sources in this enum. --- sys/sys/random.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/sys/random.h b/sys/sys/random.h index 68bb2627373..773c7058cde 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -40,13 +40,13 @@ int read_random(void *, int); enum esource { RANDOM_START = 0, RANDOM_CACHED = 0, + RANDOM_ATTACH, RANDOM_KEYBOARD, RANDOM_MOUSE, RANDOM_NET_TUN, RANDOM_NET_ETHER, RANDOM_NET_NG, RANDOM_INTERRUPT, - RANDOM_ATTACH, RANDOM_SWI, RANDOM_PURE_OCTEON, RANDOM_PURE_SAFE, From b9887f5053b59cf5f2d10a27a4204cd7a7267c2f Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Tue, 8 Oct 2013 22:14:07 +0000 Subject: [PATCH 12/20] Fix some just-noticed problems: o Allow this to work with "nodevice random" by fixing where the MALLOC pool is defined. o Fix the explicit reseed code. This was correct as submitted, but in the project branch doesn't need to set the "seeded" bit as this is done correctly in the "unblock" function. o Remove some debug ifdeffing. o Adjust comments. --- sys/dev/random/ivy.c | 15 --------------- sys/dev/random/nehemiah.c | 7 +++++-- sys/dev/random/random_adaptors.c | 6 +++--- sys/dev/random/random_adaptors.h | 2 ++ sys/dev/random/random_harvestq.c | 1 + sys/dev/random/randomdev.c | 2 -- sys/dev/random/randomdev.h | 2 -- 7 files changed, 11 insertions(+), 24 deletions(-) diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index 8eb5501d2fe..e1a42c4973c 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -57,15 +57,6 @@ struct random_hardware_source random_ivy = { .read = random_ivy_read }; -#if 1 -static inline int -ivy_rng_store(uint64_t *tmp) -{ - *tmp = 0xF001FACE; - - return (sizeof(uint64_t)); -} -#else static inline int ivy_rng_store(uint64_t *tmp) { @@ -91,7 +82,6 @@ ivy_rng_store(uint64_t *tmp) return (0); #endif } -#endif static int random_ivy_read(void *buf, int c) @@ -124,10 +114,6 @@ rdrand_modevent(module_t mod, int type, void *unused) switch (type) { case MOD_LOAD: -#if 1 - live_entropy_source_register(&random_ivy); - printf("%s: CRAP RDRAND is present\n", random_ivy.ident); -#else if (cpu_feature2 & CPUID2_RDRAND) live_entropy_source_register(&random_ivy); else @@ -136,7 +122,6 @@ rdrand_modevent(module_t mod, int type, void *unused) #endif printf("%s: RDRAND is not present\n", random_ivy.ident); -#endif break; case MOD_UNLOAD: diff --git a/sys/dev/random/nehemiah.c b/sys/dev/random/nehemiah.c index 133fe9da08e..e134ad0519f 100644 --- a/sys/dev/random/nehemiah.c +++ b/sys/dev/random/nehemiah.c @@ -57,10 +57,13 @@ struct random_hardware_source random_nehemiah = { .read = random_nehemiah_read }; -/* This H/W RNG never stores more than 8 bytes in one go */ - +/* TODO: now that the Davies-Meyer hash is gone and we only use + * the 'xstore' instruction, do we still need to preserve the + * FPU state with fpu_kern_(enter|leave)() ? + */ static struct fpu_kern_ctx *fpu_ctx_save; +/* This H/W source never stores more than 8 bytes in one go */ /* ARGSUSED */ static __inline size_t VIA_RNG_store(void *buf) diff --git a/sys/dev/random/random_adaptors.c b/sys/dev/random/random_adaptors.c index 8cbebbce4c8..e62b2c0ec74 100644 --- a/sys/dev/random/random_adaptors.c +++ b/sys/dev/random/random_adaptors.c @@ -55,6 +55,8 @@ static struct sysctl_ctx_list random_clist; struct random_adaptor *random_adaptor; +MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures"); + int random_adaptor_register(const char *name, struct random_adaptor *rsp) { @@ -233,10 +235,8 @@ random_adaptors_reseed(void *unused) { (void)unused; - if (random_adaptor != NULL) { + if (random_adaptor != NULL) (*random_adaptor->reseed)(); - random_adaptor->seeded = 1; - } arc4rand(NULL, 0, 1); } SYSINIT(random_reseed, SI_SUB_INTRINSIC_POST, SI_ORDER_SECOND, diff --git a/sys/dev/random/random_adaptors.h b/sys/dev/random/random_adaptors.h index fa5f7c82883..fb3d6e425c9 100644 --- a/sys/dev/random/random_adaptors.h +++ b/sys/dev/random/random_adaptors.h @@ -31,6 +31,8 @@ #include +MALLOC_DECLARE(M_ENTROPY); + struct random_adaptors { LIST_ENTRY(random_adaptors) entries; /* list of providers */ const char *name; /* name of random adaptor */ diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index e5e2c5aa49a..264a3c7415b 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -49,6 +49,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index f37da70c126..0593f397ffc 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -74,8 +74,6 @@ static struct cdevsw random_cdevsw = { .d_name = "random", }; -MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers"); - /* For use with make_dev(9)/destroy_dev(9). */ static struct cdev *random_dev; diff --git a/sys/dev/random/randomdev.h b/sys/dev/random/randomdev.h index 3afa94331f7..a0e002f89ae 100644 --- a/sys/dev/random/randomdev.h +++ b/sys/dev/random/randomdev.h @@ -30,8 +30,6 @@ * and non algorithm-specific for the entropy processor */ -MALLOC_DECLARE(M_ENTROPY); - typedef void random_init_func_t(void); typedef void random_deinit_func_t(void); typedef int random_block_func_t(int); From 3e8957ea8579e899ad6246b753809dc970cf5300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 9 Oct 2013 09:11:14 +0000 Subject: [PATCH 13/20] Add missing include guards and move the existing ones out of the implementation namespace. --- sys/dev/random/hash.h | 5 +++++ sys/dev/random/live_entropy_sources.h | 6 +++--- sys/dev/random/random_adaptors.h | 6 +++--- sys/dev/random/random_harvestq.h | 6 +++--- sys/dev/random/randomdev.h | 5 +++++ sys/dev/random/randomdev_soft.h | 5 +++++ sys/dev/random/rwfile.h | 5 +++++ sys/dev/random/yarrow.h | 5 +++++ 8 files changed, 34 insertions(+), 9 deletions(-) diff --git a/sys/dev/random/hash.h b/sys/dev/random/hash.h index 62f116daa00..4e6a4a0db83 100644 --- a/sys/dev/random/hash.h +++ b/sys/dev/random/hash.h @@ -26,6 +26,9 @@ * $FreeBSD$ */ +#ifndef SYS_DEV_RANDOM_HASH_H_INCLUDED +#define SYS_DEV_RANDOM_HASH_H_INCLUDED + #define KEYSIZE 32 /* (in bytes) == 256 bits */ #define BLOCKSIZE 16 /* (in bytes) == 128 bits */ @@ -43,3 +46,5 @@ 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); + +#endif diff --git a/sys/dev/random/live_entropy_sources.h b/sys/dev/random/live_entropy_sources.h index febb24ae22f..9a23070fa63 100644 --- a/sys/dev/random/live_entropy_sources.h +++ b/sys/dev/random/live_entropy_sources.h @@ -27,8 +27,8 @@ * $FreeBSD$ */ -#ifndef __LIVE_ENTROPY_SOURCES__ -#define __LIVE_ENTROPY_SOURCES__ +#ifndef SYS_DEV_RANDOM_LIVE_ENTROPY_SOURCES_H_INCLUDED +#define SYS_DEV_RANDOM_LIVE_ENTROPY_SOURCES_H_INCLUDED /* * Live entropy source is a source of entropy that can provide @@ -57,4 +57,4 @@ void live_entropy_sources_feed(int, event_proc_f); MODULE_VERSION(name, ver); \ MODULE_DEPEND(name, random, 1, 1, 1); -#endif /* __LIVE_ENTROPY_SOURCES__ */ +#endif /* SYS_DEV_RANDOM_LIVE_ENTROPY_SOURCES_H_INCLUDED */ diff --git a/sys/dev/random/random_adaptors.h b/sys/dev/random/random_adaptors.h index fb3d6e425c9..4765694a452 100644 --- a/sys/dev/random/random_adaptors.h +++ b/sys/dev/random/random_adaptors.h @@ -26,8 +26,8 @@ * $FreeBSD$ */ -#ifndef __RANDOM_ADAPTORS_H__ -#define __RANDOM_ADAPTORS_H__ +#ifndef SYS_DEV_RANDOM_RANDOM_ADAPTORS_H_INCLUDED +#define SYS_DEV_RANDOM_RANDOM_ADAPTORS_H_INCLUDED #include @@ -68,4 +68,4 @@ EVENTHANDLER_DECLARE(random_adaptor_attach, random_adaptor_attach_hook); SYSCTL_DECL(_kern_random); #endif /* SYSCTL_DECL */ -#endif /* __RANDOM_ADAPTORS_H__ */ +#endif /* SYS_DEV_RANDOM_RANDOM_ADAPTORS_H_INCLUDED */ diff --git a/sys/dev/random/random_harvestq.h b/sys/dev/random/random_harvestq.h index f4e9bb188a9..a2ac3d1f91b 100644 --- a/sys/dev/random/random_harvestq.h +++ b/sys/dev/random/random_harvestq.h @@ -26,8 +26,8 @@ * $FreeBSD$ */ -#ifndef __RANDOM_HARVEST_H__ -#define __RANDOM_HARVEST_H__ +#ifndef SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED +#define SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED typedef void (*event_proc_f)(struct harvest *event); @@ -39,4 +39,4 @@ void random_harvestq_internal(u_int64_t, const void *, extern int random_kthread_control; extern struct mtx harvest_mtx; -#endif /* __RANDOM_HARVEST_H__ */ +#endif /* SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED */ diff --git a/sys/dev/random/randomdev.h b/sys/dev/random/randomdev.h index a0e002f89ae..5be595fb714 100644 --- a/sys/dev/random/randomdev.h +++ b/sys/dev/random/randomdev.h @@ -26,6 +26,9 @@ * $FreeBSD$ */ +#ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED +#define SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED + /* This header contains only those definitions that are global * and non algorithm-specific for the entropy processor */ @@ -54,3 +57,5 @@ struct random_hardware_source { enum esource source; random_read_func_t *read; }; + +#endif diff --git a/sys/dev/random/randomdev_soft.h b/sys/dev/random/randomdev_soft.h index 7a2b62e8e76..cbee779a7d0 100644 --- a/sys/dev/random/randomdev_soft.h +++ b/sys/dev/random/randomdev_soft.h @@ -26,6 +26,9 @@ * $FreeBSD$ */ +#ifndef SYS_DEV_RANDOM_RANDOMDEV_SOFT_H_INCLUDED +#define SYS_DEV_RANDOM_RANDOMDEV_SOFT_H_INCLUDED + /* This header contains only those definitions that are global * and harvester-specific for the entropy processor */ @@ -79,3 +82,5 @@ random_check_uint_##name(SYSCTL_HANDLER_ARGS) \ return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, \ req)); \ } + +#endif diff --git a/sys/dev/random/rwfile.h b/sys/dev/random/rwfile.h index 39aba04151d..22ac06d0590 100644 --- a/sys/dev/random/rwfile.h +++ b/sys/dev/random/rwfile.h @@ -26,5 +26,10 @@ * $FreeBSD$ */ +#ifndef SYS_DEV_RANDOM_RWFILE_H_INCLUDED +#define SYS_DEV_RANDOM_RWFILE_H_INCLUDED + int randomdev_read_file(const char *filename, void *buf, size_t); int randomdev_write_file(const char *filename, void *buf, size_t); + +#endif diff --git a/sys/dev/random/yarrow.h b/sys/dev/random/yarrow.h index 0bde5b582d3..f32313ee577 100644 --- a/sys/dev/random/yarrow.h +++ b/sys/dev/random/yarrow.h @@ -26,7 +26,12 @@ * $FreeBSD$ */ +#ifndef SYS_DEV_RANDOM_YARROW_H_INCLUDED +#define SYS_DEV_RANDOM_YARROW_H_INCLUDED + void random_yarrow_init_alg(struct sysctl_ctx_list *); void random_yarrow_deinit_alg(void); int random_yarrow_read(void *, int); void random_yarrow_reseed(void); + +#endif From 85bbc612af20e429501b563bb8a85bf32f4b8cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 9 Oct 2013 09:12:44 +0000 Subject: [PATCH 14/20] comment nit --- sys/dev/random/random_harvestq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 264a3c7415b..61d852b7dfa 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -112,7 +112,7 @@ random_harvestq_cache(void *arg __unused) } /* Read and attempt to overwrite the entropy cache files. - * If the file exists, can be read and then overwritten,i + * If the file exists, can be read and then overwritten, * then use it. Ignore it otherwise, but print out what is * going on. */ From aa1c90971ffaaf6e76703033d04f01a09efed1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 9 Oct 2013 09:13:12 +0000 Subject: [PATCH 15/20] Re-add /dev/urandom for compatibility purposes. --- sys/dev/random/randomdev.c | 1 + 1 file changed, 1 insertion(+) diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index 0593f397ffc..b76cb839a02 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -179,6 +179,7 @@ random_initialize(void *p, struct random_adaptor *s) * with daemons */ random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw, RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random"); + make_dev_alias(random_dev, "urandom"); /* compatibility */ /* mark random(4) as initialized, to avoid being called again */ random_inited = 1; From 6a7ba84834971bc2d46114bed3fdb7023129cdbe Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Wed, 9 Oct 2013 17:50:36 +0000 Subject: [PATCH 16/20] Wrap some policy-rich code in 'if NOTYET' until we can thresh out what it really needs to do. --- sys/dev/random/random_harvestq.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 61d852b7dfa..c491df0fd0b 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -84,6 +84,7 @@ static const char *entropy_files[] = { NULL }; +#ifdef NOTYET /* This is full of policy stuff, needs further discussion */ /* Deal with entropy cached externally if this is present. * Lots of policy may eventually arrive in this function. * Called after / is mounted. @@ -138,6 +139,7 @@ random_harvestq_cache(void *arg __unused) free(data, M_ENTROPY); } EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0); +#endif /* NOTYET */ static void random_kthread(void *arg) From 1b277d38f6b0552ac7b33f948dbecac93287283a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 9 Oct 2013 17:51:38 +0000 Subject: [PATCH 17/20] staticize struct random_hardware_source --- sys/dev/random/ivy.c | 2 +- sys/dev/random/nehemiah.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sys/dev/random/ivy.c b/sys/dev/random/ivy.c index e1a42c4973c..7784e8be3fc 100644 --- a/sys/dev/random/ivy.c +++ b/sys/dev/random/ivy.c @@ -51,7 +51,7 @@ __FBSDID("$FreeBSD$"); static int random_ivy_read(void *, int); -struct random_hardware_source random_ivy = { +static struct random_hardware_source random_ivy = { .ident = "Hardware, Intel IvyBridge+ RNG", .source = RANDOM_PURE_RDRAND, .read = random_ivy_read diff --git a/sys/dev/random/nehemiah.c b/sys/dev/random/nehemiah.c index e134ad0519f..c5235a4cf5e 100644 --- a/sys/dev/random/nehemiah.c +++ b/sys/dev/random/nehemiah.c @@ -51,7 +51,7 @@ static void random_nehemiah_init(void); static void random_nehemiah_deinit(void); static int random_nehemiah_read(void *, int); -struct random_hardware_source random_nehemiah = { +static struct random_hardware_source random_nehemiah = { .ident = "Hardware, VIA Nehemiah Padlock RNG", .source = RANDOM_PURE_NEHEMIAH, .read = random_nehemiah_read From 1b24a3a73b018acee20de3a67332e5afc31c1a75 Mon Sep 17 00:00:00 2001 From: Mark Murray Date: Wed, 9 Oct 2013 18:11:15 +0000 Subject: [PATCH 18/20] Sigh. I can't even get a single #ifdef right. Who's got the hat? --- sys/dev/random/random_harvestq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index c491df0fd0b..84067aaeb71 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -79,12 +79,12 @@ int random_kthread_control = 0; static struct proc *random_kthread_proc; +#ifdef NOTYET /* This is full of policy stuff, needs further discussion */ static const char *entropy_files[] = { "/entropy", NULL }; -#ifdef NOTYET /* This is full of policy stuff, needs further discussion */ /* Deal with entropy cached externally if this is present. * Lots of policy may eventually arrive in this function. * Called after / is mounted. From 6916062296880fe75a64d81fca4b1b20a84cece9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 9 Oct 2013 20:12:59 +0000 Subject: [PATCH 19/20] Define RANDOM_PURE_RNDTEST for rndtest(4). --- sys/sys/random.h | 1 + 1 file changed, 1 insertion(+) diff --git a/sys/sys/random.h b/sys/sys/random.h index 773c7058cde..0f26b8b2b90 100644 --- a/sys/sys/random.h +++ b/sys/sys/random.h @@ -55,6 +55,7 @@ enum esource { RANDOM_PURE_HIFN, RANDOM_PURE_RDRAND, RANDOM_PURE_NEHEMIAH, + RANDOM_PURE_RNDTEST, ENTROPYSOURCE }; void random_harvest(void *, u_int, u_int, enum esource); From 997b0a641dda82f60148c7306d19d94134d59e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Wed, 9 Oct 2013 20:14:16 +0000 Subject: [PATCH 20/20] Add a RANDOM_RWFILE option and hide the entropy cache code behind it. Rename YARROW_RNG and FORTUNA_RNG to RANDOM_YARROW and RANDOM_FORTUNA. Add the RANDOM_* options to LINT. --- sys/conf/NOTES | 5 +++++ sys/conf/options | 5 +++-- sys/dev/random/random_harvestq.c | 15 +++++++++++---- sys/dev/random/randomdev_soft.c | 30 +++++++++++++++--------------- sys/dev/random/rwfile.c | 6 ++++++ sys/dev/random/rwfile.h | 4 ++++ 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/sys/conf/NOTES b/sys/conf/NOTES index 975d3f49208..d4b205cbec8 100644 --- a/sys/conf/NOTES +++ b/sys/conf/NOTES @@ -2962,3 +2962,8 @@ options RCTL options BROOKTREE_ALLOC_PAGES=(217*4+1) options MAXFILES=999 +# Random number generator +options RANDOM_YARROW # Yarrow RNG +##options RANDOM_FORTUNA # Fortuna RNG - not yet implemented +options RANDOM_DEBUG # Debugging messages +options RANDOM_RWFILE # Read and write entropy cache diff --git a/sys/conf/options b/sys/conf/options index 9e244762ac3..ff011448a7f 100644 --- a/sys/conf/options +++ b/sys/conf/options @@ -906,6 +906,7 @@ RACCT opt_global.h RCTL opt_global.h # Random number generator(s) -YARROW_RNG opt_random.h -FORTUNA_RNG opt_random.h +RANDOM_YARROW opt_random.h +RANDOM_FORTUNA opt_random.h RANDOM_DEBUG opt_random.h +RANDOM_RWFILE opt_random.h diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c index 84067aaeb71..471a2661190 100644 --- a/sys/dev/random/random_harvestq.c +++ b/sys/dev/random/random_harvestq.c @@ -30,6 +30,8 @@ #include __FBSDID("$FreeBSD$"); +#include "opt_random.h" + #include #include #include @@ -79,11 +81,12 @@ int random_kthread_control = 0; static struct proc *random_kthread_proc; -#ifdef NOTYET /* This is full of policy stuff, needs further discussion */ +#ifdef RANDOM_RWFILE static const char *entropy_files[] = { "/entropy", NULL }; +#endif /* Deal with entropy cached externally if this is present. * Lots of policy may eventually arrive in this function. @@ -92,10 +95,13 @@ static const char *entropy_files[] = { static void random_harvestq_cache(void *arg __unused) { - const char **entropy_file; - uint8_t *keyfile, *data, *zbuf; + uint8_t *keyfile, *data; size_t size, i; +#ifdef RANDOM_RWFILE + const char **entropy_file; + uint8_t *zbuf; int error; +#endif /* Get stuff that may have been preloaded by loader(8) */ keyfile = preload_search_by_type("/boot/entropy"); @@ -112,6 +118,7 @@ random_harvestq_cache(void *arg __unused) printf("random: no preloaded entropy cache available\n"); } +#ifdef RANDOM_RWFILE /* Read and attempt to overwrite the entropy cache files. * If the file exists, can be read and then overwritten, * then use it. Ignore it otherwise, but print out what is @@ -137,9 +144,9 @@ random_harvestq_cache(void *arg __unused) } bzero(data, PAGE_SIZE); free(data, M_ENTROPY); +#endif } EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0); -#endif /* NOTYET */ static void random_kthread(void *arg) diff --git a/sys/dev/random/randomdev_soft.c b/sys/dev/random/randomdev_soft.c index 9c3aa5366c6..db0b01617b9 100644 --- a/sys/dev/random/randomdev_soft.c +++ b/sys/dev/random/randomdev_soft.c @@ -28,12 +28,12 @@ #include "opt_random.h" -#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" +#if !defined(RANDOM_YARROW) && !defined(RANDOM_FORTUNA) +#define RANDOM_YARROW +#elif defined(RANDOM_YARROW) && defined(RANDOM_FORTUNA) +#error "Must define either RANDOM_YARROW or RANDOM_FORTUNA" #endif -#if defined(FORTUNA_RNG) +#if defined(RANDOM_FORTUNA) #error "Fortuna is not yet implemented" #endif @@ -62,10 +62,10 @@ __FBSDID("$FreeBSD$"); #include #include #include -#if defined(YARROW_RNG) +#if defined(RANDOM_YARROW) #include #endif -#if defined(FORTUNA_RNG) +#if defined(RANDOM_FORTUNA) #include #endif @@ -74,7 +74,7 @@ static int randomdev_poll(int event, struct thread *td); static int randomdev_block(int flag); static void randomdev_flush_reseed(void); -#if defined(YARROW_RNG) +#if defined(RANDOM_YARROW) static struct random_adaptor random_context = { .ident = "Software, Yarrow", .init = randomdev_init, @@ -89,7 +89,7 @@ static struct random_adaptor random_context = { #define RANDOM_CSPRNG_NAME "yarrow" #endif -#if defined(FORTUNA_RNG) +#if defined(RANDOM_FORTUNA) static struct random_adaptor random_context = { .ident = "Software, Fortuna", .init = randomdev_init, @@ -123,10 +123,10 @@ randomdev_init(void) { struct sysctl_oid *random_sys_o, *random_sys_harvest_o; -#if defined(YARROW_RNG) +#if defined(RANDOM_YARROW) random_yarrow_init_alg(&random_clist); #endif -#if defined(FORTUNA_RNG) +#if defined(RANDOM_FORTUNA) random_fortuna_init_alg(&random_clist); #endif @@ -186,10 +186,10 @@ randomdev_deinit(void) random_kthread_control = -1; tsleep((void *)&random_kthread_control, 0, "term", 0); -#if defined(YARROW_RNG) +#if defined(RANDOM_YARROW) random_yarrow_deinit_alg(); #endif -#if defined(FORTUNA_RNG) +#if defined(RANDOM_FORTUNA) random_fortuna_deinit_alg(); #endif @@ -258,11 +258,11 @@ randomdev_flush_reseed(void) while (random_kthread_control) pause("-", hz / 10); -#if defined(YARROW_RNG) +#if defined(RANDOM_YARROW) /* This ultimately calls randomdev_unblock() */ random_yarrow_reseed(); #endif -#if defined(FORTUNA_RNG) +#if defined(RANDOM_FORTUNA) /* This ultimately calls randomdev_unblock() */ random_fortuna_reseed(); #endif diff --git a/sys/dev/random/rwfile.c b/sys/dev/random/rwfile.c index 85afcb03325..9b389579169 100644 --- a/sys/dev/random/rwfile.c +++ b/sys/dev/random/rwfile.c @@ -28,6 +28,10 @@ #include __FBSDID("$FreeBSD$"); +#include "opt_random.h" + +#ifdef RANDOM_RWFILE + #include #include #include @@ -88,3 +92,5 @@ randomdev_write_file(const char *filename, void *buf, size_t length) return (error); } + +#endif diff --git a/sys/dev/random/rwfile.h b/sys/dev/random/rwfile.h index 22ac06d0590..f14fd7b7e96 100644 --- a/sys/dev/random/rwfile.h +++ b/sys/dev/random/rwfile.h @@ -29,7 +29,11 @@ #ifndef SYS_DEV_RANDOM_RWFILE_H_INCLUDED #define SYS_DEV_RANDOM_RWFILE_H_INCLUDED +#ifdef RANDOM_RWFILE + int randomdev_read_file(const char *filename, void *buf, size_t); int randomdev_write_file(const char *filename, void *buf, size_t); #endif + +#endif