summaryrefslogtreecommitdiff
path: root/sys/dev/usb/uonerng.c
blob: 1958508635a0e72f3802e4db02a14893eba85211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*	$OpenBSD: uonerng.c,v 1.4 2018/07/09 20:06:12 jasper Exp $ */
/*
 * Copyright (C) 2015 Devin Reade <gdr@gno.org>
 * Copyright (C) 2015 Sean Levy <attila@stalphonsos.com>
 * Copyright (c) 2007 Marc Balmer <mbalmer@openbsd.org>
 * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
 * Copyright (c) 1998 The NetBSD Foundation, Inc.
 *
 * 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.
 */

/*
 * Moonbase Otago OneRNG TRNG.  Note that the encoded vendor for this
 * device is OpenMoko as OpenMoko has made its device ranges available
 * for other open source / open hardware vendors.
 *
 * Product information can be found here:
 *     http://onerng.info/onerng
 *
 * Based on the ualea(4), uow(4), and umodem(4) source code.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/time.h>
#include <sys/timeout.h>
#include <machine/bus.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usbcdc.h>

#include <dev/rndvar.h>

/*
 * The OneRNG is documented to provide ~350kbits/s of entropy at
 * ~7.8 bits/byte, and when used at a lower rate providing close
 * to 8 bits/byte.
 *
 * Although this driver is able to consume the data at the full rate,
 * we tune this down to 10kbit/s as the OpenBSD RNG is better off
 * with small amounts of input at a time so as to not saturate the
 * input queue and mute other sources of entropy.
 *
 * Furthermore, unlike other implementations, for us there is no benefit
 * to discarding the initial bytes retrieved from the OneRNG, regardless
 * of the quality of the data. (Empirical tests suggest that the initial
 * quality is fine, anyway.)
 */
#define ONERNG_BUFSIZ		128
#define ONERNG_MSECS		100

#define ONERNG_TIMEOUT  	1000	/* ms */

/*
 * Define ONERNG_MEASURE_RATE to periodically log rate at which we provide
 * random data to the kernel.
 */
#ifdef ONERNG_MEASURE_RATE
#define ONERNG_RATE_SECONDS 30
#endif

/* OneRNG operational modes */
#define ONERNG_OP_ENABLE	"cmdO\n" /* start emitting data */
#define ONERNG_OP_DISABLE	"cmdo\n" /* stop emitting data */
#define ONERNG_OP_FLUSH_ENTROPY	"cmdw\n"

/* permits extracting the firmware in order to check the crypto signature */
#define ONERNG_OP_EXTRACT_FIRMWARE "cmdX\n"

/*
 * Noise sources include an avalache circuit and an RF circuit.
 * There is also a whitener to provide a uniform distribution.
 * Different combinations are possible.
 */
#define ONERNG_AVALANCHE_WHITENER	"cmd0\n" /* device default */
#define ONERNG_AVALANCHE		"cmd1\n"
#define ONERNG_AVALANCHE_RF_WHITENER	"cmd2\n"
#define ONERNG_AVALANCHE_RF		"cmd3\n"
#define ONERNG_SILENT			"cmd4\n" /* none; necessary for cmdX */
#define ONERNG_SILENT2			"cmd5\n"
#define ONERNG_RF_WHITENER		"cmd6\n"
#define ONERNG_RF			"cmd7\n"


#define ONERNG_IFACE_CTRL_INDEX	0
#define ONERNG_IFACE_DATA_INDEX	1

#define DEVNAME(_sc) ((_sc)->sc_dev.dv_xname)

struct uonerng_softc {
	struct	  device sc_dev;
	struct	  usbd_device *sc_udev;

	int	  sc_ctl_iface_no;			/* control */
	struct	  usbd_interface *sc_data_iface;	/* data */

	struct	  usbd_pipe *sc_inpipe;
	struct	  usbd_pipe *sc_outpipe;

	struct	  timeout sc_timeout;
	struct	  usb_task sc_task;
	struct	  usbd_xfer *sc_xfer;
	int      *sc_buf;
#ifdef ONERNG_MEASURE_RATE
	struct	  timeval sc_start;
	struct	  timeval sc_cur;
	int	  sc_counted_bytes;
#endif
	u_char	  sc_dtr;			/* current DTR state */
	u_char	  sc_rts;			/* current RTS state */
	u_char	  sc_first_run;
};

int uonerng_match(struct device *, void *, void *);
void uonerng_attach(struct device *, struct device *, void *);
int uonerng_detach(struct device *, int);
void uonerng_task(void *);
void uonerng_timeout(void *);
int uonerng_enable(struct uonerng_softc *sc);
void uonerng_cleanup(struct uonerng_softc *sc);
usbd_status uonerng_set_line_state(struct uonerng_softc *sc);
usbd_status uonerng_rts(struct uonerng_softc *sc, int onoff);

struct cfdriver uonerng_cd = {
	NULL, "uonerng", DV_DULL
};

const struct cfattach uonerng_ca = {
	sizeof(struct uonerng_softc), uonerng_match, uonerng_attach, uonerng_detach
};

int
uonerng_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_OPENMOKO2 ||
	    uaa->product != USB_PRODUCT_OPENMOKO2_ONERNG)
		return UMATCH_NONE;

	return UMATCH_VENDOR_PRODUCT;
}

void
uonerng_attach(struct device *parent, struct device *self, void *aux)
{
	struct uonerng_softc *sc = (struct uonerng_softc *)self;
	struct usb_attach_arg *uaa = aux;
	struct usbd_interface *iface = uaa->iface;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed;
	int ep_ibulk = -1, ep_obulk = -1;
	usbd_status err;
	int i;

	sc->sc_udev = uaa->device;
	sc->sc_dtr = -1;
	sc->sc_rts = -1;
	sc->sc_first_run = 1;

	usb_init_task(&sc->sc_task, uonerng_task, sc, USB_TASK_TYPE_GENERIC);

	/* locate the control interface number and the data interface */
	err = usbd_device2interface_handle(sc->sc_udev,
	    ONERNG_IFACE_CTRL_INDEX, &iface);
	if (err || iface == NULL) {
		printf("%s: failed to locate control interface, err=%s\n",
		    DEVNAME(sc), usbd_errstr(err));
		goto fail;
	}
	id = usbd_get_interface_descriptor(iface);
	if (id != NULL &&
	    id->bInterfaceClass == UICLASS_CDC &&
	    id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
	    id->bInterfaceProtocol == UIPROTO_CDC_AT) {
		sc->sc_ctl_iface_no = id->bInterfaceNumber;
	} else {
		printf("%s: control interface number not found\n",
		    DEVNAME(sc));
		goto fail;
	}

	err = usbd_device2interface_handle(sc->sc_udev,
	    ONERNG_IFACE_DATA_INDEX, &sc->sc_data_iface);
	if (err || sc->sc_data_iface == NULL) {
		printf("%s: failed to locate data interface, err=%s\n",
		    DEVNAME(sc), usbd_errstr(err));
		goto fail;
	}

	/* Find the bulk endpoints */
	id = usbd_get_interface_descriptor(sc->sc_data_iface);
	if (id == NULL ||
	    id->bInterfaceClass != UICLASS_CDC_DATA ||
	    id->bInterfaceSubClass != UISUBCLASS_DATA) {
		printf("%s: no data interface descriptor\n", DEVNAME(sc));
		goto fail;
	}
	for (i = 0; i < id->bNumEndpoints; i++) {
		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
		if (ed == NULL) {
			printf("%s: no endpoint descriptor for %d\n",
			    DEVNAME(sc), i);
			goto fail;
		}
		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
                        ep_ibulk = ed->bEndpointAddress;
                } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
                        ep_obulk = ed->bEndpointAddress;
                }
        }

	if (ep_ibulk == -1) {
		printf("%s: Could not find data bulk in\n", DEVNAME(sc));
		goto fail;
	}
	if (ep_obulk == -1) {
		printf("%s: Could not find data bulk out\n", DEVNAME(sc));
		goto fail;
	}

	/* Open pipes */
	err = usbd_open_pipe(sc->sc_data_iface, ep_ibulk,
	    USBD_EXCLUSIVE_USE, &sc->sc_inpipe);
	if (err) {
		printf("%s: failed to open bulk-in pipe: %s\n",
		    DEVNAME(sc), usbd_errstr(err));
		goto fail;
	}
	err = usbd_open_pipe(sc->sc_data_iface, ep_obulk,
	    USBD_EXCLUSIVE_USE, &sc->sc_outpipe);
	if (err) {
		printf("%s: failed to open bulk-out pipe: %s\n",
		    DEVNAME(sc), usbd_errstr(err));
		goto fail;
	}

	/* Allocate xfer/buffer for bulk transfers */
	sc->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
	if (sc->sc_xfer == NULL) {
		printf("%s: could not alloc xfer\n", DEVNAME(sc));
		goto fail;
	}
	sc->sc_buf = usbd_alloc_buffer(sc->sc_xfer, ONERNG_BUFSIZ);
	if (sc->sc_buf == NULL) {
		printf("%s: could not alloc %d-byte buffer\n", DEVNAME(sc),
		    ONERNG_BUFSIZ);
		goto fail;
	}

	if (uonerng_enable(sc) != 0) {
		goto fail;
	}

	timeout_set(&sc->sc_timeout, uonerng_timeout, sc);

	/* get the initial random data as early as possible */
	uonerng_task(sc);

	usb_add_task(sc->sc_udev, &sc->sc_task);
	return;

 fail:
	usbd_deactivate(sc->sc_udev);
	uonerng_cleanup(sc);
}

int
uonerng_enable(struct uonerng_softc *sc)
{
	int err;

	if ((err = uonerng_rts(sc, 0))) {
		printf("%s: failed to clear RTS: %s\n", DEVNAME(sc),
		    usbd_errstr(err));
		return (1);
	}

	usbd_setup_xfer(sc->sc_xfer, sc->sc_outpipe, sc,
	    ONERNG_AVALANCHE_WHITENER, sizeof(ONERNG_AVALANCHE_WHITENER),
	    USBD_SYNCHRONOUS, ONERNG_TIMEOUT, NULL);
	if ((err = usbd_transfer(sc->sc_xfer))) {
		printf("%s: failed to set operating mode: %s\n",
		    DEVNAME(sc), usbd_errstr(err));
		return (1);
	}

	usbd_setup_xfer(sc->sc_xfer, sc->sc_outpipe, sc,
	    ONERNG_OP_ENABLE, sizeof(ONERNG_OP_ENABLE),
	    USBD_SYNCHRONOUS, ONERNG_TIMEOUT, NULL);
	if ((err = usbd_transfer(sc->sc_xfer))) {
		printf("%s: failed to enable device: %s\n",
		    DEVNAME(sc), usbd_errstr(err));
		return (1);
	}

	return (0);
}

int
uonerng_detach(struct device *self, int flags)
{
	struct uonerng_softc *sc = (struct uonerng_softc *)self;

	usb_rem_task(sc->sc_udev, &sc->sc_task);
	if (timeout_initialized(&sc->sc_timeout)) {
		timeout_del(&sc->sc_timeout);
	}
	uonerng_cleanup(sc);
	return (0);
}

void
uonerng_cleanup(struct uonerng_softc *sc)
{
	if (sc->sc_inpipe != NULL) {
		usbd_close_pipe(sc->sc_inpipe);
		sc->sc_inpipe = NULL;
	}
	if (sc->sc_outpipe != NULL) {
		usbd_close_pipe(sc->sc_outpipe);
		sc->sc_outpipe = NULL;
	}

	/* usbd_free_xfer will also free the buffer if necessary */
	if (sc->sc_xfer != NULL) {
		usbd_free_xfer(sc->sc_xfer);
		sc->sc_xfer = NULL;
	}
}

usbd_status
uonerng_rts(struct uonerng_softc *sc, int onoff)
{
	if (sc->sc_rts == onoff)
		return USBD_NORMAL_COMPLETION;
	sc->sc_rts = onoff;

	return uonerng_set_line_state(sc);
}

usbd_status
uonerng_set_line_state(struct uonerng_softc *sc)
{
	usb_device_request_t req;
	int ls;

	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
	USETW(req.wValue, ls);
	USETW(req.wIndex, sc->sc_ctl_iface_no);
	USETW(req.wLength, 0);

	return usbd_do_request(sc->sc_udev, &req, 0);
}

void
uonerng_task(void *arg)
{
	struct uonerng_softc *sc = (struct uonerng_softc *) arg;
	usbd_status error;
	u_int32_t len, int_count, i;
#ifdef ONERNG_MEASURE_RATE
	time_t elapsed;
	int rate;
#endif

	usbd_setup_xfer(sc->sc_xfer, sc->sc_inpipe, NULL, sc->sc_buf,
	    ONERNG_BUFSIZ,
	    USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS | USBD_NO_COPY,
	    ONERNG_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;
	}

#ifdef ONERNG_MEASURE_RATE
	if (sc->sc_first_run) {
		sc->sc_counted_bytes = 0;
		getmicrotime(&(sc->sc_start));
	}
	sc->sc_counted_bytes += len;
	getmicrotime(&(sc->sc_cur));
	elapsed = sc->sc_cur.tv_sec - sc->sc_start.tv_sec;
	if (elapsed >= ONERNG_RATE_SECONDS) {
		rate = (8 * sc->sc_counted_bytes) / (elapsed * 1024);
		printf("%s: transfer rate = %d kb/s\n", DEVNAME(sc), rate);

		/* set up for next measurement */
		sc->sc_counted_bytes = 0;
		getmicrotime(&(sc->sc_start));
	}
#endif

	int_count = len / sizeof(int);
	for (i = 0; i < int_count; i++) {
		enqueue_randomness(sc->sc_buf[i]);
	}
bail:

	if (sc->sc_first_run) {
		sc->sc_first_run = 0;
	} else {
		timeout_add_msec(&sc->sc_timeout, ONERNG_MSECS);
	}
}

void
uonerng_timeout(void *arg)
{
	struct uonerng_softc *sc = arg;

	usb_add_task(sc->sc_udev, &sc->sc_task);
}