1f9f848faSopenharmony_ci/*-
2f9f848faSopenharmony_ci * Copyright (c) 2000-2015 Mark R V Murray
3f9f848faSopenharmony_ci * All rights reserved.
4f9f848faSopenharmony_ci *
5f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without
6f9f848faSopenharmony_ci * modification, are permitted provided that the following conditions
7f9f848faSopenharmony_ci * are met:
8f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
9f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer
10f9f848faSopenharmony_ci *    in this position and unchanged.
11f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
12f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
13f9f848faSopenharmony_ci *    documentation and/or other materials provided with the distribution.
14f9f848faSopenharmony_ci *
15f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16f9f848faSopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f9f848faSopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f9f848faSopenharmony_ci * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19f9f848faSopenharmony_ci * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f9f848faSopenharmony_ci * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f9f848faSopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f9f848faSopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f9f848faSopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f9f848faSopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f9f848faSopenharmony_ci */
26f9f848faSopenharmony_ci
27f9f848faSopenharmony_ci#ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
28f9f848faSopenharmony_ci#define	SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
29f9f848faSopenharmony_ci
30f9f848faSopenharmony_ci#ifdef _KERNEL
31f9f848faSopenharmony_ci
32f9f848faSopenharmony_ci/* This header contains only those definitions that are global
33f9f848faSopenharmony_ci * and non algorithm-specific for the entropy processor
34f9f848faSopenharmony_ci */
35f9f848faSopenharmony_ci
36f9f848faSopenharmony_ci#ifdef SYSCTL_DECL	/* from sysctl.h */
37f9f848faSopenharmony_ciSYSCTL_DECL(_kern_random);
38f9f848faSopenharmony_ciSYSCTL_DECL(_kern_random_initial_seeding);
39f9f848faSopenharmony_ci
40f9f848faSopenharmony_ci#define	RANDOM_CHECK_UINT(name, min, max)				\
41f9f848faSopenharmony_cistatic int								\
42f9f848faSopenharmony_cirandom_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
43f9f848faSopenharmony_ci{									\
44f9f848faSopenharmony_ci	if (oidp->oid_arg1 != NULL) {					\
45f9f848faSopenharmony_ci		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
46f9f848faSopenharmony_ci			*(u_int *)(oidp->oid_arg1) = (min);		\
47f9f848faSopenharmony_ci		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
48f9f848faSopenharmony_ci			*(u_int *)(oidp->oid_arg1) = (max);		\
49f9f848faSopenharmony_ci	}								\
50f9f848faSopenharmony_ci	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
51f9f848faSopenharmony_ci		req));							\
52f9f848faSopenharmony_ci}
53f9f848faSopenharmony_ci#endif /* SYSCTL_DECL */
54f9f848faSopenharmony_ci
55f9f848faSopenharmony_ciMALLOC_DECLARE(M_ENTROPY);
56f9f848faSopenharmony_ci
57f9f848faSopenharmony_ciextern bool random_bypass_before_seeding;
58f9f848faSopenharmony_ciextern bool read_random_bypassed_before_seeding;
59f9f848faSopenharmony_ciextern bool arc4random_bypassed_before_seeding;
60f9f848faSopenharmony_ciextern bool random_bypass_disable_warnings;
61f9f848faSopenharmony_ci
62f9f848faSopenharmony_ci#endif /* _KERNEL */
63f9f848faSopenharmony_ci
64f9f848faSopenharmony_cistruct harvest_event;
65f9f848faSopenharmony_ci
66f9f848faSopenharmony_citypedef void random_alg_init_t(void *);
67f9f848faSopenharmony_citypedef void random_alg_deinit_t(void *);
68f9f848faSopenharmony_citypedef void random_alg_pre_read_t(void);
69f9f848faSopenharmony_citypedef void random_alg_read_t(uint8_t *, u_int);
70f9f848faSopenharmony_citypedef bool random_alg_seeded_t(void);
71f9f848faSopenharmony_citypedef void random_alg_reseed_t(void);
72f9f848faSopenharmony_citypedef void random_alg_eventprocessor_t(struct harvest_event *);
73f9f848faSopenharmony_ci
74f9f848faSopenharmony_citypedef u_int random_source_read_t(void *, u_int);
75f9f848faSopenharmony_ci
76f9f848faSopenharmony_ci/*
77f9f848faSopenharmony_ci * Random Algorithm is a processor of randomness for the kernel
78f9f848faSopenharmony_ci * and for userland.
79f9f848faSopenharmony_ci */
80f9f848faSopenharmony_cistruct random_algorithm {
81f9f848faSopenharmony_ci	const char			*ra_ident;
82f9f848faSopenharmony_ci	u_int				 ra_poolcount;
83f9f848faSopenharmony_ci	void				(*ra_init_alg)(void *);
84f9f848faSopenharmony_ci	void				(*ra_deinit_alg)(void *);
85f9f848faSopenharmony_ci	random_alg_pre_read_t		*ra_pre_read;
86f9f848faSopenharmony_ci	random_alg_read_t		*ra_read;
87f9f848faSopenharmony_ci	random_alg_seeded_t		*ra_seeded;
88f9f848faSopenharmony_ci	random_alg_eventprocessor_t	*ra_event_processor;
89f9f848faSopenharmony_ci};
90f9f848faSopenharmony_ci
91f9f848faSopenharmony_ciextern struct random_algorithm random_alg_context, *p_random_alg_context;
92f9f848faSopenharmony_ci
93f9f848faSopenharmony_ci#ifdef _KERNEL
94f9f848faSopenharmony_ci
95f9f848faSopenharmony_ci/*
96f9f848faSopenharmony_ci * Random Source is a source of entropy that can provide
97f9f848faSopenharmony_ci * specified or approximate amount of entropy immediately
98f9f848faSopenharmony_ci * upon request.
99f9f848faSopenharmony_ci */
100f9f848faSopenharmony_cistruct random_source {
101f9f848faSopenharmony_ci	const char			*rs_ident;
102f9f848faSopenharmony_ci	enum random_entropy_source	 rs_source;
103f9f848faSopenharmony_ci	random_source_read_t		*rs_read;
104f9f848faSopenharmony_ci};
105f9f848faSopenharmony_ci
106f9f848faSopenharmony_cistruct random_sources {
107f9f848faSopenharmony_ci	LIST_ENTRY(random_sources)	 rrs_entries;
108f9f848faSopenharmony_ci	struct random_source		*rrs_source;
109f9f848faSopenharmony_ci};
110f9f848faSopenharmony_ci
111f9f848faSopenharmony_ciLIST_HEAD(sources_head, random_sources);
112f9f848faSopenharmony_ciextern struct sources_head source_list;
113f9f848faSopenharmony_ci
114f9f848faSopenharmony_civoid random_source_register(struct random_source *);
115f9f848faSopenharmony_civoid random_source_deregister(struct random_source *);
116f9f848faSopenharmony_ci
117f9f848faSopenharmony_ci#if defined(RANDOM_LOADABLE)
118f9f848faSopenharmony_ciextern struct sx randomdev_config_lock;
119f9f848faSopenharmony_ci#define	RANDOM_CONFIG_INIT_LOCK(x)	sx_init(&randomdev_config_lock, "configuration change lock")
120f9f848faSopenharmony_ci#define	RANDOM_CONFIG_X_LOCK(x)		sx_xlock(&randomdev_config_lock)
121f9f848faSopenharmony_ci#define	RANDOM_CONFIG_X_UNLOCK(x)	sx_xunlock(&randomdev_config_lock)
122f9f848faSopenharmony_ci#define	RANDOM_CONFIG_S_LOCK(x)		sx_slock(&randomdev_config_lock)
123f9f848faSopenharmony_ci#define	RANDOM_CONFIG_S_UNLOCK(x)	sx_sunlock(&randomdev_config_lock)
124f9f848faSopenharmony_ci#define	RANDOM_CONFIG_DEINIT_LOCK(x)	sx_destroy(&randomdev_config_lock)
125f9f848faSopenharmony_civoid random_infra_init(int (*)(struct uio *, bool), u_int (*)(void *, u_int));
126f9f848faSopenharmony_civoid random_infra_uninit(void);
127f9f848faSopenharmony_ci#endif
128f9f848faSopenharmony_ci
129f9f848faSopenharmony_ci#endif /* _KERNEL */
130f9f848faSopenharmony_ci
131f9f848faSopenharmony_civoid randomdev_unblock(void);
132f9f848faSopenharmony_ci
133f9f848faSopenharmony_ci#endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
134