]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/dummy_rng.c
Upgrade our copy of clang and llvm to 3.5.1 release. This is a bugfix
[FreeBSD/FreeBSD.git] / sys / dev / random / dummy_rng.c
1 /*-
2  * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3  * Copyright (c) 2013 Mark R V Murray
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *
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.
26  *
27  */
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_random.h"
32
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/random.h>
40 #include <sys/syslog.h>
41 #include <sys/systm.h>
42
43 #include <dev/random/randomdev.h>
44 #include <dev/random/random_adaptors.h>
45
46 static int
47 dummy_random_zero(void)
48 {
49
50         return (0);
51 }
52
53 static void
54 dummy_random(void)
55 {
56 }
57
58 /* ARGSUSED */
59 static void
60 dummy_random_init(void)
61 {
62
63 #ifdef RANDOM_DEBUG
64         printf("random: %s\n", __func__);
65 #endif
66
67         randomdev_init_reader(dummy_random_read_phony);
68 }
69
70 /* This is used only by the internal read_random(9) call, and then only
71  * if no entropy processor is loaded.
72  *
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.
75  *
76  * This is only used by the internal read_random(9) call when no other
77  * adaptor is active.
78  *
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
81  * know about.
82  *
83  * Caveat Emptor.
84  */
85 u_int
86 dummy_random_read_phony(uint8_t *buf, u_int count)
87 {
88         /* If no entropy device is loaded, don't spam the console with warnings */
89         static int warned = 0;
90         u_long randval;
91         size_t size, i;
92
93         if (!warned) {
94                 log(LOG_WARNING, "random device not loaded/active; using insecure pseudo-random number generator\n");
95                 warned = 1;
96         }
97
98         /* srandom() is called in kern/init_main.c:proc0_post() */
99
100         /* Fill buf[] with random(9) output */
101         for (i = 0; i < count; i += sizeof(randval)) {
102                 randval = random();
103                 size = MIN(count - i, sizeof(randval));
104                 memcpy(buf + i, &randval, (size_t)size);
105         }
106
107         return (count);
108 }
109
110 struct random_adaptor randomdev_dummy = {
111         .ra_ident = "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,
119 };