xref: /freebsd-12.1/sys/dev/random/randomdev.h (revision 646041a8)
102c986abSMark Murray /*-
2d1b06863SMark Murray  * Copyright (c) 2000-2015 Mark R V Murray
302c986abSMark Murray  * All rights reserved.
402c986abSMark Murray  *
502c986abSMark Murray  * Redistribution and use in source and binary forms, with or without
602c986abSMark Murray  * modification, are permitted provided that the following conditions
702c986abSMark Murray  * are met:
802c986abSMark Murray  * 1. Redistributions of source code must retain the above copyright
902c986abSMark Murray  *    notice, this list of conditions and the following disclaimer
1002c986abSMark Murray  *    in this position and unchanged.
1102c986abSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
1202c986abSMark Murray  *    notice, this list of conditions and the following disclaimer in the
1302c986abSMark Murray  *    documentation and/or other materials provided with the distribution.
1402c986abSMark Murray  *
1502c986abSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1602c986abSMark Murray  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1702c986abSMark Murray  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1802c986abSMark Murray  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1902c986abSMark Murray  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2002c986abSMark Murray  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2102c986abSMark Murray  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2202c986abSMark Murray  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2302c986abSMark Murray  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2402c986abSMark Murray  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2502c986abSMark Murray  *
2602c986abSMark Murray  * $FreeBSD$
2702c986abSMark Murray  */
2802c986abSMark Murray 
293e8957eaSDag-Erling Smørgrav #ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
303e8957eaSDag-Erling Smørgrav #define	SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
313e8957eaSDag-Erling Smørgrav 
323aa77530SMark Murray #ifdef _KERNEL
333aa77530SMark Murray 
3402c986abSMark Murray /* This header contains only those definitions that are global
3502c986abSMark Murray  * and non algorithm-specific for the entropy processor
3602c986abSMark Murray  */
3702c986abSMark Murray 
3810cb2424SMark Murray #ifdef SYSCTL_DECL	/* from sysctl.h */
3910cb2424SMark Murray SYSCTL_DECL(_kern_random);
4010cb2424SMark Murray 
4110cb2424SMark Murray #define	RANDOM_CHECK_UINT(name, min, max)				\
4210cb2424SMark Murray static int								\
4310cb2424SMark Murray random_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
4410cb2424SMark Murray {									\
4510cb2424SMark Murray 	if (oidp->oid_arg1 != NULL) {					\
4610cb2424SMark Murray 		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
4710cb2424SMark Murray 			*(u_int *)(oidp->oid_arg1) = (min);		\
4810cb2424SMark Murray 		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
4910cb2424SMark Murray 			*(u_int *)(oidp->oid_arg1) = (max);		\
5010cb2424SMark Murray 	}								\
5110cb2424SMark Murray 	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
5210cb2424SMark Murray 		req));							\
5310cb2424SMark Murray }
5410cb2424SMark Murray #endif /* SYSCTL_DECL */
553e8957eaSDag-Erling Smørgrav 
56d1b06863SMark Murray MALLOC_DECLARE(M_ENTROPY);
57d1b06863SMark Murray 
583aa77530SMark Murray #endif /* _KERNEL */
593aa77530SMark Murray 
60d1b06863SMark Murray struct harvest_event;
61d1b06863SMark Murray 
62*646041a8SMark Murray typedef void random_alg_init_t(void *);
63*646041a8SMark Murray typedef void random_alg_deinit_t(void *);
64d1b06863SMark Murray typedef void random_alg_pre_read_t(void);
65d1b06863SMark Murray typedef void random_alg_read_t(uint8_t *, u_int);
66*646041a8SMark Murray typedef bool random_alg_seeded_t(void);
67d1b06863SMark Murray typedef void random_alg_reseed_t(void);
68d1b06863SMark Murray typedef void random_alg_eventprocessor_t(struct harvest_event *);
69d1b06863SMark Murray 
70d1b06863SMark Murray typedef u_int random_source_read_t(void *, u_int);
71d1b06863SMark Murray 
72d1b06863SMark Murray /*
73d1b06863SMark Murray  * Random Algorithm is a processor of randomness for the kernel
74d1b06863SMark Murray  * and for userland.
75d1b06863SMark Murray  */
76d1b06863SMark Murray struct random_algorithm {
77d1b06863SMark Murray 	const char			*ra_ident;
78d1b06863SMark Murray 	u_int				 ra_poolcount;
793aa77530SMark Murray 	void				(*ra_init_alg)(void *);
803aa77530SMark Murray 	void				(*ra_deinit_alg)(void *);
81d1b06863SMark Murray 	random_alg_pre_read_t		*ra_pre_read;
82d1b06863SMark Murray 	random_alg_read_t		*ra_read;
83d1b06863SMark Murray 	random_alg_seeded_t		*ra_seeded;
84d1b06863SMark Murray 	random_alg_eventprocessor_t	*ra_event_processor;
85d1b06863SMark Murray };
86d1b06863SMark Murray 
87*646041a8SMark Murray extern struct random_algorithm random_alg_context, *p_random_alg_context;
88d1b06863SMark Murray 
893aa77530SMark Murray #ifdef _KERNEL
903aa77530SMark Murray 
91d1b06863SMark Murray /*
92d1b06863SMark Murray  * Random Source is a source of entropy that can provide
93d1b06863SMark Murray  * specified or approximate amount of entropy immediately
94d1b06863SMark Murray  * upon request.
95d1b06863SMark Murray  */
96d1b06863SMark Murray struct random_source {
97d1b06863SMark Murray 	const char			*rs_ident;
98d1b06863SMark Murray 	enum random_entropy_source	 rs_source;
99d1b06863SMark Murray 	random_source_read_t		*rs_read;
100d1b06863SMark Murray };
101d1b06863SMark Murray 
102d1b06863SMark Murray struct random_sources {
103d1b06863SMark Murray 	LIST_ENTRY(random_sources)	 rrs_entries;
104d1b06863SMark Murray 	struct random_source		*rrs_source;
105d1b06863SMark Murray };
106*646041a8SMark Murray 
107*646041a8SMark Murray LIST_HEAD(sources_head, random_sources);
108*646041a8SMark Murray extern struct sources_head source_list;
109d1b06863SMark Murray 
110d1b06863SMark Murray void random_source_register(struct random_source *);
111d1b06863SMark Murray void random_source_deregister(struct random_source *);
112d1b06863SMark Murray 
113*646041a8SMark Murray #if defined(RANDOM_LOADABLE)
114*646041a8SMark Murray extern struct sx randomdev_config_lock;
115*646041a8SMark Murray #define	RANDOM_CONFIG_INIT_LOCK(x)	sx_init(&randomdev_config_lock, "configuration change lock")
116*646041a8SMark Murray #define	RANDOM_CONFIG_X_LOCK(x)		sx_xlock(&randomdev_config_lock)
117*646041a8SMark Murray #define	RANDOM_CONFIG_X_UNLOCK(x)	sx_xunlock(&randomdev_config_lock)
118*646041a8SMark Murray #define	RANDOM_CONFIG_S_LOCK(x)		sx_slock(&randomdev_config_lock)
119*646041a8SMark Murray #define	RANDOM_CONFIG_S_UNLOCK(x)	sx_sunlock(&randomdev_config_lock)
120*646041a8SMark Murray #define	RANDOM_CONFIG_DEINIT_LOCK(x)	sx_destroy(&randomdev_config_lock)
121*646041a8SMark Murray void random_infra_init(int (*)(struct uio *, bool), u_int (*)(void *, u_int));
122*646041a8SMark Murray void random_infra_uninit(void);
123*646041a8SMark Murray #endif
124d1b06863SMark Murray 
1253aa77530SMark Murray #endif /* _KERNEL */
1263aa77530SMark Murray 
127d1b06863SMark Murray void randomdev_unblock(void);
128d1b06863SMark Murray 
129d1b06863SMark Murray #endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
130