2 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3 * Copyright (c) 2013 Mark R V Murray
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_random.h"
33 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/random.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.h>
43 #include <dev/random/randomdev.h>
44 #include <dev/random/random_adaptors.h>
47 dummy_random_zero(void)
60 dummy_random_init(void)
64 printf("random: %s\n", __func__);
67 randomdev_init_reader(dummy_random_read_phony);
70 /* This is used only by the internal read_random(9) call, and then only
71 * if no entropy processor is loaded.
73 * Make a token effort to provide _some_ kind of output. No warranty of
74 * the quality of this output is made, mainly because its lousy.
76 * This is only used by the internal read_random(9) call when no other
79 * It has external scope due to the way things work in
80 * randomdev_[de]init_reader() that the rest of the world doesn't need to
86 dummy_random_read_phony(uint8_t *buf, u_int count)
88 /* If no entropy device is loaded, don't spam the console with warnings */
89 static int warned = 0;
94 log(LOG_WARNING, "random device not loaded/active; using insecure pseudo-random number generator\n");
98 /* srandom() is called in kern/init_main.c:proc0_post() */
100 /* Fill buf[] with random(9) output */
101 for (i = 0; i < count; i += sizeof(randval)) {
103 size = MIN(count - i, sizeof(randval));
104 memcpy(buf + i, &randval, (size_t)size);
110 struct random_adaptor randomdev_dummy = {
112 .ra_priority = 1, /* Bottom priority, so goes to last position */
113 .ra_reseed = dummy_random,
114 .ra_seeded = (random_adaptor_seeded_func_t *)dummy_random_zero,
115 .ra_read = (random_adaptor_read_func_t *)dummy_random_zero,
116 .ra_write = (random_adaptor_write_func_t *)dummy_random_zero,
117 .ra_init = dummy_random_init,
118 .ra_deinit = dummy_random,