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