randomdev.c revision 62117
1/*-
2 * Copyright (c) 2000 Mark Murray
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/random/randomdev.c 62117 2000-06-26 12:14:20Z markm $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/conf.h>
32#include <sys/fcntl.h>
33#include <sys/uio.h>
34#include <sys/kernel.h>
35#include <sys/kobj.h>
36#include <sys/malloc.h>
37#include <sys/module.h>
38#include <sys/bus.h>
39#include <sys/random.h>
40#include <machine/bus.h>
41#include <machine/resource.h>
42#include <sys/rman.h>
43#include <sys/signalvar.h>
44#include <sys/sysctl.h>
45#include <crypto/blowfish/blowfish.h>
46
47#include <dev/randomdev/yarrow.h>
48
49static d_read_t randomread;
50static d_write_t randomwrite;
51
52#define CDEV_MAJOR	2
53#define RANDOM_MINOR	3
54
55static struct cdevsw random_cdevsw = {
56	/* open */	(d_open_t *)nullop,
57	/* close */	(d_close_t *)nullop,
58	/* read */	randomread,
59	/* write */	randomwrite,
60	/* ioctl */	noioctl,
61	/* poll */	nopoll,
62	/* mmap */	nommap,
63	/* strategy */	nostrategy,
64	/* name */	"random",
65	/* maj */	CDEV_MAJOR,
66	/* dump */	nodump,
67	/* psize */	nopsize,
68	/* flags */	0,
69	/* bmaj */	-1
70};
71
72/* For use with make_dev(9)/destroy_dev(9). */
73static dev_t randomdev;
74
75void *buf;
76
77extern void randominit(void);
78
79extern struct state state;
80
81/* This is mostly academic at the moment; as Yarrow gets extended, it will
82   become more relevant */
83SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
84SYSCTL_NODE(_kern_random, OID_AUTO, yarrow, CTLFLAG_RW, 0, "Yarrow Parameters");
85SYSCTL_INT(_kern_random_yarrow, OID_AUTO, gengateinterval, CTLFLAG_RW, &state.gengateinterval, 10, "Generator Gate Interval");
86
87static int
88randomread(dev_t dev, struct uio *uio, int flag)
89{
90	u_int c, ret;
91	int error = 0;
92
93	c = min(uio->uio_resid, PAGE_SIZE);
94	buf = (void *)malloc(c, M_TEMP, M_WAITOK);
95	while (uio->uio_resid > 0 && error == 0) {
96		ret = read_random(buf, c);
97		error = uiomove(buf, ret, uio);
98	}
99	free(buf, M_TEMP);
100	return error;
101}
102
103static int
104randomwrite(dev_t dev, struct uio *uio, int flag)
105{
106	u_int c;
107	int error = 0;
108
109	buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
110	while (uio->uio_resid > 0) {
111		c = min(uio->uio_resid, PAGE_SIZE);
112		error = uiomove(buf, c, uio);
113		if (error)
114			break;
115		/* write_random(buf, c); */
116	}
117	free(buf, M_TEMP);
118	return error;
119}
120
121static int
122random_modevent(module_t mod, int type, void *data)
123{
124	switch(type) {
125	case MOD_LOAD:
126		if (bootverbose)
127			printf("random: <entropy source>\n");
128		randomdev = make_dev(&random_cdevsw, RANDOM_MINOR, UID_ROOT,
129			GID_WHEEL, 0666, "zero");
130		randominit();
131		return 0;
132
133	case MOD_UNLOAD:
134		destroy_dev(randomdev);
135		return 0;
136
137	case MOD_SHUTDOWN:
138		return 0;
139
140	default:
141		return EOPNOTSUPP;
142	}
143}
144
145DEV_MODULE(random, random_modevent, NULL);
146