]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/random/live_entropy_sources.c
Merge ACPICA 20141107 and 20150204.
[FreeBSD/FreeBSD.git] / sys / dev / random / live_entropy_sources.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/param.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_random.h"
32
33 #include <sys/kernel.h>
34 #include <sys/libkern.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/queue.h>
39 #include <sys/random.h>
40 #include <sys/sbuf.h>
41 #include <sys/sx.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/unistd.h>
45
46 #include <machine/cpu.h>
47
48 #include <dev/random/randomdev.h>
49 #include <dev/random/randomdev_soft.h>
50 #include <dev/random/random_adaptors.h>
51 #include <dev/random/random_harvestq.h>
52
53 #include "live_entropy_sources.h"
54
55 /*
56  * The les_lock protects the consistency of the "struct les_head les_sources"
57  */
58 static struct sx les_lock; /* Need a sleepable lock for the sbuf/sysctl stuff. */
59
60 LIST_HEAD(les_head, live_entropy_sources);
61 static struct les_head les_sources = LIST_HEAD_INITIALIZER(les_sources);
62
63 void
64 live_entropy_source_register(struct live_entropy_source *rsource)
65 {
66         struct live_entropy_sources *lles;
67
68         KASSERT(rsource != NULL, ("invalid input to %s", __func__));
69
70         lles = malloc(sizeof(*lles), M_ENTROPY, M_WAITOK);
71         lles->lles_rsource = rsource;
72
73         sx_xlock(&les_lock);
74         LIST_INSERT_HEAD(&les_sources, lles, lles_entries);
75         sx_xunlock(&les_lock);
76 }
77
78 void
79 live_entropy_source_deregister(struct live_entropy_source *rsource)
80 {
81         struct live_entropy_sources *lles = NULL;
82
83         KASSERT(rsource != NULL, ("invalid input to %s", __func__));
84
85         sx_xlock(&les_lock);
86         LIST_FOREACH(lles, &les_sources, lles_entries)
87                 if (lles->lles_rsource == rsource) {
88                         LIST_REMOVE(lles, lles_entries);
89                         break;
90                 }
91         sx_xunlock(&les_lock);
92         if (lles != NULL)
93                 free(lles, M_ENTROPY);
94 }
95
96 static int
97 live_entropy_source_handler(SYSCTL_HANDLER_ARGS)
98 {
99         struct live_entropy_sources *lles;
100         struct sbuf sbuf;
101         int error, count;
102
103         sx_slock(&les_lock);
104
105         sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
106
107         count = 0;
108         LIST_FOREACH(lles, &les_sources, lles_entries) {
109                 sbuf_cat(&sbuf, (count++ ? ",'" : "'"));
110                 sbuf_cat(&sbuf, lles->lles_rsource->les_ident);
111                 sbuf_cat(&sbuf, "'");
112         }
113
114         error = sbuf_finish(&sbuf);
115         sbuf_delete(&sbuf);
116
117         sx_sunlock(&les_lock);
118
119         return (error);
120 }
121
122 /*
123  * Run through all "live" sources reading entropy for the given
124  * number of rounds, which should be a multiple of the number
125  * of entropy accumulation pools in use; 2 for Yarrow and 32
126  * for Fortuna.
127  *
128  * BEWARE!!!
129  * This function runs inside the RNG thread! Don't do anything silly!
130  */
131 /* XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle
132  * counters are built in, but on older hardware it will do a real time clock
133  * read which can be quite expensive.
134  */
135 void
136 live_entropy_sources_feed(void)
137 {
138         static struct harvest_event event;
139         struct live_entropy_sources *lles;
140         int i, read_rate;
141         u_int n;
142
143         sx_slock(&les_lock);
144
145         /*
146          * Walk over all of live entropy sources, and feed their output
147          * to the system-wide RNG.
148          */
149         read_rate = random_adaptor_read_rate();
150         LIST_FOREACH(lles, &les_sources, lles_entries) {
151
152                 for (i = 0; i < harvest_pool_count*read_rate; i++) {
153                         /* This *must* be quick, since it's a live entropy source. */
154                         n = lles->lles_rsource->les_read(event.he_entropy, HARVESTSIZE);
155                         KASSERT((n > 0 && n <= HARVESTSIZE), ("very bad return from les_read (= %d) in %s", n, __func__));
156                         memset(event.he_entropy + n, 0, HARVESTSIZE - n);
157
158                         event.he_somecounter = get_cyclecount();
159                         event.he_size = n;
160                         event.he_bits = (n*8)/2;
161                         event.he_source = lles->lles_rsource->les_source;
162                         event.he_destination = harvest_destination[event.he_source]++;
163
164                         /* Do the actual entropy insertion */
165                         harvest_process_event(&event);
166                 }
167
168         }
169
170         sx_sunlock(&les_lock);
171 }
172
173 void
174 live_entropy_sources_init(void)
175 {
176
177         SYSCTL_PROC(_kern_random, OID_AUTO, live_entropy_sources,
178             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
179             NULL, 0, live_entropy_source_handler, "",
180             "List of Active Live Entropy Sources");
181
182         sx_init(&les_lock, "live_entropy_sources");
183 }
184
185 void
186 live_entropy_sources_deinit(void)
187 {
188
189         sx_destroy(&les_lock);
190 }