xref: /minix/sys/sys/rndsource.h (revision 0a6a1f1d)
1 /*	$NetBSD: rndsource.h,v 1.3 2015/04/21 03:53:07 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 	unsigned	refcnt;
67 } krndsource_t;
68 
69 static inline void
rndsource_setcb(struct krndsource * const rs,void (* const cb)(size_t,void *),void * const arg)70 rndsource_setcb(struct krndsource *const rs, void (*const cb)(size_t, void *),
71     void *const arg)
72 {
73 	rs->get = cb;
74 	rs->getarg = arg;
75 }
76 
77 static inline void
rndsource_setenable(struct krndsource * const rs,void * const cb)78 rndsource_setenable(struct krndsource *const rs, void *const cb)
79 {
80 	rs->enable = cb;
81 }
82 
83 #define RND_ENABLED(rp) \
84         (((rp)->flags & RND_FLAG_NO_COLLECT) == 0)
85 
86 void		_rnd_add_uint32(krndsource_t *, uint32_t);
87 void		_rnd_add_uint64(krndsource_t *, uint64_t);
88 void		rnd_add_data(krndsource_t *, const void *const, uint32_t,
89 		    uint32_t);
90 void		rnd_attach_source(krndsource_t *, const char *,
91 		    uint32_t, uint32_t);
92 void		rnd_detach_source(krndsource_t *);
93 
94 static inline void
rnd_add_uint32(krndsource_t * kr,uint32_t val)95 rnd_add_uint32(krndsource_t *kr, uint32_t val)
96 {
97 	if (__predict_true(kr)) {
98 		if (RND_ENABLED(kr)) {
99 			_rnd_add_uint32(kr, val);
100 		}
101 	} else {
102 		rnd_add_data(NULL, &val, sizeof(val), 0);
103 	}
104 }
105 
106 static inline void
rnd_add_uint64(krndsource_t * kr,uint64_t val)107 rnd_add_uint64(krndsource_t *kr, uint64_t val)
108 {
109 	if (__predict_true(kr)) {
110 		if (RND_ENABLED(kr)) {
111 			_rnd_add_uint64(kr, val);
112 		}
113 	} else {
114 		rnd_add_data(NULL, &val, sizeof(val), 0);
115 	}
116 }
117 
118 #endif	/* _SYS_RNDSOURCE_H */
119