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