1 /*-
2  * Copyright (c) 2017 Oliver Pinter
3  * Copyright (c) 2017 W. Dean Freeman
4  * Copyright (c) 2000-2015 Mark R V Murray
5  * Copyright (c) 2013 Arthur Mesh
6  * Copyright (c) 2004 Robert N. M. Watson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/eventhandler.h>
39 #include <sys/hash.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/linker.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/random.h>
48 #include <sys/sbuf.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 
52 #include <machine/atomic.h>
53 #include <machine/cpu.h>
54 
55 #include <crypto/rijndael/rijndael-api-fst.h>
56 #include <crypto/sha2/sha256.h>
57 
58 #include <dev/random/hash.h>
59 #include <dev/random/randomdev.h>
60 #include <dev/random/random_harvestq.h>
61 
62 #if defined(RANDOM_ENABLE_ETHER)
63 #define _RANDOM_HARVEST_ETHER_OFF 0
64 #else
65 #define _RANDOM_HARVEST_ETHER_OFF (1u << RANDOM_NET_ETHER)
66 #endif
67 #if defined(RANDOM_ENABLE_UMA)
68 #define _RANDOM_HARVEST_UMA_OFF 0
69 #else
70 #define _RANDOM_HARVEST_UMA_OFF (1u << RANDOM_UMA)
71 #endif
72 
73 static void random_kthread(void);
74 static void random_sources_feed(void);
75 
76 static u_int read_rate;
77 
78 /*
79  * How many events to queue up. We create this many items in
80  * an 'empty' queue, then transfer them to the 'harvest' queue with
81  * supplied junk. When used, they are transferred back to the
82  * 'empty' queue.
83  */
84 #define	RANDOM_RING_MAX		1024
85 #define	RANDOM_ACCUM_MAX	8
86 
87 /* 1 to let the kernel thread run, 0 to terminate, -1 to mark completion */
88 volatile int random_kthread_control;
89 
90 
91 /* Allow the sysadmin to select the broad category of
92  * entropy types to harvest.
93  */
94 __read_frequently u_int hc_source_mask;
95 
96 struct random_sources {
97 	LIST_ENTRY(random_sources)	 rrs_entries;
98 	struct random_source		*rrs_source;
99 };
100 
101 static LIST_HEAD(sources_head, random_sources) source_list =
102     LIST_HEAD_INITIALIZER(source_list);
103 
104 SYSCTL_NODE(_kern_random, OID_AUTO, harvest, CTLFLAG_RW, 0,
105     "Entropy Device Parameters");
106 
107 /*
108  * Put all the harvest queue context stuff in one place.
109  * this make is a bit easier to lock and protect.
110  */
111 static struct harvest_context {
112 	/* The harvest mutex protects all of harvest_context and
113 	 * the related data.
114 	 */
115 	struct mtx hc_mtx;
116 	/* Round-robin destination cache. */
117 	u_int hc_destination[ENTROPYSOURCE];
118 	/* The context of the kernel thread processing harvested entropy */
119 	struct proc *hc_kthread_proc;
120 	/*
121 	 * Lockless ring buffer holding entropy events
122 	 * If ring.in == ring.out,
123 	 *     the buffer is empty.
124 	 * If ring.in != ring.out,
125 	 *     the buffer contains harvested entropy.
126 	 * If (ring.in + 1) == ring.out (mod RANDOM_RING_MAX),
127 	 *     the buffer is full.
128 	 *
129 	 * NOTE: ring.in points to the last added element,
130 	 * and ring.out points to the last consumed element.
131 	 *
132 	 * The ring.in variable needs locking as there are multiple
133 	 * sources to the ring. Only the sources may change ring.in,
134 	 * but the consumer may examine it.
135 	 *
136 	 * The ring.out variable does not need locking as there is
137 	 * only one consumer. Only the consumer may change ring.out,
138 	 * but the sources may examine it.
139 	 */
140 	struct entropy_ring {
141 		struct harvest_event ring[RANDOM_RING_MAX];
142 		volatile u_int in;
143 		volatile u_int out;
144 	} hc_entropy_ring;
145 	struct fast_entropy_accumulator {
146 		volatile u_int pos;
147 		uint32_t buf[RANDOM_ACCUM_MAX];
148 	} hc_entropy_fast_accumulator;
149 } harvest_context;
150 
151 static struct kproc_desc random_proc_kp = {
152 	"rand_harvestq",
153 	random_kthread,
154 	&harvest_context.hc_kthread_proc,
155 };
156 
157 /* Pass the given event straight through to Fortuna/Whatever. */
158 static __inline void
159 random_harvestq_fast_process_event(struct harvest_event *event)
160 {
161 	p_random_alg_context->ra_event_processor(event);
162 	explicit_bzero(event, sizeof(*event));
163 }
164 
165 static void
166 random_kthread(void)
167 {
168         u_int maxloop, ring_out, i;
169 
170 	/*
171 	 * Locking is not needed as this is the only place we modify ring.out, and
172 	 * we only examine ring.in without changing it. Both of these are volatile,
173 	 * and this is a unique thread.
174 	 */
175 	for (random_kthread_control = 1; random_kthread_control;) {
176 		/* Deal with events, if any. Restrict the number we do in one go. */
177 		maxloop = RANDOM_RING_MAX;
178 		while (harvest_context.hc_entropy_ring.out != harvest_context.hc_entropy_ring.in) {
179 			ring_out = (harvest_context.hc_entropy_ring.out + 1)%RANDOM_RING_MAX;
180 			random_harvestq_fast_process_event(harvest_context.hc_entropy_ring.ring + ring_out);
181 			harvest_context.hc_entropy_ring.out = ring_out;
182 			if (!--maxloop)
183 				break;
184 		}
185 		random_sources_feed();
186 		/* XXX: FIX!! Increase the high-performance data rate? Need some measurements first. */
187 		for (i = 0; i < RANDOM_ACCUM_MAX; i++) {
188 			if (harvest_context.hc_entropy_fast_accumulator.buf[i]) {
189 				random_harvest_direct(harvest_context.hc_entropy_fast_accumulator.buf + i, sizeof(harvest_context.hc_entropy_fast_accumulator.buf[0]), RANDOM_UMA);
190 				harvest_context.hc_entropy_fast_accumulator.buf[i] = 0;
191 			}
192 		}
193 		/* XXX: FIX!! This is a *great* place to pass hardware/live entropy to random(9) */
194 		tsleep_sbt(&harvest_context.hc_kthread_proc, 0, "-", SBT_1S/10, 0, C_PREL(1));
195 	}
196 	random_kthread_control = -1;
197 	wakeup(&harvest_context.hc_kthread_proc);
198 	kproc_exit(0);
199 	/* NOTREACHED */
200 }
201 /* This happens well after SI_SUB_RANDOM */
202 SYSINIT(random_device_h_proc, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, kproc_start,
203     &random_proc_kp);
204 
205 /*
206  * Run through all fast sources reading entropy for the given
207  * number of rounds, which should be a multiple of the number
208  * of entropy accumulation pools in use; it is 32 for Fortuna.
209  */
210 static void
211 random_sources_feed(void)
212 {
213 	uint32_t entropy[HARVESTSIZE];
214 	struct random_sources *rrs;
215 	u_int i, n, local_read_rate;
216 
217 	/*
218 	 * Step over all of live entropy sources, and feed their output
219 	 * to the system-wide RNG.
220 	 */
221 	local_read_rate = atomic_readandclear_32(&read_rate);
222 	/* Perform at least one read per round */
223 	local_read_rate = MAX(local_read_rate, 1);
224 	/* But not exceeding RANDOM_KEYSIZE_WORDS */
225 	local_read_rate = MIN(local_read_rate, RANDOM_KEYSIZE_WORDS);
226 	LIST_FOREACH(rrs, &source_list, rrs_entries) {
227 		for (i = 0; i < p_random_alg_context->ra_poolcount*local_read_rate; i++) {
228 			n = rrs->rrs_source->rs_read(entropy, sizeof(entropy));
229 			KASSERT((n <= sizeof(entropy)), ("%s: rs_read returned too much data (%u > %zu)", __func__, n, sizeof(entropy)));
230 			/*
231 			 * Sometimes the HW entropy source doesn't have anything
232 			 * ready for us.  This isn't necessarily untrustworthy.
233 			 * We don't perform any other verification of an entropy
234 			 * source (i.e., length is allowed to be anywhere from 1
235 			 * to sizeof(entropy), quality is unchecked, etc), so
236 			 * don't balk verbosely at slow random sources either.
237 			 * There are reports that RDSEED on x86 metal falls
238 			 * behind the rate at which we query it, for example.
239 			 * But it's still a better entropy source than RDRAND.
240 			 */
241 			if (n == 0)
242 				continue;
243 			random_harvest_direct(entropy, n, rrs->rrs_source->rs_source);
244 		}
245 	}
246 	explicit_bzero(entropy, sizeof(entropy));
247 }
248 
249 void
250 read_rate_increment(u_int chunk)
251 {
252 
253 	atomic_add_32(&read_rate, chunk);
254 }
255 
256 /* ARGSUSED */
257 static int
258 random_check_uint_harvestmask(SYSCTL_HANDLER_ARGS)
259 {
260 	static const u_int user_immutable_mask =
261 	    (((1 << ENTROPYSOURCE) - 1) & (-1UL << RANDOM_PURE_START)) |
262 	    _RANDOM_HARVEST_ETHER_OFF | _RANDOM_HARVEST_UMA_OFF;
263 
264 	int error;
265 	u_int value, orig_value;
266 
267 	orig_value = value = hc_source_mask;
268 	error = sysctl_handle_int(oidp, &value, 0, req);
269 	if (error != 0 || req->newptr == NULL)
270 		return (error);
271 
272 	if (flsl(value) > ENTROPYSOURCE)
273 		return (EINVAL);
274 
275 	/*
276 	 * Disallow userspace modification of pure entropy sources.
277 	 */
278 	hc_source_mask = (value & ~user_immutable_mask) |
279 	    (orig_value & user_immutable_mask);
280 	return (0);
281 }
282 SYSCTL_PROC(_kern_random_harvest, OID_AUTO, mask, CTLTYPE_UINT | CTLFLAG_RW,
283     NULL, 0, random_check_uint_harvestmask, "IU", "Entropy harvesting mask");
284 
285 /* ARGSUSED */
286 static int
287 random_print_harvestmask(SYSCTL_HANDLER_ARGS)
288 {
289 	struct sbuf sbuf;
290 	int error, i;
291 
292 	error = sysctl_wire_old_buffer(req, 0);
293 	if (error == 0) {
294 		sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
295 		for (i = ENTROPYSOURCE - 1; i >= 0; i--)
296 			sbuf_cat(&sbuf, (hc_source_mask & (1 << i)) ? "1" : "0");
297 		error = sbuf_finish(&sbuf);
298 		sbuf_delete(&sbuf);
299 	}
300 	return (error);
301 }
302 SYSCTL_PROC(_kern_random_harvest, OID_AUTO, mask_bin,
303     CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, random_print_harvestmask, "A",
304     "Entropy harvesting mask (printable)");
305 
306 static const char *random_source_descr[ENTROPYSOURCE] = {
307 	[RANDOM_CACHED] = "CACHED",
308 	[RANDOM_ATTACH] = "ATTACH",
309 	[RANDOM_KEYBOARD] = "KEYBOARD",
310 	[RANDOM_MOUSE] = "MOUSE",
311 	[RANDOM_NET_TUN] = "NET_TUN",
312 	[RANDOM_NET_ETHER] = "NET_ETHER",
313 	[RANDOM_NET_NG] = "NET_NG",
314 	[RANDOM_INTERRUPT] = "INTERRUPT",
315 	[RANDOM_SWI] = "SWI",
316 	[RANDOM_FS_ATIME] = "FS_ATIME",
317 	[RANDOM_UMA] = "UMA", /* ENVIRONMENTAL_END */
318 	[RANDOM_PURE_OCTEON] = "PURE_OCTEON", /* PURE_START */
319 	[RANDOM_PURE_SAFE] = "PURE_SAFE",
320 	[RANDOM_PURE_GLXSB] = "PURE_GLXSB",
321 	[RANDOM_PURE_UBSEC] = "PURE_UBSEC",
322 	[RANDOM_PURE_HIFN] = "PURE_HIFN",
323 	[RANDOM_PURE_RDRAND] = "PURE_RDRAND",
324 	[RANDOM_PURE_NEHEMIAH] = "PURE_NEHEMIAH",
325 	[RANDOM_PURE_RNDTEST] = "PURE_RNDTEST",
326 	[RANDOM_PURE_VIRTIO] = "PURE_VIRTIO",
327 	[RANDOM_PURE_BROADCOM] = "PURE_BROADCOM",
328 	[RANDOM_PURE_CCP] = "PURE_CCP",
329 	[RANDOM_PURE_DARN] = "PURE_DARN",
330 	[RANDOM_PURE_TPM] = "PURE_TPM",
331 	/* "ENTROPYSOURCE" */
332 };
333 
334 /* ARGSUSED */
335 static int
336 random_print_harvestmask_symbolic(SYSCTL_HANDLER_ARGS)
337 {
338 	struct sbuf sbuf;
339 	int error, i;
340 	bool first;
341 
342 	first = true;
343 	error = sysctl_wire_old_buffer(req, 0);
344 	if (error == 0) {
345 		sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
346 		for (i = ENTROPYSOURCE - 1; i >= 0; i--) {
347 			if (i >= RANDOM_PURE_START &&
348 			    (hc_source_mask & (1 << i)) == 0)
349 				continue;
350 			if (!first)
351 				sbuf_cat(&sbuf, ",");
352 			sbuf_cat(&sbuf, !(hc_source_mask & (1 << i)) ? "[" : "");
353 			sbuf_cat(&sbuf, random_source_descr[i]);
354 			sbuf_cat(&sbuf, !(hc_source_mask & (1 << i)) ? "]" : "");
355 			first = false;
356 		}
357 		error = sbuf_finish(&sbuf);
358 		sbuf_delete(&sbuf);
359 	}
360 	return (error);
361 }
362 SYSCTL_PROC(_kern_random_harvest, OID_AUTO, mask_symbolic,
363     CTLTYPE_STRING | CTLFLAG_RD, NULL, 0, random_print_harvestmask_symbolic,
364     "A", "Entropy harvesting mask (symbolic)");
365 
366 /* ARGSUSED */
367 static void
368 random_harvestq_init(void *unused __unused)
369 {
370 	static const u_int almost_everything_mask =
371 	    (((1 << (RANDOM_ENVIRONMENTAL_END + 1)) - 1) &
372 	    ~_RANDOM_HARVEST_ETHER_OFF & ~_RANDOM_HARVEST_UMA_OFF);
373 
374 	hc_source_mask = almost_everything_mask;
375 	RANDOM_HARVEST_INIT_LOCK();
376 	harvest_context.hc_entropy_ring.in = harvest_context.hc_entropy_ring.out = 0;
377 }
378 SYSINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_THIRD, random_harvestq_init, NULL);
379 
380 /*
381  * Subroutine to slice up a contiguous chunk of 'entropy' and feed it into the
382  * underlying algorithm.  Returns number of bytes actually fed into underlying
383  * algorithm.
384  */
385 static size_t
386 random_early_prime(char *entropy, size_t len)
387 {
388 	struct harvest_event event;
389 	size_t i;
390 
391 	len = rounddown(len, sizeof(event.he_entropy));
392 	if (len == 0)
393 		return (0);
394 
395 	for (i = 0; i < len; i += sizeof(event.he_entropy)) {
396 		event.he_somecounter = (uint32_t)get_cyclecount();
397 		event.he_size = sizeof(event.he_entropy);
398 		event.he_source = RANDOM_CACHED;
399 		event.he_destination =
400 		    harvest_context.hc_destination[RANDOM_CACHED]++;
401 		memcpy(event.he_entropy, entropy + i, sizeof(event.he_entropy));
402 		random_harvestq_fast_process_event(&event);
403 	}
404 	explicit_bzero(entropy, len);
405 	return (len);
406 }
407 
408 /*
409  * Subroutine to search for known loader-loaded files in memory and feed them
410  * into the underlying algorithm early in boot.  Returns the number of bytes
411  * loaded (zero if none were loaded).
412  */
413 static size_t
414 random_prime_loader_file(const char *type)
415 {
416 	uint8_t *keyfile, *data;
417 	size_t size;
418 
419 	keyfile = preload_search_by_type(type);
420 	if (keyfile == NULL)
421 		return (0);
422 
423 	data = preload_fetch_addr(keyfile);
424 	size = preload_fetch_size(keyfile);
425 	if (data == NULL)
426 		return (0);
427 
428 	return (random_early_prime(data, size));
429 }
430 
431 /*
432  * This is used to prime the RNG by grabbing any early random stuff
433  * known to the kernel, and inserting it directly into the hashing
434  * module, currently Fortuna.
435  */
436 /* ARGSUSED */
437 static void
438 random_harvestq_prime(void *unused __unused)
439 {
440 	size_t size;
441 
442 	/*
443 	 * Get entropy that may have been preloaded by loader(8)
444 	 * and use it to pre-charge the entropy harvest queue.
445 	 */
446 	size = random_prime_loader_file(RANDOM_CACHED_BOOT_ENTROPY_MODULE);
447 	if (bootverbose) {
448 		if (size > 0)
449 			printf("random: read %zu bytes from preloaded cache\n",
450 			    size);
451 		else
452 			printf("random: no preloaded entropy cache\n");
453 	}
454 }
455 SYSINIT(random_device_prime, SI_SUB_RANDOM, SI_ORDER_MIDDLE, random_harvestq_prime, NULL);
456 
457 /* ARGSUSED */
458 static void
459 random_harvestq_deinit(void *unused __unused)
460 {
461 
462 	/* Command the hash/reseed thread to end and wait for it to finish */
463 	random_kthread_control = 0;
464 	while (random_kthread_control >= 0)
465 		tsleep(&harvest_context.hc_kthread_proc, 0, "harvqterm", hz/5);
466 }
467 SYSUNINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_THIRD, random_harvestq_deinit, NULL);
468 
469 /*-
470  * Entropy harvesting queue routine.
471  *
472  * This is supposed to be fast; do not do anything slow in here!
473  * It is also illegal (and morally reprehensible) to insert any
474  * high-rate data here. "High-rate" is defined as a data source
475  * that will usually cause lots of failures of the "Lockless read"
476  * check a few lines below. This includes the "always-on" sources
477  * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources.
478  */
479 /* XXXRW: get_cyclecount() is cheap on most modern hardware, where cycle
480  * counters are built in, but on older hardware it will do a real time clock
481  * read which can be quite expensive.
482  */
483 void
484 random_harvest_queue_(const void *entropy, u_int size, enum random_entropy_source origin)
485 {
486 	struct harvest_event *event;
487 	u_int ring_in;
488 
489 	KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin));
490 	RANDOM_HARVEST_LOCK();
491 	ring_in = (harvest_context.hc_entropy_ring.in + 1)%RANDOM_RING_MAX;
492 	if (ring_in != harvest_context.hc_entropy_ring.out) {
493 		/* The ring is not full */
494 		event = harvest_context.hc_entropy_ring.ring + ring_in;
495 		event->he_somecounter = (uint32_t)get_cyclecount();
496 		event->he_source = origin;
497 		event->he_destination = harvest_context.hc_destination[origin]++;
498 		if (size <= sizeof(event->he_entropy)) {
499 			event->he_size = size;
500 			memcpy(event->he_entropy, entropy, size);
501 		}
502 		else {
503 			/* Big event, so squash it */
504 			event->he_size = sizeof(event->he_entropy[0]);
505 			event->he_entropy[0] = jenkins_hash(entropy, size, (uint32_t)(uintptr_t)event);
506 		}
507 		harvest_context.hc_entropy_ring.in = ring_in;
508 	}
509 	RANDOM_HARVEST_UNLOCK();
510 }
511 
512 /*-
513  * Entropy harvesting fast routine.
514  *
515  * This is supposed to be very fast; do not do anything slow in here!
516  * This is the right place for high-rate harvested data.
517  */
518 void
519 random_harvest_fast_(const void *entropy, u_int size)
520 {
521 	u_int pos;
522 
523 	pos = harvest_context.hc_entropy_fast_accumulator.pos;
524 	harvest_context.hc_entropy_fast_accumulator.buf[pos] ^= jenkins_hash(entropy, size, (uint32_t)get_cyclecount());
525 	harvest_context.hc_entropy_fast_accumulator.pos = (pos + 1)%RANDOM_ACCUM_MAX;
526 }
527 
528 /*-
529  * Entropy harvesting direct routine.
530  *
531  * This is not supposed to be fast, but will only be used during
532  * (e.g.) booting when initial entropy is being gathered.
533  */
534 void
535 random_harvest_direct_(const void *entropy, u_int size, enum random_entropy_source origin)
536 {
537 	struct harvest_event event;
538 
539 	KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE, ("%s: origin %d invalid\n", __func__, origin));
540 	size = MIN(size, sizeof(event.he_entropy));
541 	event.he_somecounter = (uint32_t)get_cyclecount();
542 	event.he_size = size;
543 	event.he_source = origin;
544 	event.he_destination = harvest_context.hc_destination[origin]++;
545 	memcpy(event.he_entropy, entropy, size);
546 	random_harvestq_fast_process_event(&event);
547 }
548 
549 void
550 random_harvest_register_source(enum random_entropy_source source)
551 {
552 
553 	hc_source_mask |= (1 << source);
554 }
555 
556 void
557 random_harvest_deregister_source(enum random_entropy_source source)
558 {
559 
560 	hc_source_mask &= ~(1 << source);
561 }
562 
563 void
564 random_source_register(struct random_source *rsource)
565 {
566 	struct random_sources *rrs;
567 
568 	KASSERT(rsource != NULL, ("invalid input to %s", __func__));
569 
570 	rrs = malloc(sizeof(*rrs), M_ENTROPY, M_WAITOK);
571 	rrs->rrs_source = rsource;
572 
573 	random_harvest_register_source(rsource->rs_source);
574 
575 	printf("random: registering fast source %s\n", rsource->rs_ident);
576 	LIST_INSERT_HEAD(&source_list, rrs, rrs_entries);
577 }
578 
579 void
580 random_source_deregister(struct random_source *rsource)
581 {
582 	struct random_sources *rrs = NULL;
583 
584 	KASSERT(rsource != NULL, ("invalid input to %s", __func__));
585 
586 	random_harvest_deregister_source(rsource->rs_source);
587 
588 	LIST_FOREACH(rrs, &source_list, rrs_entries)
589 		if (rrs->rrs_source == rsource) {
590 			LIST_REMOVE(rrs, rrs_entries);
591 			break;
592 		}
593 	if (rrs != NULL)
594 		free(rrs, M_ENTROPY);
595 }
596 
597 static int
598 random_source_handler(SYSCTL_HANDLER_ARGS)
599 {
600 	struct random_sources *rrs;
601 	struct sbuf sbuf;
602 	int error, count;
603 
604 	sbuf_new_for_sysctl(&sbuf, NULL, 64, req);
605 	count = 0;
606 	LIST_FOREACH(rrs, &source_list, rrs_entries) {
607 		sbuf_cat(&sbuf, (count++ ? ",'" : "'"));
608 		sbuf_cat(&sbuf, rrs->rrs_source->rs_ident);
609 		sbuf_cat(&sbuf, "'");
610 	}
611 	error = sbuf_finish(&sbuf);
612 	sbuf_delete(&sbuf);
613 	return (error);
614 }
615 SYSCTL_PROC(_kern_random, OID_AUTO, random_sources, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
616 	    NULL, 0, random_source_handler, "A",
617 	    "List of active fast entropy sources.");
618 
619 MODULE_VERSION(random_harvestq, 1);
620