From bab65a8da30b265f034009ed7fdb5b342e0ab12c Mon Sep 17 00:00:00 2001 From: enami Date: Sun, 9 Sep 2001 00:48:54 +0000 Subject: [PATCH] Mix random data directly into the pool and increase entropy instead of estimating entropy with polling based timing. --- sys/arch/i386/pci/pchb_rnd.c | 34 ++++++++++++++++++++++++---------- sys/dev/rnd.c | 24 +++++++++++++++++++++++- sys/sys/rnd.h | 8 ++++++-- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/sys/arch/i386/pci/pchb_rnd.c b/sys/arch/i386/pci/pchb_rnd.c index c4b325c23aca..f67f79ad301d 100644 --- a/sys/arch/i386/pci/pchb_rnd.c +++ b/sys/arch/i386/pci/pchb_rnd.c @@ -1,4 +1,4 @@ -/* $NetBSD: pchb_rnd.c,v 1.7 2001/03/30 12:05:02 mycroft Exp $ */ +/* $NetBSD: pchb_rnd.c,v 1.8 2001/09/09 00:48:54 enami Exp $ */ /* * Copyright (c) 2000 Michael Shalayeff @@ -77,8 +77,9 @@ pchb_attach_rnd(struct pchb_softc *sc, struct pci_attach_args *pa) return; } - if ((bus_space_read_1(sc->sc_st, sc->sc_sh, - I82802_RNG_HWST) & I82802_RNG_HWST_PRESENT) == 0) { + reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, + I82802_RNG_HWST); + if ((reg8 & I82802_RNG_HWST_PRESENT) == 0) { /* * Random number generator is not present. */ @@ -86,10 +87,16 @@ pchb_attach_rnd(struct pchb_softc *sc, struct pci_attach_args *pa) } /* Enable the RNG. */ - reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, - I82802_RNG_HWST); bus_space_write_1(sc->sc_st, sc->sc_sh, I82802_RNG_HWST, reg8 | I82802_RNG_HWST_ENABLE); + reg8 = bus_space_read_1(sc->sc_st, sc->sc_sh, + I82802_RNG_HWST); + if ((reg8 & I82802_RNG_HWST_ENABLE) == 0) { + /* + * Couldn't enable the RNG. + */ + return; + } /* Check to see if we can read data from the RNG. */ for (i = 0; i < 1000; i++) { @@ -133,8 +140,13 @@ pchb_attach_rnd(struct pchb_softc *sc, struct pci_attach_args *pa) callout_init(&sc->sc_rnd_ch); rnd_attach_source(&sc->sc_rnd_source, - sc->sc_dev.dv_xname, RND_TYPE_UNKNOWN, 0); /* XXX */ - sc->sc_rnd_i = 4; + sc->sc_dev.dv_xname, RND_TYPE_RNG, + /* + * We can't estimate entropy since we poll + * random data periodically. + */ + RND_FLAG_NO_ESTIMATE); + sc->sc_rnd_i = sizeof(sc->sc_rnd_ax); pchb_rnd_callout(sc); break; default: @@ -150,11 +162,13 @@ pchb_rnd_callout(void *v) if ((bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_RNGST) & I82802_RNG_RNGST_DATAV) != 0) { - sc->sc_rnd_ax = (sc->sc_rnd_ax << 8) | + sc->sc_rnd_ax = (sc->sc_rnd_ax << NBBY) | bus_space_read_1(sc->sc_st, sc->sc_sh, I82802_RNG_DATA); if (--sc->sc_rnd_i == 0) { - sc->sc_rnd_i = 4; - rnd_add_uint32(&sc->sc_rnd_source, sc->sc_rnd_ax); + sc->sc_rnd_i = sizeof(sc->sc_rnd_ax); + rnd_add_data(&sc->sc_rnd_source, &sc->sc_rnd_ax, + sizeof(sc->sc_rnd_ax), + sizeof(sc->sc_rnd_ax) * NBBY); } } callout_reset(&sc->sc_rnd_ch, 1, pchb_rnd_callout, sc); diff --git a/sys/dev/rnd.c b/sys/dev/rnd.c index 1157536a917e..701d3520c2c7 100644 --- a/sys/dev/rnd.c +++ b/sys/dev/rnd.c @@ -1,4 +1,4 @@ -/* $NetBSD: rnd.c,v 1.23 2001/09/09 00:32:52 enami Exp $ */ +/* $NetBSD: rnd.c,v 1.24 2001/09/09 00:48:55 enami Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -874,6 +874,28 @@ rnd_add_uint32(rndsource_element_t *rs, u_int32_t val) rst->state = rnd_sample_allocate_isr(rst); } +void +rnd_add_data(rndsource_element_t *rs, void *data, u_int32_t len, + u_int32_t entropy) +{ + rndsource_t *rst; + + /* Mix in the random data directly into the pool. */ + rndpool_add_data(&rnd_pool, data, len, entropy); + + if (rs != NULL) { + rst = &rs->data; + rst->total += entropy; + + if ((rst->flags & RND_FLAG_NO_ESTIMATE) == 0) + /* Estimate entropy using timing information */ + rnd_add_uint32(rs, *(u_int8_t *)data); + } + + /* Wake up any potential readers since we've just added some data. */ + rnd_wakeup_readers(); +} + /* * Timeout, run to process the events in the ring buffer. Only one of these * can possibly be running at a time, run at splsoftclock(). diff --git a/sys/sys/rnd.h b/sys/sys/rnd.h index 81543a9a7e8f..6d69d2231973 100644 --- a/sys/sys/rnd.h +++ b/sys/sys/rnd.h @@ -1,4 +1,4 @@ -/* $NetBSD: rnd.h,v 1.13 2001/09/09 00:32:52 enami Exp $ */ +/* $NetBSD: rnd.h,v 1.14 2001/09/09 00:48:55 enami Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -120,7 +120,9 @@ typedef struct { #define RND_TYPE_NET 2 /* source is a network device */ #define RND_TYPE_TAPE 3 /* source is a tape drive */ #define RND_TYPE_TTY 4 /* source is a tty device */ -#define RND_TYPE_MAX 4 /* last type id used */ +#define RND_TYPE_RNG 5 /* source is a random number + generator */ +#define RND_TYPE_MAX 5 /* last type id used */ #ifdef _KERNEL typedef struct __rndsource_element rndsource_element_t; @@ -152,6 +154,8 @@ int rndpool_extract_data __P((rndpool_t *, void *, u_int32_t, void rnd_init __P((void)); void rnd_add_uint32 __P((rndsource_element_t *, u_int32_t)); +void rnd_add_data __P((rndsource_element_t *, void *, u_int32_t, + u_int32_t)); int rnd_extract_data __P((void *, u_int32_t, u_int32_t)); void rnd_attach_source __P((rndsource_element_t *, char *, u_int32_t, u_int32_t));