Home | History | Annotate | Line # | Download | only in kern
subr_cprng.c revision 1.20.2.2
      1  1.20.2.2     rmind /*	$NetBSD: subr_cprng.c,v 1.20.2.2 2014/05/18 17:46:07 rmind 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.20.2.2     rmind __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.20.2.2 2014/05/18 17:46:07 rmind 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.20.2.2     rmind #include <sys/once.h>
     46      1.18  riastrad #include <sys/poll.h>		/* XXX POLLIN/POLLOUT/&c. */
     47      1.18  riastrad #include <sys/select.h>
     48       1.1       tls #include <sys/systm.h>
     49  1.20.2.2     rmind #include <sys/sysctl.h>
     50      1.18  riastrad #include <sys/rnd.h>
     51      1.18  riastrad #include <sys/rndsink.h>
     52      1.18  riastrad #if DEBUG
     53       1.1       tls #include <sys/rngtest.h>
     54      1.18  riastrad #endif
     55      1.18  riastrad 
     56      1.18  riastrad #include <crypto/nist_ctr_drbg/nist_ctr_drbg.h>
     57       1.1       tls 
     58       1.2   tsutsui #if defined(__HAVE_CPU_COUNTER)
     59       1.1       tls #include <machine/cpu_counter.h>
     60       1.2   tsutsui #endif
     61       1.1       tls 
     62  1.20.2.2     rmind static int sysctl_kern_urnd(SYSCTLFN_PROTO);
     63  1.20.2.2     rmind static int sysctl_kern_arnd(SYSCTLFN_PROTO);
     64  1.20.2.2     rmind 
     65      1.18  riastrad static void	cprng_strong_generate(struct cprng_strong *, void *, size_t);
     66      1.18  riastrad static void	cprng_strong_reseed(struct cprng_strong *);
     67      1.18  riastrad static void	cprng_strong_reseed_from(struct cprng_strong *, const void *,
     68      1.18  riastrad 		    size_t, bool);
     69      1.18  riastrad #if DEBUG
     70      1.18  riastrad static void	cprng_strong_rngtest(struct cprng_strong *);
     71      1.18  riastrad #endif
     72       1.1       tls 
     73      1.18  riastrad static rndsink_callback_t	cprng_strong_rndsink_callback;
     74       1.1       tls 
     75       1.1       tls void
     76       1.1       tls cprng_init(void)
     77       1.1       tls {
     78  1.20.2.2     rmind 	static struct sysctllog *random_sysctllog;
     79  1.20.2.2     rmind 
     80       1.1       tls 	nist_ctr_initialize();
     81  1.20.2.2     rmind 
     82  1.20.2.2     rmind 	sysctl_createv(&random_sysctllog, 0, NULL, NULL,
     83  1.20.2.2     rmind 		       CTLFLAG_PERMANENT,
     84  1.20.2.2     rmind 		       CTLTYPE_INT, "urandom",
     85  1.20.2.2     rmind 		       SYSCTL_DESCR("Random integer value"),
     86  1.20.2.2     rmind 		       sysctl_kern_urnd, 0, NULL, 0,
     87  1.20.2.2     rmind 		       CTL_KERN, KERN_URND, CTL_EOL);
     88  1.20.2.2     rmind 	sysctl_createv(&random_sysctllog, 0, NULL, NULL,
     89  1.20.2.2     rmind 		       CTLFLAG_PERMANENT,
     90  1.20.2.2     rmind 		       CTLTYPE_INT, "arandom",
     91  1.20.2.2     rmind 		       SYSCTL_DESCR("n bytes of random data"),
     92  1.20.2.2     rmind 		       sysctl_kern_arnd, 0, NULL, 0,
     93  1.20.2.2     rmind 		       CTL_KERN, KERN_ARND, CTL_EOL);
     94       1.1       tls }
     95       1.1       tls 
     96       1.1       tls static inline uint32_t
     97       1.1       tls cprng_counter(void)
     98       1.1       tls {
     99       1.1       tls 	struct timeval tv;
    100       1.1       tls 
    101       1.1       tls #if defined(__HAVE_CPU_COUNTER)
    102       1.1       tls 	if (cpu_hascounter())
    103       1.1       tls 		return cpu_counter32();
    104       1.1       tls #endif
    105       1.1       tls 	if (__predict_false(cold)) {
    106       1.1       tls 		/* microtime unsafe if clock not running yet */
    107       1.1       tls 		return 0;
    108       1.1       tls 	}
    109       1.1       tls 	microtime(&tv);
    110       1.1       tls 	return (tv.tv_sec * 1000000 + tv.tv_usec);
    111       1.1       tls }
    112       1.1       tls 
    113      1.18  riastrad struct cprng_strong {
    114      1.18  riastrad 	char		cs_name[16];
    115      1.18  riastrad 	int		cs_flags;
    116      1.18  riastrad 	kmutex_t	cs_lock;
    117      1.18  riastrad 	kcondvar_t	cs_cv;
    118      1.18  riastrad 	struct selinfo	cs_selq;
    119      1.18  riastrad 	struct rndsink	*cs_rndsink;
    120      1.18  riastrad 	bool		cs_ready;
    121      1.18  riastrad 	NIST_CTR_DRBG	cs_drbg;
    122  1.20.2.1     rmind 
    123  1.20.2.1     rmind 	/* XXX Kludge for /dev/random `information-theoretic' properties.   */
    124  1.20.2.1     rmind 	unsigned int	cs_remaining;
    125      1.18  riastrad };
    126      1.18  riastrad 
    127      1.18  riastrad struct cprng_strong *
    128      1.18  riastrad cprng_strong_create(const char *name, int ipl, int flags)
    129      1.18  riastrad {
    130      1.18  riastrad 	const uint32_t cc = cprng_counter();
    131      1.18  riastrad 	struct cprng_strong *const cprng = kmem_alloc(sizeof(*cprng),
    132      1.18  riastrad 	    KM_SLEEP);
    133      1.18  riastrad 
    134      1.18  riastrad 	/*
    135      1.18  riastrad 	 * rndsink_request takes a spin lock at IPL_VM, so we can be no
    136      1.18  riastrad 	 * higher than that.
    137      1.18  riastrad 	 */
    138  1.20.2.1     rmind 	KASSERT(ipl != IPL_SCHED && ipl != IPL_HIGH);
    139      1.18  riastrad 
    140      1.18  riastrad 	/* Initialize the easy fields.  */
    141      1.18  riastrad 	(void)strlcpy(cprng->cs_name, name, sizeof(cprng->cs_name));
    142      1.18  riastrad 	cprng->cs_flags = flags;
    143      1.18  riastrad 	mutex_init(&cprng->cs_lock, MUTEX_DEFAULT, ipl);
    144      1.18  riastrad 	cv_init(&cprng->cs_cv, cprng->cs_name);
    145      1.18  riastrad 	selinit(&cprng->cs_selq);
    146      1.18  riastrad 	cprng->cs_rndsink = rndsink_create(NIST_BLOCK_KEYLEN_BYTES,
    147      1.18  riastrad 	    &cprng_strong_rndsink_callback, cprng);
    148      1.18  riastrad 
    149      1.18  riastrad 	/* Get some initial entropy.  Record whether it is full entropy.  */
    150      1.18  riastrad 	uint8_t seed[NIST_BLOCK_KEYLEN_BYTES];
    151      1.18  riastrad 	cprng->cs_ready = rndsink_request(cprng->cs_rndsink, seed,
    152      1.18  riastrad 	    sizeof(seed));
    153      1.18  riastrad 	if (nist_ctr_drbg_instantiate(&cprng->cs_drbg, seed, sizeof(seed),
    154      1.18  riastrad 		&cc, sizeof(cc), cprng->cs_name, sizeof(cprng->cs_name)))
    155      1.18  riastrad 		/* XXX Fix nist_ctr_drbg API so this can't happen.  */
    156      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG instantiation failed",
    157      1.18  riastrad 		    cprng->cs_name);
    158      1.20  riastrad 	explicit_memset(seed, 0, sizeof(seed));
    159      1.18  riastrad 
    160  1.20.2.1     rmind 	if (ISSET(flags, CPRNG_HARD))
    161  1.20.2.1     rmind 		cprng->cs_remaining = NIST_BLOCK_KEYLEN_BYTES;
    162  1.20.2.1     rmind 	else
    163  1.20.2.1     rmind 		cprng->cs_remaining = 0;
    164  1.20.2.1     rmind 
    165      1.18  riastrad 	if (!cprng->cs_ready && !ISSET(flags, CPRNG_INIT_ANY))
    166      1.18  riastrad 		printf("cprng %s: creating with partial entropy\n",
    167      1.18  riastrad 		    cprng->cs_name);
    168      1.18  riastrad 
    169      1.18  riastrad 	return cprng;
    170      1.18  riastrad }
    171      1.18  riastrad 
    172      1.18  riastrad void
    173      1.18  riastrad cprng_strong_destroy(struct cprng_strong *cprng)
    174       1.8       tls {
    175       1.8       tls 
    176      1.18  riastrad 	/*
    177      1.18  riastrad 	 * Destroy the rndsink first to prevent calls to the callback.
    178      1.18  riastrad 	 */
    179      1.18  riastrad 	rndsink_destroy(cprng->cs_rndsink);
    180       1.8       tls 
    181      1.18  riastrad 	KASSERT(!cv_has_waiters(&cprng->cs_cv));
    182      1.18  riastrad #if 0
    183      1.18  riastrad 	KASSERT(!select_has_waiters(&cprng->cs_selq)) /* XXX ? */
    184      1.18  riastrad #endif
    185      1.15       tls 
    186      1.18  riastrad 	nist_ctr_drbg_destroy(&cprng->cs_drbg);
    187      1.18  riastrad 	seldestroy(&cprng->cs_selq);
    188      1.18  riastrad 	cv_destroy(&cprng->cs_cv);
    189      1.18  riastrad 	mutex_destroy(&cprng->cs_lock);
    190      1.18  riastrad 
    191      1.20  riastrad 	explicit_memset(cprng, 0, sizeof(*cprng)); /* paranoia */
    192      1.18  riastrad 	kmem_free(cprng, sizeof(*cprng));
    193       1.8       tls }
    194       1.8       tls 
    195      1.18  riastrad /*
    196      1.18  riastrad  * Generate some data from cprng.  Block or return zero bytes,
    197      1.18  riastrad  * depending on flags & FNONBLOCK, if cprng was created without
    198      1.18  riastrad  * CPRNG_REKEY_ANY.
    199      1.18  riastrad  */
    200      1.18  riastrad size_t
    201      1.18  riastrad cprng_strong(struct cprng_strong *cprng, void *buffer, size_t bytes, int flags)
    202       1.5       tls {
    203      1.18  riastrad 	size_t result;
    204      1.18  riastrad 
    205      1.18  riastrad 	/* Caller must loop for more than CPRNG_MAX_LEN bytes.  */
    206      1.18  riastrad 	bytes = MIN(bytes, CPRNG_MAX_LEN);
    207      1.18  riastrad 
    208      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    209      1.18  riastrad 
    210      1.18  riastrad 	if (ISSET(cprng->cs_flags, CPRNG_REKEY_ANY)) {
    211      1.18  riastrad 		if (!cprng->cs_ready)
    212      1.18  riastrad 			cprng_strong_reseed(cprng);
    213      1.18  riastrad 	} else {
    214      1.18  riastrad 		while (!cprng->cs_ready) {
    215      1.18  riastrad 			if (ISSET(flags, FNONBLOCK) ||
    216      1.18  riastrad 			    !ISSET(cprng->cs_flags, CPRNG_USE_CV) ||
    217      1.18  riastrad 			    cv_wait_sig(&cprng->cs_cv, &cprng->cs_lock)) {
    218      1.18  riastrad 				result = 0;
    219      1.18  riastrad 				goto out;
    220       1.8       tls 			}
    221       1.8       tls 		}
    222       1.5       tls 	}
    223      1.18  riastrad 
    224  1.20.2.1     rmind 	/*
    225  1.20.2.1     rmind 	 * Debit the entropy if requested.
    226  1.20.2.1     rmind 	 *
    227  1.20.2.1     rmind 	 * XXX Kludge for /dev/random `information-theoretic' properties.
    228  1.20.2.1     rmind 	 */
    229  1.20.2.1     rmind 	if (__predict_false(ISSET(cprng->cs_flags, CPRNG_HARD))) {
    230  1.20.2.1     rmind 		KASSERT(0 < cprng->cs_remaining);
    231  1.20.2.1     rmind 		KASSERT(cprng->cs_remaining <= NIST_BLOCK_KEYLEN_BYTES);
    232  1.20.2.1     rmind 		if (bytes < cprng->cs_remaining) {
    233  1.20.2.1     rmind 			cprng->cs_remaining -= bytes;
    234  1.20.2.1     rmind 		} else {
    235  1.20.2.1     rmind 			bytes = cprng->cs_remaining;
    236  1.20.2.1     rmind 			cprng->cs_remaining = NIST_BLOCK_KEYLEN_BYTES;
    237  1.20.2.1     rmind 			cprng->cs_ready = false;
    238  1.20.2.1     rmind 			rndsink_schedule(cprng->cs_rndsink);
    239  1.20.2.1     rmind 		}
    240  1.20.2.1     rmind 		KASSERT(bytes <= NIST_BLOCK_KEYLEN_BYTES);
    241  1.20.2.1     rmind 		KASSERT(0 < cprng->cs_remaining);
    242  1.20.2.1     rmind 		KASSERT(cprng->cs_remaining <= NIST_BLOCK_KEYLEN_BYTES);
    243  1.20.2.1     rmind 	}
    244  1.20.2.1     rmind 
    245      1.18  riastrad 	cprng_strong_generate(cprng, buffer, bytes);
    246      1.18  riastrad 	result = bytes;
    247      1.18  riastrad 
    248      1.18  riastrad out:	mutex_exit(&cprng->cs_lock);
    249      1.18  riastrad 	return result;
    250      1.18  riastrad }
    251      1.18  riastrad 
    252      1.18  riastrad static void	filt_cprng_detach(struct knote *);
    253      1.18  riastrad static int	filt_cprng_event(struct knote *, long);
    254      1.18  riastrad 
    255      1.18  riastrad static const struct filterops cprng_filtops =
    256      1.18  riastrad 	{ 1, NULL, filt_cprng_detach, filt_cprng_event };
    257      1.18  riastrad 
    258      1.18  riastrad int
    259      1.18  riastrad cprng_strong_kqfilter(struct cprng_strong *cprng, struct knote *kn)
    260      1.18  riastrad {
    261      1.18  riastrad 
    262      1.18  riastrad 	switch (kn->kn_filter) {
    263      1.18  riastrad 	case EVFILT_READ:
    264      1.18  riastrad 		kn->kn_fop = &cprng_filtops;
    265      1.18  riastrad 		kn->kn_hook = cprng;
    266      1.18  riastrad 		mutex_enter(&cprng->cs_lock);
    267      1.18  riastrad 		SLIST_INSERT_HEAD(&cprng->cs_selq.sel_klist, kn, kn_selnext);
    268      1.18  riastrad 		mutex_exit(&cprng->cs_lock);
    269      1.18  riastrad 		return 0;
    270      1.18  riastrad 
    271      1.18  riastrad 	case EVFILT_WRITE:
    272      1.18  riastrad 	default:
    273      1.18  riastrad 		return EINVAL;
    274       1.8       tls 	}
    275       1.5       tls }
    276       1.5       tls 
    277       1.5       tls static void
    278      1.18  riastrad filt_cprng_detach(struct knote *kn)
    279       1.1       tls {
    280      1.18  riastrad 	struct cprng_strong *const cprng = kn->kn_hook;
    281      1.18  riastrad 
    282      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    283      1.18  riastrad 	SLIST_REMOVE(&cprng->cs_selq.sel_klist, kn, knote, kn_selnext);
    284      1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    285      1.18  riastrad }
    286       1.8       tls 
    287      1.18  riastrad static int
    288      1.18  riastrad filt_cprng_event(struct knote *kn, long hint)
    289      1.18  riastrad {
    290      1.18  riastrad 	struct cprng_strong *const cprng = kn->kn_hook;
    291      1.18  riastrad 	int ret;
    292       1.1       tls 
    293      1.18  riastrad 	if (hint == NOTE_SUBMIT)
    294      1.18  riastrad 		KASSERT(mutex_owned(&cprng->cs_lock));
    295      1.18  riastrad 	else
    296      1.18  riastrad 		mutex_enter(&cprng->cs_lock);
    297      1.18  riastrad 	if (cprng->cs_ready) {
    298      1.18  riastrad 		kn->kn_data = CPRNG_MAX_LEN; /* XXX Too large?  */
    299      1.18  riastrad 		ret = 1;
    300      1.18  riastrad 	} else {
    301      1.18  riastrad 		ret = 0;
    302       1.7       tls 	}
    303      1.18  riastrad 	if (hint == NOTE_SUBMIT)
    304      1.18  riastrad 		KASSERT(mutex_owned(&cprng->cs_lock));
    305      1.18  riastrad 	else
    306      1.18  riastrad 		mutex_exit(&cprng->cs_lock);
    307       1.7       tls 
    308      1.18  riastrad 	return ret;
    309       1.1       tls }
    310       1.1       tls 
    311      1.18  riastrad int
    312      1.18  riastrad cprng_strong_poll(struct cprng_strong *cprng, int events)
    313      1.15       tls {
    314      1.18  riastrad 	int revents;
    315      1.18  riastrad 
    316      1.18  riastrad 	if (!ISSET(events, (POLLIN | POLLRDNORM)))
    317      1.18  riastrad 		return 0;
    318      1.18  riastrad 
    319      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    320      1.18  riastrad 	if (cprng->cs_ready) {
    321      1.18  riastrad 		revents = (events & (POLLIN | POLLRDNORM));
    322      1.18  riastrad 	} else {
    323      1.18  riastrad 		selrecord(curlwp, &cprng->cs_selq);
    324      1.18  riastrad 		revents = 0;
    325      1.15       tls 	}
    326      1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    327      1.18  riastrad 
    328      1.18  riastrad 	return revents;
    329      1.18  riastrad }
    330      1.18  riastrad 
    331      1.18  riastrad /*
    332      1.18  riastrad  * XXX Move nist_ctr_drbg_reseed_advised_p and
    333      1.18  riastrad  * nist_ctr_drbg_reseed_needed_p into the nist_ctr_drbg API and make
    334      1.18  riastrad  * the NIST_CTR_DRBG structure opaque.
    335      1.18  riastrad  */
    336      1.18  riastrad static bool
    337      1.18  riastrad nist_ctr_drbg_reseed_advised_p(NIST_CTR_DRBG *drbg)
    338      1.18  riastrad {
    339      1.18  riastrad 
    340      1.18  riastrad 	return (drbg->reseed_counter > (NIST_CTR_DRBG_RESEED_INTERVAL / 2));
    341      1.15       tls }
    342      1.15       tls 
    343      1.18  riastrad static bool
    344      1.18  riastrad nist_ctr_drbg_reseed_needed_p(NIST_CTR_DRBG *drbg)
    345       1.1       tls {
    346       1.1       tls 
    347      1.18  riastrad 	return (drbg->reseed_counter >= NIST_CTR_DRBG_RESEED_INTERVAL);
    348      1.18  riastrad }
    349      1.18  riastrad 
    350      1.18  riastrad /*
    351      1.18  riastrad  * Generate some data from the underlying generator.
    352      1.18  riastrad  */
    353      1.18  riastrad static void
    354  1.20.2.1     rmind cprng_strong_generate(struct cprng_strong *cprng, void *buffer, size_t bytes)
    355      1.18  riastrad {
    356      1.18  riastrad 	const uint32_t cc = cprng_counter();
    357      1.18  riastrad 
    358      1.18  riastrad 	KASSERT(bytes <= CPRNG_MAX_LEN);
    359      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    360      1.18  riastrad 
    361      1.18  riastrad 	/*
    362      1.18  riastrad 	 * Generate some data from the NIST CTR_DRBG.  Caller
    363      1.18  riastrad 	 * guarantees reseed if we're not ready, and if we exhaust the
    364      1.18  riastrad 	 * generator, we mark ourselves not ready.  Consequently, this
    365      1.18  riastrad 	 * call to the CTR_DRBG should not fail.
    366      1.18  riastrad 	 */
    367      1.18  riastrad 	if (__predict_false(nist_ctr_drbg_generate(&cprng->cs_drbg, buffer,
    368      1.18  riastrad 		    bytes, &cc, sizeof(cc))))
    369      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG failed", cprng->cs_name);
    370       1.1       tls 
    371      1.18  riastrad 	/*
    372      1.18  riastrad 	 * If we've been seeing a lot of use, ask for some fresh
    373      1.18  riastrad 	 * entropy soon.
    374      1.18  riastrad 	 */
    375      1.18  riastrad 	if (__predict_false(nist_ctr_drbg_reseed_advised_p(&cprng->cs_drbg)))
    376      1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink);
    377       1.1       tls 
    378      1.18  riastrad 	/*
    379      1.18  riastrad 	 * If we just exhausted the generator, inform the next user
    380      1.18  riastrad 	 * that we need a reseed.
    381      1.18  riastrad 	 */
    382      1.18  riastrad 	if (__predict_false(nist_ctr_drbg_reseed_needed_p(&cprng->cs_drbg))) {
    383      1.18  riastrad 		cprng->cs_ready = false;
    384      1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink); /* paranoia */
    385       1.1       tls 	}
    386      1.18  riastrad }
    387       1.1       tls 
    388      1.18  riastrad /*
    389      1.18  riastrad  * Reseed with whatever we can get from the system entropy pool right now.
    390      1.18  riastrad  */
    391      1.18  riastrad static void
    392      1.18  riastrad cprng_strong_reseed(struct cprng_strong *cprng)
    393      1.18  riastrad {
    394      1.18  riastrad 	uint8_t seed[NIST_BLOCK_KEYLEN_BYTES];
    395       1.5       tls 
    396      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    397       1.1       tls 
    398      1.18  riastrad 	const bool full_entropy = rndsink_request(cprng->cs_rndsink, seed,
    399      1.18  riastrad 	    sizeof(seed));
    400      1.18  riastrad 	cprng_strong_reseed_from(cprng, seed, sizeof(seed), full_entropy);
    401      1.20  riastrad 	explicit_memset(seed, 0, sizeof(seed));
    402       1.1       tls }
    403       1.1       tls 
    404      1.18  riastrad /*
    405      1.18  riastrad  * Reseed with the given seed.  If we now have full entropy, notify waiters.
    406      1.18  riastrad  */
    407      1.18  riastrad static void
    408      1.18  riastrad cprng_strong_reseed_from(struct cprng_strong *cprng,
    409      1.18  riastrad     const void *seed, size_t bytes, bool full_entropy)
    410       1.1       tls {
    411      1.18  riastrad 	const uint32_t cc = cprng_counter();
    412       1.1       tls 
    413      1.18  riastrad 	KASSERT(bytes == NIST_BLOCK_KEYLEN_BYTES);
    414      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    415       1.1       tls 
    416       1.1       tls 	/*
    417      1.18  riastrad 	 * Notify anyone interested in the partiality of entropy in our
    418      1.18  riastrad 	 * seed -- anyone waiting for full entropy, or any system
    419      1.18  riastrad 	 * operators interested in knowing when the entropy pool is
    420      1.18  riastrad 	 * running on fumes.
    421       1.1       tls 	 */
    422      1.18  riastrad 	if (full_entropy) {
    423      1.18  riastrad 		if (!cprng->cs_ready) {
    424      1.18  riastrad 			cprng->cs_ready = true;
    425      1.18  riastrad 			cv_broadcast(&cprng->cs_cv);
    426      1.18  riastrad 			selnotify(&cprng->cs_selq, (POLLIN | POLLRDNORM),
    427      1.18  riastrad 			    NOTE_SUBMIT);
    428      1.18  riastrad 		}
    429      1.18  riastrad 	} else {
    430      1.18  riastrad 		/*
    431      1.18  riastrad 		 * XXX Is there is any harm in reseeding with partial
    432      1.18  riastrad 		 * entropy when we had full entropy before?  If so,
    433      1.18  riastrad 		 * remove the conditional on this message.
    434      1.18  riastrad 		 */
    435      1.18  riastrad 		if (!cprng->cs_ready &&
    436      1.18  riastrad 		    !ISSET(cprng->cs_flags, CPRNG_REKEY_ANY))
    437      1.18  riastrad 			printf("cprng %s: reseeding with partial entropy\n",
    438      1.18  riastrad 			    cprng->cs_name);
    439      1.18  riastrad 	}
    440      1.18  riastrad 
    441      1.18  riastrad 	if (nist_ctr_drbg_reseed(&cprng->cs_drbg, seed, bytes, &cc, sizeof(cc)))
    442      1.18  riastrad 		/* XXX Fix nist_ctr_drbg API so this can't happen.  */
    443      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG reseed failed", cprng->cs_name);
    444       1.5       tls 
    445      1.18  riastrad #if DEBUG
    446      1.18  riastrad 	cprng_strong_rngtest(cprng);
    447       1.8       tls #endif
    448       1.1       tls }
    449       1.1       tls 
    450      1.18  riastrad #if DEBUG
    451      1.18  riastrad /*
    452      1.18  riastrad  * Generate some output and apply a statistical RNG test to it.
    453      1.18  riastrad  */
    454      1.18  riastrad static void
    455      1.18  riastrad cprng_strong_rngtest(struct cprng_strong *cprng)
    456       1.1       tls {
    457       1.5       tls 
    458      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    459      1.18  riastrad 
    460      1.18  riastrad 	/* XXX Switch to a pool cache instead?  */
    461      1.18  riastrad 	rngtest_t *const rt = kmem_intr_alloc(sizeof(*rt), KM_NOSLEEP);
    462      1.18  riastrad 	if (rt == NULL)
    463      1.18  riastrad 		/* XXX Warn?  */
    464      1.18  riastrad 		return;
    465       1.1       tls 
    466      1.18  riastrad 	(void)strlcpy(rt->rt_name, cprng->cs_name, sizeof(rt->rt_name));
    467       1.6       tls 
    468      1.18  riastrad 	if (nist_ctr_drbg_generate(&cprng->cs_drbg, rt->rt_b, sizeof(rt->rt_b),
    469      1.18  riastrad 		NULL, 0))
    470      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG failed after reseed",
    471      1.18  riastrad 		    cprng->cs_name);
    472       1.5       tls 
    473      1.18  riastrad 	if (rngtest(rt)) {
    474      1.18  riastrad 		printf("cprng %s: failed statistical RNG test\n",
    475      1.18  riastrad 		    cprng->cs_name);
    476      1.18  riastrad 		/* XXX Not clear that this does any good...  */
    477      1.18  riastrad 		cprng->cs_ready = false;
    478      1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink);
    479      1.18  riastrad 	}
    480       1.5       tls 
    481      1.20  riastrad 	explicit_memset(rt, 0, sizeof(*rt)); /* paranoia */
    482      1.18  riastrad 	kmem_intr_free(rt, sizeof(*rt));
    483       1.1       tls }
    484      1.18  riastrad #endif
    485       1.1       tls 
    486      1.18  riastrad /*
    487      1.18  riastrad  * Feed entropy from an rndsink request into the CPRNG for which the
    488      1.18  riastrad  * request was issued.
    489      1.18  riastrad  */
    490      1.18  riastrad static void
    491      1.18  riastrad cprng_strong_rndsink_callback(void *context, const void *seed, size_t bytes)
    492       1.1       tls {
    493      1.18  riastrad 	struct cprng_strong *const cprng = context;
    494       1.1       tls 
    495      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    496      1.18  riastrad 	/* Assume that rndsinks provide only full-entropy output.  */
    497      1.18  riastrad 	cprng_strong_reseed_from(cprng, seed, bytes, true);
    498      1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    499       1.1       tls }
    500  1.20.2.2     rmind 
    501  1.20.2.2     rmind static cprng_strong_t *sysctl_prng;
    502  1.20.2.2     rmind 
    503  1.20.2.2     rmind static int
    504  1.20.2.2     rmind makeprng(void)
    505  1.20.2.2     rmind {
    506  1.20.2.2     rmind 
    507  1.20.2.2     rmind 	/* can't create in cprng_init(), too early */
    508  1.20.2.2     rmind 	sysctl_prng = cprng_strong_create("sysctl", IPL_NONE,
    509  1.20.2.2     rmind 					  CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    510  1.20.2.2     rmind 	return 0;
    511  1.20.2.2     rmind }
    512  1.20.2.2     rmind 
    513  1.20.2.2     rmind /*
    514  1.20.2.2     rmind  * sysctl helper routine for kern.urandom node. Picks a random number
    515  1.20.2.2     rmind  * for you.
    516  1.20.2.2     rmind  */
    517  1.20.2.2     rmind static int
    518  1.20.2.2     rmind sysctl_kern_urnd(SYSCTLFN_ARGS)
    519  1.20.2.2     rmind {
    520  1.20.2.2     rmind 	static ONCE_DECL(control);
    521  1.20.2.2     rmind 	int v, rv;
    522  1.20.2.2     rmind 
    523  1.20.2.2     rmind 	RUN_ONCE(&control, makeprng);
    524  1.20.2.2     rmind 	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
    525  1.20.2.2     rmind 	if (rv == sizeof(v)) {
    526  1.20.2.2     rmind 		struct sysctlnode node = *rnode;
    527  1.20.2.2     rmind 		node.sysctl_data = &v;
    528  1.20.2.2     rmind 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    529  1.20.2.2     rmind 	}
    530  1.20.2.2     rmind 	else
    531  1.20.2.2     rmind 		return (EIO);	/*XXX*/
    532  1.20.2.2     rmind }
    533  1.20.2.2     rmind 
    534  1.20.2.2     rmind /*
    535  1.20.2.2     rmind  * sysctl helper routine for kern.arandom node. Picks a random number
    536  1.20.2.2     rmind  * for you.
    537  1.20.2.2     rmind  */
    538  1.20.2.2     rmind static int
    539  1.20.2.2     rmind sysctl_kern_arnd(SYSCTLFN_ARGS)
    540  1.20.2.2     rmind {
    541  1.20.2.2     rmind 	int error;
    542  1.20.2.2     rmind 	void *v;
    543  1.20.2.2     rmind 	struct sysctlnode node = *rnode;
    544  1.20.2.2     rmind 
    545  1.20.2.2     rmind 	if (*oldlenp == 0)
    546  1.20.2.2     rmind 		return 0;
    547  1.20.2.2     rmind 	/*
    548  1.20.2.2     rmind 	 * This code used to allow sucking 8192 bytes at a time out
    549  1.20.2.2     rmind 	 * of the kernel arc4random generator.  Evidently there is some
    550  1.20.2.2     rmind 	 * very old OpenBSD application code that may try to do this.
    551  1.20.2.2     rmind 	 *
    552  1.20.2.2     rmind 	 * Note that this node is documented as type "INT" -- 4 or 8
    553  1.20.2.2     rmind 	 * bytes, not 8192.
    554  1.20.2.2     rmind 	 *
    555  1.20.2.2     rmind 	 * We continue to support this abuse of the "len" pointer here
    556  1.20.2.2     rmind 	 * but only 256 bytes at a time, as, anecdotally, the actual
    557  1.20.2.2     rmind 	 * application use here was to generate RC4 keys in userspace.
    558  1.20.2.2     rmind 	 *
    559  1.20.2.2     rmind 	 * Support for such large requests will probably be removed
    560  1.20.2.2     rmind 	 * entirely in the future.
    561  1.20.2.2     rmind 	 */
    562  1.20.2.2     rmind 	if (*oldlenp > 256)
    563  1.20.2.2     rmind 		return E2BIG;
    564  1.20.2.2     rmind 
    565  1.20.2.2     rmind 	v = kmem_alloc(*oldlenp, KM_SLEEP);
    566  1.20.2.2     rmind 	cprng_fast(v, *oldlenp);
    567  1.20.2.2     rmind 	node.sysctl_data = v;
    568  1.20.2.2     rmind 	node.sysctl_size = *oldlenp;
    569  1.20.2.2     rmind 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    570  1.20.2.2     rmind 	kmem_free(v, *oldlenp);
    571  1.20.2.2     rmind 	return error;
    572  1.20.2.2     rmind }
    573