xref: /freebsd-14.2/sys/dev/random/randomdev.h (revision 95ee2897)
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 
273e8957eaSDag-Erling Smørgrav #ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
283e8957eaSDag-Erling Smørgrav #define	SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
293e8957eaSDag-Erling Smørgrav 
303aa77530SMark Murray #ifdef _KERNEL
313aa77530SMark Murray 
3202c986abSMark Murray /* This header contains only those definitions that are global
3302c986abSMark Murray  * and non algorithm-specific for the entropy processor
3402c986abSMark Murray  */
3502c986abSMark Murray 
3610cb2424SMark Murray #ifdef SYSCTL_DECL	/* from sysctl.h */
3710cb2424SMark Murray SYSCTL_DECL(_kern_random);
383782136fSConrad Meyer SYSCTL_DECL(_kern_random_initial_seeding);
3910cb2424SMark Murray 
4010cb2424SMark Murray #define	RANDOM_CHECK_UINT(name, min, max)				\
4110cb2424SMark Murray static int								\
4210cb2424SMark Murray random_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
4310cb2424SMark Murray {									\
4410cb2424SMark Murray 	if (oidp->oid_arg1 != NULL) {					\
4510cb2424SMark Murray 		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
4610cb2424SMark Murray 			*(u_int *)(oidp->oid_arg1) = (min);		\
4710cb2424SMark Murray 		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
4810cb2424SMark Murray 			*(u_int *)(oidp->oid_arg1) = (max);		\
4910cb2424SMark Murray 	}								\
5010cb2424SMark Murray 	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
5110cb2424SMark Murray 		req));							\
5210cb2424SMark Murray }
5310cb2424SMark Murray #endif /* SYSCTL_DECL */
543e8957eaSDag-Erling Smørgrav 
55d1b06863SMark Murray MALLOC_DECLARE(M_ENTROPY);
56d1b06863SMark Murray 
573782136fSConrad Meyer extern bool random_bypass_before_seeding;
583782136fSConrad Meyer extern bool read_random_bypassed_before_seeding;
593782136fSConrad Meyer extern bool arc4random_bypassed_before_seeding;
603782136fSConrad Meyer extern bool random_bypass_disable_warnings;
613782136fSConrad Meyer 
623aa77530SMark Murray #endif /* _KERNEL */
633aa77530SMark Murray 
64d1b06863SMark Murray struct harvest_event;
65d1b06863SMark Murray 
66d1b06863SMark Murray typedef void random_alg_pre_read_t(void);
67d0d71d81SConrad Meyer typedef void random_alg_read_t(uint8_t *, size_t);
68646041a8SMark Murray typedef bool random_alg_seeded_t(void);
69d1b06863SMark Murray typedef void random_alg_eventprocessor_t(struct harvest_event *);
70d1b06863SMark Murray 
71d1b06863SMark Murray typedef u_int random_source_read_t(void *, u_int);
72d1b06863SMark Murray 
73d1b06863SMark Murray /*
74d1b06863SMark Murray  * Random Algorithm is a processor of randomness for the kernel
75d1b06863SMark Murray  * and for userland.
76d1b06863SMark Murray  */
77d1b06863SMark Murray struct random_algorithm {
78d1b06863SMark Murray 	const char			*ra_ident;
79d1b06863SMark Murray 	u_int				 ra_poolcount;
80d1b06863SMark Murray 	random_alg_pre_read_t		*ra_pre_read;
81d1b06863SMark Murray 	random_alg_read_t		*ra_read;
82d1b06863SMark Murray 	random_alg_seeded_t		*ra_seeded;
83d1b06863SMark Murray 	random_alg_eventprocessor_t	*ra_event_processor;
84d1b06863SMark Murray };
85d1b06863SMark Murray 
86*3ee1d5bbSConrad Meyer #if defined(RANDOM_LOADABLE)
87*3ee1d5bbSConrad Meyer extern const struct random_algorithm *p_random_alg_context;
88*3ee1d5bbSConrad Meyer #else
89*3ee1d5bbSConrad Meyer extern const struct random_algorithm random_alg_context;
90*3ee1d5bbSConrad Meyer #define	p_random_alg_context (&random_alg_context)
91*3ee1d5bbSConrad Meyer #endif
92d1b06863SMark Murray 
933aa77530SMark Murray #ifdef _KERNEL
943aa77530SMark Murray 
95d1b06863SMark Murray /*
96d1b06863SMark Murray  * Random Source is a source of entropy that can provide
97d1b06863SMark Murray  * specified or approximate amount of entropy immediately
98d1b06863SMark Murray  * upon request.
99d1b06863SMark Murray  */
100d1b06863SMark Murray struct random_source {
101d1b06863SMark Murray 	const char			*rs_ident;
102d1b06863SMark Murray 	enum random_entropy_source	 rs_source;
103d1b06863SMark Murray 	random_source_read_t		*rs_read;
104d1b06863SMark Murray };
105d1b06863SMark Murray 
106d1b06863SMark Murray void random_source_register(struct random_source *);
107d1b06863SMark Murray void random_source_deregister(struct random_source *);
108d1b06863SMark Murray 
1093aa77530SMark Murray #endif /* _KERNEL */
1103aa77530SMark Murray 
111d1b06863SMark Murray void randomdev_unblock(void);
112d1b06863SMark Murray 
113d1b06863SMark Murray #endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
114