subr_cprng.c revision 1.3 1 1.3 tls /* $NetBSD: subr_cprng.c,v 1.3 2011/11/29 03:50:31 tls Exp $ */
2 1.1 tls
3 1.1 tls /*-
4 1.1 tls * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 tls * All rights reserved.
6 1.1 tls *
7 1.1 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tls * by Thor Lancelot Simon.
9 1.1 tls *
10 1.1 tls * Redistribution and use in source and binary forms, with or without
11 1.1 tls * modification, are permitted provided that the following conditions
12 1.1 tls * are met:
13 1.1 tls * 1. Redistributions of source code must retain the above copyright
14 1.1 tls * notice, this list of conditions and the following disclaimer.
15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tls * notice, this list of conditions and the following disclaimer in the
17 1.1 tls * documentation and/or other materials provided with the distribution.
18 1.1 tls *
19 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tls * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tls */
31 1.1 tls
32 1.1 tls #include <sys/types.h>
33 1.1 tls #include <sys/time.h>
34 1.1 tls #include <sys/param.h>
35 1.1 tls #include <sys/kernel.h>
36 1.1 tls #include <sys/systm.h>
37 1.1 tls #include <sys/kmem.h>
38 1.1 tls #include <sys/mutex.h>
39 1.1 tls #include <sys/rngtest.h>
40 1.1 tls #include <sys/rnd.h>
41 1.3 tls #include <dev/rnd_private.h>
42 1.1 tls
43 1.2 tsutsui #if defined(__HAVE_CPU_COUNTER)
44 1.1 tls #include <machine/cpu_counter.h>
45 1.2 tsutsui #endif
46 1.1 tls
47 1.1 tls #include <sys/cprng.h>
48 1.1 tls
49 1.3 tls __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.3 2011/11/29 03:50:31 tls Exp $");
50 1.1 tls
51 1.1 tls void
52 1.1 tls cprng_init(void)
53 1.1 tls {
54 1.1 tls nist_ctr_initialize();
55 1.1 tls }
56 1.1 tls
57 1.1 tls static inline uint32_t
58 1.1 tls cprng_counter(void)
59 1.1 tls {
60 1.1 tls struct timeval tv;
61 1.1 tls
62 1.1 tls #if defined(__HAVE_CPU_COUNTER)
63 1.1 tls if (cpu_hascounter())
64 1.1 tls return cpu_counter32();
65 1.1 tls #endif
66 1.1 tls if (__predict_false(cold)) {
67 1.1 tls /* microtime unsafe if clock not running yet */
68 1.1 tls return 0;
69 1.1 tls }
70 1.1 tls microtime(&tv);
71 1.1 tls return (tv.tv_sec * 1000000 + tv.tv_usec);
72 1.1 tls }
73 1.1 tls
74 1.1 tls static void
75 1.1 tls cprng_strong_reseed(void *const arg)
76 1.1 tls {
77 1.1 tls cprng_strong_t *c = arg;
78 1.1 tls uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
79 1.1 tls uint32_t cc = cprng_counter();
80 1.1 tls
81 1.1 tls mutex_enter(&c->mtx);
82 1.1 tls if (c->reseed.len != sizeof(key)) {
83 1.1 tls panic("cprng_strong_reseed: bad entropy length %d "
84 1.1 tls " (expected %d)", (int)c->reseed.len, (int)sizeof(key));
85 1.1 tls }
86 1.1 tls if (nist_ctr_drbg_reseed(&c->drbg, c->reseed.data, c->reseed.len,
87 1.1 tls &cc, sizeof(cc))) {
88 1.1 tls panic("cprng %s: nist_ctr_drbg_reseed failed.", c->name);
89 1.1 tls }
90 1.1 tls c->reseed_pending = 0;
91 1.1 tls if (c->flags & CPRNG_USE_CV) {
92 1.1 tls cv_broadcast(&c->cv);
93 1.1 tls }
94 1.1 tls mutex_exit(&c->mtx);
95 1.1 tls }
96 1.1 tls
97 1.1 tls cprng_strong_t *
98 1.1 tls cprng_strong_create(const char *const name, int ipl, int flags)
99 1.1 tls {
100 1.1 tls cprng_strong_t *c;
101 1.1 tls uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
102 1.1 tls int r, getmore = 0;;
103 1.1 tls uint32_t cc;
104 1.1 tls
105 1.1 tls c = kmem_alloc(sizeof(*c), KM_NOSLEEP);
106 1.1 tls if (c == NULL) {
107 1.1 tls return NULL;
108 1.1 tls }
109 1.1 tls c->flags = flags;
110 1.1 tls strlcpy(c->name, name, sizeof(c->name));
111 1.1 tls c->reseed_pending = 0;
112 1.1 tls c->reseed.cb = cprng_strong_reseed;
113 1.1 tls c->reseed.arg = c;
114 1.1 tls strlcpy(c->reseed.name, name, sizeof(c->reseed.name));
115 1.1 tls
116 1.1 tls mutex_init(&c->mtx, MUTEX_DEFAULT, ipl);
117 1.1 tls
118 1.1 tls if (c->flags & CPRNG_USE_CV) {
119 1.1 tls cv_init(&c->cv, name);
120 1.1 tls }
121 1.1 tls
122 1.1 tls r = rnd_extract_data(key, sizeof(key), RND_EXTRACT_GOOD);
123 1.1 tls if (r != sizeof(key)) {
124 1.1 tls if (c->flags & CPRNG_INIT_ANY) {
125 1.1 tls printf("cprng %s: WARNING insufficient "
126 1.1 tls "entropy at creation.\n", name);
127 1.1 tls rnd_extract_data(key + r, sizeof(key - r),
128 1.1 tls RND_EXTRACT_ANY);
129 1.1 tls } else {
130 1.1 tls return NULL;
131 1.1 tls }
132 1.1 tls getmore++;
133 1.1 tls }
134 1.1 tls
135 1.1 tls if (nist_ctr_drbg_instantiate(&c->drbg, key, sizeof(key),
136 1.1 tls &cc, sizeof(cc), name, strlen(name))) {
137 1.1 tls panic("cprng %s: instantiation failed.", name);
138 1.1 tls }
139 1.1 tls
140 1.1 tls if (getmore) {
141 1.1 tls int wr = 0;
142 1.1 tls
143 1.1 tls /* Ask for more. */
144 1.1 tls c->reseed_pending = 1;
145 1.1 tls c->reseed.len = sizeof(key);
146 1.1 tls rndsink_attach(&c->reseed);
147 1.1 tls if (c->flags & CPRNG_USE_CV) {
148 1.1 tls mutex_enter(&c->mtx);
149 1.1 tls do {
150 1.1 tls wr = cv_wait_sig(&c->cv, &c->mtx);
151 1.1 tls if (__predict_true(wr == 0)) {
152 1.1 tls break;
153 1.1 tls }
154 1.1 tls if (wr == ERESTART) {
155 1.1 tls continue;
156 1.1 tls } else {
157 1.1 tls cv_destroy(&c->cv);
158 1.1 tls mutex_exit(&c->mtx);
159 1.1 tls mutex_destroy(&c->mtx);
160 1.1 tls kmem_free(c, sizeof(c));
161 1.1 tls return NULL;
162 1.1 tls }
163 1.1 tls } while (1);
164 1.1 tls mutex_exit(&c->mtx);
165 1.1 tls }
166 1.1 tls }
167 1.1 tls return c;
168 1.1 tls }
169 1.1 tls
170 1.1 tls size_t
171 1.1 tls cprng_strong(cprng_strong_t *const c, void *const p, size_t len)
172 1.1 tls {
173 1.1 tls uint32_t cc = cprng_counter();
174 1.1 tls
175 1.1 tls if (len > CPRNG_MAX_LEN) { /* XXX should we loop? */
176 1.1 tls len = CPRNG_MAX_LEN; /* let the caller loop if desired */
177 1.1 tls }
178 1.1 tls
179 1.1 tls mutex_enter(&c->mtx);
180 1.1 tls again:
181 1.1 tls if (nist_ctr_drbg_generate(&c->drbg, p, len, &cc, sizeof(cc))) {
182 1.1 tls /* A generator failure really means we hit the hard limit. */
183 1.1 tls if (c->flags & CPRNG_REKEY_ANY) {
184 1.1 tls uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
185 1.1 tls
186 1.1 tls printf("cprng %s: WARNING pseudorandom rekeying.\n",
187 1.1 tls c->name);
188 1.1 tls rnd_extract_data(key, sizeof(key), RND_EXTRACT_ANY);
189 1.1 tls cc = cprng_counter();
190 1.1 tls if (nist_ctr_drbg_reseed(&c->drbg, key, sizeof(key),
191 1.1 tls &cc, sizeof(cc))) {
192 1.1 tls panic("cprng %s: nist_ctr_drbg_reseed "
193 1.1 tls "failed.", c->name);
194 1.1 tls }
195 1.1 tls if (c->flags & CPRNG_USE_CV) {
196 1.1 tls cv_broadcast(&c->cv); /* XXX unnecessary? */
197 1.1 tls }
198 1.1 tls } else {
199 1.1 tls if (c->flags & CPRNG_USE_CV) {
200 1.1 tls int wr;
201 1.1 tls
202 1.1 tls do {
203 1.1 tls wr = cv_wait_sig(&c->cv, &c->mtx);
204 1.1 tls if (wr == EINTR) {
205 1.1 tls mutex_exit(&c->mtx);
206 1.1 tls return 0;
207 1.1 tls }
208 1.1 tls } while (nist_ctr_drbg_generate(&c->drbg, p,
209 1.1 tls len, &cc,
210 1.1 tls sizeof(cc)));
211 1.1 tls } else {
212 1.1 tls mutex_exit(&c->mtx);
213 1.1 tls return 0;
214 1.1 tls }
215 1.1 tls }
216 1.1 tls }
217 1.1 tls
218 1.1 tls #ifdef DIAGNOSTIC
219 1.1 tls /*
220 1.1 tls * If the generator has just been keyed, perform
221 1.1 tls * the statistical RNG test.
222 1.1 tls */
223 1.1 tls if (__predict_false(c->drbg.reseed_counter == 1)) {
224 1.1 tls rngtest_t rt;
225 1.1 tls
226 1.1 tls strncpy(rt.rt_name, c->name, sizeof(rt.rt_name));
227 1.1 tls
228 1.1 tls if (nist_ctr_drbg_generate(&c->drbg, rt.rt_b,
229 1.1 tls sizeof(rt.rt_b), NULL, 0)) {
230 1.1 tls panic("cprng %s: nist_ctr_drbg_generate failed!",
231 1.1 tls c->name);
232 1.1 tls
233 1.1 tls }
234 1.1 tls if (rngtest(&rt)) {
235 1.1 tls printf("cprng %s: failed statistical RNG test.\n",
236 1.1 tls c->name);
237 1.1 tls c->drbg.reseed_counter =
238 1.1 tls NIST_CTR_DRBG_RESEED_INTERVAL + 1;
239 1.1 tls }
240 1.1 tls
241 1.1 tls memset(&rt, 0, sizeof(rt));
242 1.1 tls }
243 1.1 tls #endif
244 1.1 tls if (__predict_false(c->drbg.reseed_counter >
245 1.1 tls (NIST_CTR_DRBG_RESEED_INTERVAL / 2))) {
246 1.1 tls if (!(c->reseed_pending)) {
247 1.1 tls c->reseed_pending = 1;
248 1.1 tls c->reseed.len = NIST_BLOCK_KEYLEN_BYTES;
249 1.1 tls rndsink_attach(&c->reseed);
250 1.1 tls }
251 1.1 tls if (__predict_false(c->drbg.reseed_counter >
252 1.1 tls NIST_CTR_DRBG_RESEED_INTERVAL)) {
253 1.1 tls goto again; /* statistical test failure */
254 1.1 tls }
255 1.1 tls }
256 1.1 tls
257 1.1 tls mutex_exit(&c->mtx);
258 1.1 tls return len;
259 1.1 tls }
260 1.1 tls
261 1.1 tls void
262 1.1 tls cprng_strong_destroy(cprng_strong_t *c)
263 1.1 tls {
264 1.1 tls KASSERT(!mutex_owned(&c->mtx));
265 1.1 tls if (c->flags & CPRNG_USE_CV) {
266 1.1 tls KASSERT(!cv_has_waiters(&c->cv));
267 1.1 tls cv_destroy(&c->cv);
268 1.1 tls }
269 1.1 tls
270 1.1 tls mutex_destroy(&c->mtx);
271 1.1 tls if (c->reseed_pending) {
272 1.1 tls rndsink_detach(&c->reseed);
273 1.1 tls }
274 1.1 tls nist_ctr_drbg_destroy(&c->drbg);
275 1.1 tls memset(c, 0, sizeof(*c));
276 1.1 tls kmem_free(c, sizeof(*c));
277 1.1 tls }
278 1.1 tls
279 1.1 tls int
280 1.1 tls cprng_strong_getflags(cprng_strong_t *const c)
281 1.1 tls {
282 1.1 tls KASSERT(mutex_owned(&c->mtx));
283 1.1 tls return c->flags;
284 1.1 tls }
285 1.1 tls
286 1.1 tls void
287 1.1 tls cprng_strong_setflags(cprng_strong_t *const c, int flags)
288 1.1 tls {
289 1.1 tls KASSERT(mutex_owned(&c->mtx));
290 1.1 tls if (flags & CPRNG_USE_CV) {
291 1.1 tls if (!(c->flags & CPRNG_USE_CV)) {
292 1.1 tls cv_init(&c->cv, (const char *)c->name);
293 1.1 tls }
294 1.1 tls } else {
295 1.1 tls if (c->flags & CPRNG_USE_CV) {
296 1.1 tls KASSERT(!cv_has_waiters(&c->cv));
297 1.1 tls cv_destroy(&c->cv);
298 1.1 tls }
299 1.1 tls }
300 1.1 tls if (flags & CPRNG_REKEY_ANY) {
301 1.1 tls if (!(c->flags & CPRNG_REKEY_ANY)) {
302 1.1 tls if (c->flags & CPRNG_USE_CV) {
303 1.1 tls cv_broadcast(&c->cv);
304 1.1 tls }
305 1.1 tls }
306 1.1 tls }
307 1.1 tls c->flags = flags;
308 1.1 tls }
309