Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-17 Thread Martin Pieuchot
On 16/04/15(Thu) 16:06, attila wrote:
 [...] 
 After this was committed I received a critique of the driver from the
 person behind the Alea II (Andreas Gustafsson) who made a few pretty
 good points.  He felt trying to pull all the entropy off of the device
 that would theoretically be available every second was a losing
 strategy for several reasons.  Most importantly I ended up calling
 add_true_randomness() in bursts of 3200 calls every trip through
 ualea_task() whereas rnd_event_space[] in rnd.c only has 64 entries on
 a 32bit machine (42 on amd64); this almost surely means that the vast
 majority of my calls are no-ops... not so useful, it appears.
 
 The attached diff cranks the buffer size way down and now we call
 add_true_randomness() 32 times every 100ms.  When I crank ALEA_MSECS
 below 100ms I start to notice the load increases on the machine with
 an Alea II plugged in.  I guess this is because the stuff that happens
 in ualea_task() happens in the context of a process and that process
 always appears to be runnable when ALEA_MSECS is e.g. 10ms.  I crank
 the read timeout up to 5000ms because that's what he recommends in his
 sample code; under normal circumstances we never time out.
 
 The diff also explicitly looks for endpoint #1 because that's the
 endpoint that Andreas says to use, not necc. the first one that I
 find; as it turns out now the first one I find is the right one but
 just to be safe it's better to check explicitly.
 
 Maybe now this is closer to production-worthy.  All feedback and
 comments most welcome.

Applied, thanks.

Just one note, I had to apply your diff by hand because your MUA changes
the tab into space.  If you can change that for the next diff that would
be great!



Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-16 Thread Martin Pieuchot
On 15/04/15(Wed) 10:46, attila wrote:
 Martin Pieuchot m...@openbsd.org writes:
 
  On 14/04/15(Tue) 15:22, attila wrote:
  Martin Pieuchot m...@openbsd.org writes:
   
   static const struct usb_devno ualea_devs[] = {
   { USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
   };
  
   Is it possible to match your device based on the content of the device
   descriptor instead of whitelisting IDs?  Whitelisting means that if the
   company produce a compatible device with a new ID we'll need to modify
   the driver.
  
  
  Sadly, I don't think it is possible... you mean by looking at
  bDeviceClass/bDeviceSubClass/bDeviceProtocol?  He only gives me zeroes
  [...]
  Perhaps I am misunderstanding; is there something else in there I
  could/should match on?  I've changed the attach routine in the updated
  version to check vendor/product/iface, at least.
 
  I was thinking at bInterfaceClass and bInterfaceProtocol but when they
  are both to vendor defined (255), matching against the ID is the right
  thing to do. 
 
  I looked and it appears that M_ZERO is used when allocating the memory
  for all of these structures, so I take it that explicit zeroing of
  things when tearing down is neither required nor desired?  I removed
  this kind of thing from ualea.c because it looked like it wasn't
  necessary.
 
  That's right.
 
  I'm attaching the updated driver.
  
  Thank you for the critique.  I suppose I need to write a man page for
  this as well... working on it.
 
  Perfect, I'll put it in tree together with your driver :)
 
 
 Man page attached.  I am also attaching an updated copy of the driver
 (only copyright changed).
 
 Thanks so much for your help.  I look forward to contributing more.

Committed thanks.  I removed some unneeded headers and applied jmc@'s
tweak.

In the future I'd suggest you to send unified diff against the CVS tree,
it's easier to apply  test!

Cheers,
Martin



Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-16 Thread attila

Martin Pieuchot m...@openbsd.org writes:

 On 15/04/15(Wed) 10:46, attila wrote:
 Martin Pieuchot m...@openbsd.org writes:
 
  On 14/04/15(Tue) 15:22, attila wrote:
  Martin Pieuchot m...@openbsd.org writes:
   
   static const struct usb_devno ualea_devs[] = {
  { USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
   };
  
   Is it possible to match your device based on the content of the device
   descriptor instead of whitelisting IDs?  Whitelisting means that if the
   company produce a compatible device with a new ID we'll need to modify
   the driver.
  
  
  Sadly, I don't think it is possible... you mean by looking at
  bDeviceClass/bDeviceSubClass/bDeviceProtocol?  He only gives me zeroes
  [...]
  Perhaps I am misunderstanding; is there something else in there I
  could/should match on?  I've changed the attach routine in the updated
  version to check vendor/product/iface, at least.
 
  I was thinking at bInterfaceClass and bInterfaceProtocol but when they
  are both to vendor defined (255), matching against the ID is the right
  thing to do. 
 
  I looked and it appears that M_ZERO is used when allocating the memory
  for all of these structures, so I take it that explicit zeroing of
  things when tearing down is neither required nor desired?  I removed
  this kind of thing from ualea.c because it looked like it wasn't
  necessary.
 
  That's right.
 
  I'm attaching the updated driver.
  
  Thank you for the critique.  I suppose I need to write a man page for
  this as well... working on it.
 
  Perfect, I'll put it in tree together with your driver :)
 
 
 Man page attached.  I am also attaching an updated copy of the driver
 (only copyright changed).
 
 Thanks so much for your help.  I look forward to contributing more.

 Committed thanks.  I removed some unneeded headers and applied jmc@'s
 tweak.

 In the future I'd suggest you to send unified diff against the CVS tree,
 it's easier to apply  test!

After this was committed I received a critique of the driver from the
person behind the Alea II (Andreas Gustafsson) who made a few pretty
good points.  He felt trying to pull all the entropy off of the device
that would theoretically be available every second was a losing
strategy for several reasons.  Most importantly I ended up calling
add_true_randomness() in bursts of 3200 calls every trip through
ualea_task() whereas rnd_event_space[] in rnd.c only has 64 entries on
a 32bit machine (42 on amd64); this almost surely means that the vast
majority of my calls are no-ops... not so useful, it appears.

The attached diff cranks the buffer size way down and now we call
add_true_randomness() 32 times every 100ms.  When I crank ALEA_MSECS
below 100ms I start to notice the load increases on the machine with
an Alea II plugged in.  I guess this is because the stuff that happens
in ualea_task() happens in the context of a process and that process
always appears to be runnable when ALEA_MSECS is e.g. 10ms.  I crank
the read timeout up to 5000ms because that's what he recommends in his
sample code; under normal circumstances we never time out.

The diff also explicitly looks for endpoint #1 because that's the
endpoint that Andreas says to use, not necc. the first one that I
find; as it turns out now the first one I find is the right one but
just to be safe it's better to check explicitly.

Maybe now this is closer to production-worthy.  All feedback and
comments most welcome.


 Cheers,
 Martin

Pax, -A
--
att...@stalphonsos.com | http://trac.haqistan.net/~attila
keyid E6CC1EDB | 4D91 1B98 A210 1D71 2A0E  AC29 9677 D0A6 E6CC 1EDB

Index: ualea.c
===
RCS file: /cvs/src/sys/dev/usb/ualea.c,v
retrieving revision 1.1
diff -u -p -r1.1 ualea.c
--- ualea.c 16 Apr 2015 08:55:21 -  1.1
+++ ualea.c 16 Apr 2015 20:34:59 -
@@ -36,9 +36,10 @@
 #include dev/rndvar.h
 
 #define ALEA_IFACE 0
+#define ALEA_ENDPOINT  1
 #define ALEA_MSECS 100
-#define ALEA_READ_TIMEOUT  1000
-#define ALEA_BUFSIZ((1024/8)*100)  /* 100 kbits */
+#define ALEA_READ_TIMEOUT  5000
+#define ALEA_BUFSIZ128
 
 #define DEVNAME(_sc) ((_sc)-sc_dev.dv_xname)
 
@@ -101,7 +102,8 @@ ualea_attach(struct device *parent, stru
return;
}
if (UE_GET_DIR(ed-bEndpointAddress) == UE_DIR_IN 
-   UE_GET_XFERTYPE(ed-bmAttributes) == UE_BULK) {
+   UE_GET_XFERTYPE(ed-bmAttributes) == UE_BULK 
+   UE_GET_ADDR(ed-bEndpointAddress) == ALEA_ENDPOINT) {
ep_ibulk = ed-bEndpointAddress;
break;
}


Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-15 Thread attila
Martin Pieuchot m...@openbsd.org writes:

 On 14/04/15(Tue) 15:22, attila wrote:
 Martin Pieuchot m...@openbsd.org writes:
  
  static const struct usb_devno ualea_devs[] = {
{ USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
  };
 
  Is it possible to match your device based on the content of the device
  descriptor instead of whitelisting IDs?  Whitelisting means that if the
  company produce a compatible device with a new ID we'll need to modify
  the driver.
 
 
 Sadly, I don't think it is possible... you mean by looking at
 bDeviceClass/bDeviceSubClass/bDeviceProtocol?  He only gives me zeroes
 [...]
 Perhaps I am misunderstanding; is there something else in there I
 could/should match on?  I've changed the attach routine in the updated
 version to check vendor/product/iface, at least.

 I was thinking at bInterfaceClass and bInterfaceProtocol but when they
 are both to vendor defined (255), matching against the ID is the right
 thing to do. 

 I looked and it appears that M_ZERO is used when allocating the memory
 for all of these structures, so I take it that explicit zeroing of
 things when tearing down is neither required nor desired?  I removed
 this kind of thing from ualea.c because it looked like it wasn't
 necessary.

 That's right.

 I'm attaching the updated driver.
 
 Thank you for the critique.  I suppose I need to write a man page for
 this as well... working on it.

 Perfect, I'll put it in tree together with your driver :)


Man page attached.  I am also attaching an updated copy of the driver
(only copyright changed).

Thanks so much for your help.  I look forward to contributing more.

Pax, -A
--
att...@stalphonsos.com | http://trac.haqistan.net/~attila
keyid E6CC1EDB | 4D91 1B98 A210 1D71 2A0E  AC29 9677 D0A6 E6CC 1EDB

.\ $OpenBSD$
.\
.\ Copyright (c) 2007 Marc Balmer mbal...@openbsd.org
.\ Copyright (c) 2015 attila 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$
.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 attila Aq Mt att...@stalphonsos.com .
/*  $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
 *
 * 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/kernel.h
#include sys/timeout.h
#include dev/usb/usb.h
#include dev/usb/usbdevs.h
#include dev/usb/usbdi.h
#include dev/usb/usbdi_util.h
#include dev/rndvar.h

#define ALEA_IFACE  0
#define ALEA_MSECS  100
#define ALEA_READ_TIMEOUT   1000
#define ALEA_BUFSIZ ((1024/8)*100)  /* 100 kbits */

#define DEVNAME(_sc) 

Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-15 Thread Martin Pieuchot
On 14/04/15(Tue) 15:22, attila wrote:
 Martin Pieuchot m...@openbsd.org writes:
  
  static const struct usb_devno ualea_devs[] = {
 { USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
  };
 
  Is it possible to match your device based on the content of the device
  descriptor instead of whitelisting IDs?  Whitelisting means that if the
  company produce a compatible device with a new ID we'll need to modify
  the driver.
 
 
 Sadly, I don't think it is possible... you mean by looking at
 bDeviceClass/bDeviceSubClass/bDeviceProtocol?  He only gives me zeroes
 [...]
 Perhaps I am misunderstanding; is there something else in there I
 could/should match on?  I've changed the attach routine in the updated
 version to check vendor/product/iface, at least.

I was thinking at bInterfaceClass and bInterfaceProtocol but when they
are both to vendor defined (255), matching against the ID is the right
thing to do. 

 I looked and it appears that M_ZERO is used when allocating the memory
 for all of these structures, so I take it that explicit zeroing of
 things when tearing down is neither required nor desired?  I removed
 this kind of thing from ualea.c because it looked like it wasn't
 necessary.

That's right.

 I'm attaching the updated driver.
 
 Thank you for the critique.  I suppose I need to write a man page for
 this as well... working on it.

Perfect, I'll put it in tree together with your driver :)

 
 Pax, -A
 --
 att...@stalphonsos.com | http://trac.haqistan.net/~attila
 keyid E6CC1EDB | 4D91 1B98 A210 1D71 2A0E  AC29 9677 D0A6 E6CC 1EDB
 
 

 /*$OpenBSD$ */
 /*
  * Copyright (c) 2006 Alexander Yurchenko gra...@openbsd.org
  * Copyright (c) 2007 Marc Balmer mbal...@openbsd.org
  * Copyright (C) 2015 attila 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/kernel.h
 #include sys/timeout.h
 #include dev/usb/usb.h
 #include dev/usb/usbdevs.h
 #include dev/usb/usbdi.h
 #include dev/usb/usbdi_util.h
 #include dev/rndvar.h
 
 #define ALEA_IFACE0
 #define ALEA_MSECS100
 #define ALEA_READ_TIMEOUT 1000
 #define ALEA_BUFSIZ   ((1024/8)*100)  /* 100 kbits */
 
 #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 

Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-14 Thread Martin Pieuchot
On 14/04/15(Tue) 07:40, attila wrote:
 [...]
 Feedback most welcome.

See below.

 /* -*- mode:c; tab-width:8; indent-tabs-mode:t; c-basic-offset:8 -*- */

We do not include editor settings in files, the first line should
contain:

/*  $OpenBSD$ */

Which will be expanded by CVS.

 /*
  * Copyright (c) 2006 Alexander Yurchenko gra...@openbsd.org
  * Copyright (c) 2007 Marc Balmer mbal...@openbsd.org
  * Copyright (C) 2015 attila 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/
  *
  * I only have an Alea II to play with but the documentation says
  * that the Alea I is the same, so they should also work.
  *
  * I cribbed liberally from both the uow and umbg drivers, both of
  * which are similar to this situation in different ways.

The two last paragraphs do not add much information, think about what will
help you when you'll have to read this code again in a couple of years
:)

  */
 
 #include sys/param.h
 #include sys/systm.h
 #include sys/device.h
 #include sys/kernel.h
 #include sys/time.h

I believe you need sys/time.h just to make sure your driver works as
expected.  You kept this code as #ifdef ALEA_DEBUG but does it really
help to debug something?  Do you think it's worth keeping this code?
We try to not add too much verbosity to driver code.

 #include sys/timeout.h
 
 #include dev/usb/usb.h
 #include dev/usb/usbdevs.h
 #include dev/usb/usbdi.h
 #include dev/usb/usbdi_util.h
 
 #include dev/rndvar.h
 
 #define ALEA_IFACE0
 #define ALEA_MSECS10

How did you choose 10msec?  

 #define ALEA_READ_TOUT1100

Compared to a transfer timeout of 1,1 second is seems very short.  By
the way we generally spell timeout TIMEOUT :o)

 #define ALEA_BUFSIZ   ((1024/8)*100)  /* 100 kbits */
 /*#define ALEA_DEBUG  1*/ /* comment out */
 
 #define OURNAME(x)x-sc_dev.dv_xname

We generally use DEVNAME(), look at how it is defined :) 

 
 struct ualea_softc {
   struct  device sc_dev;
   struct  usbd_device *sc_udev;
   struct  usbd_interface *sc_iface;
   struct  usbd_pipe *sc_ibulk;
   struct  timeout sc_tout;
   struct  usb_task sc_task;
 #ifdef ALEA_DEBUG
   struct  timespec sc_tattach;
   u_int32_t sc_nbits;
 #endif
 };
 
 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_intr(void *);
 
 struct cfdriver ualea_cd = {
   NULL, ualea, DV_DULL
 };
 
 const struct cfattach ualea_ca = {
   sizeof(struct ualea_softc),
   ualea_match,
   ualea_attach,
   ualea_detach
 };
 
 static const struct usb_devno ualea_devs[] = {
   { USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
 };

Is it possible to match your device based on the content of the device
descriptor instead of whitelisting IDs?  Whitelisting means that if the
company produce a compatible device with a new ID we'll need to modify
the driver.

 int
 ualea_match(struct device *parent, void *match, void *aux)
 {
   struct usb_attach_arg *uaa = aux;
 
   if (uaa-iface != NULL)
   return (UMATCH_NONE);

This line means that you're waiting for the USB stack to set the
first valid configuration for you, so you don't need most of the
code in *_attach().


 #ifdef ALEA_DEBUG
   if (uaa-vendor == USB_VENDOR_ARANEUS)
   printf(ualea: vendor 0x%x (%d) (ARANEUS) product 0x%x (%d)\n,
   uaa-vendor, uaa-vendor, uaa-product, uaa-product);
 #endif

Please kill this debug chunk, you have the same information with
usbdevs(8).

   return ((usb_lookup(ualea_devs, uaa-vendor, uaa-product) != NULL) ?
   UMATCH_VENDOR_PRODUCT : 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;
   error = 

Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-14 Thread attila
Martin Pieuchot m...@openbsd.org writes:

 On 14/04/15(Tue) 07:40, attila wrote:
 [...]
 Feedback most welcome.

 See below.

 /* -*- mode:c; tab-width:8; indent-tabs-mode:t; c-basic-offset:8 -*- */

 We do not include editor settings in files, the first line should
 contain:

 /*$OpenBSD$ */

 Which will be expanded by CVS.


Done.

 /*
  * Copyright (c) 2006 Alexander Yurchenko gra...@openbsd.org
  * Copyright (c) 2007 Marc Balmer mbal...@openbsd.org
  * Copyright (C) 2015 attila 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/
  *
  * I only have an Alea II to play with but the documentation says
  * that the Alea I is the same, so they should also work.
  *
  * I cribbed liberally from both the uow and umbg drivers, both of
  * which are similar to this situation in different ways.

 The two last paragraphs do not add much information, think about what will
 help you when you'll have to read this code again in a couple of years
 :)


Ixnay to excess verbiage :-).  Gone.

  */
 
 #include sys/param.h
 #include sys/systm.h
 #include sys/device.h
 #include sys/kernel.h
 #include sys/time.h

 I believe you need sys/time.h just to make sure your driver works as
 expected.  You kept this code as #ifdef ALEA_DEBUG but does it really
 help to debug something?  Do you think it's worth keeping this code?
 We try to not add too much verbosity to driver code.


The only DEBUG code I actually cared about was the bit that measured
kbps actually delivered, and that was just to convince myself that the
device did as the mfgr said.  All ALEA_DEBUG gone now (and it was
subtly wrong anyway :-).

 #include sys/timeout.h
 
 #include dev/usb/usb.h
 #include dev/usb/usbdevs.h
 #include dev/usb/usbdi.h
 #include dev/usb/usbdi_util.h
 
 #include dev/rndvar.h
 
 #define ALEA_IFACE   0
 #define ALEA_MSECS   10

 How did you choose 10msec?  


Empirically, by cranking it down until I was seeing 100kbps all the
time, however my method for doing this was wrong so my numbers were a
little bogus.  I did some more digging and it turns out each read from
the ibulk pipe completes in just under 1000msec (990 or so).  I've
changed the timeout values to reflect this.

 #define ALEA_READ_TOUT   1100

 Compared to a transfer timeout of 1,1 second is seems very short.  By
 the way we generally spell timeout TIMEOUT :o)


Spelling fixed everywhere :-).

 #define ALEA_BUFSIZ  ((1024/8)*100)  /* 100 kbits */
 /*#define ALEA_DEBUG 1*/ /* comment out */
 
 #define OURNAME(x)   x-sc_dev.dv_xname

 We generally use DEVNAME(), look at how it is defined :) 


Fixed.

 
 struct ualea_softc {
  struct  device sc_dev;
  struct  usbd_device *sc_udev;
  struct  usbd_interface *sc_iface;
  struct  usbd_pipe *sc_ibulk;
  struct  timeout sc_tout;
  struct  usb_task sc_task;
 #ifdef ALEA_DEBUG
  struct  timespec sc_tattach;
  u_int32_t sc_nbits;
 #endif
 };
 
 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_intr(void *);
 
 struct cfdriver ualea_cd = {
  NULL, ualea, DV_DULL
 };
 
 const struct cfattach ualea_ca = {
  sizeof(struct ualea_softc),
  ualea_match,
  ualea_attach,
  ualea_detach
 };
 
 static const struct usb_devno ualea_devs[] = {
  { USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
 };

 Is it possible to match your device based on the content of the device
 descriptor instead of whitelisting IDs?  Whitelisting means that if the
 company produce a compatible device with a new ID we'll need to modify
 the driver.


Sadly, I don't think it is possible... you mean by looking at
bDeviceClass/bDeviceSubClass/bDeviceProtocol?  He only gives me zeroes
there:

Bus 005 Device 002: ID 12d8:0001  
Device Descriptor:
  bLength18
  bDescriptorType 1
  bcdUSB   2.00
  bDeviceClass0 (Defined at Interface level)
  bDeviceSubClass 0 
  

Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-14 Thread attila
Hi tech@,

I balled the copyright and license up a bit (thanks to deraadt@ and
bcallah@ for pointing this out).  I apologize to grange@ and mbalmer@
for not carrying along their copyright notices when I obviously
cribbed liberally from their code in uow.c and umbg.c, respectively.

Updated driver attached.  If anyone has one of these and can test
please let me know.  The only visible effect when you plug one in is
that Puffy's eyes in the sticker on my thinkpad glow a bit...  not
sure what's up with that, just going to go with it.

Feedback most welcome.

Pax, -A

/* -*- mode:c; tab-width:8; indent-tabs-mode:t; c-basic-offset:8 -*- */
/*
 * Copyright (c) 2006 Alexander Yurchenko gra...@openbsd.org
 * Copyright (c) 2007 Marc Balmer mbal...@openbsd.org
 * Copyright (C) 2015 attila 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/
 *
 * I only have an Alea II to play with but the documentation says
 * that the Alea I is the same, so they should also work.
 *
 * I cribbed liberally from both the uow and umbg drivers, both of
 * which are similar to this situation in different ways.
 */

#include sys/param.h
#include sys/systm.h
#include sys/device.h
#include sys/kernel.h
#include sys/time.h
#include sys/timeout.h

#include dev/usb/usb.h
#include dev/usb/usbdevs.h
#include dev/usb/usbdi.h
#include dev/usb/usbdi_util.h

#include dev/rndvar.h

#define ALEA_IFACE  0
#define ALEA_MSECS  10
#define ALEA_READ_TOUT  1100
#define ALEA_BUFSIZ ((1024/8)*100)  /* 100 kbits */
/*#define ALEA_DEBUG1*/ /* comment out */

#define OURNAME(x)  x-sc_dev.dv_xname

struct ualea_softc {
struct  device sc_dev;
struct  usbd_device *sc_udev;
struct  usbd_interface *sc_iface;
struct  usbd_pipe *sc_ibulk;
struct  timeout sc_tout;
struct  usb_task sc_task;
#ifdef ALEA_DEBUG
struct  timespec sc_tattach;
u_int32_t sc_nbits;
#endif
};

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_intr(void *);

struct cfdriver ualea_cd = {
NULL, ualea, DV_DULL
};

const struct cfattach ualea_ca = {
sizeof(struct ualea_softc),
ualea_match,
ualea_attach,
ualea_detach
};

static const struct usb_devno ualea_devs[] = {
{ USB_VENDOR_ARANEUS,   USB_PRODUCT_ARANEUS_ALEA }
};

int
ualea_match(struct device *parent, void *match, void *aux)
{
struct usb_attach_arg *uaa = aux;

if (uaa-iface != NULL)
return (UMATCH_NONE);
#ifdef ALEA_DEBUG
if (uaa-vendor == USB_VENDOR_ARANEUS)
printf(ualea: vendor 0x%x (%d) (ARANEUS) product 0x%x (%d)\n,
uaa-vendor, uaa-vendor, uaa-product, uaa-product);
#endif
return ((usb_lookup(ualea_devs, uaa-vendor, uaa-product) != NULL) ?
UMATCH_VENDOR_PRODUCT : 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;
error = usbd_set_config_index(sc-sc_udev, 0, 1);
if (error != 0) {
printf(%s: failed to set config: %s\n,
   OURNAME(sc), usbd_errstr(error));
}
error = usbd_device2interface_handle(
sc-sc_udev, ALEA_IFACE, sc-sc_iface);
if (error != 0) {
printf(%s: failed to get iface %d: %s\n,
OURNAME(sc), ALEA_IFACE, usbd_errstr(error));
return;
}
/* Get endpoint (there can be only one) */
id = usbd_get_interface_descriptor(sc-sc_iface);
for (i = 0; i  id-bNumEndpoints; i++) {
ed = usbd_interface2endpoint_descriptor(sc-sc_iface, i);
if (ed == NULL) {
printf(%s: 

Re: [NEW] Driver for the Araneus Alea II USB TRNG

2015-04-13 Thread attila
Hi tech@,

Ping?

attila att...@stalphonsos.com writes:

 Hi tech@,

 I've written a driver for the Araneus Alea II USB TRNG:
 http://www.araneus.fi/products/alea2/en/

 It produces 100kbit/sec of entropy, which my driver stuffs into
 add_true_randomness().  A small thing, but maybe valuable in
 situations that do a lot of crypto.  I mainly did it to get my head
 around more of the codebase, and I happened to have one lying around,
 so...  According to the docs the interface has not changed between the
 Alea I and the Alea II so this driver should work for both, but I only
 have a II.  I'm interested in writing drivers for other TRNGs if
 anyone else finds it useful.

 Tested under i386 and amd64.

 I'm sure I've made many mistakes.  Comments and feedback most welcome.

 Pax, -A

 diff -Nurp src.orig/sys/arch/amd64/conf/GENERIC 
 src/sys/arch/amd64/conf/GENERIC
 --- src.orig/sys/arch/amd64/conf/GENERIC  Thu Apr  2 08:24:02 2015
 +++ src/sys/arch/amd64/conf/GENERIC   Thu Apr  9 16:02:27 2015
 @@ -271,6 +271,7 @@ uvideo*   at uhub?# USB Video
  video*   at uvideo?
  udl* at uhub?# DisplayLink USB displays
  wsdisplay* at udl?
 +ualea*   at uhub?# Araneus Alea II USB TRNG
  
  puc* at pci? # PCI universal communication device
  com* at cardbus?
 diff -Nurp src.orig/sys/arch/i386/conf/GENERIC src/sys/arch/i386/conf/GENERIC
 --- src.orig/sys/arch/i386/conf/GENERIC   Thu Apr  2 08:24:02 2015
 +++ src/sys/arch/i386/conf/GENERICThu Apr  9 16:02:01 2015
 @@ -325,6 +325,7 @@ uvideo* at uhub?  # USB video
  video*  at uvideo?
  udl* at uhub?# DisplayLink USB displays
  wsdisplay* at udl?
 +ualea*   at uhub?# Araneus Alea II USB TRNG
  
  puc* at pci? # PCI universal communication device
  com* at cardbus?
 diff -Nurp src.orig/sys/arch/macppc/conf/GENERIC 
 src/sys/arch/macppc/conf/GENERIC
 --- src.orig/sys/arch/macppc/conf/GENERIC Thu Apr  2 08:24:02 2015
 +++ src/sys/arch/macppc/conf/GENERIC  Thu Apr  9 16:02:43 2015
 @@ -292,6 +292,7 @@ utrh* at uhidev?  # USBRH sensor
  utwitch* at uhidev?  # YUREX BBU sensor
  uow* at uhub?# Maxim/Dallas DS2490 1-Wire adapter
  onewire* at uow?
 +ualea*   at uhub?# Araneus Alea II USB TRNG
  
  # USB Video
  uvideo* at uhub?
 diff -Nurp src.orig/sys/arch/sparc64/conf/GENERIC 
 src/sys/arch/sparc64/conf/GENERIC
 --- src.orig/sys/arch/sparc64/conf/GENERICTue Mar 24 06:00:39 2015
 +++ src/sys/arch/sparc64/conf/GENERIC Thu Apr  9 16:02:58 2015
 @@ -245,6 +245,7 @@ ugold*at uhidev?  # gold TEMPer sensor
  utwitch* at uhidev?  # UYUREX BBU sensor
  uow* at uhub?# Maxim/Dallas DS2490 1-Wire adapter
  onewire* at uow?
 +ualea*   at uhub?# Araneus Alea II USB TRNG
  
  # USB Video
  uvideo* at uhub?
 diff -Nurp src.orig/sys/arch/zaurus/conf/GENERIC 
 src/sys/arch/zaurus/conf/GENERIC
 --- src.orig/sys/arch/zaurus/conf/GENERIC Sat Jan  3 15:24:19 2015
 +++ src/sys/arch/zaurus/conf/GENERIC  Thu Apr  9 16:03:25 2015
 @@ -135,6 +135,7 @@ umbg* at uhub?# Meinberg 
 Funkuhren USB5131
  uow* at uhub?# Maxim/Dallas DS2490 1-Wire adapter
  onewire* at uow?
  utwitch* at uhidev?  # YUREX BBU sensor
 +ualea*   at uhub?# Araneus Alea II USB TRNG
  
  scsibus* at scsi?
  sd*  at scsibus? # SCSI disk drives
 diff -Nurp src.orig/sys/dev/usb/files.usb src/sys/dev/usb/files.usb
 --- src.orig/sys/dev/usb/files.usbThu Apr  2 08:24:02 2015
 +++ src/sys/dev/usb/files.usb Thu Apr  9 16:04:38 2015
 @@ -195,6 +195,11 @@ device   utwitch: hid
  attach   utwitch at uhidbus
  file dev/usb/utwitch.c   utwitch
  
 +# Araneus Alea II TRNG
 +device   ualea
 +attach   ualea at uhub
 +file dev/usb/ualea.c ualea
 +
  # Ethernet adapters
  # ADMtek AN986 Pegasus
  device   aue: ether, ifnet, mii, ifmedia
 diff -Nurp src.orig/sys/dev/usb/ualea.c src/sys/dev/usb/ualea.c
 --- src.orig/sys/dev/usb/ualea.c  Wed Dec 31 18:00:00 1969
 +++ src/sys/dev/usb/ualea.c   Fri Apr 10 15:46:30 2015
 @@ -0,0 +1,265 @@
 +/* -*- mode:c; tab-width:8; indent-tabs-mode:t; c-basic-offset:8 -*- */
 +/*
 + * Copyright (C) 2015 by attila att...@stalphonsos.com
 + *
 + * Permission to use, copy, modify, and/or 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