Home | History | Annotate | Line # | Download | only in kern
subr_cprng.c revision 1.35
      1  1.35      maxv /*	$NetBSD: subr_cprng.c,v 1.35 2020/04/12 07:16:09 maxv 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.35      maxv __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.35 2020/04/12 07:16:09 maxv 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.23     pooka #include <sys/once.h>
     46  1.24       tls #include <sys/percpu.h>
     47  1.18  riastrad #include <sys/poll.h>		/* XXX POLLIN/POLLOUT/&c. */
     48  1.18  riastrad #include <sys/select.h>
     49   1.1       tls #include <sys/systm.h>
     50  1.23     pooka #include <sys/sysctl.h>
     51  1.18  riastrad #include <sys/rndsink.h>
     52  1.18  riastrad 
     53  1.31  riastrad #include <crypto/nist_hash_drbg/nist_hash_drbg.h>
     54   1.1       tls 
     55   1.2   tsutsui #if defined(__HAVE_CPU_COUNTER)
     56   1.1       tls #include <machine/cpu_counter.h>
     57   1.2   tsutsui #endif
     58   1.1       tls 
     59  1.23     pooka static int sysctl_kern_urnd(SYSCTLFN_PROTO);
     60  1.23     pooka static int sysctl_kern_arnd(SYSCTLFN_PROTO);
     61  1.23     pooka 
     62  1.18  riastrad static void	cprng_strong_generate(struct cprng_strong *, void *, size_t);
     63  1.18  riastrad static void	cprng_strong_reseed(struct cprng_strong *);
     64  1.18  riastrad static void	cprng_strong_reseed_from(struct cprng_strong *, const void *,
     65  1.18  riastrad 		    size_t, bool);
     66   1.1       tls 
     67  1.18  riastrad static rndsink_callback_t	cprng_strong_rndsink_callback;
     68   1.1       tls 
     69   1.1       tls void
     70   1.1       tls cprng_init(void)
     71   1.1       tls {
     72  1.23     pooka 	static struct sysctllog *random_sysctllog;
     73  1.23     pooka 
     74  1.31  riastrad 	if (nist_hash_drbg_initialize() != 0)
     75  1.31  riastrad 		panic("NIST Hash_DRBG failed self-test");
     76  1.23     pooka 
     77  1.23     pooka 	sysctl_createv(&random_sysctllog, 0, NULL, NULL,
     78  1.23     pooka 		       CTLFLAG_PERMANENT,
     79  1.23     pooka 		       CTLTYPE_INT, "urandom",
     80  1.23     pooka 		       SYSCTL_DESCR("Random integer value"),
     81  1.23     pooka 		       sysctl_kern_urnd, 0, NULL, 0,
     82  1.23     pooka 		       CTL_KERN, KERN_URND, CTL_EOL);
     83  1.23     pooka 	sysctl_createv(&random_sysctllog, 0, NULL, NULL,
     84  1.23     pooka 		       CTLFLAG_PERMANENT,
     85  1.23     pooka 		       CTLTYPE_INT, "arandom",
     86  1.23     pooka 		       SYSCTL_DESCR("n bytes of random data"),
     87  1.23     pooka 		       sysctl_kern_arnd, 0, NULL, 0,
     88  1.23     pooka 		       CTL_KERN, KERN_ARND, CTL_EOL);
     89   1.1       tls }
     90   1.1       tls 
     91   1.1       tls static inline uint32_t
     92   1.1       tls cprng_counter(void)
     93   1.1       tls {
     94   1.1       tls 	struct timeval tv;
     95   1.1       tls 
     96   1.1       tls #if defined(__HAVE_CPU_COUNTER)
     97   1.1       tls 	if (cpu_hascounter())
     98   1.1       tls 		return cpu_counter32();
     99   1.1       tls #endif
    100   1.1       tls 	if (__predict_false(cold)) {
    101  1.24       tls 		static int ctr;
    102   1.1       tls 		/* microtime unsafe if clock not running yet */
    103  1.24       tls 		return ctr++;
    104   1.1       tls 	}
    105  1.24       tls 	getmicrotime(&tv);
    106   1.1       tls 	return (tv.tv_sec * 1000000 + tv.tv_usec);
    107   1.1       tls }
    108   1.1       tls 
    109  1.18  riastrad struct cprng_strong {
    110  1.18  riastrad 	char		cs_name[16];
    111  1.18  riastrad 	int		cs_flags;
    112  1.18  riastrad 	kmutex_t	cs_lock;
    113  1.24       tls 	percpu_t	*cs_percpu;
    114  1.18  riastrad 	kcondvar_t	cs_cv;
    115  1.18  riastrad 	struct selinfo	cs_selq;
    116  1.18  riastrad 	struct rndsink	*cs_rndsink;
    117  1.18  riastrad 	bool		cs_ready;
    118  1.31  riastrad 	NIST_HASH_DRBG	cs_drbg;
    119  1.21  riastrad 
    120  1.21  riastrad 	/* XXX Kludge for /dev/random `information-theoretic' properties.   */
    121  1.21  riastrad 	unsigned int	cs_remaining;
    122  1.18  riastrad };
    123  1.18  riastrad 
    124  1.18  riastrad struct cprng_strong *
    125  1.18  riastrad cprng_strong_create(const char *name, int ipl, int flags)
    126  1.18  riastrad {
    127  1.18  riastrad 	const uint32_t cc = cprng_counter();
    128  1.18  riastrad 	struct cprng_strong *const cprng = kmem_alloc(sizeof(*cprng),
    129  1.18  riastrad 	    KM_SLEEP);
    130  1.18  riastrad 
    131  1.18  riastrad 	/*
    132  1.18  riastrad 	 * rndsink_request takes a spin lock at IPL_VM, so we can be no
    133  1.18  riastrad 	 * higher than that.
    134  1.18  riastrad 	 */
    135  1.22     skrll 	KASSERT(ipl != IPL_SCHED && ipl != IPL_HIGH);
    136  1.18  riastrad 
    137  1.18  riastrad 	/* Initialize the easy fields.  */
    138  1.30      maxv 	memset(cprng->cs_name, 0, sizeof(cprng->cs_name));
    139  1.18  riastrad 	(void)strlcpy(cprng->cs_name, name, sizeof(cprng->cs_name));
    140  1.18  riastrad 	cprng->cs_flags = flags;
    141  1.18  riastrad 	mutex_init(&cprng->cs_lock, MUTEX_DEFAULT, ipl);
    142  1.18  riastrad 	cv_init(&cprng->cs_cv, cprng->cs_name);
    143  1.18  riastrad 	selinit(&cprng->cs_selq);
    144  1.31  riastrad 	cprng->cs_rndsink = rndsink_create(NIST_HASH_DRBG_MIN_SEEDLEN_BYTES,
    145  1.18  riastrad 	    &cprng_strong_rndsink_callback, cprng);
    146  1.18  riastrad 
    147  1.18  riastrad 	/* Get some initial entropy.  Record whether it is full entropy.  */
    148  1.31  riastrad 	uint8_t seed[NIST_HASH_DRBG_MIN_SEEDLEN_BYTES];
    149  1.25  riastrad 	mutex_enter(&cprng->cs_lock);
    150  1.18  riastrad 	cprng->cs_ready = rndsink_request(cprng->cs_rndsink, seed,
    151  1.18  riastrad 	    sizeof(seed));
    152  1.31  riastrad 	if (nist_hash_drbg_instantiate(&cprng->cs_drbg, seed, sizeof(seed),
    153  1.18  riastrad 		&cc, sizeof(cc), cprng->cs_name, sizeof(cprng->cs_name)))
    154  1.31  riastrad 		/* XXX Fix nist_hash_drbg API so this can't happen.  */
    155  1.31  riastrad 		panic("cprng %s: NIST Hash_DRBG instantiation failed",
    156  1.18  riastrad 		    cprng->cs_name);
    157  1.20  riastrad 	explicit_memset(seed, 0, sizeof(seed));
    158  1.18  riastrad 
    159  1.21  riastrad 	if (ISSET(flags, CPRNG_HARD))
    160  1.31  riastrad 		cprng->cs_remaining = NIST_HASH_DRBG_MIN_SEEDLEN_BYTES;
    161  1.21  riastrad 	else
    162  1.21  riastrad 		cprng->cs_remaining = 0;
    163  1.21  riastrad 
    164  1.18  riastrad 	if (!cprng->cs_ready && !ISSET(flags, CPRNG_INIT_ANY))
    165  1.18  riastrad 		printf("cprng %s: creating with partial entropy\n",
    166  1.18  riastrad 		    cprng->cs_name);
    167  1.25  riastrad 	mutex_exit(&cprng->cs_lock);
    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.31  riastrad 	nist_hash_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.21  riastrad 	/*
    225  1.21  riastrad 	 * Debit the entropy if requested.
    226  1.21  riastrad 	 *
    227  1.21  riastrad 	 * XXX Kludge for /dev/random `information-theoretic' properties.
    228  1.21  riastrad 	 */
    229  1.21  riastrad 	if (__predict_false(ISSET(cprng->cs_flags, CPRNG_HARD))) {
    230  1.21  riastrad 		KASSERT(0 < cprng->cs_remaining);
    231  1.31  riastrad 		KASSERT(cprng->cs_remaining <=
    232  1.31  riastrad 		    NIST_HASH_DRBG_MIN_SEEDLEN_BYTES);
    233  1.21  riastrad 		if (bytes < cprng->cs_remaining) {
    234  1.21  riastrad 			cprng->cs_remaining -= bytes;
    235  1.21  riastrad 		} else {
    236  1.21  riastrad 			bytes = cprng->cs_remaining;
    237  1.31  riastrad 			cprng->cs_remaining = NIST_HASH_DRBG_MIN_SEEDLEN_BYTES;
    238  1.21  riastrad 			cprng->cs_ready = false;
    239  1.21  riastrad 			rndsink_schedule(cprng->cs_rndsink);
    240  1.21  riastrad 		}
    241  1.31  riastrad 		KASSERT(bytes <= NIST_HASH_DRBG_MIN_SEEDLEN_BYTES);
    242  1.21  riastrad 		KASSERT(0 < cprng->cs_remaining);
    243  1.31  riastrad 		KASSERT(cprng->cs_remaining <=
    244  1.31  riastrad 		    NIST_HASH_DRBG_MIN_SEEDLEN_BYTES);
    245  1.21  riastrad 	}
    246  1.21  riastrad 
    247  1.18  riastrad 	cprng_strong_generate(cprng, buffer, bytes);
    248  1.18  riastrad 	result = bytes;
    249  1.18  riastrad 
    250  1.18  riastrad out:	mutex_exit(&cprng->cs_lock);
    251  1.18  riastrad 	return result;
    252  1.18  riastrad }
    253  1.18  riastrad 
    254  1.35      maxv uint32_t
    255  1.35      maxv cprng_strong32(void)
    256  1.35      maxv {
    257  1.35      maxv 	uint32_t r;
    258  1.35      maxv 	cprng_strong(kern_cprng, &r, sizeof(r), 0);
    259  1.35      maxv 	return r;
    260  1.35      maxv }
    261  1.35      maxv 
    262  1.35      maxv uint64_t
    263  1.35      maxv cprng_strong64(void)
    264  1.35      maxv {
    265  1.35      maxv 	uint64_t r;
    266  1.35      maxv 	cprng_strong(kern_cprng, &r, sizeof(r), 0);
    267  1.35      maxv 	return r;
    268  1.35      maxv }
    269  1.35      maxv 
    270   1.5       tls static void
    271  1.18  riastrad filt_cprng_detach(struct knote *kn)
    272   1.1       tls {
    273  1.18  riastrad 	struct cprng_strong *const cprng = kn->kn_hook;
    274  1.18  riastrad 
    275  1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    276  1.18  riastrad 	SLIST_REMOVE(&cprng->cs_selq.sel_klist, kn, knote, kn_selnext);
    277  1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    278  1.18  riastrad }
    279   1.8       tls 
    280  1.18  riastrad static int
    281  1.29  christos filt_cprng_read_event(struct knote *kn, long hint)
    282  1.18  riastrad {
    283  1.18  riastrad 	struct cprng_strong *const cprng = kn->kn_hook;
    284  1.18  riastrad 	int ret;
    285   1.1       tls 
    286  1.18  riastrad 	if (hint == NOTE_SUBMIT)
    287  1.18  riastrad 		KASSERT(mutex_owned(&cprng->cs_lock));
    288  1.18  riastrad 	else
    289  1.18  riastrad 		mutex_enter(&cprng->cs_lock);
    290  1.18  riastrad 	if (cprng->cs_ready) {
    291  1.18  riastrad 		kn->kn_data = CPRNG_MAX_LEN; /* XXX Too large?  */
    292  1.18  riastrad 		ret = 1;
    293  1.18  riastrad 	} else {
    294  1.18  riastrad 		ret = 0;
    295   1.7       tls 	}
    296  1.18  riastrad 	if (hint == NOTE_SUBMIT)
    297  1.18  riastrad 		KASSERT(mutex_owned(&cprng->cs_lock));
    298  1.18  riastrad 	else
    299  1.18  riastrad 		mutex_exit(&cprng->cs_lock);
    300   1.7       tls 
    301  1.18  riastrad 	return ret;
    302   1.1       tls }
    303   1.1       tls 
    304  1.29  christos static int
    305  1.29  christos filt_cprng_write_event(struct knote *kn, long hint)
    306  1.29  christos {
    307  1.29  christos 	struct cprng_strong *const cprng = kn->kn_hook;
    308  1.29  christos 
    309  1.29  christos 	if (hint == NOTE_SUBMIT)
    310  1.29  christos 		KASSERT(mutex_owned(&cprng->cs_lock));
    311  1.29  christos 	else
    312  1.29  christos 		mutex_enter(&cprng->cs_lock);
    313  1.29  christos 
    314  1.29  christos 	kn->kn_data = 0;
    315  1.29  christos 
    316  1.29  christos 	if (hint == NOTE_SUBMIT)
    317  1.29  christos 		KASSERT(mutex_owned(&cprng->cs_lock));
    318  1.29  christos 	else
    319  1.29  christos 		mutex_exit(&cprng->cs_lock);
    320  1.29  christos 
    321  1.29  christos 	return 0;
    322  1.29  christos }
    323  1.29  christos 
    324  1.29  christos static const struct filterops cprng_read_filtops = {
    325  1.29  christos 	.f_isfd = 1,
    326  1.29  christos 	.f_attach = NULL,
    327  1.29  christos 	.f_detach = filt_cprng_detach,
    328  1.29  christos 	.f_event = filt_cprng_read_event,
    329  1.29  christos };
    330  1.29  christos 
    331  1.29  christos static const struct filterops cprng_write_filtops = {
    332  1.29  christos 	.f_isfd = 1,
    333  1.29  christos 	.f_attach = NULL,
    334  1.29  christos 	.f_detach = filt_cprng_detach,
    335  1.29  christos 	.f_event = filt_cprng_write_event,
    336  1.29  christos };
    337  1.29  christos 
    338  1.29  christos int
    339  1.29  christos cprng_strong_kqfilter(struct cprng_strong *cprng, struct knote *kn)
    340  1.29  christos {
    341  1.29  christos 
    342  1.29  christos 	switch (kn->kn_filter) {
    343  1.29  christos 	case EVFILT_READ:
    344  1.29  christos 		kn->kn_fop = &cprng_read_filtops;
    345  1.29  christos 		break;
    346  1.29  christos 	case EVFILT_WRITE:
    347  1.29  christos 		kn->kn_fop = &cprng_write_filtops;
    348  1.29  christos 		break;
    349  1.29  christos 	default:
    350  1.29  christos 		return EINVAL;
    351  1.29  christos 	}
    352  1.29  christos 
    353  1.29  christos 	kn->kn_hook = cprng;
    354  1.29  christos 	mutex_enter(&cprng->cs_lock);
    355  1.29  christos 	SLIST_INSERT_HEAD(&cprng->cs_selq.sel_klist, kn, kn_selnext);
    356  1.29  christos 	mutex_exit(&cprng->cs_lock);
    357  1.29  christos 	return 0;
    358  1.29  christos }
    359  1.29  christos 
    360  1.18  riastrad int
    361  1.18  riastrad cprng_strong_poll(struct cprng_strong *cprng, int events)
    362  1.15       tls {
    363  1.18  riastrad 	int revents;
    364  1.18  riastrad 
    365  1.18  riastrad 	if (!ISSET(events, (POLLIN | POLLRDNORM)))
    366  1.18  riastrad 		return 0;
    367  1.18  riastrad 
    368  1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    369  1.18  riastrad 	if (cprng->cs_ready) {
    370  1.18  riastrad 		revents = (events & (POLLIN | POLLRDNORM));
    371  1.18  riastrad 	} else {
    372  1.18  riastrad 		selrecord(curlwp, &cprng->cs_selq);
    373  1.18  riastrad 		revents = 0;
    374  1.15       tls 	}
    375  1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    376  1.18  riastrad 
    377  1.18  riastrad 	return revents;
    378  1.18  riastrad }
    379  1.18  riastrad 
    380  1.18  riastrad /*
    381  1.31  riastrad  * XXX Move nist_hash_drbg_reseed_advised_p and
    382  1.31  riastrad  * nist_hash_drbg_reseed_needed_p into the nist_hash_drbg API and make
    383  1.31  riastrad  * the NIST_HASH_DRBG structure opaque.
    384  1.18  riastrad  */
    385  1.18  riastrad static bool
    386  1.31  riastrad nist_hash_drbg_reseed_advised_p(NIST_HASH_DRBG *drbg)
    387  1.18  riastrad {
    388  1.18  riastrad 
    389  1.31  riastrad 	return (drbg->reseed_counter > (NIST_HASH_DRBG_RESEED_INTERVAL / 2));
    390  1.15       tls }
    391  1.15       tls 
    392  1.18  riastrad static bool
    393  1.31  riastrad nist_hash_drbg_reseed_needed_p(NIST_HASH_DRBG *drbg)
    394   1.1       tls {
    395   1.1       tls 
    396  1.31  riastrad 	return (drbg->reseed_counter >= NIST_HASH_DRBG_RESEED_INTERVAL);
    397  1.18  riastrad }
    398  1.18  riastrad 
    399  1.18  riastrad /*
    400  1.18  riastrad  * Generate some data from the underlying generator.
    401  1.18  riastrad  */
    402  1.18  riastrad static void
    403  1.21  riastrad cprng_strong_generate(struct cprng_strong *cprng, void *buffer, size_t bytes)
    404  1.18  riastrad {
    405  1.18  riastrad 	const uint32_t cc = cprng_counter();
    406  1.18  riastrad 
    407  1.18  riastrad 	KASSERT(bytes <= CPRNG_MAX_LEN);
    408  1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    409  1.18  riastrad 
    410  1.18  riastrad 	/*
    411  1.31  riastrad 	 * Generate some data from the NIST Hash_DRBG.  Caller
    412  1.18  riastrad 	 * guarantees reseed if we're not ready, and if we exhaust the
    413  1.18  riastrad 	 * generator, we mark ourselves not ready.  Consequently, this
    414  1.31  riastrad 	 * call to the Hash_DRBG should not fail.
    415  1.18  riastrad 	 */
    416  1.31  riastrad 	if (__predict_false(nist_hash_drbg_generate(&cprng->cs_drbg, buffer,
    417  1.18  riastrad 		    bytes, &cc, sizeof(cc))))
    418  1.31  riastrad 		panic("cprng %s: NIST Hash_DRBG failed", cprng->cs_name);
    419   1.1       tls 
    420  1.18  riastrad 	/*
    421  1.18  riastrad 	 * If we've been seeing a lot of use, ask for some fresh
    422  1.18  riastrad 	 * entropy soon.
    423  1.18  riastrad 	 */
    424  1.31  riastrad 	if (__predict_false(nist_hash_drbg_reseed_advised_p(&cprng->cs_drbg)))
    425  1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink);
    426   1.1       tls 
    427  1.18  riastrad 	/*
    428  1.18  riastrad 	 * If we just exhausted the generator, inform the next user
    429  1.18  riastrad 	 * that we need a reseed.
    430  1.18  riastrad 	 */
    431  1.31  riastrad 	if (__predict_false(nist_hash_drbg_reseed_needed_p(&cprng->cs_drbg))) {
    432  1.18  riastrad 		cprng->cs_ready = false;
    433  1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink); /* paranoia */
    434   1.1       tls 	}
    435  1.18  riastrad }
    436   1.1       tls 
    437  1.18  riastrad /*
    438  1.18  riastrad  * Reseed with whatever we can get from the system entropy pool right now.
    439  1.18  riastrad  */
    440  1.18  riastrad static void
    441  1.18  riastrad cprng_strong_reseed(struct cprng_strong *cprng)
    442  1.18  riastrad {
    443  1.31  riastrad 	uint8_t seed[NIST_HASH_DRBG_MIN_SEEDLEN_BYTES];
    444   1.5       tls 
    445  1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    446   1.1       tls 
    447  1.18  riastrad 	const bool full_entropy = rndsink_request(cprng->cs_rndsink, seed,
    448  1.18  riastrad 	    sizeof(seed));
    449  1.18  riastrad 	cprng_strong_reseed_from(cprng, seed, sizeof(seed), full_entropy);
    450  1.20  riastrad 	explicit_memset(seed, 0, sizeof(seed));
    451   1.1       tls }
    452   1.1       tls 
    453  1.18  riastrad /*
    454  1.18  riastrad  * Reseed with the given seed.  If we now have full entropy, notify waiters.
    455  1.18  riastrad  */
    456  1.18  riastrad static void
    457  1.18  riastrad cprng_strong_reseed_from(struct cprng_strong *cprng,
    458  1.18  riastrad     const void *seed, size_t bytes, bool full_entropy)
    459   1.1       tls {
    460  1.18  riastrad 	const uint32_t cc = cprng_counter();
    461   1.1       tls 
    462  1.31  riastrad 	KASSERT(bytes == NIST_HASH_DRBG_MIN_SEEDLEN_BYTES);
    463  1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    464   1.1       tls 
    465   1.1       tls 	/*
    466  1.18  riastrad 	 * Notify anyone interested in the partiality of entropy in our
    467  1.18  riastrad 	 * seed -- anyone waiting for full entropy, or any system
    468  1.18  riastrad 	 * operators interested in knowing when the entropy pool is
    469  1.18  riastrad 	 * running on fumes.
    470   1.1       tls 	 */
    471  1.18  riastrad 	if (full_entropy) {
    472  1.18  riastrad 		if (!cprng->cs_ready) {
    473  1.18  riastrad 			cprng->cs_ready = true;
    474  1.18  riastrad 			cv_broadcast(&cprng->cs_cv);
    475  1.18  riastrad 			selnotify(&cprng->cs_selq, (POLLIN | POLLRDNORM),
    476  1.18  riastrad 			    NOTE_SUBMIT);
    477  1.18  riastrad 		}
    478  1.18  riastrad 	} else {
    479  1.18  riastrad 		/*
    480  1.18  riastrad 		 * XXX Is there is any harm in reseeding with partial
    481  1.18  riastrad 		 * entropy when we had full entropy before?  If so,
    482  1.18  riastrad 		 * remove the conditional on this message.
    483  1.18  riastrad 		 */
    484  1.18  riastrad 		if (!cprng->cs_ready &&
    485  1.18  riastrad 		    !ISSET(cprng->cs_flags, CPRNG_REKEY_ANY))
    486  1.18  riastrad 			printf("cprng %s: reseeding with partial entropy\n",
    487  1.18  riastrad 			    cprng->cs_name);
    488  1.18  riastrad 	}
    489  1.18  riastrad 
    490  1.31  riastrad 	if (nist_hash_drbg_reseed(&cprng->cs_drbg, seed, bytes, &cc,
    491  1.31  riastrad 		sizeof(cc)))
    492  1.31  riastrad 		/* XXX Fix nist_hash_drbg API so this can't happen.  */
    493  1.31  riastrad 		panic("cprng %s: NIST Hash_DRBG reseed failed",
    494  1.31  riastrad 		    cprng->cs_name);
    495   1.1       tls }
    496   1.1       tls 
    497  1.18  riastrad /*
    498  1.18  riastrad  * Feed entropy from an rndsink request into the CPRNG for which the
    499  1.18  riastrad  * request was issued.
    500  1.18  riastrad  */
    501  1.18  riastrad static void
    502  1.18  riastrad cprng_strong_rndsink_callback(void *context, const void *seed, size_t bytes)
    503   1.1       tls {
    504  1.18  riastrad 	struct cprng_strong *const cprng = context;
    505   1.1       tls 
    506  1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    507  1.18  riastrad 	/* Assume that rndsinks provide only full-entropy output.  */
    508  1.18  riastrad 	cprng_strong_reseed_from(cprng, seed, bytes, true);
    509  1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    510   1.1       tls }
    511  1.23     pooka 
    512  1.33  riastrad static ONCE_DECL(sysctl_prng_once);
    513  1.23     pooka static cprng_strong_t *sysctl_prng;
    514  1.23     pooka 
    515  1.23     pooka static int
    516  1.23     pooka makeprng(void)
    517  1.23     pooka {
    518  1.23     pooka 
    519  1.23     pooka 	/* can't create in cprng_init(), too early */
    520  1.23     pooka 	sysctl_prng = cprng_strong_create("sysctl", IPL_NONE,
    521  1.23     pooka 					  CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    522  1.23     pooka 	return 0;
    523  1.23     pooka }
    524  1.23     pooka 
    525  1.23     pooka /*
    526  1.23     pooka  * sysctl helper routine for kern.urandom node. Picks a random number
    527  1.23     pooka  * for you.
    528  1.23     pooka  */
    529  1.23     pooka static int
    530  1.23     pooka sysctl_kern_urnd(SYSCTLFN_ARGS)
    531  1.23     pooka {
    532  1.23     pooka 	int v, rv;
    533  1.23     pooka 
    534  1.33  riastrad 	RUN_ONCE(&sysctl_prng_once, makeprng);
    535  1.23     pooka 	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
    536  1.23     pooka 	if (rv == sizeof(v)) {
    537  1.23     pooka 		struct sysctlnode node = *rnode;
    538  1.23     pooka 		node.sysctl_data = &v;
    539  1.23     pooka 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    540  1.23     pooka 	}
    541  1.23     pooka 	else
    542  1.23     pooka 		return (EIO);	/*XXX*/
    543  1.23     pooka }
    544  1.23     pooka 
    545  1.23     pooka /*
    546  1.24       tls  * sysctl helper routine for kern.arandom node.  Fills the supplied
    547  1.24       tls  * structure with random data for you.
    548  1.24       tls  *
    549  1.24       tls  * This node was originally declared as type "int" but its implementation
    550  1.24       tls  * in OpenBSD, whence it came, would happily return up to 8K of data if
    551  1.24       tls  * requested.  Evidently this was used to key RC4 in userspace.
    552  1.24       tls  *
    553  1.24       tls  * In NetBSD, the libc stack-smash-protection code reads 64 bytes
    554  1.32       nia  * from here at every program startup.  Third-party software also often
    555  1.32       nia  * uses this to obtain a key for CSPRNG, reading 32 bytes or more, while
    556  1.32       nia  * avoiding the need to open /dev/urandom.
    557  1.23     pooka  */
    558  1.23     pooka static int
    559  1.23     pooka sysctl_kern_arnd(SYSCTLFN_ARGS)
    560  1.23     pooka {
    561  1.23     pooka 	int error;
    562  1.23     pooka 	void *v;
    563  1.23     pooka 	struct sysctlnode node = *rnode;
    564  1.33  riastrad 	size_t n __diagused;
    565  1.23     pooka 
    566  1.24       tls 	switch (*oldlenp) {
    567  1.24       tls 	    case 0:
    568  1.23     pooka 		return 0;
    569  1.24       tls 	    default:
    570  1.24       tls 		if (*oldlenp > 256) {
    571  1.24       tls 			return E2BIG;
    572  1.24       tls 		}
    573  1.33  riastrad 		RUN_ONCE(&sysctl_prng_once, makeprng);
    574  1.24       tls 		v = kmem_alloc(*oldlenp, KM_SLEEP);
    575  1.33  riastrad 		n = cprng_strong(sysctl_prng, v, *oldlenp, 0);
    576  1.33  riastrad 		KASSERT(n == *oldlenp);
    577  1.24       tls 		node.sysctl_data = v;
    578  1.24       tls 		node.sysctl_size = *oldlenp;
    579  1.24       tls 		error = sysctl_lookup(SYSCTLFN_CALL(&node));
    580  1.24       tls 		kmem_free(v, *oldlenp);
    581  1.24       tls 		return error;
    582  1.24       tls 	}
    583  1.23     pooka }
    584