Deleted Added
full compact
harvest.c (126674) harvest.c (128059)
1/*-
1/*-
2 * Copyright (c) 2000, 2001, 2002, 2003 Mark R V Murray
2 * Copyright (c) 2000-2004 Mark R V Murray
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.

--- 10 unchanged lines hidden (view full) ---

21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.

--- 10 unchanged lines hidden (view full) ---

21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/random/harvest.c 126674 2004-03-05 22:42:17Z jhb $");
29__FBSDID("$FreeBSD: head/sys/dev/random/harvest.c 128059 2004-04-09 15:47:10Z markm $");
30
31#include <sys/param.h>
32#include <sys/kthread.h>
33#include <sys/lock.h>
34#include <sys/malloc.h>
35#include <sys/mutex.h>
36#include <sys/poll.h>
37#include <sys/queue.h>
38#include <sys/random.h>
39#include <sys/selinfo.h>
30
31#include <sys/param.h>
32#include <sys/kthread.h>
33#include <sys/lock.h>
34#include <sys/malloc.h>
35#include <sys/mutex.h>
36#include <sys/poll.h>
37#include <sys/queue.h>
38#include <sys/random.h>
39#include <sys/selinfo.h>
40#include <sys/sysctl.h>
41#include <sys/systm.h>
40#include <sys/systm.h>
41#include <sys/sysctl.h>
42
43#include <machine/cpu.h>
44
42
43#include <machine/cpu.h>
44
45#include
45#include <dev/random/randomdev_soft.h>
46
47static int read_random_phony(void *, int);
48
49/* Structure holding the desired entropy sources */
46
47static int read_random_phony(void *, int);
48
49/* Structure holding the desired entropy sources */
50struct harvest_select harvest = { 0, 0, 0 };
50struct harvest_select harvest = { 0, 0, 0, 0 };
51
52/* hold the address of the routine which is actually called if
53 * the randomdev is loaded
54 */
51
52/* hold the address of the routine which is actually called if
53 * the randomdev is loaded
54 */
55static void (*reap_func)(u_int64_t, void *, u_int, u_int, u_int, enum esource)
56 = NULL;
55static void (*reap_func)(u_int64_t, const void *, u_int, u_int, u_int,
56 enum esource) = NULL;
57static int (*read_func)(void *, int) = read_random_phony;
58
59/* Initialise the harvester at load time */
60void
57static int (*read_func)(void *, int) = read_random_phony;
58
59/* Initialise the harvester at load time */
60void
61random_init_harvester(void (*reaper)(u_int64_t, void *, u_int, u_int, u_int,
62 enum esource), int (*reader)(void *, int))
61random_yarrow_init_harvester(void (*reaper)(u_int64_t, const void *, u_int,
62 u_int, u_int, enum esource), int (*reader)(void *, int))
63{
64 reap_func = reaper;
65 read_func = reader;
66}
67
68/* Deinitialise the harvester at unload time */
69void
63{
64 reap_func = reaper;
65 read_func = reader;
66}
67
68/* Deinitialise the harvester at unload time */
69void
70random_deinit_harvester(void)
70random_yarrow_deinit_harvester(void)
71{
72 reap_func = NULL;
73 read_func = read_random_phony;
74}
75
76/* Entropy harvesting routine. This is supposed to be fast; do
77 * not do anything slow in here!
78 * Implemented as in indirect call to allow non-inclusion of

--- 7 unchanged lines hidden (view full) ---

86 (*reap_func)(get_cyclecount(), entropy, count, bits, frac,
87 origin);
88}
89
90/* Userland-visible version of read_random */
91int
92read_random(void *buf, int count)
93{
71{
72 reap_func = NULL;
73 read_func = read_random_phony;
74}
75
76/* Entropy harvesting routine. This is supposed to be fast; do
77 * not do anything slow in here!
78 * Implemented as in indirect call to allow non-inclusion of

--- 7 unchanged lines hidden (view full) ---

86 (*reap_func)(get_cyclecount(), entropy, count, bits, frac,
87 origin);
88}
89
90/* Userland-visible version of read_random */
91int
92read_random(void *buf, int count)
93{
94 return (*read_func)(buf, count);
94 return ((*read_func)(buf, count));
95}
96
97/* If the entropy device is not loaded, make a token effort to
98 * provide _some_ kind of randomness. This should only be used
99 * inside other RNG's, like arc4random(9).
100 */
101static int
102read_random_phony(void *buf, int count)
103{
104 u_long randval;
105 int size, i;
106
107 /* srandom() is called in kern/init_main.c:proc0_post() */
108
109 /* Fill buf[] with random(9) output */
110 for (i = 0; i < count; i+= (int)sizeof(u_long)) {
111 randval = random();
95}
96
97/* If the entropy device is not loaded, make a token effort to
98 * provide _some_ kind of randomness. This should only be used
99 * inside other RNG's, like arc4random(9).
100 */
101static int
102read_random_phony(void *buf, int count)
103{
104 u_long randval;
105 int size, i;
106
107 /* srandom() is called in kern/init_main.c:proc0_post() */
108
109 /* Fill buf[] with random(9) output */
110 for (i = 0; i < count; i+= (int)sizeof(u_long)) {
111 randval = random();
112 size = (count - i) < (int)sizeof(u_long)
113 ? (count - i)
114 : sizeof(u_long);
112 size = MIN(count - i, sizeof(u_long));
115 memcpy(&((char *)buf)[i], &randval, (size_t)size);
116 }
117
113 memcpy(&((char *)buf)[i], &randval, (size_t)size);
114 }
115
118 return count;
116 return (count);
119}
120
121/* Helper routine to enable kthread_exit() to work while the module is
122 * being (or has been) unloaded.
123 * This routine is in this file because it is always linked into the kernel,
124 * and will thus never be unloaded. This is critical for unloadable modules
125 * that have threads.
126 */
127void
128random_set_wakeup_exit(void *control)
129{
130 wakeup(control);
131 kthread_exit(0);
132 /* NOTREACHED */
133}
117}
118
119/* Helper routine to enable kthread_exit() to work while the module is
120 * being (or has been) unloaded.
121 * This routine is in this file because it is always linked into the kernel,
122 * and will thus never be unloaded. This is critical for unloadable modules
123 * that have threads.
124 */
125void
126random_set_wakeup_exit(void *control)
127{
128 wakeup(control);
129 kthread_exit(0);
130 /* NOTREACHED */
131}