Mon Apr 13 15:39:20 2015 UTC ()
Split <sys/rnd.h> into several header files.

It is silly that changing it causes ~the whole kernel to rebuild.
New header files:

- <sys/rndio.h> (user/kernel) for the ioctl interface.
- <sys/rndsource.h> (kernel-only) for the entropy source API.
- <sys/rndpool.h> (kernel-only) for the entropy pool algorithm.

(We already had <sys/rndsink.h> for the asynchronous reseed API.)

For now, <sys/rnd.h> includes all of these, until all users are
converted to include what they actually need.


(riastradh)
diff -r1.46 -r1.47 src/sys/sys/rnd.h
diff -r0 -r1.1 src/sys/sys/rndio.h
diff -r0 -r1.1 src/sys/sys/rndpool.h
diff -r0 -r1.1 src/sys/sys/rndsource.h

cvs diff -r1.46 -r1.47 src/sys/sys/rnd.h (expand / switch to unified diff)

--- src/sys/sys/rnd.h 2015/04/13 15:23:00 1.46
+++ src/sys/sys/rnd.h 2015/04/13 15:39:19 1.47
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: rnd.h,v 1.46 2015/04/13 15:23:00 riastradh Exp $ */ 1/* $NetBSD: rnd.h,v 1.47 2015/04/13 15:39:19 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Michael Graff <explorer@flame.org>. This code uses ideas and 8 * by Michael Graff <explorer@flame.org>. This code uses ideas and
9 * algorithms from the Linux driver written by Ted Ts'o. 9 * algorithms from the Linux driver written by Ted Ts'o.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,269 +28,34 @@ @@ -28,269 +28,34 @@
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33#ifndef _SYS_RND_H_ 33#ifndef _SYS_RND_H_
34#define _SYS_RND_H_ 34#define _SYS_RND_H_
35 35
36#ifndef _KERNEL 36#ifndef _KERNEL
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38#endif /* !_KERNEL */ 38#endif /* !_KERNEL */
39 39
40#include <sys/types.h> 40#include <sys/types.h>
41#include <sys/sha1.h> 41#include <sys/rndio.h> /* XXX provisional until users converted */
42 42
43#ifdef _KERNEL 43#ifdef _KERNEL
44#include <sys/queue.h> 44#include <sys/queue.h>
 45#include <sys/rndpool.h> /* XXX provisional until users converted */
 46#include <sys/rndsource.h> /* XXX provisional until users converted */
45#include <sys/rngtest.h> 47#include <sys/rngtest.h>
46#include <sys/systm.h> 48#include <sys/systm.h>
47#endif 
48 49
49#define RND_DEV_RANDOM 0 /* minor for blocking until unpredictable */ 50#define RND_DEV_RANDOM 0 /* minor for blocking until unpredictable */
50#define RND_DEV_URANDOM 1 /* minor for randomly generating data */ 51#define RND_DEV_URANDOM 1 /* minor for randomly generating data */
51 52
52/* 
53 * Exposed "size" of entropy pool, for convenience in load/save 
54 * from userspace. Do not assume this is the same as the actual in-kernel 
55 * pool size! 
56 */ 
57#define RND_SAVEWORDS 128 
58typedef struct { 
59 uint32_t entropy; 
60 uint8_t data[RND_SAVEWORDS * sizeof(uint32_t)]; 
61 uint8_t digest[SHA1_DIGEST_LENGTH]; 
62} rndsave_t; 
63 
64/* Statistics exposed by RNDGETPOOLSTAT */ 
65typedef struct 
66{ 
67 uint32_t poolsize; 
68 uint32_t threshold; 
69 uint32_t maxentropy; 
70 
71 uint32_t added; 
72 uint32_t curentropy; 
73 uint32_t removed; 
74 uint32_t discarded; 
75 uint32_t generated; 
76} rndpoolstat_t; 
77 
78/* Sanitized random source view for userspace */ 
79typedef struct { 
80 char name[16]; /* device name */ 
81 uint32_t total; /* entropy from this source */ 
82 uint32_t type; /* type */ 
83 uint32_t flags; /* flags */ 
84} rndsource_t; 
85 
86typedef struct { 
87 rndsource_t rt; 
88 uint32_t dt_samples; /* time-delta samples input */ 
89 uint32_t dt_total; /* time-delta entropy estimate */ 
90 uint32_t dv_samples; /* value-delta samples input */ 
91 uint32_t dv_total; /* value-delta entropy estimate */ 
92} rndsource_est_t; 
93 
94/* 
95 * Flags to control the source. Low byte is type, upper bits are flags. 
96 */ 
97#define RND_FLAG_NO_ESTIMATE 0x00000100 
98#define RND_FLAG_NO_COLLECT 0x00000200 
99#define RND_FLAG_FAST 0x00000400 /* process samples in bulk */ 
100#define RND_FLAG_HASCB 0x00000800 /* has get callback */ 
101#define RND_FLAG_COLLECT_TIME 0x00001000 /* use timestamp as input */ 
102#define RND_FLAG_COLLECT_VALUE 0x00002000 /* use value as input */ 
103#define RND_FLAG_ESTIMATE_TIME 0x00004000 /* estimate entropy on time */ 
104#define RND_FLAG_ESTIMATE_VALUE 0x00008000 /* estimate entropy on value */ 
105#define RND_FLAG_HASENABLE 0x00010000 /* has enable/disable fns */ 
106#define RND_FLAG_DEFAULT (RND_FLAG_COLLECT_VALUE|RND_FLAG_COLLECT_TIME|\ 
107 RND_FLAG_ESTIMATE_TIME) 
108 
109#define RND_TYPE_UNKNOWN 0 /* unknown source */ 
110#define RND_TYPE_DISK 1 /* source is physical disk */ 
111#define RND_TYPE_NET 2 /* source is a network device */ 
112#define RND_TYPE_TAPE 3 /* source is a tape drive */ 
113#define RND_TYPE_TTY 4 /* source is a tty device */ 
114#define RND_TYPE_RNG 5 /* source is a hardware RNG */ 
115#define RND_TYPE_SKEW 6 /* source is skew between clocks */ 
116#define RND_TYPE_ENV 7 /* source is temp or fan sensor */ 
117#define RND_TYPE_VM 8 /* source is VM system events */ 
118#define RND_TYPE_POWER 9 /* source is power events */ 
119#define RND_TYPE_MAX 9 /* last type id used */ 
120 
121#ifdef _KERNEL 
122/* 
123 * Size of entropy pool in 32-bit words. This _MUST_ be a power of 2. Don't 
124 * change this unless you really know what you are doing... 
125 */ 
126#ifndef RND_POOLWORDS 
127#define RND_POOLWORDS 128 
128#endif 
129#define RND_POOLBITS (RND_POOLWORDS * 32) 
130 
131typedef struct rnd_delta_estimator { 
132 uint64_t x; 
133 uint64_t dx; 
134 uint64_t d2x; 
135 uint64_t insamples; 
136 uint64_t outbits; 
137} rnd_delta_t; 
138 
139typedef struct krndsource { 
140 LIST_ENTRY(krndsource) list; /* the linked list */ 
141 char name[16]; /* device name */ 
142 rnd_delta_t time_delta; /* time delta estimator */ 
143 rnd_delta_t value_delta; /* value delta estimator */ 
144 uint32_t total; /* entropy from this source */ 
145 uint32_t type; /* type */ 
146 uint32_t flags; /* flags */ 
147 void *state; /* state information */ 
148 size_t test_cnt; /* how much test data accumulated? */ 
149 void (*get)(size_t, void *); /* pool wants N bytes (badly) */ 
150 void *getarg; /* argument to get-function */ 
151 void (*enable)(struct krndsource *, bool); /* turn on/off */ 
152 rngtest_t *test; /* test data for RNG type sources */ 
153} krndsource_t; 
154 
155static inline void 
156rndsource_setcb(struct krndsource *const rs, void (*const cb)(size_t, void *), 
157 void *const arg) 
158{ 
159 rs->get = cb; 
160 rs->getarg = arg; 
161} 
162 
163static inline void 
164rndsource_setenable(struct krndsource *const rs, void *const cb) 
165{ 
166 rs->enable = cb; 
167} 
168 
169typedef struct { 
170 uint32_t cursor; /* current add point in the pool */ 
171 uint32_t rotate; /* how many bits to rotate by */ 
172 rndpoolstat_t stats; /* current statistics */ 
173 uint32_t pool[RND_POOLWORDS]; /* random pool data */ 
174} rndpool_t; 
175 
176#define RND_ENABLED(rp) \ 
177 (((rp)->flags & RND_FLAG_NO_COLLECT) == 0) 
178 
179void rndpool_init(rndpool_t *); 
180void rndpool_init_global(void); 
181uint32_t rndpool_get_entropy_count(rndpool_t *); 
182void rndpool_set_entropy_count(rndpool_t *, uint32_t); 
183void rndpool_get_stats(rndpool_t *, void *, int); 
184void rndpool_increment_entropy_count(rndpool_t *, uint32_t); 
185uint32_t *rndpool_get_pool(rndpool_t *); 
186uint32_t rndpool_get_poolsize(void); 
187void rndpool_add_data(rndpool_t *, 
188 const void *const , uint32_t, uint32_t); 
189uint32_t rndpool_extract_data(rndpool_t *, void *, uint32_t, uint32_t); 
190void rnd_init(void); 53void rnd_init(void);
191void rnd_init_softint(void); 54void rnd_init_softint(void);
192void _rnd_add_uint32(krndsource_t *, uint32_t); 
193void _rnd_add_uint64(krndsource_t *, uint64_t); 
194void rnd_add_data(krndsource_t *, const void *const, uint32_t, 
195 uint32_t); 
196void rnd_attach_source(krndsource_t *, const char *, 
197 uint32_t, uint32_t); 
198void rnd_detach_source(krndsource_t *); 
199 
200void rnd_seed(void *, size_t); 55void rnd_seed(void *, size_t);
201 56
202static inline void 
203rnd_add_uint32(krndsource_t *kr, uint32_t val) 
204{ 
205 if (__predict_true(kr)) { 
206 if (RND_ENABLED(kr)) { 
207 _rnd_add_uint32(kr, val); 
208 } 
209 } else { 
210 rnd_add_data(NULL, &val, sizeof(val), 0); 
211 } 
212} 
213 
214static inline void 
215rnd_add_uint64(krndsource_t *kr, uint64_t val) 
216{ 
217 if (__predict_true(kr)) { 
218 if (RND_ENABLED(kr)) { 
219 _rnd_add_uint64(kr, val); 
220 } 
221 } else { 
222 rnd_add_data(NULL, &val, sizeof(val), 0); 
223 } 
224} 
225 
226extern int rnd_initial_entropy; 57extern int rnd_initial_entropy;
227 58
228#endif /* _KERNEL */ 59#endif /* _KERNEL */
229 60
230#define RND_MAXSTATCOUNT 10 /* 10 sources at once max */ 
231 
232/* 
233 * return "count" random entries, starting at "start" 
234 */ 
235typedef struct { 
236 uint32_t start; 
237 uint32_t count; 
238 rndsource_t source[RND_MAXSTATCOUNT]; 
239} rndstat_t; 
240 
241/* 
242 * return "count" random entries with estimates, starting at "start" 
243 */ 
244typedef struct { 
245 uint32_t start; 
246 uint32_t count; 
247 rndsource_est_t source[RND_MAXSTATCOUNT]; 
248} rndstat_est_t; 
249 
250 
251/* 
252 * return information on a specific source by name 
253 */ 
254typedef struct { 
255 char name[16]; 
256 rndsource_t source; 
257} rndstat_name_t; 
258 
259typedef struct { 
260 char name[16]; 
261 rndsource_est_t source; 
262} rndstat_est_name_t; 
263 
264 
265/* 
266 * set/clear device flags. If type is set to 0xff, the name is used 
267 * instead. Otherwise, the flags set/cleared apply to all devices of 
268 * the specified type, and the name is ignored. 
269 */ 
270typedef struct { 
271 char name[16]; /* the name we are adjusting */ 
272 uint32_t type; /* the type of device we want */ 
273 uint32_t flags; /* flags to set or clear */ 
274 uint32_t mask; /* mask for the flags we are setting */ 
275} rndctl_t; 
276 
277/* 
278 * Add entropy to the pool. len is the data length, in bytes. 
279 * entropy is the number of bits of estimated entropy in the data. 
280 */ 
281typedef struct { 
282 uint32_t len; 
283 uint32_t entropy; 
284 u_char data[RND_SAVEWORDS * sizeof(uint32_t)]; 
285} rnddata_t; 
286 
287#define RNDGETENTCNT _IOR('R', 101, uint32_t) /* get entropy count */ 
288#define RNDGETSRCNUM _IOWR('R', 102, rndstat_t) /* get rnd source info */ 
289#define RNDGETSRCNAME _IOWR('R', 103, rndstat_name_t) /* get src by name */ 
290#define RNDCTL _IOW('R', 104, rndctl_t) /* set/clear source flags */ 
291#define RNDADDDATA _IOW('R', 105, rnddata_t) /* add data to the pool */ 
292#define RNDGETPOOLSTAT _IOR('R', 106, rndpoolstat_t) /* get statistics */ 
293#define RNDGETESTNUM _IOWR('R', 107, rndstat_est_t) /* get srcest */ 
294#define RNDGETESTNAME _IOWR('R', 108, rndstat_est_name_t) /* " by name */ 
295 
296#endif /* !_SYS_RND_H_ */ 61#endif /* !_SYS_RND_H_ */

File Added: src/sys/sys/rndio.h
/*	$NetBSD: rndio.h,v 1.1 2015/04/13 15:39:19 riastradh Exp $	*/

/*-
 * Copyright (c) 1997 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Michael Graff <explorer@flame.org>.  This code uses ideas and
 * algorithms from the Linux driver written by Ted Ts'o.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef	_SYS_RNDIO_H
#define	_SYS_RNDIO_H

#include <sys/types.h>
#include <sys/sha1.h>

/*
 * Exposed "size" of entropy pool, for convenience in load/save
 * from userspace.  Do not assume this is the same as the actual in-kernel
 * pool size!
 */
#define RND_SAVEWORDS	128
typedef struct {
	uint32_t entropy;
	uint8_t data[RND_SAVEWORDS * sizeof(uint32_t)];
	uint8_t digest[SHA1_DIGEST_LENGTH];
} rndsave_t;

/* Statistics exposed by RNDGETPOOLSTAT */
typedef struct
{
	uint32_t	poolsize;
	uint32_t 	threshold;
	uint32_t	maxentropy;

	uint32_t	added;
	uint32_t	curentropy;
	uint32_t	removed;
	uint32_t	discarded;
	uint32_t	generated;
} rndpoolstat_t;

/* Sanitized random source view for userspace */
typedef struct {
	char		name[16];	/* device name */
	uint32_t	total;		/* entropy from this source */
	uint32_t	type;		/* type */
	uint32_t	flags;		/* flags */
} rndsource_t;

typedef struct {
	rndsource_t	rt;
	uint32_t	dt_samples;	/* time-delta samples input */
	uint32_t	dt_total;	/* time-delta entropy estimate */
	uint32_t	dv_samples;	/* value-delta samples input */
	uint32_t	dv_total;	/* value-delta entropy estimate */
} rndsource_est_t;

/*
 * Flags to control the source.  Low byte is type, upper bits are flags.
 */
#define RND_FLAG_NO_ESTIMATE	0x00000100
#define RND_FLAG_NO_COLLECT	0x00000200
#define RND_FLAG_FAST		0x00000400	/* process samples in bulk */
#define RND_FLAG_HASCB		0x00000800	/* has get callback */
#define RND_FLAG_COLLECT_TIME	0x00001000	/* use timestamp as input */
#define RND_FLAG_COLLECT_VALUE	0x00002000	/* use value as input */
#define RND_FLAG_ESTIMATE_TIME	0x00004000	/* estimate entropy on time */
#define RND_FLAG_ESTIMATE_VALUE	0x00008000	/* estimate entropy on value */
#define	RND_FLAG_HASENABLE	0x00010000	/* has enable/disable fns */
#define RND_FLAG_DEFAULT	(RND_FLAG_COLLECT_VALUE|RND_FLAG_COLLECT_TIME|\
				 RND_FLAG_ESTIMATE_TIME)

#define	RND_TYPE_UNKNOWN	0	/* unknown source */
#define	RND_TYPE_DISK		1	/* source is physical disk */
#define	RND_TYPE_NET		2	/* source is a network device */
#define	RND_TYPE_TAPE		3	/* source is a tape drive */
#define	RND_TYPE_TTY		4	/* source is a tty device */
#define	RND_TYPE_RNG		5	/* source is a hardware RNG */
#define RND_TYPE_SKEW		6	/* source is skew between clocks */
#define RND_TYPE_ENV		7	/* source is temp or fan sensor */
#define RND_TYPE_VM		8	/* source is VM system events */
#define RND_TYPE_POWER		9	/* source is power events */
#define	RND_TYPE_MAX		9	/* last type id used */

#define	RND_MAXSTATCOUNT	10	/* 10 sources at once max */

/*
 * return "count" random entries, starting at "start"
 */
typedef struct {
	uint32_t	start;
	uint32_t	count;
	rndsource_t	source[RND_MAXSTATCOUNT];
} rndstat_t;

/*
 * return "count" random entries with estimates, starting at "start"
 */
typedef struct {
	uint32_t	start;
	uint32_t	count;
	rndsource_est_t	source[RND_MAXSTATCOUNT];
} rndstat_est_t;

/*
 * return information on a specific source by name
 */
typedef struct {
	char		name[16];
	rndsource_t	source;
} rndstat_name_t;

typedef struct {
	char		name[16];
	rndsource_est_t	source;
} rndstat_est_name_t;


/*
 * set/clear device flags.  If type is set to 0xff, the name is used
 * instead.  Otherwise, the flags set/cleared apply to all devices of
 * the specified type, and the name is ignored.
 */
typedef struct {
	char		name[16];	/* the name we are adjusting */
	uint32_t	type;		/* the type of device we want */
	uint32_t	flags;		/* flags to set or clear */
	uint32_t	mask;		/* mask for the flags we are setting */
} rndctl_t;

/*
 * Add entropy to the pool.  len is the data length, in bytes.
 * entropy is the number of bits of estimated entropy in the data.
 */
typedef struct {
	uint32_t	len;
	uint32_t	entropy;
	u_char		data[RND_SAVEWORDS * sizeof(uint32_t)];
} rnddata_t;

#define	RNDGETENTCNT	_IOR('R',  101, uint32_t) /* get entropy count */
#define	RNDGETSRCNUM	_IOWR('R', 102, rndstat_t) /* get rnd source info */
#define	RNDGETSRCNAME	_IOWR('R', 103, rndstat_name_t) /* get src by name */
#define	RNDCTL		_IOW('R',  104, rndctl_t)  /* set/clear source flags */
#define	RNDADDDATA	_IOW('R',  105, rnddata_t) /* add data to the pool */
#define	RNDGETPOOLSTAT	_IOR('R',  106, rndpoolstat_t) /* get statistics */
#define	RNDGETESTNUM	_IOWR('R', 107, rndstat_est_t) /* get srcest */
#define	RNDGETESTNAME	_IOWR('R', 108, rndstat_est_name_t) /* " by name */

#endif	/* _SYS_RNDIO_H */

File Added: src/sys/sys/Attic/rndpool.h
/*	$NetBSD: rndpool.h,v 1.1 2015/04/13 15:39:19 riastradh Exp $	*/

/*-
 * Copyright (c) 1997 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Michael Graff <explorer@flame.org>.  This code uses ideas and
 * algorithms from the Linux driver written by Ted Ts'o.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef	_SYS_RNDPOOL_H
#define	_SYS_RNDPOOL_H

#ifndef _KERNEL			/* XXX */
#error <sys/rndpool.h> is meant for kernel consumers only.
#endif

#include <sys/types.h>
#include <sys/rndio.h>		/* rndpoolstat_t */

/*
 * Size of entropy pool in 32-bit words.  This _MUST_ be a power of 2.  Don't
 * change this unless you really know what you are doing...
 */
#ifndef RND_POOLWORDS
#define RND_POOLWORDS	128
#endif
#define RND_POOLBITS	(RND_POOLWORDS * 32)

typedef struct {
        uint32_t        cursor;         /* current add point in the pool */
        uint32_t        rotate;         /* how many bits to rotate by */
        rndpoolstat_t   stats;          /* current statistics */
        uint32_t        pool[RND_POOLWORDS]; /* random pool data */
} rndpool_t;

void		rndpool_init(rndpool_t *);
void		rndpool_init_global(void);
uint32_t	rndpool_get_entropy_count(rndpool_t *);
void		rndpool_set_entropy_count(rndpool_t *, uint32_t);
void		rndpool_get_stats(rndpool_t *, void *, int);
void		rndpool_increment_entropy_count(rndpool_t *, uint32_t);
uint32_t	*rndpool_get_pool(rndpool_t *);
uint32_t	rndpool_get_poolsize(void);
void		rndpool_add_data(rndpool_t *,
				 const void *const , uint32_t, uint32_t);
uint32_t	rndpool_extract_data(rndpool_t *, void *, uint32_t, uint32_t);

#endif	/* _SYS_RNDPOOL_H */

File Added: src/sys/sys/rndsource.h
/*	$NetBSD: rndsource.h,v 1.1 2015/04/13 15:39:19 riastradh Exp $	*/

/*-
 * Copyright (c) 1997 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Michael Graff <explorer@flame.org>.  This code uses ideas and
 * algorithms from the Linux driver written by Ted Ts'o.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef	_SYS_RNDSOURCE_H
#define	_SYS_RNDSOURCE_H

#ifndef _KERNEL			/* XXX */
#error <sys/rndsource.h> is meant for kernel consumers only.
#endif

#include <sys/types.h>
#include <sys/rngtest.h>

typedef struct rnd_delta_estimator {
	uint64_t	x;
	uint64_t	dx;
	uint64_t	d2x;
	uint64_t	insamples;
	uint64_t	outbits;
} rnd_delta_t;

typedef struct krndsource {
	LIST_ENTRY(krndsource) list;	/* the linked list */
        char            name[16];       /* device name */
	rnd_delta_t	time_delta;	/* time delta estimator */
	rnd_delta_t	value_delta;	/* value delta estimator */
        uint32_t        total;          /* entropy from this source */
        uint32_t        type;           /* type */
        uint32_t        flags;          /* flags */
        void            *state;         /* state information */
        size_t          test_cnt;       /* how much test data accumulated? */
	void		(*get)(size_t, void *);	/* pool wants N bytes (badly) */
	void		*getarg;	/* argument to get-function */
	void		(*enable)(struct krndsource *, bool); /* turn on/off */
	rngtest_t	*test;		/* test data for RNG type sources */
} krndsource_t;

static inline void
rndsource_setcb(struct krndsource *const rs, void (*const cb)(size_t, void *),
    void *const arg)
{
	rs->get = cb;
	rs->getarg = arg;
}

static inline void
rndsource_setenable(struct krndsource *const rs, void *const cb)
{
	rs->enable = cb;
}

#define RND_ENABLED(rp) \
        (((rp)->flags & RND_FLAG_NO_COLLECT) == 0)

void		_rnd_add_uint32(krndsource_t *, uint32_t);
void		_rnd_add_uint64(krndsource_t *, uint64_t);
void		rnd_add_data(krndsource_t *, const void *const, uint32_t,
		    uint32_t);
void		rnd_attach_source(krndsource_t *, const char *,
		    uint32_t, uint32_t);
void		rnd_detach_source(krndsource_t *);

static inline void
rnd_add_uint32(krndsource_t *kr, uint32_t val)
{
	if (__predict_true(kr)) {
		if (RND_ENABLED(kr)) {
			_rnd_add_uint32(kr, val);
		}
	} else {
		rnd_add_data(NULL, &val, sizeof(val), 0);
	}
}

static inline void
rnd_add_uint64(krndsource_t *kr, uint64_t val)
{
	if (__predict_true(kr)) {
		if (RND_ENABLED(kr)) {
			_rnd_add_uint64(kr, val);
		}
	} else {
		rnd_add_data(NULL, &val, sizeof(val), 0);
	}
}

#endif	/* _SYS_RNDSOURCE_H */