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