174072Smarkm/*-
2284959Smarkm * Copyright (c) 2000-2015 Mark R V Murray
374072Smarkm * All rights reserved.
474072Smarkm *
574072Smarkm * Redistribution and use in source and binary forms, with or without
674072Smarkm * modification, are permitted provided that the following conditions
774072Smarkm * are met:
874072Smarkm * 1. Redistributions of source code must retain the above copyright
974072Smarkm *    notice, this list of conditions and the following disclaimer
1074072Smarkm *    in this position and unchanged.
1174072Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1274072Smarkm *    notice, this list of conditions and the following disclaimer in the
1374072Smarkm *    documentation and/or other materials provided with the distribution.
1474072Smarkm *
1574072Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1674072Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1774072Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1874072Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1974072Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2074072Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2174072Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2274072Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2374072Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2474072Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2574072Smarkm *
2674072Smarkm * $FreeBSD: releng/11.0/sys/dev/random/randomdev.h 286839 2015-08-17 07:36:12Z markm $
2774072Smarkm */
2874072Smarkm
29256377Smarkm#ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
30284959Smarkm#define	SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
31256377Smarkm
32285422Smarkm#ifdef _KERNEL
33285422Smarkm
3474072Smarkm/* This header contains only those definitions that are global
3574072Smarkm * and non algorithm-specific for the entropy processor
3674072Smarkm */
3774072Smarkm
38273872Smarkm#ifdef SYSCTL_DECL	/* from sysctl.h */
39273872SmarkmSYSCTL_DECL(_kern_random);
40273872Smarkm
41284959Smarkm#define	RANDOM_CHECK_UINT(name, min, max)				\
42273872Smarkmstatic int								\
43273872Smarkmrandom_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
44273872Smarkm{									\
45273872Smarkm	if (oidp->oid_arg1 != NULL) {					\
46284959Smarkm		if (*(u_int *)(oidp->oid_arg1) <= (min))		\
47273872Smarkm			*(u_int *)(oidp->oid_arg1) = (min);		\
48284959Smarkm		else if (*(u_int *)(oidp->oid_arg1) > (max))		\
49273872Smarkm			*(u_int *)(oidp->oid_arg1) = (max);		\
50273872Smarkm	}								\
51284959Smarkm	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
52273872Smarkm		req));							\
53273872Smarkm}
54273872Smarkm#endif /* SYSCTL_DECL */
55273872Smarkm
56284959SmarkmMALLOC_DECLARE(M_ENTROPY);
57284959Smarkm
58285422Smarkm#endif /* _KERNEL */
59285422Smarkm
60284959Smarkmstruct harvest_event;
61284959Smarkm
62286839Smarkmtypedef void random_alg_init_t(void *);
63286839Smarkmtypedef void random_alg_deinit_t(void *);
64284959Smarkmtypedef void random_alg_pre_read_t(void);
65284959Smarkmtypedef void random_alg_read_t(uint8_t *, u_int);
66286839Smarkmtypedef bool random_alg_seeded_t(void);
67284959Smarkmtypedef void random_alg_reseed_t(void);
68284959Smarkmtypedef void random_alg_eventprocessor_t(struct harvest_event *);
69284959Smarkm
70284959Smarkmtypedef u_int random_source_read_t(void *, u_int);
71284959Smarkm
72284959Smarkm/*
73284959Smarkm * Random Algorithm is a processor of randomness for the kernel
74284959Smarkm * and for userland.
75284959Smarkm */
76284959Smarkmstruct random_algorithm {
77284959Smarkm	const char			*ra_ident;
78284959Smarkm	u_int				 ra_poolcount;
79285422Smarkm	void				(*ra_init_alg)(void *);
80285422Smarkm	void				(*ra_deinit_alg)(void *);
81284959Smarkm	random_alg_pre_read_t		*ra_pre_read;
82284959Smarkm	random_alg_read_t		*ra_read;
83284959Smarkm	random_alg_seeded_t		*ra_seeded;
84284959Smarkm	random_alg_eventprocessor_t	*ra_event_processor;
85284959Smarkm};
86284959Smarkm
87286839Smarkmextern struct random_algorithm random_alg_context, *p_random_alg_context;
88284959Smarkm
89285422Smarkm#ifdef _KERNEL
90285422Smarkm
91284959Smarkm/*
92284959Smarkm * Random Source is a source of entropy that can provide
93284959Smarkm * specified or approximate amount of entropy immediately
94284959Smarkm * upon request.
95284959Smarkm */
96284959Smarkmstruct random_source {
97286839Smarkm	const char			*rs_ident;
98286839Smarkm	enum random_entropy_source	 rs_source;
99286839Smarkm	random_source_read_t		*rs_read;
100284959Smarkm};
101284959Smarkm
102284959Smarkmstruct random_sources {
103286839Smarkm	LIST_ENTRY(random_sources)	 rrs_entries;
104286839Smarkm	struct random_source		*rrs_source;
105284959Smarkm};
106284959Smarkm
107286839SmarkmLIST_HEAD(sources_head, random_sources);
108286839Smarkmextern struct sources_head source_list;
109286839Smarkm
110284959Smarkmvoid random_source_register(struct random_source *);
111284959Smarkmvoid random_source_deregister(struct random_source *);
112284959Smarkm
113286839Smarkm#if defined(RANDOM_LOADABLE)
114286839Smarkmextern struct sx randomdev_config_lock;
115286839Smarkm#define	RANDOM_CONFIG_INIT_LOCK(x)	sx_init(&randomdev_config_lock, "configuration change lock")
116286839Smarkm#define	RANDOM_CONFIG_X_LOCK(x)		sx_xlock(&randomdev_config_lock)
117286839Smarkm#define	RANDOM_CONFIG_X_UNLOCK(x)	sx_xunlock(&randomdev_config_lock)
118286839Smarkm#define	RANDOM_CONFIG_S_LOCK(x)		sx_slock(&randomdev_config_lock)
119286839Smarkm#define	RANDOM_CONFIG_S_UNLOCK(x)	sx_sunlock(&randomdev_config_lock)
120286839Smarkm#define	RANDOM_CONFIG_DEINIT_LOCK(x)	sx_destroy(&randomdev_config_lock)
121286839Smarkmvoid random_infra_init(int (*)(struct uio *, bool), u_int (*)(void *, u_int));
122286839Smarkmvoid random_infra_uninit(void);
123286839Smarkm#endif
124284959Smarkm
125285422Smarkm#endif /* _KERNEL */
126285422Smarkm
127284959Smarkmvoid randomdev_unblock(void);
128284959Smarkm
129284959Smarkm#endif /* SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED */
130