xref: /freebsd/sys/dev/random/randomdev.h (revision 8b238f4126d32df3e70056bc32536b7248ebffa0)
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 #ifdef _KERNEL
33 
34 /* This header contains only those definitions that are global
35  * and non algorithm-specific for the entropy processor
36  */
37 
38 #ifdef SYSCTL_DECL	/* from sysctl.h */
39 SYSCTL_DECL(_kern_random);
40 SYSCTL_DECL(_kern_random_initial_seeding);
41 
42 #define	RANDOM_CHECK_UINT(name, min, max)				\
43 static int								\
44 random_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
45 {									\
46 	if (oidp->oid_arg1 != NULL) {					\
47 		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
48 			*(u_int *)(oidp->oid_arg1) = (min);		\
49 		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
50 			*(u_int *)(oidp->oid_arg1) = (max);		\
51 	}								\
52 	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
53 		req));							\
54 }
55 #endif /* SYSCTL_DECL */
56 
57 MALLOC_DECLARE(M_ENTROPY);
58 
59 extern bool random_bypass_before_seeding;
60 extern bool read_random_bypassed_before_seeding;
61 extern bool arc4random_bypassed_before_seeding;
62 extern bool random_bypass_disable_warnings;
63 
64 #endif /* _KERNEL */
65 
66 struct harvest_event;
67 
68 typedef void random_alg_pre_read_t(void);
69 typedef void random_alg_read_t(uint8_t *, size_t);
70 typedef bool random_alg_seeded_t(void);
71 typedef void random_alg_eventprocessor_t(struct harvest_event *);
72 
73 typedef u_int random_source_read_t(void *, u_int);
74 
75 /*
76  * Random Algorithm is a processor of randomness for the kernel
77  * and for userland.
78  */
79 struct random_algorithm {
80 	const char			*ra_ident;
81 	u_int				 ra_poolcount;
82 	void				(*ra_init_alg)(void *);
83 	void				(*ra_deinit_alg)(void *);
84 	random_alg_pre_read_t		*ra_pre_read;
85 	random_alg_read_t		*ra_read;
86 	random_alg_seeded_t		*ra_seeded;
87 	random_alg_eventprocessor_t	*ra_event_processor;
88 };
89 
90 extern struct random_algorithm random_alg_context, *p_random_alg_context;
91 
92 #ifdef _KERNEL
93 
94 /*
95  * Random Source is a source of entropy that can provide
96  * specified or approximate amount of entropy immediately
97  * upon request.
98  */
99 struct random_source {
100 	const char			*rs_ident;
101 	enum random_entropy_source	 rs_source;
102 	random_source_read_t		*rs_read;
103 };
104 
105 void random_source_register(struct random_source *);
106 void random_source_deregister(struct random_source *);
107 
108 #if defined(RANDOM_LOADABLE)
109 extern struct sx randomdev_config_lock;
110 #define	RANDOM_CONFIG_INIT_LOCK(x)	sx_init(&randomdev_config_lock, "configuration change lock")
111 #define	RANDOM_CONFIG_X_LOCK(x)		sx_xlock(&randomdev_config_lock)
112 #define	RANDOM_CONFIG_X_UNLOCK(x)	sx_xunlock(&randomdev_config_lock)
113 #define	RANDOM_CONFIG_S_LOCK(x)		sx_slock(&randomdev_config_lock)
114 #define	RANDOM_CONFIG_S_UNLOCK(x)	sx_sunlock(&randomdev_config_lock)
115 #define	RANDOM_CONFIG_DEINIT_LOCK(x)	sx_destroy(&randomdev_config_lock)
116 void random_infra_init(int (*)(struct uio *, bool), void (*)(void *, u_int),
117     bool (*)(void));
118 void random_infra_uninit(void);
119 #endif
120 
121 #endif /* _KERNEL */
122 
123 void randomdev_unblock(void);
124 
125 #endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
126