subr_cprng.c revision 1.6 1 /* $NetBSD: subr_cprng.c,v 1.6 2012/04/10 14:02:28 tls Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Thor Lancelot Simon.
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/types.h>
33 #include <sys/time.h>
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/kmem.h>
38 #include <sys/mutex.h>
39 #include <sys/rngtest.h>
40 #include <sys/rnd.h>
41 #include <dev/rnd_private.h>
42
43 #if defined(__HAVE_CPU_COUNTER)
44 #include <machine/cpu_counter.h>
45 #endif
46
47 #include <sys/cprng.h>
48
49 __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.6 2012/04/10 14:02:28 tls Exp $");
50
51 void
52 cprng_init(void)
53 {
54 nist_ctr_initialize();
55 }
56
57 static inline uint32_t
58 cprng_counter(void)
59 {
60 struct timeval tv;
61
62 #if defined(__HAVE_CPU_COUNTER)
63 if (cpu_hascounter())
64 return cpu_counter32();
65 #endif
66 if (__predict_false(cold)) {
67 /* microtime unsafe if clock not running yet */
68 return 0;
69 }
70 microtime(&tv);
71 return (tv.tv_sec * 1000000 + tv.tv_usec);
72 }
73
74 static void
75 cprng_strong_sched_reseed(cprng_strong_t *const c)
76 {
77 KASSERT(mutex_owned(&c->mtx));
78 if (!(c->reseed_pending) && mutex_tryenter(&c->reseed.mtx)) {
79 c->reseed_pending = 1;
80 c->reseed.len = NIST_BLOCK_KEYLEN_BYTES;
81 rndsink_attach(&c->reseed);
82 mutex_spin_exit(&c->reseed.mtx);
83 }
84 }
85
86 static void
87 cprng_strong_reseed(void *const arg)
88 {
89 cprng_strong_t *c = arg;
90 uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
91 uint32_t cc = cprng_counter();
92
93 mutex_enter(&c->mtx);
94 if (c->reseed.len != sizeof(key)) {
95 panic("cprng_strong_reseed: bad entropy length %d "
96 " (expected %d)", (int)c->reseed.len, (int)sizeof(key));
97 }
98 if (nist_ctr_drbg_reseed(&c->drbg, c->reseed.data, c->reseed.len,
99 &cc, sizeof(cc))) {
100 panic("cprng %s: nist_ctr_drbg_reseed failed.", c->name);
101 }
102 c->reseed_pending = 0;
103 if (c->flags & CPRNG_USE_CV) {
104 cv_broadcast(&c->cv);
105 }
106 selnotify(&c->selq, 0, 0);
107 mutex_exit(&c->mtx);
108 }
109
110 cprng_strong_t *
111 cprng_strong_create(const char *const name, int ipl, int flags)
112 {
113 cprng_strong_t *c;
114 uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
115 int r, getmore = 0, hard = 0;
116 uint32_t cc;
117
118 c = kmem_alloc(sizeof(*c), KM_NOSLEEP);
119 if (c == NULL) {
120 return NULL;
121 }
122 c->flags = flags;
123 strlcpy(c->name, name, sizeof(c->name));
124 c->reseed_pending = 0;
125 c->reseed.cb = cprng_strong_reseed;
126 c->reseed.arg = c;
127 mutex_init(&c->reseed.mtx, MUTEX_DEFAULT, IPL_VM);
128 strlcpy(c->reseed.name, name, sizeof(c->reseed.name));
129
130 mutex_init(&c->mtx, MUTEX_DEFAULT, ipl);
131
132 if (c->flags & CPRNG_USE_CV) {
133 cv_init(&c->cv, name);
134 }
135
136 selinit(&c->selq);
137
138 r = rnd_extract_data(key, sizeof(key), RND_EXTRACT_GOOD);
139 if (r != sizeof(key)) {
140 if (c->flags & CPRNG_INIT_ANY) {
141 #ifdef DEBUG
142 printf("cprng %s: WARNING insufficient "
143 "entropy at creation.\n", name);
144 #endif
145 rnd_extract_data(key + r, sizeof(key - r),
146 RND_EXTRACT_ANY);
147 } else {
148 hard++;
149 }
150 getmore++;
151 }
152
153 if (nist_ctr_drbg_instantiate(&c->drbg, key, sizeof(key),
154 &cc, sizeof(cc), name, strlen(name))) {
155 panic("cprng %s: instantiation failed.", name);
156 }
157
158 if (getmore) {
159 /* Cause readers to wait for rekeying. */
160 if (hard) {
161 c->drbg.reseed_counter =
162 NIST_CTR_DRBG_RESEED_INTERVAL + 1;
163 } else {
164 c->drbg.reseed_counter =
165 (NIST_CTR_DRBG_RESEED_INTERVAL / 2) + 1;
166 }
167 }
168 return c;
169 }
170
171 size_t
172 cprng_strong(cprng_strong_t *const c, void *const p, size_t len, int flags)
173 {
174 uint32_t cc = cprng_counter();
175 #ifdef DEBUG
176 int testfail = 0;
177 #endif
178 if (len > CPRNG_MAX_LEN) { /* XXX should we loop? */
179 len = CPRNG_MAX_LEN; /* let the caller loop if desired */
180 }
181 mutex_enter(&c->mtx);
182
183 if (nist_ctr_drbg_generate(&c->drbg, p, len, &cc, sizeof(cc))) {
184 /* A generator failure really means we hit the hard limit. */
185 if (c->flags & CPRNG_REKEY_ANY) {
186 uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
187
188 printf("cprng %s: WARNING pseudorandom rekeying.\n",
189 c->name);
190 rnd_extract_data(key, sizeof(key), RND_EXTRACT_ANY);
191 cc = cprng_counter();
192 if (nist_ctr_drbg_reseed(&c->drbg, key, sizeof(key),
193 &cc, sizeof(cc))) {
194 panic("cprng %s: nist_ctr_drbg_reseed "
195 "failed.", c->name);
196 }
197 } else {
198 if (!(flags & FNONBLOCK) &&
199 (c->flags & CPRNG_USE_CV)) {
200 int wr;
201
202 cprng_strong_sched_reseed(c);
203 do {
204 wr = cv_wait_sig(&c->cv, &c->mtx);
205 if (wr == ERESTART) {
206 mutex_exit(&c->mtx);
207 return 0;
208 }
209 } while (nist_ctr_drbg_generate(&c->drbg, p,
210 len, &cc,
211 sizeof(cc)));
212 } else {
213 len = 0;
214 }
215 }
216 }
217
218 #ifdef DEBUG
219 /*
220 * If the generator has just been keyed, perform
221 * the statistical RNG test.
222 */
223 if (__predict_false(c->drbg.reseed_counter == 1)) {
224 rngtest_t *rt = kmem_alloc(sizeof(*rt), KM_NOSLEEP);
225
226 if (rt) {
227
228 strncpy(rt->rt_name, c->name, sizeof(rt->rt_name));
229
230 if (nist_ctr_drbg_generate(&c->drbg, rt->rt_b,
231 sizeof(rt->rt_b), NULL, 0)) {
232 panic("cprng %s: nist_ctr_drbg_generate "
233 "failed!", c->name);
234
235 }
236 testfail = rngtest(rt);
237
238 if (testfail) {
239 printf("cprng %s: failed statistical RNG "
240 "test.\n", c->name);
241 c->drbg.reseed_counter =
242 NIST_CTR_DRBG_RESEED_INTERVAL + 1;
243 len = 0;
244 }
245 memset(rt, 0, sizeof(*rt));
246 kmem_free(rt, sizeof(*rt));
247 }
248 }
249 #endif
250 if (__predict_false(c->drbg.reseed_counter >
251 (NIST_CTR_DRBG_RESEED_INTERVAL / 2))) {
252 cprng_strong_sched_reseed(c);
253 }
254
255 if (rnd_full) {
256 if (!c->rekeyed_on_full) {
257 c->rekeyed_on_full++;
258 cprng_strong_sched_reseed(c);
259 }
260 } else {
261 c->rekeyed_on_full = 0;
262 }
263
264 mutex_exit(&c->mtx);
265 return len;
266 }
267
268 void
269 cprng_strong_destroy(cprng_strong_t *c)
270 {
271 mutex_spin_enter(&c->reseed.mtx);
272 mutex_enter(&c->mtx);
273
274 if (c->flags & CPRNG_USE_CV) {
275 KASSERT(!cv_has_waiters(&c->cv));
276 cv_destroy(&c->cv);
277 }
278 seldestroy(&c->selq);
279
280 if (c->reseed_pending) {
281 rndsink_detach(&c->reseed);
282 }
283 mutex_spin_exit(&c->reseed.mtx);
284
285 nist_ctr_drbg_destroy(&c->drbg);
286
287 mutex_exit(&c->mtx);
288 mutex_destroy(&c->mtx);
289
290 memset(c, 0, sizeof(*c));
291 kmem_free(c, sizeof(*c));
292 }
293
294 int
295 cprng_strong_getflags(cprng_strong_t *const c)
296 {
297 KASSERT(mutex_owned(&c->mtx));
298 return c->flags;
299 }
300
301 void
302 cprng_strong_setflags(cprng_strong_t *const c, int flags)
303 {
304 KASSERT(mutex_owned(&c->mtx));
305 if (flags & CPRNG_USE_CV) {
306 if (!(c->flags & CPRNG_USE_CV)) {
307 cv_init(&c->cv, (const char *)c->name);
308 }
309 } else {
310 if (c->flags & CPRNG_USE_CV) {
311 KASSERT(!cv_has_waiters(&c->cv));
312 cv_destroy(&c->cv);
313 }
314 }
315 if (flags & CPRNG_REKEY_ANY) {
316 if (!(c->flags & CPRNG_REKEY_ANY)) {
317 if (c->flags & CPRNG_USE_CV) {
318 cv_broadcast(&c->cv);
319 }
320 selnotify(&c->selq, 0, 0);
321 }
322 }
323 c->flags = flags;
324 }
325