Home | History | Annotate | Line # | Download | only in kern
subr_cprng.c revision 1.23.2.1
      1  1.23.2.1       tls /*	$NetBSD: subr_cprng.c,v 1.23.2.1 2014/07/17 14:03:33 tls 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.23.2.1       tls __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.23.2.1 2014/07/17 14:03:33 tls 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.23.2.1       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/rnd.h>
     52      1.18  riastrad #include <sys/rndsink.h>
     53      1.18  riastrad #if DEBUG
     54       1.1       tls #include <sys/rngtest.h>
     55      1.18  riastrad #endif
     56      1.18  riastrad 
     57      1.18  riastrad #include <crypto/nist_ctr_drbg/nist_ctr_drbg.h>
     58  1.23.2.1       tls #include <crypto/ccrand/ccrand.h>
     59       1.1       tls 
     60       1.2   tsutsui #if defined(__HAVE_CPU_COUNTER)
     61       1.1       tls #include <machine/cpu_counter.h>
     62       1.2   tsutsui #endif
     63       1.1       tls 
     64  1.23.2.1       tls #define CPRNGF_MAXBYTES           (512 * 1024 * 1024)
     65  1.23.2.1       tls #define CPRNGF_HARDMAX            (1 * 1024 * 1024 * 1024)
     66  1.23.2.1       tls #define CPRNGF_RESEED_SECONDS     600
     67  1.23.2.1       tls 
     68  1.23.2.1       tls typedef struct {
     69  1.23.2.1       tls 	ccrand_t	ccrand;
     70  1.23.2.1       tls 	int		numbytes;
     71  1.23.2.1       tls 	time_t		nextreseed;
     72  1.23.2.1       tls } cprng_fast_ctx_t;
     73  1.23.2.1       tls 
     74      1.23     pooka static int sysctl_kern_urnd(SYSCTLFN_PROTO);
     75      1.23     pooka static int sysctl_kern_arnd(SYSCTLFN_PROTO);
     76      1.23     pooka 
     77      1.18  riastrad static void	cprng_strong_generate(struct cprng_strong *, void *, size_t);
     78      1.18  riastrad static void	cprng_strong_reseed(struct cprng_strong *);
     79      1.18  riastrad static void	cprng_strong_reseed_from(struct cprng_strong *, const void *,
     80      1.18  riastrad 		    size_t, bool);
     81  1.23.2.1       tls static void	cprng_fast_randrekey(cprng_fast_ctx_t *);
     82  1.23.2.1       tls void		*cprng_fast_rekey_softintr = NULL;
     83  1.23.2.1       tls 
     84      1.18  riastrad #if DEBUG
     85      1.18  riastrad static void	cprng_strong_rngtest(struct cprng_strong *);
     86  1.23.2.1       tls static void	cprng_fast_rngtest(void);
     87      1.18  riastrad #endif
     88       1.1       tls 
     89      1.18  riastrad static rndsink_callback_t	cprng_strong_rndsink_callback;
     90       1.1       tls 
     91  1.23.2.1       tls percpu_t *percpu_cprng_fast_ctx;
     92  1.23.2.1       tls static int cprng_fast_initialized;
     93  1.23.2.1       tls 
     94       1.1       tls void
     95       1.1       tls cprng_init(void)
     96       1.1       tls {
     97      1.23     pooka 	static struct sysctllog *random_sysctllog;
     98      1.23     pooka 
     99       1.1       tls 	nist_ctr_initialize();
    100      1.23     pooka 
    101      1.23     pooka 	sysctl_createv(&random_sysctllog, 0, NULL, NULL,
    102      1.23     pooka 		       CTLFLAG_PERMANENT,
    103      1.23     pooka 		       CTLTYPE_INT, "urandom",
    104      1.23     pooka 		       SYSCTL_DESCR("Random integer value"),
    105      1.23     pooka 		       sysctl_kern_urnd, 0, NULL, 0,
    106      1.23     pooka 		       CTL_KERN, KERN_URND, CTL_EOL);
    107      1.23     pooka 	sysctl_createv(&random_sysctllog, 0, NULL, NULL,
    108      1.23     pooka 		       CTLFLAG_PERMANENT,
    109      1.23     pooka 		       CTLTYPE_INT, "arandom",
    110      1.23     pooka 		       SYSCTL_DESCR("n bytes of random data"),
    111      1.23     pooka 		       sysctl_kern_arnd, 0, NULL, 0,
    112      1.23     pooka 		       CTL_KERN, KERN_ARND, CTL_EOL);
    113       1.1       tls }
    114       1.1       tls 
    115       1.1       tls static inline uint32_t
    116       1.1       tls cprng_counter(void)
    117       1.1       tls {
    118       1.1       tls 	struct timeval tv;
    119       1.1       tls 
    120       1.1       tls #if defined(__HAVE_CPU_COUNTER)
    121       1.1       tls 	if (cpu_hascounter())
    122       1.1       tls 		return cpu_counter32();
    123       1.1       tls #endif
    124       1.1       tls 	if (__predict_false(cold)) {
    125  1.23.2.1       tls 		static int ctr;
    126       1.1       tls 		/* microtime unsafe if clock not running yet */
    127  1.23.2.1       tls 		return ctr++;
    128       1.1       tls 	}
    129  1.23.2.1       tls 	getmicrotime(&tv);
    130       1.1       tls 	return (tv.tv_sec * 1000000 + tv.tv_usec);
    131       1.1       tls }
    132       1.1       tls 
    133      1.18  riastrad struct cprng_strong {
    134      1.18  riastrad 	char		cs_name[16];
    135      1.18  riastrad 	int		cs_flags;
    136      1.18  riastrad 	kmutex_t	cs_lock;
    137      1.18  riastrad 	kcondvar_t	cs_cv;
    138      1.18  riastrad 	struct selinfo	cs_selq;
    139      1.18  riastrad 	struct rndsink	*cs_rndsink;
    140      1.18  riastrad 	bool		cs_ready;
    141      1.18  riastrad 	NIST_CTR_DRBG	cs_drbg;
    142      1.21  riastrad 
    143      1.21  riastrad 	/* XXX Kludge for /dev/random `information-theoretic' properties.   */
    144      1.21  riastrad 	unsigned int	cs_remaining;
    145      1.18  riastrad };
    146      1.18  riastrad 
    147      1.18  riastrad struct cprng_strong *
    148      1.18  riastrad cprng_strong_create(const char *name, int ipl, int flags)
    149      1.18  riastrad {
    150      1.18  riastrad 	const uint32_t cc = cprng_counter();
    151      1.18  riastrad 	struct cprng_strong *const cprng = kmem_alloc(sizeof(*cprng),
    152      1.18  riastrad 	    KM_SLEEP);
    153      1.18  riastrad 
    154      1.18  riastrad 	/*
    155      1.18  riastrad 	 * rndsink_request takes a spin lock at IPL_VM, so we can be no
    156      1.18  riastrad 	 * higher than that.
    157      1.18  riastrad 	 */
    158      1.22     skrll 	KASSERT(ipl != IPL_SCHED && ipl != IPL_HIGH);
    159      1.18  riastrad 
    160      1.18  riastrad 	/* Initialize the easy fields.  */
    161      1.18  riastrad 	(void)strlcpy(cprng->cs_name, name, sizeof(cprng->cs_name));
    162      1.18  riastrad 	cprng->cs_flags = flags;
    163      1.18  riastrad 	mutex_init(&cprng->cs_lock, MUTEX_DEFAULT, ipl);
    164      1.18  riastrad 	cv_init(&cprng->cs_cv, cprng->cs_name);
    165      1.18  riastrad 	selinit(&cprng->cs_selq);
    166      1.18  riastrad 	cprng->cs_rndsink = rndsink_create(NIST_BLOCK_KEYLEN_BYTES,
    167      1.18  riastrad 	    &cprng_strong_rndsink_callback, cprng);
    168      1.18  riastrad 
    169      1.18  riastrad 	/* Get some initial entropy.  Record whether it is full entropy.  */
    170      1.18  riastrad 	uint8_t seed[NIST_BLOCK_KEYLEN_BYTES];
    171      1.18  riastrad 	cprng->cs_ready = rndsink_request(cprng->cs_rndsink, seed,
    172      1.18  riastrad 	    sizeof(seed));
    173      1.18  riastrad 	if (nist_ctr_drbg_instantiate(&cprng->cs_drbg, seed, sizeof(seed),
    174      1.18  riastrad 		&cc, sizeof(cc), cprng->cs_name, sizeof(cprng->cs_name)))
    175      1.18  riastrad 		/* XXX Fix nist_ctr_drbg API so this can't happen.  */
    176      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG instantiation failed",
    177      1.18  riastrad 		    cprng->cs_name);
    178      1.20  riastrad 	explicit_memset(seed, 0, sizeof(seed));
    179      1.18  riastrad 
    180      1.21  riastrad 	if (ISSET(flags, CPRNG_HARD))
    181      1.21  riastrad 		cprng->cs_remaining = NIST_BLOCK_KEYLEN_BYTES;
    182      1.21  riastrad 	else
    183      1.21  riastrad 		cprng->cs_remaining = 0;
    184      1.21  riastrad 
    185      1.18  riastrad 	if (!cprng->cs_ready && !ISSET(flags, CPRNG_INIT_ANY))
    186      1.18  riastrad 		printf("cprng %s: creating with partial entropy\n",
    187      1.18  riastrad 		    cprng->cs_name);
    188      1.18  riastrad 
    189      1.18  riastrad 	return cprng;
    190      1.18  riastrad }
    191      1.18  riastrad 
    192      1.18  riastrad void
    193      1.18  riastrad cprng_strong_destroy(struct cprng_strong *cprng)
    194       1.8       tls {
    195       1.8       tls 
    196      1.18  riastrad 	/*
    197      1.18  riastrad 	 * Destroy the rndsink first to prevent calls to the callback.
    198      1.18  riastrad 	 */
    199      1.18  riastrad 	rndsink_destroy(cprng->cs_rndsink);
    200       1.8       tls 
    201      1.18  riastrad 	KASSERT(!cv_has_waiters(&cprng->cs_cv));
    202      1.18  riastrad #if 0
    203      1.18  riastrad 	KASSERT(!select_has_waiters(&cprng->cs_selq)) /* XXX ? */
    204      1.18  riastrad #endif
    205      1.15       tls 
    206      1.18  riastrad 	nist_ctr_drbg_destroy(&cprng->cs_drbg);
    207      1.18  riastrad 	seldestroy(&cprng->cs_selq);
    208      1.18  riastrad 	cv_destroy(&cprng->cs_cv);
    209      1.18  riastrad 	mutex_destroy(&cprng->cs_lock);
    210      1.18  riastrad 
    211      1.20  riastrad 	explicit_memset(cprng, 0, sizeof(*cprng)); /* paranoia */
    212      1.18  riastrad 	kmem_free(cprng, sizeof(*cprng));
    213       1.8       tls }
    214       1.8       tls 
    215      1.18  riastrad /*
    216      1.18  riastrad  * Generate some data from cprng.  Block or return zero bytes,
    217      1.18  riastrad  * depending on flags & FNONBLOCK, if cprng was created without
    218      1.18  riastrad  * CPRNG_REKEY_ANY.
    219      1.18  riastrad  */
    220      1.18  riastrad size_t
    221      1.18  riastrad cprng_strong(struct cprng_strong *cprng, void *buffer, size_t bytes, int flags)
    222       1.5       tls {
    223      1.18  riastrad 	size_t result;
    224      1.18  riastrad 
    225      1.18  riastrad 	/* Caller must loop for more than CPRNG_MAX_LEN bytes.  */
    226      1.18  riastrad 	bytes = MIN(bytes, CPRNG_MAX_LEN);
    227      1.18  riastrad 
    228      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    229      1.18  riastrad 
    230      1.18  riastrad 	if (ISSET(cprng->cs_flags, CPRNG_REKEY_ANY)) {
    231      1.18  riastrad 		if (!cprng->cs_ready)
    232      1.18  riastrad 			cprng_strong_reseed(cprng);
    233      1.18  riastrad 	} else {
    234      1.18  riastrad 		while (!cprng->cs_ready) {
    235      1.18  riastrad 			if (ISSET(flags, FNONBLOCK) ||
    236      1.18  riastrad 			    !ISSET(cprng->cs_flags, CPRNG_USE_CV) ||
    237      1.18  riastrad 			    cv_wait_sig(&cprng->cs_cv, &cprng->cs_lock)) {
    238      1.18  riastrad 				result = 0;
    239      1.18  riastrad 				goto out;
    240       1.8       tls 			}
    241       1.8       tls 		}
    242       1.5       tls 	}
    243      1.18  riastrad 
    244      1.21  riastrad 	/*
    245      1.21  riastrad 	 * Debit the entropy if requested.
    246      1.21  riastrad 	 *
    247      1.21  riastrad 	 * XXX Kludge for /dev/random `information-theoretic' properties.
    248      1.21  riastrad 	 */
    249      1.21  riastrad 	if (__predict_false(ISSET(cprng->cs_flags, CPRNG_HARD))) {
    250      1.21  riastrad 		KASSERT(0 < cprng->cs_remaining);
    251      1.21  riastrad 		KASSERT(cprng->cs_remaining <= NIST_BLOCK_KEYLEN_BYTES);
    252      1.21  riastrad 		if (bytes < cprng->cs_remaining) {
    253      1.21  riastrad 			cprng->cs_remaining -= bytes;
    254      1.21  riastrad 		} else {
    255      1.21  riastrad 			bytes = cprng->cs_remaining;
    256      1.21  riastrad 			cprng->cs_remaining = NIST_BLOCK_KEYLEN_BYTES;
    257      1.21  riastrad 			cprng->cs_ready = false;
    258      1.21  riastrad 			rndsink_schedule(cprng->cs_rndsink);
    259      1.21  riastrad 		}
    260      1.21  riastrad 		KASSERT(bytes <= NIST_BLOCK_KEYLEN_BYTES);
    261      1.21  riastrad 		KASSERT(0 < cprng->cs_remaining);
    262      1.21  riastrad 		KASSERT(cprng->cs_remaining <= NIST_BLOCK_KEYLEN_BYTES);
    263      1.21  riastrad 	}
    264      1.21  riastrad 
    265      1.18  riastrad 	cprng_strong_generate(cprng, buffer, bytes);
    266      1.18  riastrad 	result = bytes;
    267      1.18  riastrad 
    268      1.18  riastrad out:	mutex_exit(&cprng->cs_lock);
    269      1.18  riastrad 	return result;
    270      1.18  riastrad }
    271      1.18  riastrad 
    272      1.18  riastrad static void	filt_cprng_detach(struct knote *);
    273      1.18  riastrad static int	filt_cprng_event(struct knote *, long);
    274      1.18  riastrad 
    275      1.18  riastrad static const struct filterops cprng_filtops =
    276      1.18  riastrad 	{ 1, NULL, filt_cprng_detach, filt_cprng_event };
    277      1.18  riastrad 
    278      1.18  riastrad int
    279      1.18  riastrad cprng_strong_kqfilter(struct cprng_strong *cprng, struct knote *kn)
    280      1.18  riastrad {
    281      1.18  riastrad 
    282      1.18  riastrad 	switch (kn->kn_filter) {
    283      1.18  riastrad 	case EVFILT_READ:
    284      1.18  riastrad 		kn->kn_fop = &cprng_filtops;
    285      1.18  riastrad 		kn->kn_hook = cprng;
    286      1.18  riastrad 		mutex_enter(&cprng->cs_lock);
    287      1.18  riastrad 		SLIST_INSERT_HEAD(&cprng->cs_selq.sel_klist, kn, kn_selnext);
    288      1.18  riastrad 		mutex_exit(&cprng->cs_lock);
    289      1.18  riastrad 		return 0;
    290      1.18  riastrad 
    291      1.18  riastrad 	case EVFILT_WRITE:
    292      1.18  riastrad 	default:
    293      1.18  riastrad 		return EINVAL;
    294       1.8       tls 	}
    295       1.5       tls }
    296       1.5       tls 
    297       1.5       tls static void
    298      1.18  riastrad filt_cprng_detach(struct knote *kn)
    299       1.1       tls {
    300      1.18  riastrad 	struct cprng_strong *const cprng = kn->kn_hook;
    301      1.18  riastrad 
    302      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    303      1.18  riastrad 	SLIST_REMOVE(&cprng->cs_selq.sel_klist, kn, knote, kn_selnext);
    304      1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    305      1.18  riastrad }
    306       1.8       tls 
    307      1.18  riastrad static int
    308      1.18  riastrad filt_cprng_event(struct knote *kn, long hint)
    309      1.18  riastrad {
    310      1.18  riastrad 	struct cprng_strong *const cprng = kn->kn_hook;
    311      1.18  riastrad 	int ret;
    312       1.1       tls 
    313      1.18  riastrad 	if (hint == NOTE_SUBMIT)
    314      1.18  riastrad 		KASSERT(mutex_owned(&cprng->cs_lock));
    315      1.18  riastrad 	else
    316      1.18  riastrad 		mutex_enter(&cprng->cs_lock);
    317      1.18  riastrad 	if (cprng->cs_ready) {
    318      1.18  riastrad 		kn->kn_data = CPRNG_MAX_LEN; /* XXX Too large?  */
    319      1.18  riastrad 		ret = 1;
    320      1.18  riastrad 	} else {
    321      1.18  riastrad 		ret = 0;
    322       1.7       tls 	}
    323      1.18  riastrad 	if (hint == NOTE_SUBMIT)
    324      1.18  riastrad 		KASSERT(mutex_owned(&cprng->cs_lock));
    325      1.18  riastrad 	else
    326      1.18  riastrad 		mutex_exit(&cprng->cs_lock);
    327       1.7       tls 
    328      1.18  riastrad 	return ret;
    329       1.1       tls }
    330       1.1       tls 
    331      1.18  riastrad int
    332      1.18  riastrad cprng_strong_poll(struct cprng_strong *cprng, int events)
    333      1.15       tls {
    334      1.18  riastrad 	int revents;
    335      1.18  riastrad 
    336      1.18  riastrad 	if (!ISSET(events, (POLLIN | POLLRDNORM)))
    337      1.18  riastrad 		return 0;
    338      1.18  riastrad 
    339      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    340      1.18  riastrad 	if (cprng->cs_ready) {
    341      1.18  riastrad 		revents = (events & (POLLIN | POLLRDNORM));
    342      1.18  riastrad 	} else {
    343      1.18  riastrad 		selrecord(curlwp, &cprng->cs_selq);
    344      1.18  riastrad 		revents = 0;
    345      1.15       tls 	}
    346      1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    347      1.18  riastrad 
    348      1.18  riastrad 	return revents;
    349      1.18  riastrad }
    350      1.18  riastrad 
    351      1.18  riastrad /*
    352      1.18  riastrad  * XXX Move nist_ctr_drbg_reseed_advised_p and
    353      1.18  riastrad  * nist_ctr_drbg_reseed_needed_p into the nist_ctr_drbg API and make
    354      1.18  riastrad  * the NIST_CTR_DRBG structure opaque.
    355      1.18  riastrad  */
    356      1.18  riastrad static bool
    357      1.18  riastrad nist_ctr_drbg_reseed_advised_p(NIST_CTR_DRBG *drbg)
    358      1.18  riastrad {
    359      1.18  riastrad 
    360      1.18  riastrad 	return (drbg->reseed_counter > (NIST_CTR_DRBG_RESEED_INTERVAL / 2));
    361      1.15       tls }
    362      1.15       tls 
    363      1.18  riastrad static bool
    364      1.18  riastrad nist_ctr_drbg_reseed_needed_p(NIST_CTR_DRBG *drbg)
    365       1.1       tls {
    366       1.1       tls 
    367      1.18  riastrad 	return (drbg->reseed_counter >= NIST_CTR_DRBG_RESEED_INTERVAL);
    368      1.18  riastrad }
    369      1.18  riastrad 
    370      1.18  riastrad /*
    371      1.18  riastrad  * Generate some data from the underlying generator.
    372      1.18  riastrad  */
    373      1.18  riastrad static void
    374      1.21  riastrad cprng_strong_generate(struct cprng_strong *cprng, void *buffer, size_t bytes)
    375      1.18  riastrad {
    376      1.18  riastrad 	const uint32_t cc = cprng_counter();
    377      1.18  riastrad 
    378      1.18  riastrad 	KASSERT(bytes <= CPRNG_MAX_LEN);
    379      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    380      1.18  riastrad 
    381      1.18  riastrad 	/*
    382      1.18  riastrad 	 * Generate some data from the NIST CTR_DRBG.  Caller
    383      1.18  riastrad 	 * guarantees reseed if we're not ready, and if we exhaust the
    384      1.18  riastrad 	 * generator, we mark ourselves not ready.  Consequently, this
    385      1.18  riastrad 	 * call to the CTR_DRBG should not fail.
    386      1.18  riastrad 	 */
    387      1.18  riastrad 	if (__predict_false(nist_ctr_drbg_generate(&cprng->cs_drbg, buffer,
    388      1.18  riastrad 		    bytes, &cc, sizeof(cc))))
    389      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG failed", cprng->cs_name);
    390       1.1       tls 
    391      1.18  riastrad 	/*
    392      1.18  riastrad 	 * If we've been seeing a lot of use, ask for some fresh
    393      1.18  riastrad 	 * entropy soon.
    394      1.18  riastrad 	 */
    395      1.18  riastrad 	if (__predict_false(nist_ctr_drbg_reseed_advised_p(&cprng->cs_drbg)))
    396      1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink);
    397       1.1       tls 
    398      1.18  riastrad 	/*
    399      1.18  riastrad 	 * If we just exhausted the generator, inform the next user
    400      1.18  riastrad 	 * that we need a reseed.
    401      1.18  riastrad 	 */
    402      1.18  riastrad 	if (__predict_false(nist_ctr_drbg_reseed_needed_p(&cprng->cs_drbg))) {
    403      1.18  riastrad 		cprng->cs_ready = false;
    404      1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink); /* paranoia */
    405       1.1       tls 	}
    406      1.18  riastrad }
    407       1.1       tls 
    408      1.18  riastrad /*
    409      1.18  riastrad  * Reseed with whatever we can get from the system entropy pool right now.
    410      1.18  riastrad  */
    411      1.18  riastrad static void
    412      1.18  riastrad cprng_strong_reseed(struct cprng_strong *cprng)
    413      1.18  riastrad {
    414      1.18  riastrad 	uint8_t seed[NIST_BLOCK_KEYLEN_BYTES];
    415       1.5       tls 
    416      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    417       1.1       tls 
    418      1.18  riastrad 	const bool full_entropy = rndsink_request(cprng->cs_rndsink, seed,
    419      1.18  riastrad 	    sizeof(seed));
    420      1.18  riastrad 	cprng_strong_reseed_from(cprng, seed, sizeof(seed), full_entropy);
    421      1.20  riastrad 	explicit_memset(seed, 0, sizeof(seed));
    422       1.1       tls }
    423       1.1       tls 
    424      1.18  riastrad /*
    425      1.18  riastrad  * Reseed with the given seed.  If we now have full entropy, notify waiters.
    426      1.18  riastrad  */
    427      1.18  riastrad static void
    428      1.18  riastrad cprng_strong_reseed_from(struct cprng_strong *cprng,
    429      1.18  riastrad     const void *seed, size_t bytes, bool full_entropy)
    430       1.1       tls {
    431      1.18  riastrad 	const uint32_t cc = cprng_counter();
    432       1.1       tls 
    433      1.18  riastrad 	KASSERT(bytes == NIST_BLOCK_KEYLEN_BYTES);
    434      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    435       1.1       tls 
    436       1.1       tls 	/*
    437      1.18  riastrad 	 * Notify anyone interested in the partiality of entropy in our
    438      1.18  riastrad 	 * seed -- anyone waiting for full entropy, or any system
    439      1.18  riastrad 	 * operators interested in knowing when the entropy pool is
    440      1.18  riastrad 	 * running on fumes.
    441       1.1       tls 	 */
    442      1.18  riastrad 	if (full_entropy) {
    443      1.18  riastrad 		if (!cprng->cs_ready) {
    444      1.18  riastrad 			cprng->cs_ready = true;
    445      1.18  riastrad 			cv_broadcast(&cprng->cs_cv);
    446      1.18  riastrad 			selnotify(&cprng->cs_selq, (POLLIN | POLLRDNORM),
    447      1.18  riastrad 			    NOTE_SUBMIT);
    448      1.18  riastrad 		}
    449      1.18  riastrad 	} else {
    450      1.18  riastrad 		/*
    451      1.18  riastrad 		 * XXX Is there is any harm in reseeding with partial
    452      1.18  riastrad 		 * entropy when we had full entropy before?  If so,
    453      1.18  riastrad 		 * remove the conditional on this message.
    454      1.18  riastrad 		 */
    455      1.18  riastrad 		if (!cprng->cs_ready &&
    456      1.18  riastrad 		    !ISSET(cprng->cs_flags, CPRNG_REKEY_ANY))
    457      1.18  riastrad 			printf("cprng %s: reseeding with partial entropy\n",
    458      1.18  riastrad 			    cprng->cs_name);
    459      1.18  riastrad 	}
    460      1.18  riastrad 
    461      1.18  riastrad 	if (nist_ctr_drbg_reseed(&cprng->cs_drbg, seed, bytes, &cc, sizeof(cc)))
    462      1.18  riastrad 		/* XXX Fix nist_ctr_drbg API so this can't happen.  */
    463      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG reseed failed", cprng->cs_name);
    464       1.5       tls 
    465      1.18  riastrad #if DEBUG
    466      1.18  riastrad 	cprng_strong_rngtest(cprng);
    467       1.8       tls #endif
    468       1.1       tls }
    469       1.1       tls 
    470      1.18  riastrad #if DEBUG
    471      1.18  riastrad /*
    472      1.18  riastrad  * Generate some output and apply a statistical RNG test to it.
    473      1.18  riastrad  */
    474      1.18  riastrad static void
    475      1.18  riastrad cprng_strong_rngtest(struct cprng_strong *cprng)
    476       1.1       tls {
    477       1.5       tls 
    478      1.18  riastrad 	KASSERT(mutex_owned(&cprng->cs_lock));
    479      1.18  riastrad 
    480      1.18  riastrad 	/* XXX Switch to a pool cache instead?  */
    481      1.18  riastrad 	rngtest_t *const rt = kmem_intr_alloc(sizeof(*rt), KM_NOSLEEP);
    482      1.18  riastrad 	if (rt == NULL)
    483      1.18  riastrad 		/* XXX Warn?  */
    484      1.18  riastrad 		return;
    485       1.1       tls 
    486      1.18  riastrad 	(void)strlcpy(rt->rt_name, cprng->cs_name, sizeof(rt->rt_name));
    487       1.6       tls 
    488      1.18  riastrad 	if (nist_ctr_drbg_generate(&cprng->cs_drbg, rt->rt_b, sizeof(rt->rt_b),
    489      1.18  riastrad 		NULL, 0))
    490      1.18  riastrad 		panic("cprng %s: NIST CTR_DRBG failed after reseed",
    491      1.18  riastrad 		    cprng->cs_name);
    492       1.5       tls 
    493      1.18  riastrad 	if (rngtest(rt)) {
    494      1.18  riastrad 		printf("cprng %s: failed statistical RNG test\n",
    495      1.18  riastrad 		    cprng->cs_name);
    496      1.18  riastrad 		/* XXX Not clear that this does any good...  */
    497      1.18  riastrad 		cprng->cs_ready = false;
    498      1.18  riastrad 		rndsink_schedule(cprng->cs_rndsink);
    499      1.18  riastrad 	}
    500       1.5       tls 
    501      1.20  riastrad 	explicit_memset(rt, 0, sizeof(*rt)); /* paranoia */
    502      1.18  riastrad 	kmem_intr_free(rt, sizeof(*rt));
    503       1.1       tls }
    504  1.23.2.1       tls 
    505  1.23.2.1       tls static void cprng_fast_rngtest(void)
    506  1.23.2.1       tls {
    507  1.23.2.1       tls 	rngtest_t *const rt = kmem_intr_alloc(sizeof(*rt), KM_NOSLEEP);
    508  1.23.2.1       tls 	if (rt == NULL)
    509  1.23.2.1       tls 		/* XXX Warn? */
    510  1.23.2.1       tls 		return;
    511  1.23.2.1       tls 
    512  1.23.2.1       tls 	(void)snprintf(rt->rt_name, sizeof(rt->rt_name),
    513  1.23.2.1       tls 		       "cpu%d", curcpu()->ci_index);
    514  1.23.2.1       tls 	cprng_fast(rt->rt_b, sizeof(rt->rt_b));
    515  1.23.2.1       tls 
    516  1.23.2.1       tls 	if (rngtest(rt)) {
    517  1.23.2.1       tls 		printf("cprng_fast for %s: failed statistical RNG test\n",
    518  1.23.2.1       tls 		       rt->rt_name);
    519  1.23.2.1       tls 	}
    520  1.23.2.1       tls 	explicit_memset(rt, 0, sizeof(*rt));
    521  1.23.2.1       tls 	kmem_intr_free(rt, sizeof(*rt));
    522  1.23.2.1       tls }
    523      1.18  riastrad #endif
    524       1.1       tls 
    525      1.18  riastrad /*
    526      1.18  riastrad  * Feed entropy from an rndsink request into the CPRNG for which the
    527      1.18  riastrad  * request was issued.
    528      1.18  riastrad  */
    529      1.18  riastrad static void
    530      1.18  riastrad cprng_strong_rndsink_callback(void *context, const void *seed, size_t bytes)
    531       1.1       tls {
    532      1.18  riastrad 	struct cprng_strong *const cprng = context;
    533       1.1       tls 
    534      1.18  riastrad 	mutex_enter(&cprng->cs_lock);
    535      1.18  riastrad 	/* Assume that rndsinks provide only full-entropy output.  */
    536      1.18  riastrad 	cprng_strong_reseed_from(cprng, seed, bytes, true);
    537      1.18  riastrad 	mutex_exit(&cprng->cs_lock);
    538       1.1       tls }
    539      1.23     pooka 
    540      1.23     pooka static cprng_strong_t *sysctl_prng;
    541      1.23     pooka 
    542      1.23     pooka static int
    543      1.23     pooka makeprng(void)
    544      1.23     pooka {
    545      1.23     pooka 
    546      1.23     pooka 	/* can't create in cprng_init(), too early */
    547      1.23     pooka 	sysctl_prng = cprng_strong_create("sysctl", IPL_NONE,
    548      1.23     pooka 					  CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    549      1.23     pooka 	return 0;
    550      1.23     pooka }
    551      1.23     pooka 
    552      1.23     pooka /*
    553      1.23     pooka  * sysctl helper routine for kern.urandom node. Picks a random number
    554      1.23     pooka  * for you.
    555      1.23     pooka  */
    556      1.23     pooka static int
    557      1.23     pooka sysctl_kern_urnd(SYSCTLFN_ARGS)
    558      1.23     pooka {
    559      1.23     pooka 	static ONCE_DECL(control);
    560      1.23     pooka 	int v, rv;
    561      1.23     pooka 
    562      1.23     pooka 	RUN_ONCE(&control, makeprng);
    563      1.23     pooka 	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
    564      1.23     pooka 	if (rv == sizeof(v)) {
    565      1.23     pooka 		struct sysctlnode node = *rnode;
    566      1.23     pooka 		node.sysctl_data = &v;
    567      1.23     pooka 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    568      1.23     pooka 	}
    569      1.23     pooka 	else
    570      1.23     pooka 		return (EIO);	/*XXX*/
    571      1.23     pooka }
    572      1.23     pooka 
    573      1.23     pooka /*
    574  1.23.2.1       tls  * sysctl helper routine for kern.arandom node.  Fills the supplied
    575  1.23.2.1       tls  * structure with random data for you.
    576  1.23.2.1       tls  *
    577  1.23.2.1       tls  * This node was originally declared as type "int" but its implementation
    578  1.23.2.1       tls  * in OpenBSD, whence it came, would happily return up to 8K of data if
    579  1.23.2.1       tls  * requested.  Evidently this was used to key RC4 in userspace.
    580  1.23.2.1       tls  *
    581  1.23.2.1       tls  * In NetBSD, the libc stack-smash-protection code reads 64 bytes
    582  1.23.2.1       tls  * from here at every program startup.  So though it would be nice
    583  1.23.2.1       tls  * to make this node return only 32 or 64 bits, we can't.  Too bad!
    584      1.23     pooka  */
    585      1.23     pooka static int
    586      1.23     pooka sysctl_kern_arnd(SYSCTLFN_ARGS)
    587      1.23     pooka {
    588      1.23     pooka 	int error;
    589      1.23     pooka 	void *v;
    590      1.23     pooka 	struct sysctlnode node = *rnode;
    591      1.23     pooka 
    592  1.23.2.1       tls 	switch (*oldlenp) {
    593  1.23.2.1       tls 	    case 0:
    594      1.23     pooka 		return 0;
    595  1.23.2.1       tls 	    default:
    596  1.23.2.1       tls 		if (*oldlenp > 256) {
    597  1.23.2.1       tls 			return E2BIG;
    598  1.23.2.1       tls 		}
    599  1.23.2.1       tls 		v = kmem_alloc(*oldlenp, KM_SLEEP);
    600  1.23.2.1       tls 		cprng_fast(v, *oldlenp);
    601  1.23.2.1       tls 		node.sysctl_data = v;
    602  1.23.2.1       tls 		node.sysctl_size = *oldlenp;
    603  1.23.2.1       tls 		error = sysctl_lookup(SYSCTLFN_CALL(&node));
    604  1.23.2.1       tls 		kmem_free(v, *oldlenp);
    605  1.23.2.1       tls 		return error;
    606  1.23.2.1       tls 	}
    607  1.23.2.1       tls }
    608  1.23.2.1       tls 
    609  1.23.2.1       tls static void
    610  1.23.2.1       tls cprng_fast_randrekey(cprng_fast_ctx_t *ctx)
    611  1.23.2.1       tls {
    612  1.23.2.1       tls 	uint8_t key[16];
    613  1.23.2.1       tls 	int s;
    614  1.23.2.1       tls 
    615  1.23.2.1       tls 	int have_initial = rnd_initial_entropy;
    616  1.23.2.1       tls 
    617  1.23.2.1       tls 	cprng_strong(kern_cprng, key, sizeof(key), FASYNC);
    618  1.23.2.1       tls 	s = splhigh();
    619  1.23.2.1       tls 	ccrand_reseed(&ctx->ccrand, (uint32_t *)key,
    620  1.23.2.1       tls 		      sizeof(key) / sizeof(uint32_t));
    621  1.23.2.1       tls 	splx(s);
    622  1.23.2.1       tls 	explicit_memset(key, 0, sizeof(key));
    623      1.23     pooka 	/*
    624  1.23.2.1       tls 	 * Reset for next reseed cycle.
    625      1.23     pooka 	 */
    626  1.23.2.1       tls 	ctx->nextreseed = time_uptime +
    627  1.23.2.1       tls 	    (have_initial ? CPRNGF_RESEED_SECONDS : 0);
    628  1.23.2.1       tls 	ctx->numbytes = 0;
    629  1.23.2.1       tls 
    630  1.23.2.1       tls #if DEBUG
    631  1.23.2.1       tls 	cprng_fast_rngtest();
    632  1.23.2.1       tls #endif
    633  1.23.2.1       tls }
    634  1.23.2.1       tls 
    635  1.23.2.1       tls static void
    636  1.23.2.1       tls cprng_fast_init_ctx(void *v,
    637  1.23.2.1       tls 	      void *arg __unused,
    638  1.23.2.1       tls 	      struct cpu_info * ci __unused)
    639  1.23.2.1       tls {
    640  1.23.2.1       tls 	cprng_fast_ctx_t *ctx = v;
    641  1.23.2.1       tls 	cprng_fast_randrekey(ctx);
    642  1.23.2.1       tls }
    643  1.23.2.1       tls 
    644  1.23.2.1       tls static void
    645  1.23.2.1       tls cprng_fast_rekey_one(void *arg __unused)
    646  1.23.2.1       tls {
    647  1.23.2.1       tls 	cprng_fast_ctx_t *ctx = percpu_getref(percpu_cprng_fast_ctx);
    648  1.23.2.1       tls 
    649  1.23.2.1       tls 	cprng_fast_randrekey(ctx);
    650  1.23.2.1       tls 	percpu_putref(percpu_cprng_fast_ctx);
    651  1.23.2.1       tls }
    652  1.23.2.1       tls 
    653  1.23.2.1       tls /*
    654  1.23.2.1       tls  * Because we key the cprng_fast instances from the kernel_cprng,
    655  1.23.2.1       tls  * and we try not to initialize the kernel_cprng until there is at
    656  1.23.2.1       tls  * least some chance there's entropy available for it, this must
    657  1.23.2.1       tls  * be called somewhat later than cprng_init() and is thus a separate
    658  1.23.2.1       tls  * function.
    659  1.23.2.1       tls  */
    660  1.23.2.1       tls void
    661  1.23.2.1       tls cprng_fast_init(void)
    662  1.23.2.1       tls {
    663  1.23.2.1       tls         percpu_cprng_fast_ctx = percpu_alloc(sizeof(cprng_fast_ctx_t));
    664  1.23.2.1       tls 
    665  1.23.2.1       tls         percpu_foreach(percpu_cprng_fast_ctx, cprng_fast_init_ctx, NULL);
    666  1.23.2.1       tls 	cprng_fast_rekey_softintr =
    667  1.23.2.1       tls 	    softint_establish(SOFTINT_CLOCK|SOFTINT_MPSAFE,
    668  1.23.2.1       tls 			      cprng_fast_rekey_one, NULL);
    669  1.23.2.1       tls 	cprng_fast_initialized++;
    670  1.23.2.1       tls }
    671  1.23.2.1       tls 
    672  1.23.2.1       tls static inline void
    673  1.23.2.1       tls cprng_fast_checkrekey(cprng_fast_ctx_t *ctx)
    674  1.23.2.1       tls {
    675  1.23.2.1       tls 	extern void *cprng_fast_rekey_softintr;
    676  1.23.2.1       tls 
    677  1.23.2.1       tls 	if (__predict_false((ctx->numbytes > CPRNGF_MAXBYTES) ||
    678  1.23.2.1       tls 			    (time_uptime > ctx->nextreseed))) {
    679  1.23.2.1       tls 		/* Schedule a deferred reseed */
    680  1.23.2.1       tls 		softint_schedule(cprng_fast_rekey_softintr);
    681  1.23.2.1       tls 	}
    682  1.23.2.1       tls }
    683  1.23.2.1       tls 
    684  1.23.2.1       tls uint32_t
    685  1.23.2.1       tls cprng_fast32(void)
    686  1.23.2.1       tls {
    687  1.23.2.1       tls 	cprng_fast_ctx_t *ctx = percpu_getref(percpu_cprng_fast_ctx);
    688  1.23.2.1       tls 	int s;
    689  1.23.2.1       tls 	uint32_t ret;
    690  1.23.2.1       tls 
    691  1.23.2.1       tls 	cprng_fast_checkrekey(ctx);
    692  1.23.2.1       tls 
    693  1.23.2.1       tls 	s = splhigh();
    694  1.23.2.1       tls 	ret = ccrand32(&ctx->ccrand);
    695  1.23.2.1       tls 	splx(s);
    696  1.23.2.1       tls 	ctx->numbytes+= sizeof(ret);
    697  1.23.2.1       tls 	percpu_putref(percpu_cprng_fast_ctx);
    698  1.23.2.1       tls 	return ret;
    699  1.23.2.1       tls }
    700  1.23.2.1       tls 
    701  1.23.2.1       tls uint64_t
    702  1.23.2.1       tls cprng_fast64(void)
    703  1.23.2.1       tls {
    704  1.23.2.1       tls 	cprng_fast_ctx_t *ctx = percpu_getref(percpu_cprng_fast_ctx);
    705  1.23.2.1       tls 	int s;
    706  1.23.2.1       tls 	uint64_t ret;
    707  1.23.2.1       tls 
    708  1.23.2.1       tls 	cprng_fast_checkrekey(ctx);
    709  1.23.2.1       tls 
    710  1.23.2.1       tls 	s = splhigh();
    711  1.23.2.1       tls 	ret = ccrand64(&ctx->ccrand);
    712  1.23.2.1       tls 	splx(s);
    713  1.23.2.1       tls 	ctx->numbytes += sizeof(ret);
    714  1.23.2.1       tls 	percpu_putref(percpu_cprng_fast_ctx);
    715  1.23.2.1       tls 	return ret;
    716  1.23.2.1       tls }
    717  1.23.2.1       tls 
    718  1.23.2.1       tls size_t
    719  1.23.2.1       tls cprng_fast(void *p, size_t len)
    720  1.23.2.1       tls {
    721  1.23.2.1       tls 	cprng_fast_ctx_t *ctx = percpu_getref(percpu_cprng_fast_ctx);
    722  1.23.2.1       tls 	int s;
    723  1.23.2.1       tls 
    724  1.23.2.1       tls 	cprng_fast_checkrekey(ctx);
    725      1.23     pooka 
    726  1.23.2.1       tls 	s = splhigh();
    727  1.23.2.1       tls 	ccrand_bytes(&ctx->ccrand, p, len);
    728  1.23.2.1       tls 	splx(s);
    729  1.23.2.1       tls 	ctx->numbytes += len;
    730  1.23.2.1       tls 	percpu_putref(percpu_cprng_fast_ctx);
    731  1.23.2.1       tls 	return len;
    732      1.23     pooka }
    733