Date: Fri, 15 Jul 2016 12:32:30 +0800 From: Jia-Shiun Li <jiashiun@gmail.com> To: "Jukka A. Ukkonen" <jau789@gmail.com> Cc: "freebsd-arm@freebsd.org" <freebsd-arm@freebsd.org> Subject: Re: Random number generator on rpi Message-ID: <CAHNYxxPQtyXZU4%2ByNAVEDPu60mpK87O-4xcCuniCW576-meY0g@mail.gmail.com> In-Reply-To: <5d8ec4d4-4c36-139d-6102-4fdb200fdf65@gmail.com> References: <CAHNYxxP8fqEdXsL2Jwu-=P4g9_w5P2pnJ-yOvtU1ssEP4ZpqCQ@mail.gmail.com> <5d8ec4d4-4c36-139d-6102-4fdb200fdf65@gmail.com>
index | next in thread | previous in thread | raw e-mail
[-- Attachment #1 --]
sorry for replying so late. Turns out I need to attach rndtest device to
hook it on. Updated patch attached. Also commented some code lines in
rndtest to print report messages. You should be able to see repeated kernel
messages like below:
bcmrng0: rndtest: runs pass zeros interval 1 (2343 < 2543 < 2657)
bcmrng0: rndtest: runs pass zeros interval 2 (1135 < 1255 < 1365)
bcmrng0: rndtest: runs pass zeros interval 3 (542 < 624 < 708)
bcmrng0: rndtest: runs pass zeros interval 4 (251 < 301 < 373)
bcmrng0: rndtest: runs pass zeros interval 5 (111 < 158 < 201)
bcmrng0: rndtest: runs pass zeros interval 6 (111 < 149 < 201)
bcmrng0: rndtest: runs pass ones interval 1 (2343 < 2535 < 2657)
bcmrng0: rndtest: runs pass ones interval 2 (1135 < 1265 < 1365)
bcmrng0: rndtest: runs pass ones interval 3 (542 < 576 < 708)
bcmrng0: rndtest: runs pass ones interval 4 (251 < 315 < 373)
bcmrng0: rndtest: runs pass ones interval 5 (111 < 185 < 201)
bcmrng0: rndtest: runs pass ones interval 6 (111 < 153 < 201)
bcmrng0: rndtest: chi^2(4): pass (sum 1570182)
bcmrng0: rndtest: longruns pass (15 ones, 12 zeros)
by the rndtest result, guess I can safely conclude the hardware rng working
correctly?
On Thu, Jun 9, 2016 at 3:53 PM, Jukka A. Ukkonen <jau789@gmail.com> wrote:
>
> So, does this somehow indicate that fortuna has attached the
> new random device as a source of true randomness?
>
> root@rpi2:~ # sysctl kern.random
> kern.random.fortuna.minpoolsize: 64
> kern.random.harvest.mask_symbolic:
>
> [UMA],[FS_ATIME],SWI,INTERRUPT,NET_NG,NET_ETHER,NET_TUN,MOUSE,KEYBOARD,ATTACH,CACHED
> kern.random.harvest.mask_bin: 00111111111
> kern.random.harvest.mask: 511
> kern.random.random_sources:
>
The mask only reports environmental sources, not hardware rng sources.
-Jia-Shiun.
[-- Attachment #2 --]
Index: arm/broadcom/bcm2835/bcm2835_rng.c
===================================================================
--- arm/broadcom/bcm2835/bcm2835_rng.c (nonexistent)
+++ arm/broadcom/bcm2835/bcm2835_rng.c (working copy)
@@ -0,0 +1,198 @@
+/*-
+ * Copyright (c) 2016 Jia-Shiun Li <jiashiun@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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 "opt_bcmrng.h"
+
+#include <sys/cdefs.h>
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/watchdog.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/rman.h>
+#include <sys/conf.h>
+#include <sys/malloc.h>
+#include <sys/random.h>
+
+#include <dev/random/randomdev.h>
+#include <dev/fdt/fdt_common.h>
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+
+#include <machine/bus.h>
+#include <machine/cpufunc.h>
+#include <machine/machdep.h>
+
+#ifdef BCMRNG_RNDTEST
+#include <dev/rndtest/rndtest.h>
+#endif
+
+#define READ(_sc, _r) bus_space_read_4((_sc)->bst, (_sc)->bsh, (_r))
+#define WRITE(_sc, _r, _v) bus_space_write_4((_sc)->bst, (_sc)->bsh, (_r), (_v))
+
+#define RNG_REG_CTRL 0x00
+#define RNG_REG_STATUS 0x04
+#define RNG_REG_DATA 0x08
+
+struct bcmrng_softc {
+ device_t dev;
+ struct resource * res;
+ bus_space_tag_t bst;
+ bus_space_handle_t bsh;
+
+ struct rndtest_state *sc_rndtest; /* RNG test state */
+ void (*sc_harvest)(struct rndtest_state *,
+ void *, u_int);
+ struct callout sc_callout;
+};
+
+static uint32_t bcmrng_read(struct bcmrng_softc* sc)
+{
+ return READ(sc, RNG_REG_DATA);
+}
+
+#define RNG_READ_SIZE 4
+
+static void bcmrng_harvest(struct rndtest_state *state, void *buf, u_int count)
+{
+ random_harvest_queue(buf, count,
+ count * 8 / 2, RANDOM_PURE_BCMRNG);
+}
+
+static void bcmrng_do_rng(void* arg)
+{
+ struct bcmrng_softc* sc = arg;
+ uint32_t entropy[RNG_READ_SIZE];
+ int i;
+
+ for (i = 0; i < RNG_READ_SIZE; i++) {
+ entropy[i] = bcmrng_read(sc);
+ if (0 == READ(sc, RNG_REG_STATUS) >> 24) {
+ i++;
+ break;
+ }
+ }
+
+ (sc->sc_harvest)(sc->sc_rndtest, entropy, sizeof(entropy[0]) * i);
+
+ callout_reset(&sc->sc_callout, hz * 1, bcmrng_do_rng, sc);
+}
+
+
+static int
+bcmrng_probe(device_t dev)
+{
+
+ if (!ofw_bus_status_okay(dev))
+ return (ENXIO);
+
+ if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-rng")) {
+ device_set_desc(dev, "BCM2708/2835 random number generator");
+ return (BUS_PROBE_DEFAULT);
+ }
+
+ return (ENXIO);
+}
+
+static int
+bcmrng_attach(device_t dev)
+{
+ struct bcmrng_softc *sc;
+ int rid;
+
+ sc = device_get_softc(dev);
+ sc->dev = dev;
+
+ rid = 0;
+ sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
+ if (sc->res == NULL) {
+ device_printf(dev, "could not allocate memory resource\n");
+ return (ENXIO);
+ }
+
+ sc->bst = rman_get_bustag(sc->res);
+ sc->bsh = rman_get_bushandle(sc->res);
+
+ /* enable and warn up */
+ WRITE(sc, RNG_REG_STATUS, 0x40000);
+ WRITE(sc, RNG_REG_CTRL, 0x01);
+
+#ifdef BCMRNG_RNDTEST
+ sc->sc_rndtest = rndtest_attach(sc->dev);
+ if (sc->sc_rndtest) {
+ printf("RNDTEST ATTACHED!\n");
+ sc->sc_harvest = rndtest_harvest;
+ } else {
+ printf("NO RNDTEST AVAILABLE!\n");
+ sc->sc_harvest = bcmrng_harvest;
+ }
+#else
+ sc->sc_harvest = bcmrng_harvest;
+#endif
+ /* init callout */
+ callout_init(&sc->sc_callout, 1);
+ callout_reset(&sc->sc_callout, hz * 1, bcmrng_do_rng, sc);
+
+ return (0);
+}
+
+static int
+bcmrng_detach(device_t dev)
+{
+ struct bcmrng_softc *sc;
+
+ sc = device_get_softc(dev);
+ callout_stop(&sc->sc_callout);
+#ifdef BCMRNG_RNDTEST
+ rndtest_detach(sc->sc_rndtest);
+#endif
+ bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->res),
+ sc->res);
+ return 0;
+}
+
+static device_method_t bcmrng_methods[] = {
+ DEVMETHOD(device_probe, bcmrng_probe),
+ DEVMETHOD(device_attach, bcmrng_attach),
+ DEVMETHOD(device_detach, bcmrng_detach),
+
+ DEVMETHOD_END
+};
+
+static driver_t bcmrng_driver = {
+ "bcmrng",
+ bcmrng_methods,
+ sizeof(struct bcmrng_softc),
+};
+
+static devclass_t bcmrng_devclass;
+
+DRIVER_MODULE(bcmrng, simplebus, bcmrng_driver, bcmrng_devclass, 0, 0);
+#ifdef BCMRNG_RNDTEST
+MODULE_DEPEND(bcmrng, rndtest, 1, 1, 1);
+#endif
Property changes on: arm/broadcom/bcm2835/bcm2835_rng.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+FreeBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: arm/broadcom/bcm2835/files.bcm283x
===================================================================
--- arm/broadcom/bcm2835/files.bcm283x (revision 302306)
+++ arm/broadcom/bcm2835/files.bcm283x (working copy)
@@ -10,6 +10,7 @@
arm/broadcom/bcm2835/bcm2835_intr.c standard
arm/broadcom/bcm2835/bcm2835_machdep.c standard
arm/broadcom/bcm2835/bcm2835_mbox.c standard
+arm/broadcom/bcm2835/bcm2835_rng.c standard
arm/broadcom/bcm2835/bcm2835_sdhci.c optional sdhci
arm/broadcom/bcm2835/bcm2835_spi.c optional bcm2835_spi
arm/broadcom/bcm2835/bcm2835_vcio.c standard
Index: arm/conf/RPI2
===================================================================
--- arm/conf/RPI2 (revision 302306)
+++ arm/conf/RPI2 (working copy)
@@ -55,6 +55,8 @@
options ROOTDEVNAME=\"ufs:mmcsd0s2\"
+device rndtest
+options BCMRNG_RNDTEST
# ARM Generic Timer
device generic_timer
Index: boot/fdt/dts/arm/bcm2835.dtsi
===================================================================
--- boot/fdt/dts/arm/bcm2835.dtsi (revision 302306)
+++ boot/fdt/dts/arm/bcm2835.dtsi (working copy)
@@ -101,6 +101,12 @@
*/
};
+ rng {
+ compatible = "broadcom,bcm2835-rng",
+ "broadcom,bcm2708-rng";
+ reg = <0x104000 0x10>;
+ };
+
timer {
compatible = "broadcom,bcm2835-system-timer",
"broadcom,bcm2708-system-timer";
Index: boot/fdt/dts/arm/bcm2836.dtsi
===================================================================
--- boot/fdt/dts/arm/bcm2836.dtsi (revision 302306)
+++ boot/fdt/dts/arm/bcm2836.dtsi (working copy)
@@ -112,6 +112,12 @@
*/
};
+ rng {
+ compatible = "broadcom,bcm2835-rng",
+ "broadcom,bcm2708-rng";
+ reg = <0x104000 0x10>;
+ };
+
watchdog0 {
compatible = "broadcom,bcm2835-wdt",
"broadcom,bcm2708-wdt";
Index: conf/options
===================================================================
--- conf/options (revision 302306)
+++ conf/options (working copy)
@@ -756,6 +756,9 @@
SAFE_NO_RNG opt_safe.h
SAFE_RNDTEST opt_safe.h
+# options for bcmrng driver
+BCMRNG_RNDTEST opt_bcmrng.h
+
# syscons/vt options
MAXCONS opt_syscons.h
SC_ALT_MOUSE_IMAGE opt_syscons.h
Index: dev/rndtest/rndtest.c
===================================================================
--- dev/rndtest/rndtest.c (revision 302306)
+++ dev/rndtest/rndtest.c (working copy)
@@ -169,8 +169,10 @@
if (rndtest_verbose == 0)
return;
+#if 0 /* see what succeeded run will report */
if (!failure && rndtest_verbose == 1) /* don't report successes */
return;
+#endif
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
Index: sys/random.h
===================================================================
--- sys/random.h (revision 302306)
+++ sys/random.h (working copy)
@@ -90,6 +90,7 @@
RANDOM_PURE_NEHEMIAH,
RANDOM_PURE_RNDTEST,
RANDOM_PURE_VIRTIO,
+ RANDOM_PURE_BCMRNG,
ENTROPYSOURCE
};
home |
help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAHNYxxPQtyXZU4%2ByNAVEDPu60mpK87O-4xcCuniCW576-meY0g>
