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