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