Home | History | Annotate | Line # | Download | only in cprng_fast
cprng_fast.c revision 1.17
      1  1.17  riastrad /*	$NetBSD: cprng_fast.c,v 1.17 2022/06/01 15:44:37 riastradh Exp $	*/
      2   1.2       tls 
      3   1.2       tls /*-
      4   1.2       tls  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5   1.2       tls  * All rights reserved.
      6   1.2       tls  *
      7   1.2       tls  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2       tls  * by Taylor R. Campbell.
      9   1.2       tls  *
     10   1.2       tls  * Redistribution and use in source and binary forms, with or without
     11   1.2       tls  * modification, are permitted provided that the following conditions
     12   1.2       tls  * are met:
     13   1.2       tls  * 1. Redistributions of source code must retain the above copyright
     14   1.2       tls  *    notice, this list of conditions and the following disclaimer.
     15   1.2       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2       tls  *    notice, this list of conditions and the following disclaimer in the
     17   1.2       tls  *    documentation and/or other materials provided with the distribution.
     18   1.2       tls  *
     19   1.2       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.2       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.2       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.2       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.2       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.2       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.2       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.2       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.2       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.2       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.2       tls  * POSSIBILITY OF SUCH DAMAGE.
     30   1.2       tls  */
     31   1.2       tls 
     32   1.2       tls #include <sys/cdefs.h>
     33  1.17  riastrad __KERNEL_RCSID(0, "$NetBSD: cprng_fast.c,v 1.17 2022/06/01 15:44:37 riastradh Exp $");
     34   1.2       tls 
     35   1.2       tls #include <sys/types.h>
     36   1.4  riastrad #include <sys/param.h>
     37   1.2       tls #include <sys/bitops.h>
     38   1.4  riastrad #include <sys/cprng.h>
     39   1.2       tls #include <sys/cpu.h>
     40  1.14  riastrad #include <sys/entropy.h>
     41  1.15  riastrad #include <sys/evcnt.h>
     42  1.15  riastrad #include <sys/kmem.h>
     43   1.2       tls #include <sys/percpu.h>
     44   1.2       tls 
     45  1.16  riastrad #include <crypto/chacha/chacha.h>
     46   1.2       tls 
     47  1.16  riastrad #define	CPRNG_FAST_SEED_BYTES	CHACHA_STREAM_KEYBYTES
     49   1.2       tls 
     50  1.16  riastrad struct cprng_fast {
     51  1.16  riastrad 	/* 128-bit vector unit generates 256 bytes at once */
     52  1.16  riastrad 	uint8_t		buf[256];
     53  1.16  riastrad 	uint8_t		key[CPRNG_FAST_SEED_BYTES];
     54  1.16  riastrad 	uint8_t		nonce[CHACHA_STREAM_NONCEBYTES];
     55  1.15  riastrad 	unsigned	i;
     56  1.14  riastrad 	struct evcnt	*reseed_evcnt;
     57   1.2       tls 	unsigned	epoch;
     58   1.2       tls };
     59   1.8  riastrad 
     60  1.17  riastrad static void	cprng_fast_init_cpu(void *, void *, struct cpu_info *);
     61   1.2       tls static void	cprng_fast_reseed(struct cprng_fast *);
     62   1.6  riastrad 
     63   1.2       tls static void	cprng_fast_seed(struct cprng_fast *, const void *);
     64   1.2       tls static void	cprng_fast_buf(struct cprng_fast *, void *, unsigned);
     65   1.2       tls 
     66   1.2       tls static void	cprng_fast_buf_short(void *, size_t);
     67   1.2       tls static void	cprng_fast_buf_long(void *, size_t);
     68   1.2       tls 
     69   1.2       tls static percpu_t	*cprng_fast_percpu	__read_mostly;
     70   1.2       tls 
     71   1.2       tls void
     72   1.2       tls cprng_fast_init(void)
     73   1.2       tls {
     74  1.15  riastrad 
     75  1.15  riastrad 	cprng_fast_percpu = percpu_create(sizeof(struct cprng_fast),
     76   1.2       tls 	    cprng_fast_init_cpu, NULL, NULL);
     77   1.2       tls }
     78   1.8  riastrad 
     79  1.15  riastrad static void
     80   1.8  riastrad cprng_fast_init_cpu(void *p, void *arg __unused, struct cpu_info *ci)
     81   1.8  riastrad {
     82   1.8  riastrad 	struct cprng_fast *const cprng = p;
     83  1.17  riastrad 
     84  1.15  riastrad 	cprng->epoch = 0;
     85  1.15  riastrad 
     86  1.15  riastrad 	cprng->reseed_evcnt = kmem_alloc(sizeof(*cprng->reseed_evcnt),
     87  1.15  riastrad 	    KM_SLEEP);
     88  1.15  riastrad 	evcnt_attach_dynamic(cprng->reseed_evcnt, EVCNT_TYPE_MISC, NULL,
     89   1.8  riastrad 	    ci->ci_cpuname, "cprng_fast reseed");
     90   1.9  riastrad }
     91  1.16  riastrad 
     92   1.2       tls static int
     94   1.9  riastrad cprng_fast_get(struct cprng_fast **cprngp)
     95   1.9  riastrad {
     96   1.9  riastrad 	struct cprng_fast *cprng;
     97  1.17  riastrad 	int s;
     98  1.17  riastrad 
     99   1.9  riastrad 	KASSERT(!cpu_intr_p());
    100  1.17  riastrad 
    101   1.2       tls 	*cprngp = cprng = percpu_getref(cprng_fast_percpu);
    102  1.17  riastrad 	s = splsoftserial();
    103  1.17  riastrad 
    104  1.17  riastrad 	if (__predict_false(cprng->epoch != entropy_epoch())) {
    105  1.17  riastrad 		splx(s);
    106  1.17  riastrad 		cprng_fast_reseed(cprng);
    107   1.9  riastrad 		s = splsoftserial();
    108   1.9  riastrad 	}
    109   1.2       tls 
    110   1.2       tls 	return s;
    111  1.16  riastrad }
    112   1.2       tls 
    113   1.2       tls static void
    114   1.2       tls cprng_fast_put(struct cprng_fast *cprng, int s)
    115   1.2       tls {
    116   1.2       tls 
    117   1.2       tls 	KASSERT((cprng == percpu_getref(cprng_fast_percpu)) &&
    118   1.2       tls 	    (percpu_putref(cprng_fast_percpu), true));
    119   1.2       tls 	splx(s);
    120   1.9  riastrad 	percpu_putref(cprng_fast_percpu);
    121  1.11    justin }
    122  1.17  riastrad 
    123   1.2       tls static void
    124  1.14  riastrad cprng_fast_reseed(struct cprng_fast *cprng)
    125   1.2       tls {
    126   1.7  riastrad 	unsigned epoch = entropy_epoch();
    127   1.2       tls 	uint8_t seed[CPRNG_FAST_SEED_BYTES];
    128  1.12  riastrad 	int s;
    129   1.2       tls 
    130  1.17  riastrad 	cprng_strong(kern_cprng, seed, sizeof(seed), 0);
    131   1.2       tls 
    132  1.14  riastrad 	s = splsoftserial();
    133  1.15  riastrad 	cprng_fast_seed(cprng, seed);
    134   1.7  riastrad 	cprng->epoch = epoch;
    135   1.2       tls 	cprng->reseed_evcnt->ev_count++;
    136   1.2       tls 	splx(s);
    137   1.2       tls 
    138   1.2       tls 	explicit_memset(seed, 0, sizeof(seed));
    139   1.2       tls }
    140   1.2       tls 
    141   1.6  riastrad /* CPRNG algorithm */
    143   1.2       tls 
    144   1.2       tls static void
    145  1.16  riastrad cprng_fast_seed(struct cprng_fast *cprng, const void *seed)
    146   1.2       tls {
    147   1.2       tls 
    148  1.16  riastrad 	(void)memset(cprng->buf, 0, sizeof cprng->buf);
    149   1.2       tls 	(void)memcpy(cprng->key, seed, sizeof cprng->key);
    150   1.2       tls 	(void)memset(cprng->nonce, 0, sizeof cprng->nonce);
    151  1.16  riastrad 	cprng->i = sizeof cprng->buf;
    152  1.16  riastrad }
    153   1.2       tls 
    154  1.16  riastrad static void
    155  1.16  riastrad cprng_fast_buf(struct cprng_fast *cprng, void *buf, unsigned len)
    156  1.16  riastrad {
    157  1.16  riastrad 	uint8_t *p = buf;
    158  1.16  riastrad 	unsigned n = len, n0;
    159   1.2       tls 
    160  1.16  riastrad 	KASSERT(cprng->i <= sizeof(cprng->buf));
    161  1.16  riastrad 	KASSERT(len <= sizeof(cprng->buf));
    162  1.16  riastrad 
    163  1.16  riastrad 	n0 = MIN(n, sizeof(cprng->buf) - cprng->i);
    164  1.16  riastrad 	memcpy(p, &cprng->buf[cprng->i], n0);
    165  1.16  riastrad 	if ((n -= n0) == 0) {
    166   1.2       tls 		cprng->i += n0;
    167  1.16  riastrad 		KASSERT(cprng->i <= sizeof(cprng->buf));
    168  1.16  riastrad 		return;
    169  1.16  riastrad 	}
    170  1.16  riastrad 	p += n0;
    171  1.16  riastrad 	le64enc(cprng->nonce, 1 + le64dec(cprng->nonce));
    172  1.16  riastrad 	chacha_stream(cprng->buf, sizeof(cprng->buf), 0, cprng->nonce,
    173   1.2       tls 	    cprng->key, 8);
    174  1.16  riastrad 	memcpy(p, cprng->buf, n);
    175  1.16  riastrad 	cprng->i = n;
    176   1.2       tls }
    177  1.16  riastrad 
    178  1.16  riastrad /* Public API */
    180  1.16  riastrad 
    181  1.16  riastrad static void
    182   1.2       tls cprng_fast_buf_short(void *buf, size_t len)
    183  1.16  riastrad {
    184  1.10  riastrad 	struct cprng_fast *cprng;
    185  1.16  riastrad 	int s;
    186  1.16  riastrad 
    187  1.16  riastrad 	KASSERT(len <= sizeof(cprng->buf));
    188   1.2       tls 
    189  1.16  riastrad 	s = cprng_fast_get(&cprng);
    190   1.2       tls 	cprng_fast_buf(cprng, buf, len);
    191  1.16  riastrad 	cprng_fast_put(cprng, s);
    192   1.2       tls }
    193  1.16  riastrad 
    194  1.16  riastrad static void
    195   1.2       tls cprng_fast_buf_long(void *buf, size_t len)
    196  1.16  riastrad {
    197  1.16  riastrad 	uint8_t seed[CHACHA_STREAM_KEYBYTES];
    198  1.16  riastrad 	uint8_t nonce[CHACHA_STREAM_NONCEBYTES] = {0};
    199  1.16  riastrad 
    200  1.16  riastrad 	CTASSERT(sizeof(seed) <= sizeof(((struct cprng_fast *)0)->buf));
    201  1.16  riastrad 
    202   1.2       tls #if SIZE_MAX >= 0x3fffffffff
    203  1.16  riastrad 	/* >=256 GB is not reasonable */
    204  1.16  riastrad 	KASSERT(len <= 0x3fffffffff);
    205   1.2       tls #endif
    206  1.16  riastrad 
    207   1.2       tls 	cprng_fast_buf_short(seed, sizeof seed);
    208   1.2       tls 	chacha_stream(buf, len, 0, nonce, seed, 8);
    209   1.2       tls 
    210   1.2       tls 	(void)explicit_memset(seed, 0, sizeof seed);
    211   1.2       tls }
    212   1.2       tls 
    213   1.2       tls uint32_t
    214  1.16  riastrad cprng_fast32(void)
    215   1.2       tls {
    216   1.2       tls 	uint32_t v;
    217   1.2       tls 
    218   1.2       tls 	cprng_fast_buf_short(&v, sizeof v);
    219   1.2       tls 
    220   1.2       tls 	return v;
    221   1.2       tls }
    222  1.16  riastrad 
    223   1.2       tls uint64_t
    224  1.16  riastrad cprng_fast64(void)
    225   1.2       tls {
    226  1.16  riastrad 	uint64_t v;
    227   1.2       tls 
    228   1.2       tls 	cprng_fast_buf_short(&v, sizeof v);
    229   1.2       tls 
    230   1.2       tls 	return v;
    231   1.2       tls }
    232   1.2       tls 
    233   1.2       tls size_t
    234   1.2       tls cprng_fast(void *buf, size_t len)
    235   1.2       tls {
    236  1.16  riastrad 
    237   1.2       tls 	/*
    238  1.16  riastrad 	 * We don't want to hog the CPU, so we use the short version,
    239   1.2       tls 	 * to generate output without preemption, only if we can do it
    240   1.2       tls 	 * with at most one ChaCha call.
    241   1.2       tls 	 */
    242   1.2       tls 	if (len <= sizeof(((struct cprng_fast *)0)->buf))
    243  1.16  riastrad 		cprng_fast_buf_short(buf, len);
    244   1.2       tls 	else
    245                 		cprng_fast_buf_long(buf, len);
    246                 
    247                 	return len;		/* hysterical raisins */
    248                 }
    249