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