Hi Instead of adding a new driver for each USB RNG device here's a diff to combine the existing ualea(4) driver and support for a new device (ChaosKey support by abieber@) into urng(4).
The plan is to merge uonerng(4) into it as well as support for future devices into this single driver. This was tested by abieber@ with a ChaosKey and Sean with an Alea II. OK? Cheers, Jasper Index: share/man/man4/Makefile =================================================================== RCS file: /cvs/src/share/man/man4/Makefile,v retrieving revision 1.656 diff -u -p -r1.656 Makefile --- share/man/man4/Makefile 25 Aug 2017 11:17:35 -0000 1.656 +++ share/man/man4/Makefile 26 Aug 2017 19:44:32 -0000 @@ -61,7 +61,7 @@ MAN= aac.4 ac97.4 acphy.4 \ stp.4 sv.4 switch.4 sym.4 tcic.4 tcp.4 termios.4 tht.4 ti.4 tl.4 \ tlphy.4 thmc.4 tpm.4 tqphy.4 trm.4 trunk.4 tsl.4 tty.4 tun.4 tap.4 \ twe.4 \ - txp.4 txphy.4 ualea.4 uaudio.4 uark.4 uath.4 ubcmtp.4 uberry.4 ubsa.4 \ + txp.4 txphy.4 uaudio.4 uark.4 uath.4 ubcmtp.4 uberry.4 ubsa.4 \ ubsec.4 \ ucom.4 uchcom.4 ucycom.4 uslhcom.4 udav.4 udcf.4 udl.4 udp.4 udsbr.4 \ uftdi.4 ugen.4 ugl.4 ugold.4 uguru.4 uhci.4 uhid.4 uhidev.4 uipaq.4 \ @@ -69,7 +69,7 @@ MAN= aac.4 ac97.4 acphy.4 \ ukphy.4 ulpt.4 umass.4 umb.4 umbg.4 umcs.4 umct.4 umidi.4 umodem.4 \ ums.4 umsm.4 unix.4 uonerng.4 uow.4 uoaklux.4 uoakrh.4 uoakv.4 upd.4 \ upgt.4 upl.4 uplcom.4 ural.4 ure.4 url.4 urlphy.4 \ - urndis.4 urtw.4 urtwn.4 usb.4 uscom.4 uslcom.4 usps.4 \ + urndis.4 urng.4 urtw.4 urtwn.4 usb.4 uscom.4 uslcom.4 usps.4 \ uthum.4 uticom.4 utpms.4 utwitch.4 utrh.4 uts.4 utvfu.4 uvideo.4 \ uvisor.4 uvscom.4 uwacom.4 \ vether.4 vga.4 vgafb.4 vge.4 \ Index: share/man/man4/ualea.4 =================================================================== RCS file: share/man/man4/ualea.4 diff -N share/man/man4/ualea.4 --- share/man/man4/ualea.4 16 Apr 2015 08:56:53 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,54 +0,0 @@ -.\" $OpenBSD: ualea.4,v 1.1 2015/04/16 08:56:53 mpi Exp $ -.\" -.\" Copyright (c) 2007 Marc Balmer <mbal...@openbsd.org> -.\" Copyright (c) 2015 Sean Levy <att...@stalphonsos.com> -.\" -.\" Permission to use, copy, modify, and distribute this software for any -.\" purpose with or without fee is hereby granted, provided that the above -.\" copyright notice and this permission notice appear in all copies. -.\" -.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -.\" -.Dd $Mdocdate: April 16 2015 $ -.Dt UALEA 4 -.Os -.Sh NAME -.Nm ualea -.Nd Araneus Alea II USB TRNG -.Sh SYNOPSIS -.Cd "ualea* at uhub?" -.Sh DESCRIPTION -The -.Nm -driver provides support for the Araneus Alea II, a USB true random -number generator (TRNG). -It delivers 100kbit/sec of hardware-generated entropy. -.Nm -reads raw entropy from the Alea II and uses -.Xr add_true_randomness 9 -to add it to the system entropy pool. -.Pp -The product documentation states that the USB interface used by the -Alea II is the same as that used by its predecessor the Alea I; -theoretically this means that the Alea I should work but this has not -been tested. -.Sh SEE ALSO -.Xr intro 4 , -.Xr usb 4 , -.Xr add_true_randomness 9 -.Sh HISTORY -The -.Nm -driver first appeared in -.Ox 5.7 . -.Sh AUTHORS -The -.Nm -driver was written by -.An Sean Levy Aq Mt att...@stalphonsos.com . Index: share/man/man4/urng.4 =================================================================== RCS file: share/man/man4/urng.4 diff -N share/man/man4/urng.4 --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ share/man/man4/urng.4 26 Aug 2017 20:34:54 -0000 @@ -0,0 +1,63 @@ +.\" $OpenBSD$ +.\" +.\" Copyright (c) 2015 Sean Levy <att...@stalphonsos.com> +.\" Copyright (c) 2017 Jasper Lievisse Adriaanse <jas...@openbsd.org> +.\" +.\" Permission to use, copy, modify, and distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd $Mdocdate$ +.Dt URNG 4 +.Os +.Sh NAME +.Nm urng +.Nd USB Random Number Generator devices +.Sh SYNOPSIS +.Cd "urng* at uhub?" +.Sh DESCRIPTION +The +.Nm +driver provides support for various true random number generator +(TRNG) devices connected via USB. +.Nm +reads raw entropy from the device at a (hardware dependent) interval +and stirs it into the system entropy pool with +.Xr add_true_randomness 9 . +.Sh HARDWARE +The following devices are supported by this driver: +.Pp +.Bl -tag -width "Altusmetrum" +.It Araneus Alea II +Capable of delivering 100kbit/sec of hardware-generated entropy. +The product documentation states that the USB interface used by the +Alea II is the same as that used by its predecessor the Alea I; +theoretically this means that the Alea I should work but this has not +been tested. +.It Altusmetrum ChaosKey 1.0 +This device is capable of providing entropy at 10Mbit/s. +.El +.Sh SEE ALSO +.Xr intro 4 , +.Xr usb 4 , +.Xr add_true_randomness 9 +.Sh HISTORY +The +.Nm +driver first appeared in +.Ox 6.2 . +.Sh AUTHORS +The +.Nm +driver was written by +.An Aaron Bieber Aq Mt abie...@openbsd.org , +.An Jasper Lievisse Adriaanse Aq Mt jas...@openbsd.org , +.An Sean Levy Aq Mt att...@stalphonsos.com . Index: share/man/man4/usb.4 =================================================================== RCS file: /cvs/src/share/man/man4/usb.4,v retrieving revision 1.183 diff -u -p -r1.183 usb.4 --- share/man/man4/usb.4 22 Mar 2017 06:09:31 -0000 1.183 +++ share/man/man4/usb.4 26 Aug 2017 20:10:25 -0000 @@ -288,8 +288,6 @@ USB Mobile Broadband Interface Model (MB .El .Ss Miscellaneous devices .Bl -tag -width 12n -offset ind -compact -.It Xr ualea 4 -Araneus Alea II USB TRNG .It Xr uberry 4 Research In Motion BlackBerry .It Xr ugen 4 @@ -304,6 +302,8 @@ Moonbase Otago OneRNG TRNG Maxim/Dallas DS2490 USB 1-Wire adapter .It Xr upl 4 Prolific based host-to-host adapters +.It Xr urng 4 +USB Random Number Generator devices .It Xr usps 4 USPS composite AC power and temperature sensor .It Xr uts 4 Index: sys/arch/alpha/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/alpha/conf/GENERIC,v retrieving revision 1.259 diff -u -p -r1.259 GENERIC --- sys/arch/alpha/conf/GENERIC 31 Aug 2016 12:46:45 -0000 1.259 +++ sys/arch/alpha/conf/GENERIC 26 Aug 2017 19:43:23 -0000 @@ -62,7 +62,7 @@ usb* at ohci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/amd64/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/amd64/conf/GENERIC,v retrieving revision 1.444 diff -u -p -r1.444 GENERIC --- sys/arch/amd64/conf/GENERIC 14 Jun 2017 22:35:43 -0000 1.444 +++ sys/arch/amd64/conf/GENERIC 26 Aug 2017 19:43:21 -0000 @@ -204,8 +204,9 @@ usb* at ohci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG +urng* at uhub? # USB Random Number Generator umodem* at uhub? # USB Modems/Serial ucom* at umodem? uvisor* at uhub? # Handspring Visor Index: sys/arch/arm64/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/arm64/conf/GENERIC,v retrieving revision 1.35 diff -u -p -r1.35 GENERIC --- sys/arch/arm64/conf/GENERIC 25 Aug 2017 10:29:54 -0000 1.35 +++ sys/arch/arm64/conf/GENERIC 26 Aug 2017 19:43:17 -0000 @@ -133,7 +133,7 @@ usb* at xhci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/armv7/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/armv7/conf/GENERIC,v retrieving revision 1.88 diff -u -p -r1.88 GENERIC --- sys/arch/armv7/conf/GENERIC 14 Aug 2017 21:46:02 -0000 1.88 +++ sys/arch/armv7/conf/GENERIC 26 Aug 2017 19:43:14 -0000 @@ -180,7 +180,7 @@ usb* at ehci? flags 0x1 # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/hppa/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/hppa/conf/GENERIC,v retrieving revision 1.173 diff -u -p -r1.173 GENERIC --- sys/arch/hppa/conf/GENERIC 4 Sep 2016 10:22:06 -0000 1.173 +++ sys/arch/hppa/conf/GENERIC 26 Aug 2017 19:43:10 -0000 @@ -120,7 +120,7 @@ ure* at uhub? # Realtek RTL8152 based a rsu* at uhub? # Realtek RTL8188SU/RTL8191SU/RTL8192SU # untested USB devices -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/i386/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/i386/conf/GENERIC,v retrieving revision 1.828 diff -u -p -r1.828 GENERIC --- sys/arch/i386/conf/GENERIC 18 May 2017 13:29:32 -0000 1.828 +++ sys/arch/i386/conf/GENERIC 26 Aug 2017 19:43:08 -0000 @@ -228,7 +228,7 @@ usb* at ohci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/landisk/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/landisk/conf/GENERIC,v retrieving revision 1.51 diff -u -p -r1.51 GENERIC --- sys/arch/landisk/conf/GENERIC 28 Jun 2016 04:41:37 -0000 1.51 +++ sys/arch/landisk/conf/GENERIC 26 Aug 2017 19:43:05 -0000 @@ -86,7 +86,7 @@ usb* at ohci? # USB Hubs uhub* at usb? uhub* at uhub? port ? configuration ? interface ? -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/loongson/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/loongson/conf/GENERIC,v retrieving revision 1.55 diff -u -p -r1.55 GENERIC --- sys/arch/loongson/conf/GENERIC 21 May 2017 13:00:53 -0000 1.55 +++ sys/arch/loongson/conf/GENERIC 26 Aug 2017 19:43:01 -0000 @@ -119,7 +119,7 @@ usb* at ehci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/macppc/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/macppc/conf/GENERIC,v retrieving revision 1.260 diff -u -p -r1.260 GENERIC --- sys/arch/macppc/conf/GENERIC 12 Sep 2016 08:28:44 -0000 1.260 +++ sys/arch/macppc/conf/GENERIC 26 Aug 2017 19:42:55 -0000 @@ -207,7 +207,7 @@ usb* at ehci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/octeon/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/octeon/conf/GENERIC,v retrieving revision 1.39 diff -u -p -r1.39 GENERIC --- sys/arch/octeon/conf/GENERIC 1 Aug 2017 16:18:11 -0000 1.39 +++ sys/arch/octeon/conf/GENERIC 26 Aug 2017 19:42:50 -0000 @@ -91,7 +91,7 @@ usb* at xhci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/arch/sparc64/conf/GENERIC =================================================================== RCS file: /cvs/src/sys/arch/sparc64/conf/GENERIC,v retrieving revision 1.306 diff -u -p -r1.306 GENERIC --- sys/arch/sparc64/conf/GENERIC 26 Jan 2017 12:18:55 -0000 1.306 +++ sys/arch/sparc64/conf/GENERIC 26 Aug 2017 19:42:27 -0000 @@ -175,7 +175,7 @@ usb* at xhci? # USB devices uhub* at usb? # USB Hubs uhub* at uhub? # USB Hubs -ualea* at uhub? # Araneus Alea II TRNG +urng* at uhub? # USB Random Number Generator uonerng* at uhub? # Moonbase Otago OneRNG umodem* at uhub? # USB Modems/Serial ucom* at umodem? Index: sys/dev/usb/files.usb =================================================================== RCS file: /cvs/src/sys/dev/usb/files.usb,v retrieving revision 1.131 diff -u -p -r1.131 files.usb --- sys/dev/usb/files.usb 12 Sep 2016 08:12:06 -0000 1.131 +++ sys/dev/usb/files.usb 26 Aug 2017 19:40:34 -0000 @@ -167,15 +167,15 @@ attach uoakv at uhidbus file dev/usb/uoakv.c uoakv # Misc -# Araneus Alea II TRNG -device ualea -attach ualea at uhub -file dev/usb/ualea.c ualea - # Moonbase Otago OneRNG TRNG device uonerng attach uonerng at uhub file dev/usb/uonerng.c uonerng + +# USB Random Number Generator +device urng +attach urng at uhub +file dev/usb/urng.c # Gude Expert mouseCLOCK DCF77 time signal station receiver device udcf Index: sys/dev/usb/ualea.c =================================================================== RCS file: sys/dev/usb/ualea.c diff -N sys/dev/usb/ualea.c --- sys/dev/usb/ualea.c 17 Apr 2015 07:17:51 -0000 1.2 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,195 +0,0 @@ -/* $OpenBSD: ualea.c,v 1.2 2015/04/17 07:17:51 mpi Exp $ */ -/* - * Copyright (c) 2006 Alexander Yurchenko <gra...@openbsd.org> - * Copyright (c) 2007 Marc Balmer <mbal...@openbsd.org> - * Copyright (C) 2015 Sean Levy <att...@stalphonsos.com> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Alea II TRNG. Produces 100kbit/sec of entropy by black magic - * - * Product information in English can be found here: - * http://www.araneus.fi/products/alea2/en/ - */ - -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/device.h> -#include <sys/timeout.h> - -#include <dev/usb/usb.h> -#include <dev/usb/usbdi.h> -#include <dev/usb/usbdevs.h> - -#include <dev/rndvar.h> - -#define ALEA_IFACE 0 -#define ALEA_ENDPOINT 1 -#define ALEA_MSECS 100 -#define ALEA_READ_TIMEOUT 5000 -#define ALEA_BUFSIZ 128 - -#define DEVNAME(_sc) ((_sc)->sc_dev.dv_xname) - -struct ualea_softc { - struct device sc_dev; - struct usbd_device *sc_udev; - struct usbd_pipe *sc_pipe; - struct timeout sc_timeout; - struct usb_task sc_task; - struct usbd_xfer *sc_xfer; - int *sc_buf; -}; - -int ualea_match(struct device *, void *, void *); -void ualea_attach(struct device *, struct device *, void *); -int ualea_detach(struct device *, int); -void ualea_task(void *); -void ualea_timeout(void *); - -struct cfdriver ualea_cd = { - NULL, "ualea", DV_DULL -}; - -const struct cfattach ualea_ca = { - sizeof(struct ualea_softc), ualea_match, ualea_attach, ualea_detach -}; - -int -ualea_match(struct device *parent, void *match, void *aux) -{ - struct usb_attach_arg *uaa = aux; - - if (uaa->iface == NULL) - return (UMATCH_NONE); - if ((uaa->vendor == USB_VENDOR_ARANEUS) && - (uaa->product == USB_PRODUCT_ARANEUS_ALEA) && - (uaa->ifaceno == ALEA_IFACE)) - return (UMATCH_VENDOR_PRODUCT); - return (UMATCH_NONE); -} - -void -ualea_attach(struct device *parent, struct device *self, void *aux) -{ - struct ualea_softc *sc = (struct ualea_softc *)self; - struct usb_attach_arg *uaa = aux; - usb_interface_descriptor_t *id; - usb_endpoint_descriptor_t *ed; - int ep_ibulk = -1; - usbd_status error; - int i; - - sc->sc_udev = uaa->device; - id = usbd_get_interface_descriptor(uaa->iface); - for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(uaa->iface, i); - if (ed == NULL) { - printf("%s: failed to get endpoint %d descriptor\n", - DEVNAME(sc), i); - return; - } - if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && - UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK && - UE_GET_ADDR(ed->bEndpointAddress) == ALEA_ENDPOINT) { - ep_ibulk = ed->bEndpointAddress; - break; - } - } - if (ep_ibulk == -1) { - printf("%s: missing endpoint\n", DEVNAME(sc)); - return; - } - error = usbd_open_pipe(uaa->iface, ep_ibulk, USBD_EXCLUSIVE_USE, - &sc->sc_pipe); - if (error) { - printf("%s: failed to open bulk-in pipe: %s\n", - DEVNAME(sc), usbd_errstr(error)); - return; - } - sc->sc_xfer = usbd_alloc_xfer(sc->sc_udev); - if (sc->sc_xfer == NULL) { - printf("%s: could not alloc xfer\n", DEVNAME(sc)); - return; - } - sc->sc_buf = usbd_alloc_buffer(sc->sc_xfer, ALEA_BUFSIZ); - if (sc->sc_buf == NULL) { - printf("%s: could not alloc %d-byte buffer\n", DEVNAME(sc), - ALEA_BUFSIZ); - return; - } - usb_init_task(&sc->sc_task, ualea_task, sc, USB_TASK_TYPE_GENERIC); - timeout_set(&sc->sc_timeout, ualea_timeout, sc); - usb_add_task(sc->sc_udev, &sc->sc_task); -} - -int -ualea_detach(struct device *self, int flags) -{ - struct ualea_softc *sc = (struct ualea_softc *)self; - - usb_rem_task(sc->sc_udev, &sc->sc_task); - if (timeout_initialized(&sc->sc_timeout)) - timeout_del(&sc->sc_timeout); - if (sc->sc_xfer) - usbd_free_xfer(sc->sc_xfer); - if (sc->sc_pipe != NULL) - usbd_close_pipe(sc->sc_pipe); - - return (0); -} - -void -ualea_task(void *arg) -{ - struct ualea_softc *sc = (struct ualea_softc *)arg; - usbd_status error; - u_int32_t len, i; - - usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe, NULL, sc->sc_buf, - ALEA_BUFSIZ, USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS, - ALEA_READ_TIMEOUT, NULL); - error = usbd_transfer(sc->sc_xfer); - if (error) { - printf("%s: xfer failed: %s\n", DEVNAME(sc), - usbd_errstr(error)); - goto bail; - } - usbd_get_xfer_status(sc->sc_xfer, NULL, NULL, &len, NULL); - if (len < sizeof(int)) { - printf("%s: xfer too short (%u bytes) - dropping\n", - DEVNAME(sc), len); - goto bail; - } - len /= sizeof(int); - /* - * A random(|ness) koan: - * children chug entropy like thirsty rhinos - * surfing at the mall - * privacy died in their hands - */ - for (i = 0; i < len; i++) - add_true_randomness(sc->sc_buf[i]); -bail: - timeout_add_msec(&sc->sc_timeout, ALEA_MSECS); -} - -void -ualea_timeout(void *arg) -{ - struct ualea_softc *sc = arg; - - usb_add_task(sc->sc_udev, &sc->sc_task); -} Index: sys/dev/usb/urng.c =================================================================== RCS file: sys/dev/usb/urng.c diff -N sys/dev/usb/urng.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ sys/dev/usb/urng.c 26 Aug 2017 20:19:10 -0000 @@ -0,0 +1,240 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2006 Alexander Yurchenko <gra...@openbsd.org> + * Copyright (c) 2007 Marc Balmer <mbal...@openbsd.org> + * Copyright (C) 2015 Sean Levy <att...@stalphonsos.com> + * Copyright (c) 2017 Aaron Bieber <abie...@openbsd.org> + * Copyright (c) 2017 Jasper Lievisse Adriaanse <jas...@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Universal TRNG driver for a collection of TRNG devices: + * - ChaosKey TRNG + * http://altusmetrum.org/ChaosKey/ + * - Alea II TRNG. Produces 100kbit/sec of entropy by black magic + * http://www.araneus.fi/products/alea2/en/ + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/device.h> +#include <sys/timeout.h> + +#include <dev/usb/usb.h> +#include <dev/usb/usbdi.h> +#include <dev/usb/usbdevs.h> + +#include <dev/rndvar.h> + +#define DEVNAME(_sc) ((_sc)->sc_dev.dv_xname) + +#define URNG_DEBUG 1 +#ifdef URNG_DEBUG +#define DPRINTF(x) printf x +#else +#define DPRINTF(x) +#endif + +struct urng_chip { + int bufsiz; + int endpoint; + int iface; + int msecs; + int read_timeout; +}; + +struct urng_softc { + struct device sc_dev; + struct usbd_device *sc_udev; + struct usbd_pipe *sc_pipe; + struct timeout sc_timeout; + struct usb_task sc_task; + struct usbd_xfer *sc_xfer; + struct urng_chip sc_chip; + int *sc_buf; +}; + +int urng_match(struct device *, void *, void *); +void urng_attach(struct device *, struct device *, void *); +int urng_detach(struct device *, int); +void urng_task(void *); +void urng_timeout(void *); + +struct cfdriver urng_cd = { + NULL, "urng", DV_DULL +}; + +const struct cfattach urng_ca = { + sizeof(struct urng_softc), urng_match, urng_attach, urng_detach +}; + +struct urng_type { + struct usb_devno urng_dev; + struct urng_chip urng_chip; +}; + +static const struct urng_type urng_devs[] = { + { { USB_VENDOR_OPENMOKO2, USB_PRODUCT_OPENMOKO2_CHAOSKEY }, {64, 6, 0, 100, 5000} }, + { { USB_VENDOR_ARANEUS, USB_PRODUCT_ARANEUS_ALEA }, {128, 1, 0, 100, 5000} }, +}; +#define urng_lookup(v, p) ((struct urng_type *)usb_lookup(urng_devs, v, p)) + +int +urng_match(struct device *parent, void *match, void *aux) +{ + struct usb_attach_arg *uaa = aux; + + if (uaa->iface == NULL) + return (UMATCH_NONE); + + if (urng_lookup(uaa->vendor, uaa->product) != NULL) + return (UMATCH_VENDOR_PRODUCT); + + return (UMATCH_NONE); +} + +void +urng_attach(struct device *parent, struct device *self, void *aux) +{ + struct urng_softc *sc = (struct urng_softc *)self; + struct usb_attach_arg *uaa = aux; + usb_interface_descriptor_t *id; + usb_endpoint_descriptor_t *ed; + int ep_ibulk = -1; + usbd_status error; + int i; + + sc->sc_udev = uaa->device; + sc->sc_chip.bufsiz = urng_lookup(uaa->vendor, uaa->product)->urng_chip.bufsiz; + sc->sc_chip.endpoint = urng_lookup(uaa->vendor, uaa->product)->urng_chip.endpoint; + sc->sc_chip.iface = urng_lookup(uaa->vendor, uaa->product)->urng_chip.iface; + sc->sc_chip.msecs = urng_lookup(uaa->vendor, uaa->product)->urng_chip.msecs; + sc->sc_chip.read_timeout = urng_lookup(uaa->vendor, uaa->product)->urng_chip.read_timeout; + + DPRINTF(("%s: bufsiz: %d, endpoint: %d iface: %d, msecs: %d, read_timeout: %d\n", + DEVNAME(sc), + sc->sc_chip.bufsiz, + sc->sc_chip.endpoint, + sc->sc_chip.iface, + sc->sc_chip.msecs, + sc->sc_chip.read_timeout)); + + id = usbd_get_interface_descriptor(uaa->iface); + for (i = 0; i < id->bNumEndpoints; i++) { + ed = usbd_interface2endpoint_descriptor(uaa->iface, i); + if (ed == NULL) { + printf("%s: failed to get endpoint %d descriptor\n", + DEVNAME(sc), i); + return; + } + if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && + UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK && + UE_GET_ADDR(ed->bEndpointAddress) == sc->sc_chip.endpoint) { + ep_ibulk = ed->bEndpointAddress; + break; + } + } + + if (ep_ibulk == -1) { + printf("%s: missing endpoint\n", DEVNAME(sc)); + return; + } + + error = usbd_open_pipe(uaa->iface, ep_ibulk, USBD_EXCLUSIVE_USE, + &sc->sc_pipe); + if (error) { + printf("%s: failed to open bulk-in pipe: %s\n", + DEVNAME(sc), usbd_errstr(error)); + return; + } + + sc->sc_xfer = usbd_alloc_xfer(sc->sc_udev); + if (sc->sc_xfer == NULL) { + printf("%s: could not alloc xfer\n", DEVNAME(sc)); + return; + } + + sc->sc_buf = usbd_alloc_buffer(sc->sc_xfer, sc->sc_chip.bufsiz); + if (sc->sc_buf == NULL) { + printf("%s: could not alloc %d-byte buffer\n", DEVNAME(sc), + sc->sc_chip.bufsiz); + return; + } + + usb_init_task(&sc->sc_task, urng_task, sc, USB_TASK_TYPE_GENERIC); + timeout_set(&sc->sc_timeout, urng_timeout, sc); + usb_add_task(sc->sc_udev, &sc->sc_task); + + return; +} + +int +urng_detach(struct device *self, int flags) +{ + struct urng_softc *sc = (struct urng_softc *)self; + + usb_rem_task(sc->sc_udev, &sc->sc_task); + if (timeout_initialized(&sc->sc_timeout)) + timeout_del(&sc->sc_timeout); + if (sc->sc_xfer) + usbd_free_xfer(sc->sc_xfer); + if (sc->sc_pipe != NULL) + usbd_close_pipe(sc->sc_pipe); + + return (0); +} + + +void +urng_task(void *arg) +{ + struct urng_softc *sc = (struct urng_softc *)arg; + usbd_status error; + u_int32_t len, i; + + usbd_setup_xfer(sc->sc_xfer, sc->sc_pipe, NULL, sc->sc_buf, + sc->sc_chip.bufsiz, USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS, + sc->sc_chip.read_timeout, NULL); + + error = usbd_transfer(sc->sc_xfer); + if (error) { + printf("%s: xfer failed: %s\n", DEVNAME(sc), + usbd_errstr(error)); + goto bail; + } + + usbd_get_xfer_status(sc->sc_xfer, NULL, NULL, &len, NULL); + if (len < sizeof(int)) { + printf("%s: xfer too short (%u bytes) - dropping\n", + DEVNAME(sc), len); + goto bail; + } + + len /= sizeof(int); + for (i = 0; i < len; i++) { + add_true_randomness(sc->sc_buf[i]); + } +bail: + timeout_add_msec(&sc->sc_timeout, sc->sc_chip.msecs); +} + +void +urng_timeout(void *arg) +{ + struct urng_softc *sc = arg; + + usb_add_task(sc->sc_udev, &sc->sc_task); +} -- jasper