xref: /netbsd/sys/sys/rnd.h (revision 6550d01e)
1 /*	$NetBSD: rnd.h,v 1.21 2008/04/28 20:24:11 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael Graff <explorer@flame.org>.  This code uses ideas and
9  * algorithms from the Linux driver written by Ted Ts'o.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _SYS_RND_H_
34 #define	_SYS_RND_H_
35 
36 #ifndef _KERNEL
37 #include <sys/cdefs.h>
38 #endif /* !_KERNEL */
39 
40 #include <sys/types.h>
41 
42 #ifdef _KERNEL
43 #include <sys/queue.h>
44 #endif
45 
46 #define	RND_DEV_RANDOM	0	/* minor devices for random and kinda random */
47 #define	RND_DEV_URANDOM	1
48 
49 /*
50  * Size of entropy pool in 32-bit words.  This _MUST_ be a power of 2.  Don't
51  * change this unless you really know what you are doing...
52  */
53 #ifndef RND_POOLWORDS
54 #define	RND_POOLWORDS	128
55 #endif
56 #define	RND_POOLBITS	(RND_POOLWORDS * 32)
57 
58 /*
59  * Number of bytes returned per hash.  This value is used in both
60  * rnd.c and rndpool.c to decide when enough entropy exists to do a
61  * hash to extract it.
62  */
63 #define	RND_ENTROPY_THRESHOLD	10
64 
65 /*
66  * Size of the event queue.  This _MUST_ be a power of 2.
67  */
68 #ifndef RND_EVENTQSIZE
69 #define	RND_EVENTQSIZE	128
70 #endif
71 
72 typedef struct
73 {
74 	uint32_t	poolsize;
75 	uint32_t 	threshold;
76 	uint32_t	maxentropy;
77 
78 	uint32_t	added;
79 	uint32_t	curentropy;
80 	uint32_t	removed;
81 	uint32_t	discarded;
82 	uint32_t	generated;
83 } rndpoolstat_t;
84 
85 
86 typedef struct {
87 	uint32_t	cursor;		/* current add point in the pool */
88 	uint32_t	rotate;		/* how many bits to rotate by */
89 	rndpoolstat_t	stats;		/* current statistics */
90 	uint32_t	pool[RND_POOLWORDS]; /* random pool data */
91 } rndpool_t;
92 
93 typedef struct {
94 	char		name[16];	/* device name */
95 	uint32_t	last_time;	/* last time recorded */
96 	uint32_t	last_delta;	/* last delta value */
97 	uint32_t	last_delta2;	/* last delta2 value */
98 	uint32_t	total;		/* entropy from this source */
99 	uint32_t	type;		/* type */
100 	uint32_t	flags;		/* flags */
101 	void		*state;		/* state informaiton */
102 } rndsource_t;
103 
104 
105 /*
106  * Flags to control the source.  Low byte is type, upper bits are flags.
107  */
108 #define	RND_FLAG_NO_ESTIMATE	0x00000100	/* don't estimate entropy */
109 #define	RND_FLAG_NO_COLLECT	0x00000200	/* don't collect entropy */
110 
111 #define	RND_TYPE_UNKNOWN	0	/* unknown source */
112 #define	RND_TYPE_DISK		1	/* source is physical disk */
113 #define	RND_TYPE_NET		2	/* source is a network device */
114 #define	RND_TYPE_TAPE		3	/* source is a tape drive */
115 #define	RND_TYPE_TTY		4	/* source is a tty device */
116 #define	RND_TYPE_RNG		5	/* source is a random number
117 					   generator */
118 #define	RND_TYPE_MAX		5	/* last type id used */
119 
120 #ifdef _KERNEL
121 typedef struct __rndsource_element rndsource_element_t;
122 
123 struct __rndsource_element {
124 	LIST_ENTRY(__rndsource_element) list; /* the linked list */
125 	rndsource_t	data;		/* the actual data */
126 };
127 
128 /*
129  * Used by rnd_extract_data() and rndpool_extract_data() to describe how
130  * "good" the data has to be.
131  */
132 #define	RND_EXTRACT_ANY		0  /* extract anything, even if no entropy */
133 #define	RND_EXTRACT_GOOD	1  /* return as many good bytes
134 				      (short read ok) */
135 
136 #define RND_ENABLED(rp) \
137         (((rp)->data.flags & RND_FLAG_NO_COLLECT) == 0)
138 
139 void		rndpool_init(rndpool_t *);
140 void		rndpool_init_global(void);
141 uint32_t	rndpool_get_entropy_count(rndpool_t *);
142 void		rndpool_get_stats(rndpool_t *, void *, int);
143 void		rndpool_increment_entropy_count(rndpool_t *, uint32_t);
144 uint32_t	*rndpool_get_pool(rndpool_t *);
145 uint32_t	rndpool_get_poolsize(void);
146 void		rndpool_add_data(rndpool_t *, void *, uint32_t, uint32_t);
147 uint32_t	rndpool_extract_data(rndpool_t *, void *, uint32_t,
148 		    uint32_t);
149 
150 void		rnd_init(void);
151 void		rnd_add_uint32(rndsource_element_t *, uint32_t);
152 void		rnd_add_data(rndsource_element_t *, void *, uint32_t,
153 		    uint32_t);
154 uint32_t	rnd_extract_data(void *, uint32_t, uint32_t);
155 void		rnd_attach_source(rndsource_element_t *, const char *,
156 		    uint32_t, uint32_t);
157 void		rnd_detach_source(rndsource_element_t *);
158 
159 #endif /* _KERNEL */
160 
161 #define	RND_MAXSTATCOUNT	10	/* 10 sources at once max */
162 
163 /*
164  * return "count" random entries, starting at "start"
165  */
166 typedef struct {
167 	uint32_t	start;
168 	uint32_t	count;
169 	rndsource_t	source[RND_MAXSTATCOUNT];
170 } rndstat_t;
171 
172 /*
173  * return information on a specific source by name
174  */
175 typedef struct {
176 	char		name[16];
177 	rndsource_t	source;
178 } rndstat_name_t;
179 
180 /*
181  * set/clear device flags.  If type is set to 0xff, the name is used
182  * instead.  Otherwise, the flags set/cleared apply to all devices of
183  * the specified type, and the name is ignored.
184  */
185 typedef struct {
186 	char		name[16];	/* the name we are adjusting */
187 	uint32_t	type;		/* the type of device we want */
188 	uint32_t	flags;		/* flags to set or clear */
189 	uint32_t	mask;		/* mask for the flags we are setting */
190 } rndctl_t;
191 
192 typedef struct {
193 	uint32_t	len;
194 	uint32_t	entropy;
195 	u_char		data[RND_POOLWORDS * 4];
196 } rnddata_t;
197 
198 #define	RNDGETENTCNT	_IOR('R',  101, uint32_t) /* get entropy count */
199 #define	RNDGETSRCNUM	_IOWR('R', 102, rndstat_t) /* get rnd source info */
200 #define	RNDGETSRCNAME	_IOWR('R', 103, rndstat_name_t) /* get src by name */
201 #define	RNDCTL		_IOW('R',  104, rndctl_t)  /* set/clear source flags */
202 #define	RNDADDDATA	_IOW('R',  105, rnddata_t) /* add data to the pool */
203 #define	RNDGETPOOLSTAT	_IOR('R',  106, rndpoolstat_t)
204 
205 #endif /* !_SYS_RND_H_ */
206