Mon Apr 13 14:41:06 2015 UTC ()
Move rndpool_(maybe_)extract to rndq, rename to rnd_(try)extract.

Make rnd_extract_data static.


(riastradh)
diff -r1.5 -r1.6 src/sys/dev/rnd_private.h
diff -r1.43 -r1.44 src/sys/kern/kern_rndq.c
diff -r1.10 -r1.11 src/sys/kern/kern_rndsink.c

cvs diff -r1.5 -r1.6 src/sys/dev/Attic/rnd_private.h (expand / switch to unified diff)

--- src/sys/dev/Attic/rnd_private.h 2015/04/13 14:30:05 1.5
+++ src/sys/dev/Attic/rnd_private.h 2015/04/13 14:41:06 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: rnd_private.h,v 1.5 2015/04/13 14:30:05 riastradh Exp $ */ 1/* $NetBSD: rnd_private.h,v 1.6 2015/04/13 14:41:06 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997 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 Michael Graff <explorer@flame.org>. This code uses ideas and 8 * by Michael Graff <explorer@flame.org>. This code uses ideas and
9 * algorithms from the Linux driver written by Ted Ts'o. 9 * algorithms from the Linux driver written by Ted Ts'o.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -37,15 +37,16 @@ @@ -37,15 +37,16 @@
37 * rnd.c and rndpool.c to decide when enough entropy exists to do a 37 * rnd.c and rndpool.c to decide when enough entropy exists to do a
38 * hash to extract it. 38 * hash to extract it.
39 */ 39 */
40#define RND_ENTROPY_THRESHOLD 10 40#define RND_ENTROPY_THRESHOLD 10
41 41
42/* 42/*
43 * Used by rnd_extract_data() and rndpool_extract_data() to describe how 43 * Used by rnd_extract_data() and rndpool_extract_data() to describe how
44 * "good" the data has to be. 44 * "good" the data has to be.
45 */ 45 */
46#define RND_EXTRACT_ANY 0 /* extract anything, even if no entropy */ 46#define RND_EXTRACT_ANY 0 /* extract anything, even if no entropy */
47#define RND_EXTRACT_GOOD 1 /* return as many good bytes 47#define RND_EXTRACT_GOOD 1 /* return as many good bytes
48 (short read ok) */ 48 (short read ok) */
49 49
50uint32_t rnd_extract_data(void *, uint32_t, uint32_t); 50bool rnd_extract(void *, size_t);
 51bool rnd_tryextract(void *, size_t);
51#endif 52#endif

cvs diff -r1.43 -r1.44 src/sys/kern/Attic/kern_rndq.c (expand / switch to unified diff)

--- src/sys/kern/Attic/kern_rndq.c 2015/04/08 14:13:55 1.43
+++ src/sys/kern/Attic/kern_rndq.c 2015/04/13 14:41:06 1.44
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_rndq.c,v 1.43 2015/04/08 14:13:55 riastradh Exp $ */ 1/* $NetBSD: kern_rndq.c,v 1.44 2015/04/13 14:41:06 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997-2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997-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 Michael Graff <explorer@flame.org> and Thor Lancelot Simon. 8 * by Michael Graff <explorer@flame.org> and Thor Lancelot Simon.
9 * This code uses ideas and algorithms from the Linux driver written by 9 * This code uses ideas and algorithms from the Linux driver written by
10 * Ted Ts'o. 10 * Ted Ts'o.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE. 31 * POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.43 2015/04/08 14:13:55 riastradh Exp $"); 35__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.44 2015/04/13 14:41:06 riastradh Exp $");
36 36
37#include <sys/param.h> 37#include <sys/param.h>
38#include <sys/atomic.h> 38#include <sys/atomic.h>
39#include <sys/ioctl.h> 39#include <sys/ioctl.h>
40#include <sys/fcntl.h> 40#include <sys/fcntl.h>
41#include <sys/select.h> 41#include <sys/select.h>
42#include <sys/poll.h> 42#include <sys/poll.h>
43#include <sys/kmem.h> 43#include <sys/kmem.h>
44#include <sys/mutex.h> 44#include <sys/mutex.h>
45#include <sys/proc.h> 45#include <sys/proc.h>
46#include <sys/kernel.h> 46#include <sys/kernel.h>
47#include <sys/conf.h> 47#include <sys/conf.h>
48#include <sys/systm.h> 48#include <sys/systm.h>
@@ -1138,27 +1138,27 @@ skip: SIMPLEQ_INSERT_TAIL(&df_samples,  @@ -1138,27 +1138,27 @@ skip: SIMPLEQ_INSERT_TAIL(&df_samples,
1138 1138
1139static void 1139static void
1140rnd_intr(void *arg) 1140rnd_intr(void *arg)
1141{ 1141{
1142 rnd_process_events(); 1142 rnd_process_events();
1143} 1143}
1144 1144
1145static void 1145static void
1146rnd_wake(void *arg) 1146rnd_wake(void *arg)
1147{ 1147{
1148 rnd_wakeup_readers(); 1148 rnd_wakeup_readers();
1149} 1149}
1150 1150
1151u_int32_t 1151static uint32_t
1152rnd_extract_data(void *p, u_int32_t len, u_int32_t flags) 1152rnd_extract_data(void *p, u_int32_t len, u_int32_t flags)
1153{ 1153{
1154 static int timed_in; 1154 static int timed_in;
1155 int entropy_count; 1155 int entropy_count;
1156 uint32_t retval; 1156 uint32_t retval;
1157 1157
1158 mutex_spin_enter(&rndpool_mtx); 1158 mutex_spin_enter(&rndpool_mtx);
1159 if (__predict_false(!timed_in)) { 1159 if (__predict_false(!timed_in)) {
1160 if (boottime.tv_sec) { 1160 if (boottime.tv_sec) {
1161 rndpool_add_data(&rnd_pool, &boottime, 1161 rndpool_add_data(&rnd_pool, &boottime,
1162 sizeof(boottime), 0); 1162 sizeof(boottime), 0);
1163 } 1163 }
1164 timed_in++; 1164 timed_in++;
@@ -1216,26 +1216,87 @@ rnd_extract_data(void *p, u_int32_t len, @@ -1216,26 +1216,87 @@ rnd_extract_data(void *p, u_int32_t len,
1216#endif 1216#endif
1217 entropy_count = rndpool_get_entropy_count(&rnd_pool); 1217 entropy_count = rndpool_get_entropy_count(&rnd_pool);
1218 if (entropy_count < (RND_ENTROPY_THRESHOLD * 2 + len) * NBBY) { 1218 if (entropy_count < (RND_ENTROPY_THRESHOLD * 2 + len) * NBBY) {
1219 rnd_printf_verbose("rnd: empty, asking for %d bytes\n", 1219 rnd_printf_verbose("rnd: empty, asking for %d bytes\n",
1220 (int)(howmany((RND_POOLBITS - entropy_count), NBBY))); 1220 (int)(howmany((RND_POOLBITS - entropy_count), NBBY)));
1221 rnd_getmore(howmany((RND_POOLBITS - entropy_count), NBBY)); 1221 rnd_getmore(howmany((RND_POOLBITS - entropy_count), NBBY));
1222 } 1222 }
1223 retval = rndpool_extract_data(&rnd_pool, p, len, flags); 1223 retval = rndpool_extract_data(&rnd_pool, p, len, flags);
1224 mutex_spin_exit(&rndpool_mtx); 1224 mutex_spin_exit(&rndpool_mtx);
1225 1225
1226 return retval; 1226 return retval;
1227} 1227}
1228 1228
 1229/*
 1230 * Fill the buffer with as much entropy as we can. Return true if it
 1231 * has full entropy and false if not.
 1232 */
 1233bool
 1234rnd_extract(void *buffer, size_t bytes)
 1235{
 1236 const size_t extracted = rnd_extract_data(buffer, bytes,
 1237 RND_EXTRACT_GOOD);
 1238
 1239 if (extracted < bytes) {
 1240 (void)rnd_extract_data((uint8_t *)buffer + extracted,
 1241 bytes - extracted, RND_EXTRACT_ANY);
 1242 mutex_spin_enter(&rndpool_mtx);
 1243 rnd_getmore(bytes - extracted);
 1244 mutex_spin_exit(&rndpool_mtx);
 1245 return false;
 1246 }
 1247
 1248 return true;
 1249}
 1250
 1251/*
 1252 * If we have as much entropy as is requested, fill the buffer with it
 1253 * and return true. Otherwise, leave the buffer alone and return
 1254 * false.
 1255 */
 1256
 1257CTASSERT(RND_ENTROPY_THRESHOLD <= 0xffffffffUL);
 1258CTASSERT(RNDSINK_MAX_BYTES <= (0xffffffffUL - RND_ENTROPY_THRESHOLD));
 1259CTASSERT((RNDSINK_MAX_BYTES + RND_ENTROPY_THRESHOLD) <=
 1260 (0xffffffffUL / NBBY));
 1261
 1262bool
 1263rnd_tryextract(void *buffer, size_t bytes)
 1264{
 1265 bool ok;
 1266
 1267 KASSERT(bytes <= RNDSINK_MAX_BYTES);
 1268
 1269 const uint32_t bits_needed = ((bytes + RND_ENTROPY_THRESHOLD) * NBBY);
 1270
 1271 mutex_spin_enter(&rndpool_mtx);
 1272 if (bits_needed <= rndpool_get_entropy_count(&rnd_pool)) {
 1273 const uint32_t extracted __diagused =
 1274 rndpool_extract_data(&rnd_pool, buffer, bytes,
 1275 RND_EXTRACT_GOOD);
 1276
 1277 KASSERT(extracted == bytes);
 1278
 1279 ok = true;
 1280 } else {
 1281 ok = false;
 1282 rnd_getmore(howmany(bits_needed -
 1283 rndpool_get_entropy_count(&rnd_pool), NBBY));
 1284 }
 1285 mutex_spin_exit(&rndpool_mtx);
 1286
 1287 return ok;
 1288}
 1289
1229void 1290void
1230rnd_seed(void *base, size_t len) 1291rnd_seed(void *base, size_t len)
1231{ 1292{
1232 SHA1_CTX s; 1293 SHA1_CTX s;
1233 uint8_t digest[SHA1_DIGEST_LENGTH]; 1294 uint8_t digest[SHA1_DIGEST_LENGTH];
1234 1295
1235 if (len != sizeof(*boot_rsp)) { 1296 if (len != sizeof(*boot_rsp)) {
1236 rnd_printf("rnd: bad seed length %d\n", (int)len); 1297 rnd_printf("rnd: bad seed length %d\n", (int)len);
1237 return; 1298 return;
1238 } 1299 }
1239 1300
1240 boot_rsp = (rndsave_t *)base; 1301 boot_rsp = (rndsave_t *)base;
1241 SHA1Init(&s); 1302 SHA1Init(&s);

cvs diff -r1.10 -r1.11 src/sys/kern/Attic/kern_rndsink.c (expand / switch to unified diff)

--- src/sys/kern/Attic/kern_rndsink.c 2014/10/26 18:22:32 1.10
+++ src/sys/kern/Attic/kern_rndsink.c 2015/04/13 14:41:06 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_rndsink.c,v 1.10 2014/10/26 18:22:32 tls Exp $ */ 1/* $NetBSD: kern_rndsink.c,v 1.11 2015/04/13 14:41:06 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 4 * Copyright (c) 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 Taylor R. Campbell. 8 * by 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: kern_rndsink.c,v 1.10 2014/10/26 18:22:32 tls Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: kern_rndsink.c,v 1.11 2015/04/13 14:41:06 riastradh 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/kmem.h> 38#include <sys/kmem.h>
39#include <sys/mutex.h> 39#include <sys/mutex.h>
40#include <sys/queue.h> 40#include <sys/queue.h>
41#include <sys/rnd.h> 41#include <sys/rnd.h>
42#include <sys/rndsink.h> 42#include <sys/rndsink.h>
43 43
44#include <dev/rnd_private.h> /* XXX provisional, for rnd_extract_data */ 44#include <dev/rnd_private.h> /* XXX provisional, for rnd_extract_data */
45 45
46enum rsink_state { 46enum rsink_state {
@@ -76,108 +76,40 @@ static TAILQ_HEAD(, rndsink) rndsinks =  @@ -76,108 +76,40 @@ static TAILQ_HEAD(, rndsink) rndsinks =
76void 76void
77rndsinks_init(void) 77rndsinks_init(void)
78{ 78{
79 79
80 /* 80 /*
81 * This mutex must be at an ipl as high as the highest ipl of 81 * This mutex must be at an ipl as high as the highest ipl of
82 * anyone who wants to call rndsink_request. 82 * anyone who wants to call rndsink_request.
83 * 83 *
84 * XXX Call this IPL_RND, perhaps. 84 * XXX Call this IPL_RND, perhaps.
85 */ 85 */
86 mutex_init(&rndsinks_lock, MUTEX_DEFAULT, IPL_VM); 86 mutex_init(&rndsinks_lock, MUTEX_DEFAULT, IPL_VM);
87} 87}
88 88
89/* 
90 * XXX Provisional -- rndpool_extract and rndpool_maybe_extract should 
91 * move into kern_rndpool.c. 
92 */ 
93extern rndpool_t rnd_pool; 
94extern kmutex_t rndpool_mtx; 
95 
96/* 
97 * Fill the buffer with as much entropy as we can. Return true if it 
98 * has full entropy and false if not. 
99 */ 
100static bool 
101rndpool_extract(void *buffer, size_t bytes) 
102{ 
103 const size_t extracted = rnd_extract_data(buffer, bytes, 
104 RND_EXTRACT_GOOD); 
105 
106 if (extracted < bytes) { 
107 (void)rnd_extract_data((uint8_t *)buffer + extracted, 
108 bytes - extracted, RND_EXTRACT_ANY); 
109 mutex_spin_enter(&rndpool_mtx); 
110 rnd_getmore(bytes - extracted); 
111 mutex_spin_exit(&rndpool_mtx); 
112 return false; 
113 } 
114 
115 return true; 
116} 
117 
118/* 
119 * If we have as much entropy as is requested, fill the buffer with it 
120 * and return true. Otherwise, leave the buffer alone and return 
121 * false. 
122 */ 
123 
124CTASSERT(RND_ENTROPY_THRESHOLD <= 0xffffffffUL); 
125CTASSERT(RNDSINK_MAX_BYTES <= (0xffffffffUL - RND_ENTROPY_THRESHOLD)); 
126CTASSERT((RNDSINK_MAX_BYTES + RND_ENTROPY_THRESHOLD) <= 
127 (0xffffffffUL / NBBY)); 
128 
129static bool 
130rndpool_maybe_extract(void *buffer, size_t bytes) 
131{ 
132 bool ok; 
133 
134 KASSERT(bytes <= RNDSINK_MAX_BYTES); 
135 
136 const uint32_t bits_needed = ((bytes + RND_ENTROPY_THRESHOLD) * NBBY); 
137 
138 mutex_spin_enter(&rndpool_mtx); 
139 if (bits_needed <= rndpool_get_entropy_count(&rnd_pool)) { 
140 const uint32_t extracted __diagused = 
141 rndpool_extract_data(&rnd_pool, buffer, bytes, 
142 RND_EXTRACT_GOOD); 
143 
144 KASSERT(extracted == bytes); 
145 
146 ok = true; 
147 } else { 
148 ok = false; 
149 rnd_getmore(howmany(bits_needed - 
150 rndpool_get_entropy_count(&rnd_pool), NBBY)); 
151 } 
152 mutex_spin_exit(&rndpool_mtx); 
153 
154 return ok; 
155} 
156 
157void 89void
158rndsinks_distribute(void) 90rndsinks_distribute(void)
159{ 91{
160 uint8_t buffer[RNDSINK_MAX_BYTES]; 92 uint8_t buffer[RNDSINK_MAX_BYTES];
161 struct rndsink *rndsink; 93 struct rndsink *rndsink;
162 94
163 explicit_memset(buffer, 0, sizeof(buffer)); /* paranoia */ 95 explicit_memset(buffer, 0, sizeof(buffer)); /* paranoia */
164 96
165 mutex_spin_enter(&rndsinks_lock); 97 mutex_spin_enter(&rndsinks_lock);
166 while ((rndsink = TAILQ_FIRST(&rndsinks)) != NULL) { 98 while ((rndsink = TAILQ_FIRST(&rndsinks)) != NULL) {
167 KASSERT(rndsink->rsink_state == RNDSINK_QUEUED); 99 KASSERT(rndsink->rsink_state == RNDSINK_QUEUED);
168 100
169 /* Bail if we can't get some entropy for this rndsink. */ 101 /* Bail if we can't get some entropy for this rndsink. */
170 if (!rndpool_maybe_extract(buffer, rndsink->rsink_bytes)) 102 if (!rnd_tryextract(buffer, rndsink->rsink_bytes))
171 break; 103 break;
172 104
173 /* 105 /*
174 * Got some entropy. Take the sink off the queue and 106 * Got some entropy. Take the sink off the queue and
175 * feed the entropy to the callback, with rndsinks_lock 107 * feed the entropy to the callback, with rndsinks_lock
176 * dropped. While running the callback, lock out 108 * dropped. While running the callback, lock out
177 * rndsink_destroy by marking the sink in flight. 109 * rndsink_destroy by marking the sink in flight.
178 */ 110 */
179 TAILQ_REMOVE(&rndsinks, rndsink, rsink_entry); 111 TAILQ_REMOVE(&rndsinks, rndsink, rsink_entry);
180 rndsink->rsink_state = RNDSINK_IN_FLIGHT; 112 rndsink->rsink_state = RNDSINK_IN_FLIGHT;
181 mutex_spin_exit(&rndsinks_lock); 113 mutex_spin_exit(&rndsinks_lock);
182 114
183 (*rndsink->rsink_callback)(rndsink->rsink_arg, buffer, 115 (*rndsink->rsink_callback)(rndsink->rsink_arg, buffer,
@@ -215,29 +147,32 @@ rndsinks_enqueue(struct rndsink *rndsink @@ -215,29 +147,32 @@ rndsinks_enqueue(struct rndsink *rndsink
215 /* 147 /*
216 * XXX This should request only rndsink->rs_bytes bytes of 148 * XXX This should request only rndsink->rs_bytes bytes of
217 * entropy, but that might get buffered up indefinitely because 149 * entropy, but that might get buffered up indefinitely because
218 * kern_rndq has no bound on the duration before it will 150 * kern_rndq has no bound on the duration before it will
219 * process queued entropy samples. To work around this, we are 151 * process queued entropy samples. To work around this, we are
220 * a little too incestuous with kern_rndq: we avoid marking polled 152 * a little too incestuous with kern_rndq: we avoid marking polled
221 * sources "fast" there, and know here that for non-fast sources, 153 * sources "fast" there, and know here that for non-fast sources,
222 * that code will buffer two ints worth of data per source. 154 * that code will buffer two ints worth of data per source.
223 * Later, we ought to (a) bound the duration before 155 * Later, we ought to (a) bound the duration before
224 * queued entropy samples get processed, and (b) add a target 156 * queued entropy samples get processed, and (b) add a target
225 * or something -- as soon as we get that much from the entropy 157 * or something -- as soon as we get that much from the entropy
226 * sources, distribute it. 158 * sources, distribute it.
227 */ 159 */
 160 {
 161 extern kmutex_t rndpool_mtx;
228 mutex_spin_enter(&rndpool_mtx); 162 mutex_spin_enter(&rndpool_mtx);
229 rnd_getmore(MAX(rndsink->rsink_bytes, 2 * sizeof(uint32_t))); 163 rnd_getmore(MAX(rndsink->rsink_bytes, 2 * sizeof(uint32_t)));
230 mutex_spin_exit(&rndpool_mtx); 164 mutex_spin_exit(&rndpool_mtx);
 165 }
231 166
232 switch (rndsink->rsink_state) { 167 switch (rndsink->rsink_state) {
233 case RNDSINK_IDLE: 168 case RNDSINK_IDLE:
234 /* Not on the queue and nobody is handling it. */ 169 /* Not on the queue and nobody is handling it. */
235 TAILQ_INSERT_TAIL(&rndsinks, rndsink, rsink_entry); 170 TAILQ_INSERT_TAIL(&rndsinks, rndsink, rsink_entry);
236 rndsink->rsink_state = RNDSINK_QUEUED; 171 rndsink->rsink_state = RNDSINK_QUEUED;
237 break; 172 break;
238 173
239 case RNDSINK_QUEUED: 174 case RNDSINK_QUEUED:
240 /* Already on the queue. */ 175 /* Already on the queue. */
241 break; 176 break;
242 177
243 case RNDSINK_IN_FLIGHT: 178 case RNDSINK_IN_FLIGHT:
@@ -321,20 +256,20 @@ rndsink_schedule(struct rndsink *rndsink @@ -321,20 +256,20 @@ rndsink_schedule(struct rndsink *rndsink
321 mutex_spin_enter(&rndsinks_lock); 256 mutex_spin_enter(&rndsinks_lock);
322 rndsinks_enqueue(rndsink); 257 rndsinks_enqueue(rndsink);
323 mutex_spin_exit(&rndsinks_lock); 258 mutex_spin_exit(&rndsinks_lock);
324 } 259 }
325} 260}
326 261
327bool 262bool
328rndsink_request(struct rndsink *rndsink, void *buffer, size_t bytes) 263rndsink_request(struct rndsink *rndsink, void *buffer, size_t bytes)
329{ 264{
330 265
331 KASSERT(bytes == rndsink->rsink_bytes); 266 KASSERT(bytes == rndsink->rsink_bytes);
332 267
333 mutex_spin_enter(&rndsinks_lock); 268 mutex_spin_enter(&rndsinks_lock);
334 const bool full_entropy = rndpool_extract(buffer, bytes); 269 const bool full_entropy = rnd_extract(buffer, bytes);
335 if (!full_entropy) 270 if (!full_entropy)
336 rndsinks_enqueue(rndsink); 271 rndsinks_enqueue(rndsink);
337 mutex_spin_exit(&rndsinks_lock); 272 mutex_spin_exit(&rndsinks_lock);
338 273
339 return full_entropy; 274 return full_entropy;
340} 275}