xref: /freebsd/sys/sys/random.h (revision 312809fe7fefbc8d5caa2b59089a5d9266378057)
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 void read_random(void *, u_int);
41 int read_random_uio(struct uio *, bool);
42 bool is_random_seeded(void);
43 
44 /*
45  * Note: if you add or remove members of random_entropy_source, remember to
46  * also update the strings in the static array random_source_descr[] in
47  * random_harvestq.c.
48  */
49 enum random_entropy_source {
50 	RANDOM_START = 0,
51 	RANDOM_CACHED = 0,
52 	/* Environmental sources */
53 	RANDOM_ATTACH,
54 	RANDOM_KEYBOARD,
55 	RANDOM_MOUSE,
56 	RANDOM_NET_TUN,
57 	RANDOM_NET_ETHER,
58 	RANDOM_NET_NG,
59 	RANDOM_INTERRUPT,
60 	RANDOM_SWI,
61 	RANDOM_FS_ATIME,
62 	RANDOM_UMA,	/* Special!! UMA/SLAB Allocator */
63 	RANDOM_ENVIRONMENTAL_END = RANDOM_UMA,
64 	/* Fast hardware random-number sources from here on. */
65 	RANDOM_PURE_START,
66 	RANDOM_PURE_OCTEON = RANDOM_PURE_START,
67 	RANDOM_PURE_SAFE,
68 	RANDOM_PURE_GLXSB,
69 	RANDOM_PURE_UBSEC,
70 	RANDOM_PURE_HIFN,
71 	RANDOM_PURE_RDRAND,
72 	RANDOM_PURE_NEHEMIAH,
73 	RANDOM_PURE_RNDTEST,
74 	RANDOM_PURE_VIRTIO,
75 	RANDOM_PURE_BROADCOM,
76 	RANDOM_PURE_CCP,
77 	RANDOM_PURE_DARN,
78 	RANDOM_PURE_TPM,
79 	ENTROPYSOURCE
80 };
81 _Static_assert(ENTROPYSOURCE <= 32,
82     "hardcoded assumption that values fit in a typical word-sized bitset");
83 
84 #define RANDOM_CACHED_BOOT_ENTROPY_MODULE	"boot_entropy_cache"
85 
86 extern u_int hc_source_mask;
87 void random_harvest_queue_(const void *, u_int, enum random_entropy_source);
88 void random_harvest_fast_(const void *, u_int);
89 void random_harvest_direct_(const void *, u_int, enum random_entropy_source);
90 
91 static __inline void
92 random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin)
93 {
94 
95 	if (hc_source_mask & (1 << origin))
96 		random_harvest_queue_(entropy, size, origin);
97 }
98 
99 static __inline void
100 random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin)
101 {
102 
103 	if (hc_source_mask & (1 << origin))
104 		random_harvest_fast_(entropy, size);
105 }
106 
107 static __inline void
108 random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin)
109 {
110 
111 	if (hc_source_mask & (1 << origin))
112 		random_harvest_direct_(entropy, size, origin);
113 }
114 
115 void random_harvest_register_source(enum random_entropy_source);
116 void random_harvest_deregister_source(enum random_entropy_source);
117 
118 #if defined(RANDOM_ENABLE_UMA)
119 #define random_harvest_fast_uma(a, b, c)	random_harvest_fast(a, b, c)
120 #else /* !defined(RANDOM_ENABLE_UMA) */
121 #define random_harvest_fast_uma(a, b, c)	do {} while (0)
122 #endif /* defined(RANDOM_ENABLE_UMA) */
123 
124 #if defined(RANDOM_ENABLE_ETHER)
125 #define random_harvest_queue_ether(a, b)	random_harvest_queue(a, b, RANDOM_NET_ETHER)
126 #else /* !defined(RANDOM_ENABLE_ETHER) */
127 #define random_harvest_queue_ether(a, b)	do {} while (0)
128 #endif /* defined(RANDOM_ENABLE_ETHER) */
129 
130 
131 #endif /* _KERNEL */
132 
133 #define GRND_NONBLOCK	0x1
134 #define GRND_RANDOM	0x2
135 
136 __BEGIN_DECLS
137 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
138 __END_DECLS
139 
140 #endif /* _SYS_RANDOM_H_ */
141