cprng_fast.c revision 1.19 1 1.19 riastrad /* $NetBSD: cprng_fast.c,v 1.19 2023/08/05 11:39:18 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.19 riastrad __KERNEL_RCSID(0, "$NetBSD: cprng_fast.c,v 1.19 2023/08/05 11:39:18 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.18 riastrad #include <sys/pserialize.h>
45 1.2 tls
46 1.16 riastrad #include <crypto/chacha/chacha.h>
47 1.2 tls
48 1.16 riastrad #define CPRNG_FAST_SEED_BYTES CHACHA_STREAM_KEYBYTES
50 1.2 tls
51 1.16 riastrad struct cprng_fast {
52 1.16 riastrad /* 128-bit vector unit generates 256 bytes at once */
53 1.16 riastrad uint8_t buf[256];
54 1.16 riastrad uint8_t key[CPRNG_FAST_SEED_BYTES];
55 1.16 riastrad uint8_t nonce[CHACHA_STREAM_NONCEBYTES];
56 1.15 riastrad unsigned i;
57 1.14 riastrad struct evcnt *reseed_evcnt;
58 1.2 tls unsigned epoch;
59 1.2 tls };
60 1.8 riastrad
61 1.19 riastrad static void cprng_fast_init_cpu(void *, void *, struct cpu_info *);
62 1.2 tls static void cprng_fast_reseed(struct cprng_fast **, unsigned);
63 1.6 riastrad
64 1.2 tls static void cprng_fast_seed(struct cprng_fast *, const void *);
65 1.2 tls static void cprng_fast_buf(struct cprng_fast *, void *, unsigned);
66 1.2 tls
67 1.2 tls static void cprng_fast_buf_short(void *, size_t);
68 1.2 tls static void cprng_fast_buf_long(void *, size_t);
69 1.2 tls
70 1.2 tls static percpu_t *cprng_fast_percpu __read_mostly;
71 1.2 tls
72 1.2 tls void
73 1.2 tls cprng_fast_init(void)
74 1.2 tls {
75 1.15 riastrad
76 1.15 riastrad cprng_fast_percpu = percpu_create(sizeof(struct cprng_fast),
77 1.2 tls cprng_fast_init_cpu, NULL, NULL);
78 1.2 tls }
79 1.8 riastrad
80 1.15 riastrad static void
81 1.8 riastrad cprng_fast_init_cpu(void *p, void *arg __unused, struct cpu_info *ci)
82 1.8 riastrad {
83 1.8 riastrad struct cprng_fast *const cprng = p;
84 1.17 riastrad
85 1.15 riastrad cprng->epoch = 0;
86 1.15 riastrad
87 1.15 riastrad cprng->reseed_evcnt = kmem_alloc(sizeof(*cprng->reseed_evcnt),
88 1.15 riastrad KM_SLEEP);
89 1.15 riastrad evcnt_attach_dynamic(cprng->reseed_evcnt, EVCNT_TYPE_MISC, NULL,
90 1.8 riastrad ci->ci_cpuname, "cprng_fast reseed");
91 1.9 riastrad }
92 1.16 riastrad
93 1.2 tls static int
95 1.9 riastrad cprng_fast_get(struct cprng_fast **cprngp)
96 1.19 riastrad {
97 1.9 riastrad struct cprng_fast *cprng;
98 1.9 riastrad unsigned epoch;
99 1.17 riastrad int s;
100 1.18 riastrad
101 1.17 riastrad KASSERT(!cpu_intr_p());
102 1.9 riastrad KASSERT(pserialize_not_in_read_section());
103 1.17 riastrad
104 1.2 tls *cprngp = cprng = percpu_getref(cprng_fast_percpu);
105 1.19 riastrad s = splsoftserial();
106 1.19 riastrad
107 1.17 riastrad epoch = entropy_epoch();
108 1.19 riastrad if (__predict_false(cprng->epoch != epoch)) {
109 1.17 riastrad splx(s);
110 1.17 riastrad cprng_fast_reseed(cprngp, epoch);
111 1.9 riastrad s = splsoftserial();
112 1.9 riastrad }
113 1.2 tls
114 1.2 tls return s;
115 1.16 riastrad }
116 1.2 tls
117 1.2 tls static void
118 1.2 tls cprng_fast_put(struct cprng_fast *cprng, int s)
119 1.2 tls {
120 1.2 tls
121 1.2 tls KASSERT((cprng == percpu_getref(cprng_fast_percpu)) &&
122 1.2 tls (percpu_putref(cprng_fast_percpu), true));
123 1.2 tls splx(s);
124 1.9 riastrad percpu_putref(cprng_fast_percpu);
125 1.11 justin }
126 1.19 riastrad
127 1.2 tls static void
128 1.19 riastrad cprng_fast_reseed(struct cprng_fast **cprngp, unsigned epoch)
129 1.2 tls {
130 1.7 riastrad struct cprng_fast *cprng;
131 1.2 tls uint8_t seed[CPRNG_FAST_SEED_BYTES];
132 1.19 riastrad int s;
133 1.19 riastrad
134 1.19 riastrad /*
135 1.19 riastrad * Drop the percpu(9) reference to extract a fresh seed from
136 1.19 riastrad * the entropy pool. cprng_strong may sleep on an adaptive
137 1.19 riastrad * lock, which invalidates our percpu(9) reference.
138 1.19 riastrad *
139 1.19 riastrad * This may race with reseeding in another thread, which is no
140 1.19 riastrad * big deal -- worst case, we rewind the entropy epoch here and
141 1.19 riastrad * cause the next caller to reseed again, and in the end we
142 1.19 riastrad * just reseed a couple more times than necessary.
143 1.12 riastrad */
144 1.19 riastrad percpu_putref(cprng_fast_percpu);
145 1.2 tls cprng_strong(kern_cprng, seed, sizeof(seed), 0);
146 1.17 riastrad *cprngp = cprng = percpu_getref(cprng_fast_percpu);
147 1.2 tls
148 1.14 riastrad s = splsoftserial();
149 1.15 riastrad cprng_fast_seed(cprng, seed);
150 1.7 riastrad cprng->epoch = epoch;
151 1.2 tls cprng->reseed_evcnt->ev_count++;
152 1.2 tls splx(s);
153 1.2 tls
154 1.2 tls explicit_memset(seed, 0, sizeof(seed));
155 1.2 tls }
156 1.2 tls
157 1.6 riastrad /* CPRNG algorithm */
159 1.2 tls
160 1.2 tls static void
161 1.16 riastrad cprng_fast_seed(struct cprng_fast *cprng, const void *seed)
162 1.2 tls {
163 1.2 tls
164 1.16 riastrad (void)memset(cprng->buf, 0, sizeof cprng->buf);
165 1.2 tls (void)memcpy(cprng->key, seed, sizeof cprng->key);
166 1.2 tls (void)memset(cprng->nonce, 0, sizeof cprng->nonce);
167 1.16 riastrad cprng->i = sizeof cprng->buf;
168 1.16 riastrad }
169 1.2 tls
170 1.16 riastrad static void
171 1.16 riastrad cprng_fast_buf(struct cprng_fast *cprng, void *buf, unsigned len)
172 1.16 riastrad {
173 1.16 riastrad uint8_t *p = buf;
174 1.16 riastrad unsigned n = len, n0;
175 1.2 tls
176 1.16 riastrad KASSERT(cprng->i <= sizeof(cprng->buf));
177 1.16 riastrad KASSERT(len <= sizeof(cprng->buf));
178 1.16 riastrad
179 1.16 riastrad n0 = MIN(n, sizeof(cprng->buf) - cprng->i);
180 1.16 riastrad memcpy(p, &cprng->buf[cprng->i], n0);
181 1.16 riastrad if ((n -= n0) == 0) {
182 1.2 tls cprng->i += n0;
183 1.16 riastrad KASSERT(cprng->i <= sizeof(cprng->buf));
184 1.16 riastrad return;
185 1.16 riastrad }
186 1.16 riastrad p += n0;
187 1.16 riastrad le64enc(cprng->nonce, 1 + le64dec(cprng->nonce));
188 1.16 riastrad chacha_stream(cprng->buf, sizeof(cprng->buf), 0, cprng->nonce,
189 1.2 tls cprng->key, 8);
190 1.16 riastrad memcpy(p, cprng->buf, n);
191 1.16 riastrad cprng->i = n;
192 1.2 tls }
193 1.16 riastrad
194 1.16 riastrad /* Public API */
196 1.16 riastrad
197 1.16 riastrad static void
198 1.2 tls cprng_fast_buf_short(void *buf, size_t len)
199 1.16 riastrad {
200 1.10 riastrad struct cprng_fast *cprng;
201 1.16 riastrad int s;
202 1.16 riastrad
203 1.16 riastrad KASSERT(len <= sizeof(cprng->buf));
204 1.2 tls
205 1.16 riastrad s = cprng_fast_get(&cprng);
206 1.2 tls cprng_fast_buf(cprng, buf, len);
207 1.16 riastrad cprng_fast_put(cprng, s);
208 1.2 tls }
209 1.16 riastrad
210 1.16 riastrad static void
211 1.2 tls cprng_fast_buf_long(void *buf, size_t len)
212 1.16 riastrad {
213 1.16 riastrad uint8_t seed[CHACHA_STREAM_KEYBYTES];
214 1.16 riastrad uint8_t nonce[CHACHA_STREAM_NONCEBYTES] = {0};
215 1.16 riastrad
216 1.16 riastrad CTASSERT(sizeof(seed) <= sizeof(((struct cprng_fast *)0)->buf));
217 1.16 riastrad
218 1.2 tls #if SIZE_MAX >= 0x3fffffffff
219 1.16 riastrad /* >=256 GB is not reasonable */
220 1.16 riastrad KASSERT(len <= 0x3fffffffff);
221 1.2 tls #endif
222 1.16 riastrad
223 1.2 tls cprng_fast_buf_short(seed, sizeof seed);
224 1.2 tls chacha_stream(buf, len, 0, nonce, seed, 8);
225 1.2 tls
226 1.2 tls (void)explicit_memset(seed, 0, sizeof seed);
227 1.2 tls }
228 1.2 tls
229 1.2 tls uint32_t
230 1.16 riastrad cprng_fast32(void)
231 1.2 tls {
232 1.2 tls uint32_t v;
233 1.2 tls
234 1.2 tls cprng_fast_buf_short(&v, sizeof v);
235 1.2 tls
236 1.2 tls return v;
237 1.2 tls }
238 1.16 riastrad
239 1.2 tls uint64_t
240 1.16 riastrad cprng_fast64(void)
241 1.2 tls {
242 1.16 riastrad uint64_t v;
243 1.2 tls
244 1.2 tls cprng_fast_buf_short(&v, sizeof v);
245 1.2 tls
246 1.2 tls return v;
247 1.2 tls }
248 1.2 tls
249 1.2 tls size_t
250 1.2 tls cprng_fast(void *buf, size_t len)
251 1.2 tls {
252 1.16 riastrad
253 1.2 tls /*
254 1.16 riastrad * We don't want to hog the CPU, so we use the short version,
255 1.2 tls * to generate output without preemption, only if we can do it
256 1.2 tls * with at most one ChaCha call.
257 1.2 tls */
258 1.2 tls if (len <= sizeof(((struct cprng_fast *)0)->buf))
259 1.16 riastrad cprng_fast_buf_short(buf, len);
260 1.2 tls else
261 cprng_fast_buf_long(buf, len);
262
263 return len; /* hysterical raisins */
264 }
265