| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: subr_cprng.c,v 1.34 2019/12/04 05:36:34 riastradh Exp $ */ | | 1 | /* $NetBSD: subr_cprng.c,v 1.35 2020/04/12 07:16:09 maxv Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011-2013 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2011-2013 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Thor Lancelot Simon and Taylor R. Campbell. | | 8 | * by Thor Lancelot Simon and Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -20,27 +20,27 @@ | | | @@ -20,27 +20,27 @@ |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 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 | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 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 | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 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 | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #include <sys/cdefs.h> | | 32 | #include <sys/cdefs.h> |
33 | __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.34 2019/12/04 05:36:34 riastradh Exp $"); | | 33 | __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.35 2020/04/12 07:16:09 maxv Exp $"); |
34 | | | 34 | |
35 | #include <sys/param.h> | | 35 | #include <sys/param.h> |
36 | #include <sys/types.h> | | 36 | #include <sys/types.h> |
37 | #include <sys/condvar.h> | | 37 | #include <sys/condvar.h> |
38 | #include <sys/cprng.h> | | 38 | #include <sys/cprng.h> |
39 | #include <sys/errno.h> | | 39 | #include <sys/errno.h> |
40 | #include <sys/event.h> /* XXX struct knote */ | | 40 | #include <sys/event.h> /* XXX struct knote */ |
41 | #include <sys/fcntl.h> /* XXX FNONBLOCK */ | | 41 | #include <sys/fcntl.h> /* XXX FNONBLOCK */ |
42 | #include <sys/kernel.h> | | 42 | #include <sys/kernel.h> |
43 | #include <sys/kmem.h> | | 43 | #include <sys/kmem.h> |
44 | #include <sys/lwp.h> | | 44 | #include <sys/lwp.h> |
45 | #include <sys/once.h> | | 45 | #include <sys/once.h> |
46 | #include <sys/percpu.h> | | 46 | #include <sys/percpu.h> |
| @@ -241,26 +241,42 @@ cprng_strong(struct cprng_strong *cprng, | | | @@ -241,26 +241,42 @@ cprng_strong(struct cprng_strong *cprng, |
241 | KASSERT(bytes <= NIST_HASH_DRBG_MIN_SEEDLEN_BYTES); | | 241 | KASSERT(bytes <= NIST_HASH_DRBG_MIN_SEEDLEN_BYTES); |
242 | KASSERT(0 < cprng->cs_remaining); | | 242 | KASSERT(0 < cprng->cs_remaining); |
243 | KASSERT(cprng->cs_remaining <= | | 243 | KASSERT(cprng->cs_remaining <= |
244 | NIST_HASH_DRBG_MIN_SEEDLEN_BYTES); | | 244 | NIST_HASH_DRBG_MIN_SEEDLEN_BYTES); |
245 | } | | 245 | } |
246 | | | 246 | |
247 | cprng_strong_generate(cprng, buffer, bytes); | | 247 | cprng_strong_generate(cprng, buffer, bytes); |
248 | result = bytes; | | 248 | result = bytes; |
249 | | | 249 | |
250 | out: mutex_exit(&cprng->cs_lock); | | 250 | out: mutex_exit(&cprng->cs_lock); |
251 | return result; | | 251 | return result; |
252 | } | | 252 | } |
253 | | | 253 | |
| | | 254 | uint32_t |
| | | 255 | cprng_strong32(void) |
| | | 256 | { |
| | | 257 | uint32_t r; |
| | | 258 | cprng_strong(kern_cprng, &r, sizeof(r), 0); |
| | | 259 | return r; |
| | | 260 | } |
| | | 261 | |
| | | 262 | uint64_t |
| | | 263 | cprng_strong64(void) |
| | | 264 | { |
| | | 265 | uint64_t r; |
| | | 266 | cprng_strong(kern_cprng, &r, sizeof(r), 0); |
| | | 267 | return r; |
| | | 268 | } |
| | | 269 | |
254 | static void | | 270 | static void |
255 | filt_cprng_detach(struct knote *kn) | | 271 | filt_cprng_detach(struct knote *kn) |
256 | { | | 272 | { |
257 | struct cprng_strong *const cprng = kn->kn_hook; | | 273 | struct cprng_strong *const cprng = kn->kn_hook; |
258 | | | 274 | |
259 | mutex_enter(&cprng->cs_lock); | | 275 | mutex_enter(&cprng->cs_lock); |
260 | SLIST_REMOVE(&cprng->cs_selq.sel_klist, kn, knote, kn_selnext); | | 276 | SLIST_REMOVE(&cprng->cs_selq.sel_klist, kn, knote, kn_selnext); |
261 | mutex_exit(&cprng->cs_lock); | | 277 | mutex_exit(&cprng->cs_lock); |
262 | } | | 278 | } |
263 | | | 279 | |
264 | static int | | 280 | static int |
265 | filt_cprng_read_event(struct knote *kn, long hint) | | 281 | filt_cprng_read_event(struct knote *kn, long hint) |
266 | { | | 282 | { |