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