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