Home | History | Annotate | Line # | Download | only in kern
subr_cprng.c revision 1.40.6.1
      1  1.40.6.1   thorpej /*	$NetBSD: subr_cprng.c,v 1.40.6.1 2021/08/01 22:42:38 thorpej Exp $	*/
      2       1.1       tls 
      3       1.1       tls /*-
      4      1.36  riastrad  * Copyright (c) 2019 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.36  riastrad  * by 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.36  riastrad /*
     33      1.36  riastrad  * cprng_strong
     34      1.36  riastrad  *
     35      1.36  riastrad  *	Per-CPU NIST Hash_DRBG, reseeded automatically from the entropy
     36      1.36  riastrad  *	pool when we transition to full entropy, never blocking.  This
     37      1.36  riastrad  *	is slightly different from the old cprng_strong API, but the
     38      1.36  riastrad  *	only users of the old one fell into three categories:
     39      1.36  riastrad  *
     40      1.36  riastrad  *	1. never-blocking, oughta-be-per-CPU (kern_cprng, sysctl_prng)
     41      1.36  riastrad  *	2. never-blocking, used per-CPU anyway (/dev/urandom short reads)
     42      1.36  riastrad  *	3. /dev/random
     43      1.36  riastrad  *
     44      1.36  riastrad  *	This code serves the first two categories without having extra
     45      1.36  riastrad  *	logic for /dev/random.
     46      1.36  riastrad  *
     47      1.36  riastrad  *	kern_cprng - available at IPL_VM or lower
     48      1.36  riastrad  *	user_cprng - available only at IPL_NONE in thread context
     49      1.36  riastrad  *
     50      1.36  riastrad  *	The name kern_cprng is for hysterical raisins.  The name
     51      1.36  riastrad  *	user_cprng serves only to contrast with kern_cprng.
     52      1.36  riastrad  */
     53      1.36  riastrad 
     54      1.18  riastrad #include <sys/cdefs.h>
     55  1.40.6.1   thorpej __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.40.6.1 2021/08/01 22:42:38 thorpej Exp $");
     56      1.18  riastrad 
     57  1.40.6.1   thorpej #include <sys/param.h>
     58       1.1       tls #include <sys/types.h>
     59      1.18  riastrad #include <sys/cprng.h>
     60      1.36  riastrad #include <sys/cpu.h>
     61      1.36  riastrad #include <sys/entropy.h>
     62      1.18  riastrad #include <sys/errno.h>
     63      1.36  riastrad #include <sys/evcnt.h>
     64      1.36  riastrad #include <sys/intr.h>
     65      1.18  riastrad #include <sys/kmem.h>
     66      1.24       tls #include <sys/percpu.h>
     67      1.36  riastrad #include <sys/sysctl.h>
     68       1.1       tls #include <sys/systm.h>
     69      1.18  riastrad 
     70      1.31  riastrad #include <crypto/nist_hash_drbg/nist_hash_drbg.h>
     71       1.1       tls 
     72      1.36  riastrad /*
     73      1.36  riastrad  * struct cprng_strong
     74      1.36  riastrad  */
     75      1.18  riastrad struct cprng_strong {
     76      1.36  riastrad 	struct percpu		*cs_percpu; /* struct cprng_cpu */
     77      1.36  riastrad 	ipl_cookie_t		cs_iplcookie;
     78      1.36  riastrad };
     79      1.21  riastrad 
     80      1.36  riastrad /*
     81      1.36  riastrad  * struct cprng_cpu
     82      1.36  riastrad  *
     83      1.36  riastrad  *	Per-CPU state for a cprng_strong.  The DRBG and evcnt are
     84      1.36  riastrad  *	allocated separately because percpu(9) sometimes moves per-CPU
     85      1.36  riastrad  *	objects around without zeroing them.
     86      1.36  riastrad  */
     87      1.36  riastrad struct cprng_cpu {
     88      1.36  riastrad 	struct nist_hash_drbg	*cc_drbg;
     89      1.36  riastrad 	struct {
     90      1.36  riastrad 		struct evcnt	reseed;
     91      1.36  riastrad 		struct evcnt	intr;
     92      1.36  riastrad 	}			*cc_evcnt;
     93      1.36  riastrad 	unsigned		cc_epoch;
     94      1.18  riastrad };
     95      1.18  riastrad 
     96      1.36  riastrad static int	sysctl_kern_urandom(SYSCTLFN_ARGS);
     97      1.36  riastrad static int	sysctl_kern_arandom(SYSCTLFN_ARGS);
     98      1.36  riastrad static void	cprng_init_cpu(void *, void *, struct cpu_info *);
     99      1.36  riastrad static void	cprng_fini_cpu(void *, void *, struct cpu_info *);
    100      1.36  riastrad 
    101      1.36  riastrad /* Well-known CPRNG instances */
    102      1.36  riastrad struct cprng_strong *kern_cprng __read_mostly; /* IPL_VM */
    103      1.36  riastrad struct cprng_strong *user_cprng __read_mostly; /* IPL_NONE */
    104      1.18  riastrad 
    105      1.36  riastrad static struct sysctllog *cprng_sysctllog __read_mostly;
    106      1.18  riastrad 
    107      1.18  riastrad void
    108      1.36  riastrad cprng_init(void)
    109       1.8       tls {
    110       1.8       tls 
    111      1.36  riastrad 	if (__predict_false(nist_hash_drbg_initialize() != 0))
    112      1.36  riastrad 		panic("NIST Hash_DRBG failed self-test");
    113      1.36  riastrad 
    114      1.18  riastrad 	/*
    115      1.36  riastrad 	 * Create CPRNG instances at two IPLs: IPL_VM for kernel use
    116      1.36  riastrad 	 * that may occur inside IPL_VM interrupt handlers (!!??!?!?),
    117      1.36  riastrad 	 * and IPL_NONE for userland use which need not block
    118      1.36  riastrad 	 * interrupts.
    119      1.18  riastrad 	 */
    120      1.36  riastrad 	kern_cprng = cprng_strong_create("kern", IPL_VM, 0);
    121      1.36  riastrad 	user_cprng = cprng_strong_create("user", IPL_NONE, 0);
    122       1.8       tls 
    123      1.36  riastrad 	/* Create kern.urandom and kern.arandom sysctl nodes.  */
    124      1.36  riastrad 	sysctl_createv(&cprng_sysctllog, 0, NULL, NULL,
    125      1.36  riastrad 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT, "urandom",
    126      1.36  riastrad 	    SYSCTL_DESCR("Independent uniform random 32-bit integer"),
    127      1.36  riastrad 	    sysctl_kern_urandom, 0, NULL, 0, CTL_KERN, KERN_URND, CTL_EOL);
    128      1.36  riastrad 	sysctl_createv(&cprng_sysctllog, 0, NULL, NULL,
    129      1.36  riastrad 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT /*lie*/, "arandom",
    130      1.36  riastrad 	    SYSCTL_DESCR("Independent uniform random bytes, up to 256 bytes"),
    131      1.36  riastrad 	    sysctl_kern_arandom, 0, NULL, 0, CTL_KERN, KERN_ARND, CTL_EOL);
    132       1.8       tls }
    133       1.8       tls 
    134      1.18  riastrad /*
    135      1.36  riastrad  * sysctl kern.urandom
    136      1.36  riastrad  *
    137      1.36  riastrad  *	Independent uniform random 32-bit integer.  Read-only.
    138      1.18  riastrad  */
    139      1.18  riastrad static int
    140      1.36  riastrad sysctl_kern_urandom(SYSCTLFN_ARGS)
    141      1.18  riastrad {
    142      1.36  riastrad 	struct sysctlnode node = *rnode;
    143      1.36  riastrad 	int v;
    144      1.36  riastrad 	int error;
    145       1.1       tls 
    146      1.36  riastrad 	/* Generate an int's worth of data.  */
    147      1.36  riastrad 	cprng_strong(user_cprng, &v, sizeof v, 0);
    148       1.7       tls 
    149      1.36  riastrad 	/* Do the sysctl dance.  */
    150      1.36  riastrad 	node.sysctl_data = &v;
    151      1.36  riastrad 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    152      1.36  riastrad 
    153      1.36  riastrad 	/* Clear the buffer before returning the sysctl error.  */
    154      1.36  riastrad 	explicit_memset(&v, 0, sizeof v);
    155      1.36  riastrad 	return error;
    156       1.1       tls }
    157       1.1       tls 
    158      1.36  riastrad /*
    159      1.36  riastrad  * sysctl kern.arandom
    160      1.36  riastrad  *
    161      1.36  riastrad  *	Independent uniform random bytes, up to 256 bytes.  Read-only.
    162      1.36  riastrad  */
    163      1.29  christos static int
    164      1.36  riastrad sysctl_kern_arandom(SYSCTLFN_ARGS)
    165      1.29  christos {
    166      1.36  riastrad 	struct sysctlnode node = *rnode;
    167      1.36  riastrad 	uint8_t buf[256];
    168      1.36  riastrad 	int error;
    169      1.29  christos 
    170      1.36  riastrad 	/*
    171      1.36  riastrad 	 * Clamp to a reasonably small size.  256 bytes is kind of
    172      1.36  riastrad 	 * arbitrary; 32 would be more reasonable, but we used 256 in
    173      1.36  riastrad 	 * the past, so let's not break compatibility.
    174      1.36  riastrad 	 */
    175      1.36  riastrad 	if (*oldlenp > 256)	/* size_t, so never negative */
    176      1.37       nia 		*oldlenp = 256;
    177      1.29  christos 
    178      1.36  riastrad 	/* Generate data.  */
    179      1.36  riastrad 	cprng_strong(user_cprng, buf, *oldlenp, 0);
    180      1.29  christos 
    181      1.36  riastrad 	/* Do the sysctl dance.  */
    182      1.36  riastrad 	node.sysctl_data = buf;
    183      1.36  riastrad 	node.sysctl_size = *oldlenp;
    184      1.36  riastrad 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    185      1.36  riastrad 
    186      1.36  riastrad 	/* Clear the buffer before returning the sysctl error.  */
    187      1.36  riastrad 	explicit_memset(buf, 0, sizeof buf);
    188      1.36  riastrad 	return error;
    189      1.29  christos }
    190      1.29  christos 
    191      1.36  riastrad struct cprng_strong *
    192      1.36  riastrad cprng_strong_create(const char *name, int ipl, int flags)
    193      1.29  christos {
    194      1.36  riastrad 	struct cprng_strong *cprng;
    195      1.29  christos 
    196      1.36  riastrad 	cprng = kmem_alloc(sizeof(*cprng), KM_SLEEP);
    197      1.36  riastrad 	cprng->cs_iplcookie = makeiplcookie(ipl);
    198      1.36  riastrad 	cprng->cs_percpu = percpu_create(sizeof(struct cprng_cpu),
    199      1.36  riastrad 	    cprng_init_cpu, cprng_fini_cpu, __UNCONST(name));
    200      1.29  christos 
    201      1.36  riastrad 	return cprng;
    202      1.29  christos }
    203      1.29  christos 
    204      1.36  riastrad void
    205      1.36  riastrad cprng_strong_destroy(struct cprng_strong *cprng)
    206      1.15       tls {
    207      1.18  riastrad 
    208      1.36  riastrad 	percpu_free(cprng->cs_percpu, sizeof(struct cprng_cpu));
    209      1.36  riastrad 	kmem_free(cprng, sizeof(*cprng));
    210      1.15       tls }
    211      1.15       tls 
    212      1.18  riastrad static void
    213      1.36  riastrad cprng_init_cpu(void *ptr, void *cookie, struct cpu_info *ci)
    214      1.18  riastrad {
    215      1.36  riastrad 	struct cprng_cpu *cc = ptr;
    216      1.36  riastrad 	const char *name = cookie;
    217      1.39  riastrad 	const char *cpuname;
    218      1.36  riastrad 	uint8_t zero[NIST_HASH_DRBG_SEEDLEN_BYTES] = {0};
    219      1.36  riastrad 	char namebuf[64];	/* XXX size? */
    220      1.18  riastrad 
    221      1.18  riastrad 	/*
    222      1.36  riastrad 	 * Format the name as, e.g., kern/8 if we're on cpu8.  This
    223      1.36  riastrad 	 * doesn't get displayed anywhere; it just ensures that if
    224      1.36  riastrad 	 * there were a bug causing us to use the same otherwise secure
    225      1.36  riastrad 	 * seed on multiple CPUs, we would still get independent output
    226      1.36  riastrad 	 * from the NIST Hash_DRBG.
    227      1.18  riastrad 	 */
    228      1.36  riastrad 	snprintf(namebuf, sizeof namebuf, "%s/%u", name, cpu_index(ci));
    229       1.1       tls 
    230      1.18  riastrad 	/*
    231      1.36  riastrad 	 * Allocate the struct nist_hash_drbg and struct evcnt
    232      1.36  riastrad 	 * separately, since percpu(9) may move objects around in
    233      1.36  riastrad 	 * memory without zeroing.
    234      1.18  riastrad 	 */
    235      1.36  riastrad 	cc->cc_drbg = kmem_zalloc(sizeof(*cc->cc_drbg), KM_SLEEP);
    236      1.36  riastrad 	cc->cc_evcnt = kmem_alloc(sizeof(*cc->cc_evcnt), KM_SLEEP);
    237       1.1       tls 
    238      1.18  riastrad 	/*
    239      1.36  riastrad 	 * Initialize the DRBG with no seed.  We do this in order to
    240      1.36  riastrad 	 * defer reading from the entropy pool as long as possible.
    241      1.18  riastrad 	 */
    242      1.36  riastrad 	if (__predict_false(nist_hash_drbg_instantiate(cc->cc_drbg,
    243      1.36  riastrad 		    zero, sizeof zero, NULL, 0, namebuf, strlen(namebuf))))
    244      1.36  riastrad 		panic("nist_hash_drbg_instantiate");
    245      1.36  riastrad 
    246      1.36  riastrad 	/* Attach the event counters.  */
    247      1.39  riastrad 	/* XXX ci_cpuname may not be initialized early enough.  */
    248      1.39  riastrad 	cpuname = ci->ci_cpuname[0] == '\0' ? "cpu0" : ci->ci_cpuname;
    249      1.36  riastrad 	evcnt_attach_dynamic(&cc->cc_evcnt->intr, EVCNT_TYPE_MISC, NULL,
    250      1.39  riastrad 	    cpuname, "cprng_strong intr");
    251      1.36  riastrad 	evcnt_attach_dynamic(&cc->cc_evcnt->reseed, EVCNT_TYPE_MISC, NULL,
    252      1.39  riastrad 	    cpuname, "cprng_strong reseed");
    253      1.36  riastrad 
    254      1.36  riastrad 	/* Set the epoch uninitialized so we reseed on first use.  */
    255      1.36  riastrad 	cc->cc_epoch = 0;
    256      1.18  riastrad }
    257       1.1       tls 
    258      1.18  riastrad static void
    259      1.36  riastrad cprng_fini_cpu(void *ptr, void *cookie, struct cpu_info *ci)
    260      1.18  riastrad {
    261      1.36  riastrad 	struct cprng_cpu *cc = ptr;
    262       1.5       tls 
    263      1.36  riastrad 	evcnt_detach(&cc->cc_evcnt->reseed);
    264      1.36  riastrad 	evcnt_detach(&cc->cc_evcnt->intr);
    265      1.36  riastrad 	if (__predict_false(nist_hash_drbg_destroy(cc->cc_drbg)))
    266      1.36  riastrad 		panic("nist_hash_drbg_destroy");
    267       1.1       tls 
    268      1.36  riastrad 	kmem_free(cc->cc_evcnt, sizeof(*cc->cc_evcnt));
    269      1.36  riastrad 	kmem_free(cc->cc_drbg, sizeof(*cc->cc_drbg));
    270       1.1       tls }
    271       1.1       tls 
    272      1.36  riastrad size_t
    273      1.36  riastrad cprng_strong(struct cprng_strong *cprng, void *buf, size_t len, int flags)
    274       1.1       tls {
    275      1.36  riastrad 	uint32_t seed[NIST_HASH_DRBG_SEEDLEN_BYTES];
    276      1.36  riastrad 	struct cprng_cpu *cc;
    277      1.36  riastrad 	unsigned epoch;
    278      1.36  riastrad 	int s;
    279       1.1       tls 
    280       1.1       tls 	/*
    281      1.36  riastrad 	 * Verify maximum request length.  Caller should really limit
    282      1.36  riastrad 	 * their requests to 32 bytes to avoid spending much time with
    283      1.36  riastrad 	 * preemption disabled -- use the 32 bytes to seed a private
    284      1.36  riastrad 	 * DRBG instance if you need more data.
    285       1.1       tls 	 */
    286      1.36  riastrad 	KASSERT(len <= CPRNG_MAX_LEN);
    287      1.18  riastrad 
    288      1.36  riastrad 	/* Verify legacy API use.  */
    289      1.36  riastrad 	KASSERT(flags == 0);
    290       1.1       tls 
    291      1.36  riastrad 	/* Acquire per-CPU state and block interrupts.  */
    292      1.36  riastrad 	cc = percpu_getref(cprng->cs_percpu);
    293      1.36  riastrad 	s = splraiseipl(cprng->cs_iplcookie);
    294      1.36  riastrad 
    295      1.36  riastrad 	if (cpu_intr_p())
    296      1.36  riastrad 		cc->cc_evcnt->intr.ev_count++;
    297      1.36  riastrad 
    298      1.36  riastrad 	/* If the entropy epoch has changed, (re)seed.  */
    299      1.36  riastrad 	epoch = entropy_epoch();
    300      1.36  riastrad 	if (__predict_false(epoch != cc->cc_epoch)) {
    301      1.36  riastrad 		entropy_extract(seed, sizeof seed, 0);
    302      1.36  riastrad 		cc->cc_evcnt->reseed.ev_count++;
    303      1.36  riastrad 		if (__predict_false(nist_hash_drbg_reseed(cc->cc_drbg,
    304      1.36  riastrad 			    seed, sizeof seed, NULL, 0)))
    305      1.36  riastrad 			panic("nist_hash_drbg_reseed");
    306      1.36  riastrad 		explicit_memset(seed, 0, sizeof seed);
    307      1.36  riastrad 		cc->cc_epoch = epoch;
    308      1.36  riastrad 	}
    309       1.1       tls 
    310      1.36  riastrad 	/* Generate data.  Failure here means it's time to reseed.  */
    311      1.36  riastrad 	if (__predict_false(nist_hash_drbg_generate(cc->cc_drbg, buf, len,
    312      1.36  riastrad 		    NULL, 0))) {
    313      1.36  riastrad 		entropy_extract(seed, sizeof seed, 0);
    314      1.36  riastrad 		cc->cc_evcnt->reseed.ev_count++;
    315      1.36  riastrad 		if (__predict_false(nist_hash_drbg_reseed(cc->cc_drbg,
    316      1.36  riastrad 			    seed, sizeof seed, NULL, 0)))
    317      1.36  riastrad 			panic("nist_hash_drbg_reseed");
    318      1.36  riastrad 		explicit_memset(seed, 0, sizeof seed);
    319      1.36  riastrad 		if (__predict_false(nist_hash_drbg_generate(cc->cc_drbg,
    320      1.36  riastrad 			    buf, len, NULL, 0)))
    321      1.36  riastrad 			panic("nist_hash_drbg_generate");
    322      1.36  riastrad 	}
    323      1.23     pooka 
    324      1.36  riastrad 	/* Release state and interrupts.  */
    325      1.36  riastrad 	splx(s);
    326      1.36  riastrad 	percpu_putref(cprng->cs_percpu);
    327      1.23     pooka 
    328      1.36  riastrad 	/* Return the number of bytes generated, for hysterical raisins.  */
    329      1.36  riastrad 	return len;
    330      1.23     pooka }
    331      1.23     pooka 
    332      1.36  riastrad uint32_t
    333      1.36  riastrad cprng_strong32(void)
    334      1.23     pooka {
    335      1.36  riastrad 	uint32_t r;
    336      1.36  riastrad 	cprng_strong(kern_cprng, &r, sizeof(r), 0);
    337      1.36  riastrad 	return r;
    338      1.23     pooka }
    339      1.23     pooka 
    340      1.36  riastrad uint64_t
    341      1.36  riastrad cprng_strong64(void)
    342      1.23     pooka {
    343      1.36  riastrad 	uint64_t r;
    344      1.36  riastrad 	cprng_strong(kern_cprng, &r, sizeof(r), 0);
    345      1.36  riastrad 	return r;
    346      1.23     pooka }
    347