2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2015, 2017 Mark R. V. Murray
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #ifndef _SYS_RANDOM_H_
32 #define _SYS_RANDOM_H_
34 #include <sys/types.h>
40 #if defined(DEV_RANDOM)
41 u_int read_random(void *, u_int);
42 int read_random_uio(struct uio *, bool);
45 read_random_uio(void *a __unused, u_int b __unused)
50 read_random(void *a __unused, u_int b __unused)
57 * Note: if you add or remove members of random_entropy_source, remember to
58 * also update the strings in the static array random_source_descr[] in
61 * NOTE: complain loudly to markm@ or on the lists if this enum gets more than 32
62 * distinct values (0-31)! ENTROPYSOURCE may be == 32, but not > 32.
64 enum random_entropy_source {
67 /* Environmental sources */
77 RANDOM_UMA, /* Special!! UMA/SLAB Allocator */
78 RANDOM_ENVIRONMENTAL_END = RANDOM_UMA,
79 /* Fast hardware random-number sources from here on. */
81 RANDOM_PURE_OCTEON = RANDOM_PURE_START,
96 #define RANDOM_HARVEST_EVERYTHING_MASK ((1 << (RANDOM_ENVIRONMENTAL_END + 1)) - 1)
97 #define RANDOM_HARVEST_PURE_MASK (((1 << ENTROPYSOURCE) - 1) & (-1UL << RANDOM_PURE_START))
99 #define RANDOM_LEGACY_BOOT_ENTROPY_MODULE "/boot/entropy"
100 #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache"
101 #define RANDOM_CACHED_SKIP_START 256
103 #if defined(DEV_RANDOM)
104 extern u_int hc_source_mask;
105 void random_harvest_queue_(const void *, u_int, enum random_entropy_source);
106 void random_harvest_fast_(const void *, u_int);
107 void random_harvest_direct_(const void *, u_int, enum random_entropy_source);
110 random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin)
113 if (hc_source_mask & (1 << origin))
114 random_harvest_queue_(entropy, size, origin);
118 random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin)
121 if (hc_source_mask & (1 << origin))
122 random_harvest_fast_(entropy, size);
126 random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin)
129 if (hc_source_mask & (1 << origin))
130 random_harvest_direct_(entropy, size, origin);
133 void random_harvest_register_source(enum random_entropy_source);
134 void random_harvest_deregister_source(enum random_entropy_source);
136 #define random_harvest_queue(a, b, c) do {} while (0)
137 #define random_harvest_fast(a, b, c) do {} while (0)
138 #define random_harvest_direct(a, b, c) do {} while (0)
139 #define random_harvest_register_source(a) do {} while (0)
140 #define random_harvest_deregister_source(a) do {} while (0)
143 #if defined(RANDOM_ENABLE_UMA)
144 #define random_harvest_fast_uma(a, b, c) random_harvest_fast(a, b, c)
145 #else /* !defined(RANDOM_ENABLE_UMA) */
146 #define random_harvest_fast_uma(a, b, c) do {} while (0)
147 #endif /* defined(RANDOM_ENABLE_UMA) */
149 #if defined(RANDOM_ENABLE_ETHER)
150 #define random_harvest_queue_ether(a, b) random_harvest_queue(a, b, RANDOM_NET_ETHER)
151 #else /* !defined(RANDOM_ENABLE_ETHER) */
152 #define random_harvest_queue_ether(a, b) do {} while (0)
153 #endif /* defined(RANDOM_ENABLE_ETHER) */
158 #define GRND_NONBLOCK 0x1
159 #define GRND_RANDOM 0x2
160 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
162 #endif /* _SYS_RANDOM_H_ */