xref: /freebsd/sys/dev/random/randomdev.h (revision b78ee15e9f04ae15c3e1200df974473167524d17)
1 /*-
2  * Copyright (c) 2000-2015 Mark R V Murray
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
30 #define	SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
31 
32 /* This header contains only those definitions that are global
33  * and non algorithm-specific for the entropy processor
34  */
35 
36 #ifdef SYSCTL_DECL	/* from sysctl.h */
37 SYSCTL_DECL(_kern_random);
38 
39 #define	RANDOM_CHECK_UINT(name, min, max)				\
40 static int								\
41 random_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
42 {									\
43 	if (oidp->oid_arg1 != NULL) {					\
44 		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
45 			*(u_int *)(oidp->oid_arg1) = (min);		\
46 		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
47 			*(u_int *)(oidp->oid_arg1) = (max);		\
48 	}								\
49 	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
50 		req));							\
51 }
52 #endif /* SYSCTL_DECL */
53 
54 MALLOC_DECLARE(M_ENTROPY);
55 
56 #define	RANDOM_ALG_READ_RATE_MINIMUM	32
57 
58 struct harvest_event;
59 
60 typedef void random_alg_pre_read_t(void);
61 typedef void random_alg_read_t(uint8_t *, u_int);
62 typedef void random_alg_post_read_t(void);
63 typedef void random_alg_write_t(uint8_t *, u_int);
64 typedef int random_alg_seeded_t(void);
65 typedef void random_alg_reseed_t(void);
66 typedef void random_alg_eventprocessor_t(struct harvest_event *);
67 
68 typedef u_int random_source_read_t(void *, u_int);
69 
70 /*
71  * Random Algorithm is a processor of randomness for the kernel
72  * and for userland.
73  */
74 struct random_algorithm {
75 	const char			*ra_ident;
76 	u_int				 ra_poolcount;
77 	random_alg_pre_read_t		*ra_pre_read;
78 	random_alg_read_t		*ra_read;
79 	random_alg_post_read_t		*ra_post_read;
80 	random_alg_write_t		*ra_write;
81 	random_alg_reseed_t		*ra_reseed;
82 	random_alg_seeded_t		*ra_seeded;
83 	random_alg_eventprocessor_t	*ra_event_processor;
84 };
85 
86 extern struct random_algorithm random_alg_context;
87 
88 /*
89  * Random Source is a source of entropy that can provide
90  * specified or approximate amount of entropy immediately
91  * upon request.
92  */
93 struct random_source {
94 	const char				*rs_ident;
95 	enum random_entropy_source		 rs_source;
96 	random_source_read_t			*rs_read;
97 };
98 
99 #if !defined(RANDOM_DUMMY)
100 struct random_sources {
101 	LIST_ENTRY(random_sources)		 rrs_entries;
102 	struct random_source			*rrs_source;
103 };
104 #endif /* !defined(RANDOM_DUMMY) */
105 
106 void random_source_register(struct random_source *);
107 void random_source_deregister(struct random_source *);
108 
109 void random_sources_feed(void);
110 
111 void randomdev_unblock(void);
112 
113 #endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
114