xref: /freebsd/sys/sys/random.h (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2015, 2017 Mark R. V. Murray
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef	_SYS_RANDOM_H_
32 #define	_SYS_RANDOM_H_
33 
34 #ifdef _KERNEL
35 
36 #include <sys/types.h>
37 
38 #if !defined(KLD_MODULE)
39 #if defined(RANDOM_LOADABLE) && defined(RANDOM_YARROW)
40 #error "Cannot define both RANDOM_LOADABLE and RANDOM_YARROW"
41 #endif
42 #endif
43 
44 struct uio;
45 
46 #if defined(DEV_RANDOM)
47 u_int read_random(void *, u_int);
48 int read_random_uio(struct uio *, bool);
49 #else
50 static __inline int
51 read_random_uio(void *a __unused, u_int b __unused)
52 {
53 	return (0);
54 }
55 static __inline u_int
56 read_random(void *a __unused, u_int b __unused)
57 {
58 	return (0);
59 }
60 #endif
61 
62 /*
63  * Note: if you add or remove members of random_entropy_source, remember to
64  * also update the strings in the static array random_source_descr[] in
65  * random_harvestq.c.
66  *
67  * NOTE: complain loudly to markm@ or on the lists if this enum gets more than 32
68  * distinct values (0-31)! ENTROPYSOURCE may be == 32, but not > 32.
69  */
70 enum random_entropy_source {
71 	RANDOM_START = 0,
72 	RANDOM_CACHED = 0,
73 	/* Environmental sources */
74 	RANDOM_ATTACH,
75 	RANDOM_KEYBOARD,
76 	RANDOM_MOUSE,
77 	RANDOM_NET_TUN,
78 	RANDOM_NET_ETHER,
79 	RANDOM_NET_NG,
80 	RANDOM_INTERRUPT,
81 	RANDOM_SWI,
82 	RANDOM_FS_ATIME,
83 	RANDOM_UMA,	/* Special!! UMA/SLAB Allocator */
84 	RANDOM_ENVIRONMENTAL_END = RANDOM_UMA,
85 	/* Fast hardware random-number sources from here on. */
86 	RANDOM_PURE_START,
87 	RANDOM_PURE_OCTEON = RANDOM_PURE_START,
88 	RANDOM_PURE_SAFE,
89 	RANDOM_PURE_GLXSB,
90 	RANDOM_PURE_UBSEC,
91 	RANDOM_PURE_HIFN,
92 	RANDOM_PURE_RDRAND,
93 	RANDOM_PURE_NEHEMIAH,
94 	RANDOM_PURE_RNDTEST,
95 	RANDOM_PURE_VIRTIO,
96 	RANDOM_PURE_BROADCOM,
97 	RANDOM_PURE_CCP,
98 	ENTROPYSOURCE
99 };
100 
101 #define RANDOM_HARVEST_EVERYTHING_MASK ((1 << (RANDOM_ENVIRONMENTAL_END + 1)) - 1)
102 #define RANDOM_HARVEST_PURE_MASK (((1 << ENTROPYSOURCE) - 1) & (-1UL << RANDOM_PURE_START))
103 
104 #define RANDOM_LEGACY_BOOT_ENTROPY_MODULE	"/boot/entropy"
105 #define RANDOM_CACHED_BOOT_ENTROPY_MODULE	"boot_entropy_cache"
106 #define	RANDOM_CACHED_SKIP_START	256
107 
108 #if defined(DEV_RANDOM)
109 void random_harvest_queue(const void *, u_int, u_int, enum random_entropy_source);
110 void random_harvest_fast(const void *, u_int, u_int, enum random_entropy_source);
111 void random_harvest_direct(const void *, u_int, u_int, enum random_entropy_source);
112 void random_harvest_register_source(enum random_entropy_source);
113 void random_harvest_deregister_source(enum random_entropy_source);
114 #else
115 #define random_harvest_queue(a, b, c, d) do {} while (0)
116 #define random_harvest_fast(a, b, c, d) do {} while (0)
117 #define random_harvest_direct(a, b, c, d) do {} while (0)
118 #define random_harvest_register_source(a) do {} while (0)
119 #define random_harvest_deregister_source(a) do {} while (0)
120 #endif
121 
122 #if defined(RANDOM_ENABLE_UMA)
123 #define random_harvest_fast_uma(a, b, c, d)	random_harvest_fast(a, b, c, d)
124 #else /* !defined(RANDOM_ENABLE_UMA) */
125 #define random_harvest_fast_uma(a, b, c, d)	do {} while (0)
126 #endif /* defined(RANDOM_ENABLE_UMA) */
127 
128 #endif /* _KERNEL */
129 
130 #endif /* _SYS_RANDOM_H_ */
131