FreeBSD/Linux Kernel Cross Reference
sys/sys/random.h
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 #include <sys/types.h>
35
36 #ifdef _KERNEL
37
38 struct uio;
39
40 /*
41 * In the loadable random world, there are set of dangling pointers left in the
42 * core kernel:
43 * * read_random, read_random_uio, is_random_seeded are function pointers,
44 * rather than functions.
45 * * p_random_alg_context is a true pointer in loadable random kernels.
46 *
47 * These are initialized at SI_SUB_RANDOM:SI_ORDER_SECOND during boot. The
48 * read-type pointers are initialized by random_alg_context_init() in
49 * randomdev.c and p_random_alg_context in the algorithm, e.g., fortuna.c's
50 * random_fortuna_init_alg(). The nice thing about function pointers is they
51 * have a similar calling convention to ordinary functions.
52 *
53 * (In !loadable, the read_random, etc, routines are just plain functions;
54 * p_random_alg_context is a macro for the public visibility
55 * &random_alg_context.)
56 */
57 #if defined(DEV_RANDOM)
58 #if defined(RANDOM_LOADABLE)
59 extern void (*_read_random)(void *, u_int);
60 extern int (*_read_random_uio)(struct uio *, bool);
61 extern bool (*_is_random_seeded)(void);
62 #define read_random(a, b) (*_read_random)(a, b)
63 #define read_random_uio(a, b) (*_read_random_uio)(a, b)
64 #define is_random_seeded() (*_is_random_seeded)()
65 #else
66 void read_random(void *, u_int);
67 int read_random_uio(struct uio *, bool);
68 bool is_random_seeded(void);
69 #endif //RANDOM_LOADABLE
70
71 #else
72 static __inline int
73 read_random_uio(void *a __unused, u_int b __unused)
74 {
75 return (0);
76 }
77 static __inline void
78 read_random(void *a __unused, u_int b __unused)
79 {
80 }
81 static __inline bool
82 is_random_seeded(void)
83 {
84 return (false);
85 }
86 #endif //DEV_RANDOM
87
88 /*
89 * Note: if you add or remove members of random_entropy_source, remember to
90 * also update the strings in the static array random_source_descr[] in
91 * random_harvestq.c.
92 *
93 * NOTE: complain loudly to markm@ or on the lists if this enum gets more than 32
94 * distinct values (0-31)! ENTROPYSOURCE may be == 32, but not > 32.
95 */
96 enum random_entropy_source {
97 RANDOM_START = 0,
98 RANDOM_CACHED = 0,
99 /* Environmental sources */
100 RANDOM_ATTACH,
101 RANDOM_KEYBOARD,
102 RANDOM_MOUSE,
103 RANDOM_NET_TUN,
104 RANDOM_NET_ETHER,
105 RANDOM_NET_NG,
106 RANDOM_INTERRUPT,
107 RANDOM_SWI,
108 RANDOM_FS_ATIME,
109 RANDOM_UMA, /* Special!! UMA/SLAB Allocator */
110 RANDOM_ENVIRONMENTAL_END = RANDOM_UMA,
111 /* Fast hardware random-number sources from here on. */
112 RANDOM_PURE_START,
113 RANDOM_PURE_OCTEON = RANDOM_PURE_START,
114 RANDOM_PURE_SAFE,
115 RANDOM_PURE_GLXSB,
116 RANDOM_PURE_UBSEC,
117 RANDOM_PURE_HIFN,
118 RANDOM_PURE_RDRAND,
119 RANDOM_PURE_NEHEMIAH,
120 RANDOM_PURE_RNDTEST,
121 RANDOM_PURE_VIRTIO,
122 RANDOM_PURE_BROADCOM,
123 RANDOM_PURE_CCP,
124 RANDOM_PURE_DARN,
125 RANDOM_PURE_TPM,
126 ENTROPYSOURCE
127 };
128
129 #define RANDOM_HARVEST_EVERYTHING_MASK ((1 << (RANDOM_ENVIRONMENTAL_END + 1)) - 1)
130 #define RANDOM_HARVEST_PURE_MASK (((1 << ENTROPYSOURCE) - 1) & (-1UL << RANDOM_PURE_START))
131
132 #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache"
133
134 #if defined(DEV_RANDOM)
135 extern u_int hc_source_mask;
136 void random_harvest_queue_(const void *, u_int, enum random_entropy_source);
137 void random_harvest_fast_(const void *, u_int);
138 void random_harvest_direct_(const void *, u_int, enum random_entropy_source);
139
140 static __inline void
141 random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin)
142 {
143
144 if (hc_source_mask & (1 << origin))
145 random_harvest_queue_(entropy, size, origin);
146 }
147
148 static __inline void
149 random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin)
150 {
151
152 if (hc_source_mask & (1 << origin))
153 random_harvest_fast_(entropy, size);
154 }
155
156 static __inline void
157 random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin)
158 {
159
160 if (hc_source_mask & (1 << origin))
161 random_harvest_direct_(entropy, size, origin);
162 }
163
164 void random_harvest_register_source(enum random_entropy_source);
165 void random_harvest_deregister_source(enum random_entropy_source);
166 #else
167 #define random_harvest_queue(a, b, c) do {} while (0)
168 #define random_harvest_fast(a, b, c) do {} while (0)
169 #define random_harvest_direct(a, b, c) do {} while (0)
170 #define random_harvest_register_source(a) do {} while (0)
171 #define random_harvest_deregister_source(a) do {} while (0)
172 #endif
173
174 #if defined(RANDOM_ENABLE_UMA)
175 #define random_harvest_fast_uma(a, b, c) random_harvest_fast(a, b, c)
176 #else /* !defined(RANDOM_ENABLE_UMA) */
177 #define random_harvest_fast_uma(a, b, c) do {} while (0)
178 #endif /* defined(RANDOM_ENABLE_UMA) */
179
180 #if defined(RANDOM_ENABLE_ETHER)
181 #define random_harvest_queue_ether(a, b) random_harvest_queue(a, b, RANDOM_NET_ETHER)
182 #else /* !defined(RANDOM_ENABLE_ETHER) */
183 #define random_harvest_queue_ether(a, b) do {} while (0)
184 #endif /* defined(RANDOM_ENABLE_ETHER) */
185
186
187 #endif /* _KERNEL */
188
189 #define GRND_NONBLOCK 0x1
190 #define GRND_RANDOM 0x2
191
192 __BEGIN_DECLS
193 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
194 __END_DECLS
195
196 #endif /* _SYS_RANDOM_H_ */
Cache object: 9a4c36ec7f14dce1335dcffb30e1536d
|