subr_cprng.c revision 1.19 1 1.19 riastrad /* $NetBSD: subr_cprng.c,v 1.19 2013/06/24 00:56:21 riastradh Exp $ */
2 1.1 tls
3 1.1 tls /*-
4 1.18 riastrad * Copyright (c) 2011-2013 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.18 riastrad * by Thor Lancelot Simon and Taylor R. Campbell.
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.18 riastrad #include <sys/cdefs.h>
33 1.19 riastrad __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.19 2013/06/24 00:56:21 riastradh Exp $");
34 1.18 riastrad
35 1.18 riastrad #include <sys/param.h>
36 1.1 tls #include <sys/types.h>
37 1.18 riastrad #include <sys/condvar.h>
38 1.18 riastrad #include <sys/cprng.h>
39 1.18 riastrad #include <sys/errno.h>
40 1.18 riastrad #include <sys/event.h> /* XXX struct knote */
41 1.18 riastrad #include <sys/fcntl.h> /* XXX FNONBLOCK */
42 1.1 tls #include <sys/kernel.h>
43 1.18 riastrad #include <sys/kmem.h>
44 1.19 riastrad #include <sys/lwp.h>
45 1.18 riastrad #include <sys/poll.h> /* XXX POLLIN/POLLOUT/&c. */
46 1.18 riastrad #include <sys/select.h>
47 1.1 tls #include <sys/systm.h>
48 1.18 riastrad #include <sys/rnd.h>
49 1.18 riastrad #include <sys/rndsink.h>
50 1.18 riastrad #if DEBUG
51 1.1 tls #include <sys/rngtest.h>
52 1.18 riastrad #endif
53 1.18 riastrad
54 1.18 riastrad #include <crypto/nist_ctr_drbg/nist_ctr_drbg.h>
55 1.1 tls
56 1.2 tsutsui #if defined(__HAVE_CPU_COUNTER)
57 1.1 tls #include <machine/cpu_counter.h>
58 1.2 tsutsui #endif
59 1.1 tls
60 1.18 riastrad static void cprng_strong_generate(struct cprng_strong *, void *, size_t);
61 1.18 riastrad static void cprng_strong_reseed(struct cprng_strong *);
62 1.18 riastrad static void cprng_strong_reseed_from(struct cprng_strong *, const void *,
63 1.18 riastrad size_t, bool);
64 1.18 riastrad #if DEBUG
65 1.18 riastrad static void cprng_strong_rngtest(struct cprng_strong *);
66 1.18 riastrad #endif
67 1.1 tls
68 1.18 riastrad static rndsink_callback_t cprng_strong_rndsink_callback;
69 1.1 tls
70 1.1 tls void
71 1.1 tls cprng_init(void)
72 1.1 tls {
73 1.1 tls nist_ctr_initialize();
74 1.1 tls }
75 1.1 tls
76 1.1 tls static inline uint32_t
77 1.1 tls cprng_counter(void)
78 1.1 tls {
79 1.1 tls struct timeval tv;
80 1.1 tls
81 1.1 tls #if defined(__HAVE_CPU_COUNTER)
82 1.1 tls if (cpu_hascounter())
83 1.1 tls return cpu_counter32();
84 1.1 tls #endif
85 1.1 tls if (__predict_false(cold)) {
86 1.1 tls /* microtime unsafe if clock not running yet */
87 1.1 tls return 0;
88 1.1 tls }
89 1.1 tls microtime(&tv);
90 1.1 tls return (tv.tv_sec * 1000000 + tv.tv_usec);
91 1.1 tls }
92 1.1 tls
93 1.18 riastrad struct cprng_strong {
94 1.18 riastrad char cs_name[16];
95 1.18 riastrad int cs_flags;
96 1.18 riastrad kmutex_t cs_lock;
97 1.18 riastrad kcondvar_t cs_cv;
98 1.18 riastrad struct selinfo cs_selq;
99 1.18 riastrad struct rndsink *cs_rndsink;
100 1.18 riastrad bool cs_ready;
101 1.18 riastrad NIST_CTR_DRBG cs_drbg;
102 1.18 riastrad };
103 1.18 riastrad
104 1.18 riastrad struct cprng_strong *
105 1.18 riastrad cprng_strong_create(const char *name, int ipl, int flags)
106 1.18 riastrad {
107 1.18 riastrad const uint32_t cc = cprng_counter();
108 1.18 riastrad struct cprng_strong *const cprng = kmem_alloc(sizeof(*cprng),
109 1.18 riastrad KM_SLEEP);
110 1.18 riastrad
111 1.18 riastrad /*
112 1.18 riastrad * rndsink_request takes a spin lock at IPL_VM, so we can be no
113 1.18 riastrad * higher than that.
114 1.18 riastrad */
115 1.18 riastrad KASSERT(ipl <= IPL_VM);
116 1.18 riastrad
117 1.18 riastrad /* Initialize the easy fields. */
118 1.18 riastrad (void)strlcpy(cprng->cs_name, name, sizeof(cprng->cs_name));
119 1.18 riastrad cprng->cs_flags = flags;
120 1.18 riastrad mutex_init(&cprng->cs_lock, MUTEX_DEFAULT, ipl);
121 1.18 riastrad cv_init(&cprng->cs_cv, cprng->cs_name);
122 1.18 riastrad selinit(&cprng->cs_selq);
123 1.18 riastrad cprng->cs_rndsink = rndsink_create(NIST_BLOCK_KEYLEN_BYTES,
124 1.18 riastrad &cprng_strong_rndsink_callback, cprng);
125 1.18 riastrad
126 1.18 riastrad /* Get some initial entropy. Record whether it is full entropy. */
127 1.18 riastrad uint8_t seed[NIST_BLOCK_KEYLEN_BYTES];
128 1.18 riastrad cprng->cs_ready = rndsink_request(cprng->cs_rndsink, seed,
129 1.18 riastrad sizeof(seed));
130 1.18 riastrad if (nist_ctr_drbg_instantiate(&cprng->cs_drbg, seed, sizeof(seed),
131 1.18 riastrad &cc, sizeof(cc), cprng->cs_name, sizeof(cprng->cs_name)))
132 1.18 riastrad /* XXX Fix nist_ctr_drbg API so this can't happen. */
133 1.18 riastrad panic("cprng %s: NIST CTR_DRBG instantiation failed",
134 1.18 riastrad cprng->cs_name);
135 1.18 riastrad explicit_bzero(seed, sizeof(seed));
136 1.18 riastrad
137 1.18 riastrad if (!cprng->cs_ready && !ISSET(flags, CPRNG_INIT_ANY))
138 1.18 riastrad printf("cprng %s: creating with partial entropy\n",
139 1.18 riastrad cprng->cs_name);
140 1.18 riastrad
141 1.18 riastrad return cprng;
142 1.18 riastrad }
143 1.18 riastrad
144 1.18 riastrad void
145 1.18 riastrad cprng_strong_destroy(struct cprng_strong *cprng)
146 1.8 tls {
147 1.8 tls
148 1.18 riastrad /*
149 1.18 riastrad * Destroy the rndsink first to prevent calls to the callback.
150 1.18 riastrad */
151 1.18 riastrad rndsink_destroy(cprng->cs_rndsink);
152 1.8 tls
153 1.18 riastrad KASSERT(!cv_has_waiters(&cprng->cs_cv));
154 1.18 riastrad #if 0
155 1.18 riastrad KASSERT(!select_has_waiters(&cprng->cs_selq)) /* XXX ? */
156 1.18 riastrad #endif
157 1.15 tls
158 1.18 riastrad nist_ctr_drbg_destroy(&cprng->cs_drbg);
159 1.18 riastrad seldestroy(&cprng->cs_selq);
160 1.18 riastrad cv_destroy(&cprng->cs_cv);
161 1.18 riastrad mutex_destroy(&cprng->cs_lock);
162 1.18 riastrad
163 1.18 riastrad explicit_bzero(cprng, sizeof(*cprng)); /* paranoia */
164 1.18 riastrad kmem_free(cprng, sizeof(*cprng));
165 1.8 tls }
166 1.8 tls
167 1.18 riastrad /*
168 1.18 riastrad * Generate some data from cprng. Block or return zero bytes,
169 1.18 riastrad * depending on flags & FNONBLOCK, if cprng was created without
170 1.18 riastrad * CPRNG_REKEY_ANY.
171 1.18 riastrad */
172 1.18 riastrad size_t
173 1.18 riastrad cprng_strong(struct cprng_strong *cprng, void *buffer, size_t bytes, int flags)
174 1.5 tls {
175 1.18 riastrad size_t result;
176 1.18 riastrad
177 1.18 riastrad /* Caller must loop for more than CPRNG_MAX_LEN bytes. */
178 1.18 riastrad bytes = MIN(bytes, CPRNG_MAX_LEN);
179 1.18 riastrad
180 1.18 riastrad mutex_enter(&cprng->cs_lock);
181 1.18 riastrad
182 1.18 riastrad if (ISSET(cprng->cs_flags, CPRNG_REKEY_ANY)) {
183 1.18 riastrad if (!cprng->cs_ready)
184 1.18 riastrad cprng_strong_reseed(cprng);
185 1.18 riastrad } else {
186 1.18 riastrad while (!cprng->cs_ready) {
187 1.18 riastrad if (ISSET(flags, FNONBLOCK) ||
188 1.18 riastrad !ISSET(cprng->cs_flags, CPRNG_USE_CV) ||
189 1.18 riastrad cv_wait_sig(&cprng->cs_cv, &cprng->cs_lock)) {
190 1.18 riastrad result = 0;
191 1.18 riastrad goto out;
192 1.8 tls }
193 1.8 tls }
194 1.5 tls }
195 1.18 riastrad
196 1.18 riastrad cprng_strong_generate(cprng, buffer, bytes);
197 1.18 riastrad result = bytes;
198 1.18 riastrad
199 1.18 riastrad out: mutex_exit(&cprng->cs_lock);
200 1.18 riastrad return result;
201 1.18 riastrad }
202 1.18 riastrad
203 1.18 riastrad static void filt_cprng_detach(struct knote *);
204 1.18 riastrad static int filt_cprng_event(struct knote *, long);
205 1.18 riastrad
206 1.18 riastrad static const struct filterops cprng_filtops =
207 1.18 riastrad { 1, NULL, filt_cprng_detach, filt_cprng_event };
208 1.18 riastrad
209 1.18 riastrad int
210 1.18 riastrad cprng_strong_kqfilter(struct cprng_strong *cprng, struct knote *kn)
211 1.18 riastrad {
212 1.18 riastrad
213 1.18 riastrad switch (kn->kn_filter) {
214 1.18 riastrad case EVFILT_READ:
215 1.18 riastrad kn->kn_fop = &cprng_filtops;
216 1.18 riastrad kn->kn_hook = cprng;
217 1.18 riastrad mutex_enter(&cprng->cs_lock);
218 1.18 riastrad SLIST_INSERT_HEAD(&cprng->cs_selq.sel_klist, kn, kn_selnext);
219 1.18 riastrad mutex_exit(&cprng->cs_lock);
220 1.18 riastrad return 0;
221 1.18 riastrad
222 1.18 riastrad case EVFILT_WRITE:
223 1.18 riastrad default:
224 1.18 riastrad return EINVAL;
225 1.8 tls }
226 1.5 tls }
227 1.5 tls
228 1.5 tls static void
229 1.18 riastrad filt_cprng_detach(struct knote *kn)
230 1.1 tls {
231 1.18 riastrad struct cprng_strong *const cprng = kn->kn_hook;
232 1.18 riastrad
233 1.18 riastrad mutex_enter(&cprng->cs_lock);
234 1.18 riastrad SLIST_REMOVE(&cprng->cs_selq.sel_klist, kn, knote, kn_selnext);
235 1.18 riastrad mutex_exit(&cprng->cs_lock);
236 1.18 riastrad }
237 1.8 tls
238 1.18 riastrad static int
239 1.18 riastrad filt_cprng_event(struct knote *kn, long hint)
240 1.18 riastrad {
241 1.18 riastrad struct cprng_strong *const cprng = kn->kn_hook;
242 1.18 riastrad int ret;
243 1.1 tls
244 1.18 riastrad if (hint == NOTE_SUBMIT)
245 1.18 riastrad KASSERT(mutex_owned(&cprng->cs_lock));
246 1.18 riastrad else
247 1.18 riastrad mutex_enter(&cprng->cs_lock);
248 1.18 riastrad if (cprng->cs_ready) {
249 1.18 riastrad kn->kn_data = CPRNG_MAX_LEN; /* XXX Too large? */
250 1.18 riastrad ret = 1;
251 1.18 riastrad } else {
252 1.18 riastrad ret = 0;
253 1.7 tls }
254 1.18 riastrad if (hint == NOTE_SUBMIT)
255 1.18 riastrad KASSERT(mutex_owned(&cprng->cs_lock));
256 1.18 riastrad else
257 1.18 riastrad mutex_exit(&cprng->cs_lock);
258 1.7 tls
259 1.18 riastrad return ret;
260 1.1 tls }
261 1.1 tls
262 1.18 riastrad int
263 1.18 riastrad cprng_strong_poll(struct cprng_strong *cprng, int events)
264 1.15 tls {
265 1.18 riastrad int revents;
266 1.18 riastrad
267 1.18 riastrad if (!ISSET(events, (POLLIN | POLLRDNORM)))
268 1.18 riastrad return 0;
269 1.18 riastrad
270 1.18 riastrad mutex_enter(&cprng->cs_lock);
271 1.18 riastrad if (cprng->cs_ready) {
272 1.18 riastrad revents = (events & (POLLIN | POLLRDNORM));
273 1.18 riastrad } else {
274 1.18 riastrad selrecord(curlwp, &cprng->cs_selq);
275 1.18 riastrad revents = 0;
276 1.15 tls }
277 1.18 riastrad mutex_exit(&cprng->cs_lock);
278 1.18 riastrad
279 1.18 riastrad return revents;
280 1.18 riastrad }
281 1.18 riastrad
282 1.18 riastrad /*
283 1.18 riastrad * XXX Kludge for the current /dev/random implementation.
284 1.18 riastrad */
285 1.18 riastrad void
286 1.18 riastrad cprng_strong_deplete(struct cprng_strong *cprng)
287 1.18 riastrad {
288 1.18 riastrad
289 1.18 riastrad mutex_enter(&cprng->cs_lock);
290 1.18 riastrad cprng->cs_ready = false;
291 1.18 riastrad rndsink_schedule(cprng->cs_rndsink);
292 1.18 riastrad mutex_exit(&cprng->cs_lock);
293 1.18 riastrad }
294 1.18 riastrad
295 1.18 riastrad /*
296 1.18 riastrad * XXX Move nist_ctr_drbg_reseed_advised_p and
297 1.18 riastrad * nist_ctr_drbg_reseed_needed_p into the nist_ctr_drbg API and make
298 1.18 riastrad * the NIST_CTR_DRBG structure opaque.
299 1.18 riastrad */
300 1.18 riastrad static bool
301 1.18 riastrad nist_ctr_drbg_reseed_advised_p(NIST_CTR_DRBG *drbg)
302 1.18 riastrad {
303 1.18 riastrad
304 1.18 riastrad return (drbg->reseed_counter > (NIST_CTR_DRBG_RESEED_INTERVAL / 2));
305 1.15 tls }
306 1.15 tls
307 1.18 riastrad static bool
308 1.18 riastrad nist_ctr_drbg_reseed_needed_p(NIST_CTR_DRBG *drbg)
309 1.1 tls {
310 1.1 tls
311 1.18 riastrad return (drbg->reseed_counter >= NIST_CTR_DRBG_RESEED_INTERVAL);
312 1.18 riastrad }
313 1.18 riastrad
314 1.18 riastrad /*
315 1.18 riastrad * Generate some data from the underlying generator.
316 1.18 riastrad */
317 1.18 riastrad static void
318 1.18 riastrad cprng_strong_generate(struct cprng_strong *cprng, void *buffer,
319 1.18 riastrad size_t bytes)
320 1.18 riastrad {
321 1.18 riastrad const uint32_t cc = cprng_counter();
322 1.18 riastrad
323 1.18 riastrad KASSERT(bytes <= CPRNG_MAX_LEN);
324 1.18 riastrad KASSERT(mutex_owned(&cprng->cs_lock));
325 1.18 riastrad
326 1.18 riastrad /*
327 1.18 riastrad * Generate some data from the NIST CTR_DRBG. Caller
328 1.18 riastrad * guarantees reseed if we're not ready, and if we exhaust the
329 1.18 riastrad * generator, we mark ourselves not ready. Consequently, this
330 1.18 riastrad * call to the CTR_DRBG should not fail.
331 1.18 riastrad */
332 1.18 riastrad if (__predict_false(nist_ctr_drbg_generate(&cprng->cs_drbg, buffer,
333 1.18 riastrad bytes, &cc, sizeof(cc))))
334 1.18 riastrad panic("cprng %s: NIST CTR_DRBG failed", cprng->cs_name);
335 1.1 tls
336 1.18 riastrad /*
337 1.18 riastrad * If we've been seeing a lot of use, ask for some fresh
338 1.18 riastrad * entropy soon.
339 1.18 riastrad */
340 1.18 riastrad if (__predict_false(nist_ctr_drbg_reseed_advised_p(&cprng->cs_drbg)))
341 1.18 riastrad rndsink_schedule(cprng->cs_rndsink);
342 1.1 tls
343 1.18 riastrad /*
344 1.18 riastrad * If we just exhausted the generator, inform the next user
345 1.18 riastrad * that we need a reseed.
346 1.18 riastrad *
347 1.18 riastrad * XXX For /dev/random CPRNGs, the criterion is supposed to be
348 1.18 riastrad * `Has this seeding generated 32 bytes?'.
349 1.18 riastrad */
350 1.18 riastrad if (__predict_false(nist_ctr_drbg_reseed_needed_p(&cprng->cs_drbg))) {
351 1.18 riastrad cprng->cs_ready = false;
352 1.18 riastrad rndsink_schedule(cprng->cs_rndsink); /* paranoia */
353 1.1 tls }
354 1.18 riastrad }
355 1.1 tls
356 1.18 riastrad /*
357 1.18 riastrad * Reseed with whatever we can get from the system entropy pool right now.
358 1.18 riastrad */
359 1.18 riastrad static void
360 1.18 riastrad cprng_strong_reseed(struct cprng_strong *cprng)
361 1.18 riastrad {
362 1.18 riastrad uint8_t seed[NIST_BLOCK_KEYLEN_BYTES];
363 1.5 tls
364 1.18 riastrad KASSERT(mutex_owned(&cprng->cs_lock));
365 1.1 tls
366 1.18 riastrad const bool full_entropy = rndsink_request(cprng->cs_rndsink, seed,
367 1.18 riastrad sizeof(seed));
368 1.18 riastrad cprng_strong_reseed_from(cprng, seed, sizeof(seed), full_entropy);
369 1.18 riastrad explicit_bzero(seed, sizeof(seed));
370 1.1 tls }
371 1.1 tls
372 1.18 riastrad /*
373 1.18 riastrad * Reseed with the given seed. If we now have full entropy, notify waiters.
374 1.18 riastrad */
375 1.18 riastrad static void
376 1.18 riastrad cprng_strong_reseed_from(struct cprng_strong *cprng,
377 1.18 riastrad const void *seed, size_t bytes, bool full_entropy)
378 1.1 tls {
379 1.18 riastrad const uint32_t cc = cprng_counter();
380 1.1 tls
381 1.18 riastrad KASSERT(bytes == NIST_BLOCK_KEYLEN_BYTES);
382 1.18 riastrad KASSERT(mutex_owned(&cprng->cs_lock));
383 1.1 tls
384 1.1 tls /*
385 1.18 riastrad * Notify anyone interested in the partiality of entropy in our
386 1.18 riastrad * seed -- anyone waiting for full entropy, or any system
387 1.18 riastrad * operators interested in knowing when the entropy pool is
388 1.18 riastrad * running on fumes.
389 1.1 tls */
390 1.18 riastrad if (full_entropy) {
391 1.18 riastrad if (!cprng->cs_ready) {
392 1.18 riastrad cprng->cs_ready = true;
393 1.18 riastrad cv_broadcast(&cprng->cs_cv);
394 1.18 riastrad selnotify(&cprng->cs_selq, (POLLIN | POLLRDNORM),
395 1.18 riastrad NOTE_SUBMIT);
396 1.18 riastrad }
397 1.18 riastrad } else {
398 1.18 riastrad /*
399 1.18 riastrad * XXX Is there is any harm in reseeding with partial
400 1.18 riastrad * entropy when we had full entropy before? If so,
401 1.18 riastrad * remove the conditional on this message.
402 1.18 riastrad */
403 1.18 riastrad if (!cprng->cs_ready &&
404 1.18 riastrad !ISSET(cprng->cs_flags, CPRNG_REKEY_ANY))
405 1.18 riastrad printf("cprng %s: reseeding with partial entropy\n",
406 1.18 riastrad cprng->cs_name);
407 1.18 riastrad }
408 1.18 riastrad
409 1.18 riastrad if (nist_ctr_drbg_reseed(&cprng->cs_drbg, seed, bytes, &cc, sizeof(cc)))
410 1.18 riastrad /* XXX Fix nist_ctr_drbg API so this can't happen. */
411 1.18 riastrad panic("cprng %s: NIST CTR_DRBG reseed failed", cprng->cs_name);
412 1.5 tls
413 1.18 riastrad #if DEBUG
414 1.18 riastrad cprng_strong_rngtest(cprng);
415 1.8 tls #endif
416 1.1 tls }
417 1.1 tls
418 1.18 riastrad #if DEBUG
419 1.18 riastrad /*
420 1.18 riastrad * Generate some output and apply a statistical RNG test to it.
421 1.18 riastrad */
422 1.18 riastrad static void
423 1.18 riastrad cprng_strong_rngtest(struct cprng_strong *cprng)
424 1.1 tls {
425 1.5 tls
426 1.18 riastrad KASSERT(mutex_owned(&cprng->cs_lock));
427 1.18 riastrad
428 1.18 riastrad /* XXX Switch to a pool cache instead? */
429 1.18 riastrad rngtest_t *const rt = kmem_intr_alloc(sizeof(*rt), KM_NOSLEEP);
430 1.18 riastrad if (rt == NULL)
431 1.18 riastrad /* XXX Warn? */
432 1.18 riastrad return;
433 1.1 tls
434 1.18 riastrad (void)strlcpy(rt->rt_name, cprng->cs_name, sizeof(rt->rt_name));
435 1.6 tls
436 1.18 riastrad if (nist_ctr_drbg_generate(&cprng->cs_drbg, rt->rt_b, sizeof(rt->rt_b),
437 1.18 riastrad NULL, 0))
438 1.18 riastrad panic("cprng %s: NIST CTR_DRBG failed after reseed",
439 1.18 riastrad cprng->cs_name);
440 1.5 tls
441 1.18 riastrad if (rngtest(rt)) {
442 1.18 riastrad printf("cprng %s: failed statistical RNG test\n",
443 1.18 riastrad cprng->cs_name);
444 1.18 riastrad /* XXX Not clear that this does any good... */
445 1.18 riastrad cprng->cs_ready = false;
446 1.18 riastrad rndsink_schedule(cprng->cs_rndsink);
447 1.18 riastrad }
448 1.5 tls
449 1.18 riastrad explicit_bzero(rt, sizeof(*rt)); /* paranoia */
450 1.18 riastrad kmem_intr_free(rt, sizeof(*rt));
451 1.1 tls }
452 1.18 riastrad #endif
453 1.1 tls
454 1.18 riastrad /*
455 1.18 riastrad * Feed entropy from an rndsink request into the CPRNG for which the
456 1.18 riastrad * request was issued.
457 1.18 riastrad */
458 1.18 riastrad static void
459 1.18 riastrad cprng_strong_rndsink_callback(void *context, const void *seed, size_t bytes)
460 1.1 tls {
461 1.18 riastrad struct cprng_strong *const cprng = context;
462 1.1 tls
463 1.18 riastrad mutex_enter(&cprng->cs_lock);
464 1.18 riastrad /* Assume that rndsinks provide only full-entropy output. */
465 1.18 riastrad cprng_strong_reseed_from(cprng, seed, bytes, true);
466 1.18 riastrad mutex_exit(&cprng->cs_lock);
467 1.1 tls }
468