xref: /netbsd-src/sys/sys/rndsource.h (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: rndsource.h,v 1.2 2015/04/13 16:02:48 riastradh 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_RNDSOURCE_H
34 #define	_SYS_RNDSOURCE_H
35 
36 #ifndef _KERNEL			/* XXX */
37 #error <sys/rndsource.h> is meant for kernel consumers only.
38 #endif
39 
40 #include <sys/types.h>
41 #include <sys/rndio.h>		/* RND_TYPE_*, RND_FLAG_* */
42 #include <sys/rngtest.h>
43 
44 typedef struct rnd_delta_estimator {
45 	uint64_t	x;
46 	uint64_t	dx;
47 	uint64_t	d2x;
48 	uint64_t	insamples;
49 	uint64_t	outbits;
50 } rnd_delta_t;
51 
52 typedef struct krndsource {
53 	LIST_ENTRY(krndsource) list;	/* the linked list */
54         char            name[16];       /* device name */
55 	rnd_delta_t	time_delta;	/* time delta estimator */
56 	rnd_delta_t	value_delta;	/* value delta estimator */
57         uint32_t        total;          /* entropy from this source */
58         uint32_t        type;           /* type */
59         uint32_t        flags;          /* flags */
60         void            *state;         /* state information */
61         size_t          test_cnt;       /* how much test data accumulated? */
62 	void		(*get)(size_t, void *);	/* pool wants N bytes (badly) */
63 	void		*getarg;	/* argument to get-function */
64 	void		(*enable)(struct krndsource *, bool); /* turn on/off */
65 	rngtest_t	*test;		/* test data for RNG type sources */
66 } krndsource_t;
67 
68 static inline void
69 rndsource_setcb(struct krndsource *const rs, void (*const cb)(size_t, void *),
70     void *const arg)
71 {
72 	rs->get = cb;
73 	rs->getarg = arg;
74 }
75 
76 static inline void
77 rndsource_setenable(struct krndsource *const rs, void *const cb)
78 {
79 	rs->enable = cb;
80 }
81 
82 #define RND_ENABLED(rp) \
83         (((rp)->flags & RND_FLAG_NO_COLLECT) == 0)
84 
85 void		_rnd_add_uint32(krndsource_t *, uint32_t);
86 void		_rnd_add_uint64(krndsource_t *, uint64_t);
87 void		rnd_add_data(krndsource_t *, const void *const, uint32_t,
88 		    uint32_t);
89 void		rnd_attach_source(krndsource_t *, const char *,
90 		    uint32_t, uint32_t);
91 void		rnd_detach_source(krndsource_t *);
92 
93 static inline void
94 rnd_add_uint32(krndsource_t *kr, uint32_t val)
95 {
96 	if (__predict_true(kr)) {
97 		if (RND_ENABLED(kr)) {
98 			_rnd_add_uint32(kr, val);
99 		}
100 	} else {
101 		rnd_add_data(NULL, &val, sizeof(val), 0);
102 	}
103 }
104 
105 static inline void
106 rnd_add_uint64(krndsource_t *kr, uint64_t val)
107 {
108 	if (__predict_true(kr)) {
109 		if (RND_ENABLED(kr)) {
110 			_rnd_add_uint64(kr, val);
111 		}
112 	} else {
113 		rnd_add_data(NULL, &val, sizeof(val), 0);
114 	}
115 }
116 
117 #endif	/* _SYS_RNDSOURCE_H */
118