subr_cprng.c revision 1.12.2.3 1 1.12.2.3 tls /* $NetBSD: subr_cprng.c,v 1.12.2.3 2013/06/23 06:18:58 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.12.2.3 tls __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.12.2.3 2013/06/23 06:18:58 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.8 tls cprng_strong_doreseed(cprng_strong_t *const c)
76 1.8 tls {
77 1.8 tls uint32_t cc = cprng_counter();
78 1.8 tls
79 1.8 tls KASSERT(mutex_owned(&c->mtx));
80 1.8 tls KASSERT(mutex_owned(&c->reseed.mtx));
81 1.8 tls KASSERT(c->reseed.len == NIST_BLOCK_KEYLEN_BYTES);
82 1.8 tls
83 1.8 tls if (nist_ctr_drbg_reseed(&c->drbg, c->reseed.data, c->reseed.len,
84 1.8 tls &cc, sizeof(cc))) {
85 1.8 tls panic("cprng %s: nist_ctr_drbg_reseed failed.", c->name);
86 1.8 tls }
87 1.12.2.2 tls memset(c->reseed.data, 0, c->reseed.len);
88 1.12.2.2 tls
89 1.8 tls #ifdef RND_VERBOSE
90 1.8 tls printf("cprng %s: reseeded with rnd_filled = %d\n", c->name,
91 1.8 tls rnd_filled);
92 1.8 tls #endif
93 1.8 tls c->entropy_serial = rnd_filled;
94 1.8 tls c->reseed.state = RSTATE_IDLE;
95 1.8 tls if (c->flags & CPRNG_USE_CV) {
96 1.8 tls cv_broadcast(&c->cv);
97 1.8 tls }
98 1.8 tls selnotify(&c->selq, 0, 0);
99 1.8 tls }
100 1.8 tls
101 1.8 tls static void
102 1.5 tls cprng_strong_sched_reseed(cprng_strong_t *const c)
103 1.5 tls {
104 1.5 tls KASSERT(mutex_owned(&c->mtx));
105 1.8 tls if (mutex_tryenter(&c->reseed.mtx)) {
106 1.8 tls switch (c->reseed.state) {
107 1.8 tls case RSTATE_IDLE:
108 1.8 tls c->reseed.state = RSTATE_PENDING;
109 1.8 tls c->reseed.len = NIST_BLOCK_KEYLEN_BYTES;
110 1.8 tls rndsink_attach(&c->reseed);
111 1.8 tls break;
112 1.8 tls case RSTATE_HASBITS:
113 1.8 tls /* Just rekey the underlying generator now. */
114 1.8 tls cprng_strong_doreseed(c);
115 1.8 tls break;
116 1.8 tls case RSTATE_PENDING:
117 1.8 tls if (c->entropy_serial != rnd_filled) {
118 1.8 tls rndsink_detach(&c->reseed);
119 1.8 tls rndsink_attach(&c->reseed);
120 1.8 tls }
121 1.8 tls break;
122 1.8 tls default:
123 1.8 tls panic("cprng %s: bad reseed state %d",
124 1.8 tls c->name, c->reseed.state);
125 1.8 tls break;
126 1.8 tls }
127 1.6 tls mutex_spin_exit(&c->reseed.mtx);
128 1.5 tls }
129 1.8 tls #ifdef RND_VERBOSE
130 1.8 tls else {
131 1.8 tls printf("cprng %s: skipping sched_reseed, sink busy\n",
132 1.8 tls c->name);
133 1.8 tls }
134 1.8 tls #endif
135 1.5 tls }
136 1.5 tls
137 1.5 tls static void
138 1.1 tls cprng_strong_reseed(void *const arg)
139 1.1 tls {
140 1.1 tls cprng_strong_t *c = arg;
141 1.8 tls
142 1.8 tls KASSERT(mutex_owned(&c->reseed.mtx));
143 1.8 tls KASSERT(RSTATE_HASBITS == c->reseed.state);
144 1.1 tls
145 1.7 tls if (!mutex_tryenter(&c->mtx)) {
146 1.8 tls #ifdef RND_VERBOSE
147 1.8 tls printf("cprng: sink %s cprng busy, no reseed\n", c->reseed.name);
148 1.8 tls #endif
149 1.9 tls if (c->flags & CPRNG_USE_CV) { /* XXX if flags change? */
150 1.9 tls cv_broadcast(&c->cv);
151 1.9 tls }
152 1.7 tls return;
153 1.7 tls }
154 1.7 tls
155 1.8 tls cprng_strong_doreseed(c);
156 1.1 tls mutex_exit(&c->mtx);
157 1.1 tls }
158 1.1 tls
159 1.12.2.2 tls static size_t
160 1.12.2.3 tls cprng_entropy_try(uint8_t *key, size_t keylen)
161 1.12.2.2 tls {
162 1.12.2.2 tls int r;
163 1.12.2.2 tls r = rnd_extract_data(key, keylen, RND_EXTRACT_GOOD);
164 1.12.2.3 tls if (r != keylen) { /* Always fill in, for safety */
165 1.12.2.2 tls rnd_extract_data(key + r, keylen - r, RND_EXTRACT_ANY);
166 1.12.2.2 tls }
167 1.12.2.2 tls return r;
168 1.12.2.2 tls }
169 1.12.2.2 tls
170 1.1 tls cprng_strong_t *
171 1.1 tls cprng_strong_create(const char *const name, int ipl, int flags)
172 1.1 tls {
173 1.1 tls cprng_strong_t *c;
174 1.1 tls uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
175 1.5 tls int r, getmore = 0, hard = 0;
176 1.1 tls uint32_t cc;
177 1.1 tls
178 1.1 tls c = kmem_alloc(sizeof(*c), KM_NOSLEEP);
179 1.1 tls if (c == NULL) {
180 1.1 tls return NULL;
181 1.1 tls }
182 1.1 tls c->flags = flags;
183 1.1 tls strlcpy(c->name, name, sizeof(c->name));
184 1.8 tls c->reseed.state = RSTATE_IDLE;
185 1.1 tls c->reseed.cb = cprng_strong_reseed;
186 1.1 tls c->reseed.arg = c;
187 1.10 tls c->entropy_serial = rnd_initial_entropy ? rnd_filled : -1;
188 1.6 tls mutex_init(&c->reseed.mtx, MUTEX_DEFAULT, IPL_VM);
189 1.1 tls strlcpy(c->reseed.name, name, sizeof(c->reseed.name));
190 1.1 tls
191 1.1 tls mutex_init(&c->mtx, MUTEX_DEFAULT, ipl);
192 1.1 tls
193 1.1 tls if (c->flags & CPRNG_USE_CV) {
194 1.12.2.2 tls cv_init(&c->cv, (const char *)c->name);
195 1.1 tls }
196 1.1 tls
197 1.5 tls selinit(&c->selq);
198 1.5 tls
199 1.12.2.3 tls r = cprng_entropy_try(key, sizeof(key));
200 1.1 tls if (r != sizeof(key)) {
201 1.1 tls if (c->flags & CPRNG_INIT_ANY) {
202 1.5 tls #ifdef DEBUG
203 1.12.2.3 tls /*
204 1.12.2.3 tls * If we have ever crossed the pool's
205 1.12.2.3 tls * minimum-entropy threshold, then we are
206 1.12.2.3 tls * providing cryptographically strong
207 1.12.2.3 tls * random output -- if not information-
208 1.12.2.3 tls * theoretically strong. Warn elsewise.
209 1.12.2.3 tls */
210 1.12.2.3 tls if (!rnd_initial_entropy) {
211 1.12.2.3 tls printf("cprng %s: WARNING insufficient "
212 1.12.2.3 tls "entropy at creation.\n", name);
213 1.12.2.3 tls }
214 1.5 tls #endif
215 1.1 tls } else {
216 1.5 tls hard++;
217 1.1 tls }
218 1.1 tls getmore++;
219 1.1 tls }
220 1.1 tls
221 1.1 tls if (nist_ctr_drbg_instantiate(&c->drbg, key, sizeof(key),
222 1.1 tls &cc, sizeof(cc), name, strlen(name))) {
223 1.1 tls panic("cprng %s: instantiation failed.", name);
224 1.1 tls }
225 1.1 tls
226 1.1 tls if (getmore) {
227 1.5 tls /* Cause readers to wait for rekeying. */
228 1.5 tls if (hard) {
229 1.5 tls c->drbg.reseed_counter =
230 1.5 tls NIST_CTR_DRBG_RESEED_INTERVAL + 1;
231 1.5 tls } else {
232 1.5 tls c->drbg.reseed_counter =
233 1.5 tls (NIST_CTR_DRBG_RESEED_INTERVAL / 2) + 1;
234 1.1 tls }
235 1.1 tls }
236 1.1 tls return c;
237 1.1 tls }
238 1.1 tls
239 1.1 tls size_t
240 1.5 tls cprng_strong(cprng_strong_t *const c, void *const p, size_t len, int flags)
241 1.1 tls {
242 1.1 tls uint32_t cc = cprng_counter();
243 1.5 tls #ifdef DEBUG
244 1.5 tls int testfail = 0;
245 1.5 tls #endif
246 1.1 tls if (len > CPRNG_MAX_LEN) { /* XXX should we loop? */
247 1.1 tls len = CPRNG_MAX_LEN; /* let the caller loop if desired */
248 1.1 tls }
249 1.5 tls mutex_enter(&c->mtx);
250 1.1 tls
251 1.10 tls /* If we were initialized with the pool empty, rekey ASAP */
252 1.10 tls if (__predict_false(c->entropy_serial == -1) && rnd_initial_entropy) {
253 1.11 tls c->entropy_serial = 0;
254 1.10 tls goto rekeyany; /* We have _some_ entropy, use it. */
255 1.10 tls }
256 1.10 tls
257 1.1 tls if (nist_ctr_drbg_generate(&c->drbg, p, len, &cc, sizeof(cc))) {
258 1.1 tls /* A generator failure really means we hit the hard limit. */
259 1.10 tls rekeyany:
260 1.1 tls if (c->flags & CPRNG_REKEY_ANY) {
261 1.1 tls uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
262 1.1 tls
263 1.12.2.3 tls if (cprng_entropy_try(key, sizeof(key)) !=
264 1.12.2.2 tls sizeof(key)) {
265 1.12.2.2 tls printf("cprng %s: WARNING "
266 1.12.2.2 tls "pseudorandom rekeying.\n", c->name);
267 1.12.2.2 tls }
268 1.1 tls cc = cprng_counter();
269 1.1 tls if (nist_ctr_drbg_reseed(&c->drbg, key, sizeof(key),
270 1.1 tls &cc, sizeof(cc))) {
271 1.1 tls panic("cprng %s: nist_ctr_drbg_reseed "
272 1.1 tls "failed.", c->name);
273 1.1 tls }
274 1.12.2.2 tls memset(key, 0, sizeof(key));
275 1.1 tls } else {
276 1.9 tls int wr;
277 1.1 tls
278 1.9 tls do {
279 1.5 tls cprng_strong_sched_reseed(c);
280 1.9 tls if ((flags & FNONBLOCK) ||
281 1.9 tls !(c->flags & CPRNG_USE_CV)) {
282 1.9 tls len = 0;
283 1.9 tls break;
284 1.9 tls }
285 1.9 tls /*
286 1.9 tls * XXX There's a race with the cv_broadcast
287 1.9 tls * XXX in cprng_strong_sched_reseed, because
288 1.9 tls * XXX of the use of tryenter in that function.
289 1.9 tls * XXX This "timedwait" hack works around it,
290 1.9 tls * XXX at the expense of occasionaly polling
291 1.9 tls * XXX for success on a /dev/random rekey.
292 1.9 tls */
293 1.9 tls wr = cv_timedwait_sig(&c->cv, &c->mtx,
294 1.9 tls mstohz(100));
295 1.9 tls if (wr == ERESTART) {
296 1.9 tls mutex_exit(&c->mtx);
297 1.9 tls return 0;
298 1.9 tls }
299 1.9 tls } while (nist_ctr_drbg_generate(&c->drbg, p,
300 1.9 tls len, &cc,
301 1.9 tls sizeof(cc)));
302 1.1 tls }
303 1.1 tls }
304 1.1 tls
305 1.5 tls #ifdef DEBUG
306 1.1 tls /*
307 1.1 tls * If the generator has just been keyed, perform
308 1.1 tls * the statistical RNG test.
309 1.1 tls */
310 1.12 msaitoh if (__predict_false(c->drbg.reseed_counter == 1) &&
311 1.12 msaitoh (flags & FASYNC) == 0) {
312 1.12.2.1 tls rngtest_t *rt = kmem_intr_alloc(sizeof(*rt), KM_NOSLEEP);
313 1.1 tls
314 1.5 tls if (rt) {
315 1.1 tls
316 1.5 tls strncpy(rt->rt_name, c->name, sizeof(rt->rt_name));
317 1.5 tls
318 1.5 tls if (nist_ctr_drbg_generate(&c->drbg, rt->rt_b,
319 1.5 tls sizeof(rt->rt_b), NULL, 0)) {
320 1.5 tls panic("cprng %s: nist_ctr_drbg_generate "
321 1.5 tls "failed!", c->name);
322 1.1 tls
323 1.5 tls }
324 1.5 tls testfail = rngtest(rt);
325 1.5 tls
326 1.5 tls if (testfail) {
327 1.5 tls printf("cprng %s: failed statistical RNG "
328 1.5 tls "test.\n", c->name);
329 1.5 tls c->drbg.reseed_counter =
330 1.5 tls NIST_CTR_DRBG_RESEED_INTERVAL + 1;
331 1.5 tls len = 0;
332 1.5 tls }
333 1.5 tls memset(rt, 0, sizeof(*rt));
334 1.12.2.1 tls kmem_intr_free(rt, sizeof(*rt));
335 1.1 tls }
336 1.1 tls }
337 1.1 tls #endif
338 1.1 tls if (__predict_false(c->drbg.reseed_counter >
339 1.5 tls (NIST_CTR_DRBG_RESEED_INTERVAL / 2))) {
340 1.5 tls cprng_strong_sched_reseed(c);
341 1.8 tls } else if (rnd_full) {
342 1.8 tls if (c->entropy_serial != rnd_filled) {
343 1.8 tls #ifdef RND_VERBOSE
344 1.8 tls printf("cprng %s: reseeding from full pool "
345 1.8 tls "(serial %d vs pool %d)\n", c->name,
346 1.8 tls c->entropy_serial, rnd_filled);
347 1.8 tls #endif
348 1.5 tls cprng_strong_sched_reseed(c);
349 1.1 tls }
350 1.1 tls }
351 1.1 tls
352 1.1 tls mutex_exit(&c->mtx);
353 1.1 tls return len;
354 1.1 tls }
355 1.1 tls
356 1.1 tls void
357 1.1 tls cprng_strong_destroy(cprng_strong_t *c)
358 1.1 tls {
359 1.7 tls mutex_enter(&c->mtx);
360 1.6 tls mutex_spin_enter(&c->reseed.mtx);
361 1.5 tls
362 1.1 tls if (c->flags & CPRNG_USE_CV) {
363 1.1 tls KASSERT(!cv_has_waiters(&c->cv));
364 1.1 tls cv_destroy(&c->cv);
365 1.1 tls }
366 1.5 tls seldestroy(&c->selq);
367 1.1 tls
368 1.8 tls if (RSTATE_PENDING == c->reseed.state) {
369 1.1 tls rndsink_detach(&c->reseed);
370 1.1 tls }
371 1.6 tls mutex_spin_exit(&c->reseed.mtx);
372 1.7 tls mutex_destroy(&c->reseed.mtx);
373 1.6 tls
374 1.1 tls nist_ctr_drbg_destroy(&c->drbg);
375 1.5 tls
376 1.5 tls mutex_exit(&c->mtx);
377 1.5 tls mutex_destroy(&c->mtx);
378 1.5 tls
379 1.1 tls memset(c, 0, sizeof(*c));
380 1.1 tls kmem_free(c, sizeof(*c));
381 1.1 tls }
382 1.1 tls
383 1.1 tls int
384 1.1 tls cprng_strong_getflags(cprng_strong_t *const c)
385 1.1 tls {
386 1.1 tls KASSERT(mutex_owned(&c->mtx));
387 1.1 tls return c->flags;
388 1.1 tls }
389 1.1 tls
390 1.1 tls void
391 1.1 tls cprng_strong_setflags(cprng_strong_t *const c, int flags)
392 1.1 tls {
393 1.1 tls KASSERT(mutex_owned(&c->mtx));
394 1.1 tls if (flags & CPRNG_USE_CV) {
395 1.1 tls if (!(c->flags & CPRNG_USE_CV)) {
396 1.1 tls cv_init(&c->cv, (const char *)c->name);
397 1.1 tls }
398 1.1 tls } else {
399 1.1 tls if (c->flags & CPRNG_USE_CV) {
400 1.1 tls KASSERT(!cv_has_waiters(&c->cv));
401 1.1 tls cv_destroy(&c->cv);
402 1.1 tls }
403 1.1 tls }
404 1.1 tls if (flags & CPRNG_REKEY_ANY) {
405 1.1 tls if (!(c->flags & CPRNG_REKEY_ANY)) {
406 1.1 tls if (c->flags & CPRNG_USE_CV) {
407 1.1 tls cv_broadcast(&c->cv);
408 1.1 tls }
409 1.5 tls selnotify(&c->selq, 0, 0);
410 1.1 tls }
411 1.1 tls }
412 1.1 tls c->flags = flags;
413 1.1 tls }
414