randomdev.h revision 286839
160841Sdarrenr/*-
260841Sdarrenr * Copyright (c) 2000-2015 Mark R V Murray
360841Sdarrenr * All rights reserved.
460841Sdarrenr *
560841Sdarrenr * Redistribution and use in source and binary forms, with or without
660841Sdarrenr * modification, are permitted provided that the following conditions
760841Sdarrenr * are met:
860841Sdarrenr * 1. Redistributions of source code must retain the above copyright
960841Sdarrenr *    notice, this list of conditions and the following disclaimer
1060841Sdarrenr *    in this position and unchanged.
1160841Sdarrenr * 2. Redistributions in binary form must reproduce the above copyright
1260841Sdarrenr *    notice, this list of conditions and the following disclaimer in the
1360841Sdarrenr *    documentation and/or other materials provided with the distribution.
1460841Sdarrenr *
1560841Sdarrenr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1660841Sdarrenr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1760841Sdarrenr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1860841Sdarrenr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1960841Sdarrenr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2060841Sdarrenr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2160841Sdarrenr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2260841Sdarrenr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2360841Sdarrenr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2460841Sdarrenr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2560841Sdarrenr *
2660841Sdarrenr * $FreeBSD: head/sys/dev/random/randomdev.h 286839 2015-08-17 07:36:12Z markm $
2760841Sdarrenr */
2860841Sdarrenr
2960841Sdarrenr#ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
3060841Sdarrenr#define	SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
3160841Sdarrenr
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 */
39SYSCTL_DECL(_kern_random);
40
41#define	RANDOM_CHECK_UINT(name, min, max)				\
42static int								\
43random_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
44{									\
45	if (oidp->oid_arg1 != NULL) {					\
46		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
47			*(u_int *)(oidp->oid_arg1) = (min);		\
48		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
49			*(u_int *)(oidp->oid_arg1) = (max);		\
50	}								\
51	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
52		req));							\
53}
54#endif /* SYSCTL_DECL */
55
56MALLOC_DECLARE(M_ENTROPY);
57
58#endif /* _KERNEL */
59
60struct harvest_event;
61
62typedef void random_alg_init_t(void *);
63typedef void random_alg_deinit_t(void *);
64typedef void random_alg_pre_read_t(void);
65typedef void random_alg_read_t(uint8_t *, u_int);
66typedef bool random_alg_seeded_t(void);
67typedef void random_alg_reseed_t(void);
68typedef void random_alg_eventprocessor_t(struct harvest_event *);
69
70typedef u_int random_source_read_t(void *, u_int);
71
72/*
73 * Random Algorithm is a processor of randomness for the kernel
74 * and for userland.
75 */
76struct random_algorithm {
77	const char			*ra_ident;
78	u_int				 ra_poolcount;
79	void				(*ra_init_alg)(void *);
80	void				(*ra_deinit_alg)(void *);
81	random_alg_pre_read_t		*ra_pre_read;
82	random_alg_read_t		*ra_read;
83	random_alg_seeded_t		*ra_seeded;
84	random_alg_eventprocessor_t	*ra_event_processor;
85};
86
87extern struct random_algorithm random_alg_context, *p_random_alg_context;
88
89#ifdef _KERNEL
90
91/*
92 * Random Source is a source of entropy that can provide
93 * specified or approximate amount of entropy immediately
94 * upon request.
95 */
96struct random_source {
97	const char			*rs_ident;
98	enum random_entropy_source	 rs_source;
99	random_source_read_t		*rs_read;
100};
101
102struct random_sources {
103	LIST_ENTRY(random_sources)	 rrs_entries;
104	struct random_source		*rrs_source;
105};
106
107LIST_HEAD(sources_head, random_sources);
108extern struct sources_head source_list;
109
110void random_source_register(struct random_source *);
111void random_source_deregister(struct random_source *);
112
113#if defined(RANDOM_LOADABLE)
114extern struct sx randomdev_config_lock;
115#define	RANDOM_CONFIG_INIT_LOCK(x)	sx_init(&randomdev_config_lock, "configuration change lock")
116#define	RANDOM_CONFIG_X_LOCK(x)		sx_xlock(&randomdev_config_lock)
117#define	RANDOM_CONFIG_X_UNLOCK(x)	sx_xunlock(&randomdev_config_lock)
118#define	RANDOM_CONFIG_S_LOCK(x)		sx_slock(&randomdev_config_lock)
119#define	RANDOM_CONFIG_S_UNLOCK(x)	sx_sunlock(&randomdev_config_lock)
120#define	RANDOM_CONFIG_DEINIT_LOCK(x)	sx_destroy(&randomdev_config_lock)
121void random_infra_init(int (*)(struct uio *, bool), u_int (*)(void *, u_int));
122void random_infra_uninit(void);
123#endif
124
125#endif /* _KERNEL */
126
127void randomdev_unblock(void);
128
129#endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
130