2 * Copyright (c) 2000-2013 Mark R V Murray
3 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * This file is compiled into the kernel unconditionally. Any random(4)
33 * infrastructure that needs to be in the kernel by default goes here!
37 * The adaptor code all goes into random_adaptor.c, which is also compiled
38 * the kernel by default. The module in that file is initialised before
41 * Other modules must be initialised after the above two, and are
42 * software random processors which plug into random_adaptor.c.
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include "opt_random.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/fcntl.h>
56 #include <sys/filio.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
60 #include <sys/module.h>
61 #include <sys/malloc.h>
63 #include <sys/random.h>
64 #include <sys/sysctl.h>
65 #include <sys/systm.h>
67 #include <sys/unistd.h>
69 #include <dev/random/randomdev.h>
70 #include <dev/random/random_adaptors.h>
71 #include <dev/random/random_harvestq.h>
73 #define RANDOM_MINOR 0
75 static d_ioctl_t randomdev_ioctl;
77 static struct cdevsw random_cdevsw = {
79 .d_version = D_VERSION,
80 .d_read = random_adaptor_read,
81 .d_write = random_adaptor_write,
82 .d_poll = random_adaptor_poll,
83 .d_ioctl = randomdev_ioctl,
86 /* For use with make_dev(9)/destroy_dev(9). */
87 static struct cdev *random_dev;
89 /* Set up the sysctl root node for the entropy device */
90 SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
92 MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures");
96 randomdev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr __unused,
97 int flags __unused, struct thread *td __unused)
102 /* Really handled in upper layer */
115 /* Helper routine to enable kproc_exit() to work while the module is
116 * being (or has been) unloaded.
117 * This routine is in this file because it is always linked into the kernel,
118 * and will thus never be unloaded. This is critical for unloadable modules
122 randomdev_set_wakeup_exit(void *control)
132 randomdev_modevent(module_t mod __unused, int type, void *data __unused)
138 printf("random: entropy device infrastructure driver\n");
139 random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
140 RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0644, "random");
141 make_dev_alias(random_dev, "urandom"); /* compatibility */
142 random_adaptors_init();
146 random_adaptors_deinit();
147 destroy_dev(random_dev);
162 #define EARLY_2_DEV_MODULE(name, evh, arg) \
163 static moduledata_t name##_mod = { \
168 DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND)
170 EARLY_2_DEV_MODULE(randomdev, randomdev_modevent, NULL);
171 MODULE_VERSION(randomdev, 1);
178 /* Internal stub/fake routine for when no entropy processor is loaded.
179 * If the entropy device is not loaded, don't act on harvesting calls
184 random_harvest_phony(const void *entropy __unused, u_int count __unused,
185 u_int bits __unused, enum random_entropy_source origin __unused)
189 /* Hold the address of the routine which is actually called */
190 static void (*reap_func)(const void *, u_int, u_int, enum random_entropy_source) = random_harvest_phony;
192 /* Initialise the harvester when/if it is loaded */
194 randomdev_init_harvester(void (*reaper)(const void *, u_int, u_int, enum random_entropy_source))
200 /* Deinitialise the harvester when/if it is unloaded */
202 randomdev_deinit_harvester(void)
205 reap_func = random_harvest_phony;
208 /* Entropy harvesting routine.
209 * Implemented as in indirect call to allow non-inclusion of
210 * the entropy device.
213 random_harvest(const void *entropy, u_int count, u_int bits, enum random_entropy_source origin)
216 (*reap_func)(entropy, count, bits, origin);
219 /* ================================
220 * Internal reading stubs and fakes
221 * ================================
224 /* Hold the address of the routine which is actually called */
225 static u_int (*read_func)(uint8_t *, u_int) = dummy_random_read_phony;
227 /* Initialise the reader when/if it is loaded */
229 randomdev_init_reader(u_int (*reader)(uint8_t *, u_int))
235 /* Deinitialise the reader when/if it is unloaded */
237 randomdev_deinit_reader(void)
240 read_func = dummy_random_read_phony;
243 /* Kernel API version of read_random().
244 * Implemented as in indirect call to allow non-inclusion of
245 * the entropy device.
248 read_random(void *buf, int count)
251 return ((int)(*read_func)(buf, (u_int)count));