14eeb4f04SMark Murray /*- 2c4e20cadSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3c4e20cadSPedro F. Giffuni * 4150890b0SMark Murray * Copyright (c) 2000-2015, 2017 Mark R. V. Murray 54eeb4f04SMark Murray * All rights reserved. 61bb2d314SMark Murray * 71bb2d314SMark Murray * Redistribution and use in source and binary forms, with or without 81bb2d314SMark Murray * modification, are permitted provided that the following conditions 91bb2d314SMark Murray * are met: 101bb2d314SMark Murray * 1. Redistributions of source code must retain the above copyright 114eeb4f04SMark Murray * notice, this list of conditions and the following disclaimer 124eeb4f04SMark Murray * in this position and unchanged. 131bb2d314SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 141bb2d314SMark Murray * notice, this list of conditions and the following disclaimer in the 151bb2d314SMark Murray * documentation and/or other materials provided with the distribution. 161bb2d314SMark Murray * 174eeb4f04SMark Murray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 184eeb4f04SMark Murray * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 194eeb4f04SMark Murray * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 204eeb4f04SMark Murray * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 214eeb4f04SMark Murray * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 224eeb4f04SMark Murray * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 234eeb4f04SMark Murray * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244eeb4f04SMark Murray * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254eeb4f04SMark Murray * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 264eeb4f04SMark Murray * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271bb2d314SMark Murray * 284eeb4f04SMark Murray * $FreeBSD$ 291bb2d314SMark Murray */ 301bb2d314SMark Murray 31a3b693c9SBruce Evans #ifndef _SYS_RANDOM_H_ 32a3b693c9SBruce Evans #define _SYS_RANDOM_H_ 331bb2d314SMark Murray 34707d98feSEd Schouten #include <sys/types.h> 35707d98feSEd Schouten 36e9ac2743SConrad Meyer #ifdef _KERNEL 37e9ac2743SConrad Meyer 38e866d8f0SMark Murray #if !defined(KLD_MODULE) 39646041a8SMark Murray #if defined(RANDOM_LOADABLE) && defined(RANDOM_YARROW) 40646041a8SMark Murray #error "Cannot define both RANDOM_LOADABLE and RANDOM_YARROW" 41646041a8SMark Murray #endif 42e866d8f0SMark Murray #endif 43646041a8SMark Murray 44707d98feSEd Schouten struct uio; 45707d98feSEd Schouten 46646041a8SMark Murray #if defined(DEV_RANDOM) 47d1b06863SMark Murray u_int read_random(void *, u_int); 48707d98feSEd Schouten int read_random_uio(struct uio *, bool); 49646041a8SMark Murray #else 50646041a8SMark Murray static __inline int 51646041a8SMark Murray read_random_uio(void *a __unused, u_int b __unused) 52646041a8SMark Murray { 53646041a8SMark Murray return (0); 54646041a8SMark Murray } 55646041a8SMark Murray static __inline u_int 56646041a8SMark Murray read_random(void *a __unused, u_int b __unused) 57646041a8SMark Murray { 58646041a8SMark Murray return (0); 59646041a8SMark Murray } 60646041a8SMark Murray #endif 611bb2d314SMark Murray 62a9befd40SRobert Watson /* 63f04c11c3SConrad Meyer * Note: if you add or remove members of random_entropy_source, remember to 64f04c11c3SConrad Meyer * also update the strings in the static array random_source_descr[] in 65f04c11c3SConrad Meyer * random_harvestq.c. 6610cb2424SMark Murray * 6710cb2424SMark Murray * NOTE: complain loudly to markm@ or on the lists if this enum gets more than 32 6810cb2424SMark Murray * distinct values (0-31)! ENTROPYSOURCE may be == 32, but not > 32. 69a9befd40SRobert Watson */ 7010cb2424SMark Murray enum random_entropy_source { 71e1199601SMark Murray RANDOM_START = 0, 721a3c1f06SMark Murray RANDOM_CACHED = 0, 7310cb2424SMark Murray /* Environmental sources */ 748cc20a46SMark Murray RANDOM_ATTACH, 75e1199601SMark Murray RANDOM_KEYBOARD, 76e1199601SMark Murray RANDOM_MOUSE, 77c495c935SMark Murray RANDOM_NET_TUN, 78c495c935SMark Murray RANDOM_NET_ETHER, 79c495c935SMark Murray RANDOM_NET_NG, 80e1199601SMark Murray RANDOM_INTERRUPT, 81c495c935SMark Murray RANDOM_SWI, 82d1b06863SMark Murray RANDOM_FS_ATIME, 83e866d8f0SMark Murray RANDOM_UMA, /* Special!! UMA/SLAB Allocator */ 84e866d8f0SMark Murray RANDOM_ENVIRONMENTAL_END = RANDOM_UMA, 85d1b06863SMark Murray /* Fast hardware random-number sources from here on. */ 86095db7e6SConrad Meyer RANDOM_PURE_START, 87095db7e6SConrad Meyer RANDOM_PURE_OCTEON = RANDOM_PURE_START, 88f02e47dcSMark Murray RANDOM_PURE_SAFE, 89f02e47dcSMark Murray RANDOM_PURE_GLXSB, 90f02e47dcSMark Murray RANDOM_PURE_UBSEC, 91f02e47dcSMark Murray RANDOM_PURE_HIFN, 92f02e47dcSMark Murray RANDOM_PURE_RDRAND, 93f02e47dcSMark Murray RANDOM_PURE_NEHEMIAH, 9469160622SDag-Erling Smørgrav RANDOM_PURE_RNDTEST, 9510c40180SBryan Venteicher RANDOM_PURE_VIRTIO, 96a1acc06fSMark Murray RANDOM_PURE_BROADCOM, 97e1700905SConrad Meyer RANDOM_PURE_CCP, 98*b14959daSJustin Hibbits RANDOM_PURE_DARN, 99e1199601SMark Murray ENTROPYSOURCE 100e1199601SMark Murray }; 101d1b06863SMark Murray 102d1b06863SMark Murray #define RANDOM_HARVEST_EVERYTHING_MASK ((1 << (RANDOM_ENVIRONMENTAL_END + 1)) - 1) 103095db7e6SConrad Meyer #define RANDOM_HARVEST_PURE_MASK (((1 << ENTROPYSOURCE) - 1) & (-1UL << RANDOM_PURE_START)) 104d1b06863SMark Murray 105150890b0SMark Murray #define RANDOM_LEGACY_BOOT_ENTROPY_MODULE "/boot/entropy" 106150890b0SMark Murray #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache" 107150890b0SMark Murray #define RANDOM_CACHED_SKIP_START 256 108150890b0SMark Murray 109646041a8SMark Murray #if defined(DEV_RANDOM) 110a6bc59f2SMatt Macy extern u_int hc_source_mask; 111a6bc59f2SMatt Macy void random_harvest_queue_(const void *, u_int, u_int, enum random_entropy_source); 112a6bc59f2SMatt Macy void random_harvest_fast_(const void *, u_int, u_int); 113a6bc59f2SMatt Macy void random_harvest_direct_(const void *, u_int, u_int, enum random_entropy_source); 114a6bc59f2SMatt Macy 115a6bc59f2SMatt Macy static __inline void 116a6bc59f2SMatt Macy random_harvest_queue(const void *entropy, u_int size, u_int bits, 117a6bc59f2SMatt Macy enum random_entropy_source origin) 118a6bc59f2SMatt Macy { 119a6bc59f2SMatt Macy 120a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin)) 121a6bc59f2SMatt Macy random_harvest_queue_(entropy, size, bits, origin); 122a6bc59f2SMatt Macy } 123a6bc59f2SMatt Macy 124a6bc59f2SMatt Macy static __inline void 125a6bc59f2SMatt Macy random_harvest_fast(const void *entropy, u_int size, u_int bits, 126a6bc59f2SMatt Macy enum random_entropy_source origin) 127a6bc59f2SMatt Macy { 128a6bc59f2SMatt Macy 129a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin)) 130a6bc59f2SMatt Macy random_harvest_fast_(entropy, size, bits); 131a6bc59f2SMatt Macy } 132a6bc59f2SMatt Macy 133a6bc59f2SMatt Macy static __inline void 134a6bc59f2SMatt Macy random_harvest_direct(const void *entropy, u_int size, u_int bits, 135a6bc59f2SMatt Macy enum random_entropy_source origin) 136a6bc59f2SMatt Macy { 137a6bc59f2SMatt Macy 138a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin)) 139a6bc59f2SMatt Macy random_harvest_direct_(entropy, size, bits, origin); 140a6bc59f2SMatt Macy } 141a6bc59f2SMatt Macy 142095db7e6SConrad Meyer void random_harvest_register_source(enum random_entropy_source); 143095db7e6SConrad Meyer void random_harvest_deregister_source(enum random_entropy_source); 144646041a8SMark Murray #else 145646041a8SMark Murray #define random_harvest_queue(a, b, c, d) do {} while (0) 146646041a8SMark Murray #define random_harvest_fast(a, b, c, d) do {} while (0) 147646041a8SMark Murray #define random_harvest_direct(a, b, c, d) do {} while (0) 148095db7e6SConrad Meyer #define random_harvest_register_source(a) do {} while (0) 149095db7e6SConrad Meyer #define random_harvest_deregister_source(a) do {} while (0) 150646041a8SMark Murray #endif 15114636c3bSMark Murray 152e866d8f0SMark Murray #if defined(RANDOM_ENABLE_UMA) 153e866d8f0SMark Murray #define random_harvest_fast_uma(a, b, c, d) random_harvest_fast(a, b, c, d) 154e866d8f0SMark Murray #else /* !defined(RANDOM_ENABLE_UMA) */ 155e866d8f0SMark Murray #define random_harvest_fast_uma(a, b, c, d) do {} while (0) 156e866d8f0SMark Murray #endif /* defined(RANDOM_ENABLE_UMA) */ 157e866d8f0SMark Murray 158a6bc59f2SMatt Macy #if defined(RANDOM_ENABLE_ETHER) 159a6bc59f2SMatt Macy #define random_harvest_queue_ether(a, b, c) random_harvest_queue(a, b, c, RANDOM_NET_ETHER) 160a6bc59f2SMatt Macy #else /* !defined(RANDOM_ENABLE_ETHER) */ 161a6bc59f2SMatt Macy #define random_harvest_queue_ether(a, b, c) do {} while (0) 162a6bc59f2SMatt Macy #endif /* defined(RANDOM_ENABLE_ETHER) */ 163a6bc59f2SMatt Macy 164a6bc59f2SMatt Macy 1654a7cdfd7SMark Murray #endif /* _KERNEL */ 166da3fb6b4SMark Murray 167e9ac2743SConrad Meyer #define GRND_NONBLOCK 0x1 168e9ac2743SConrad Meyer #define GRND_RANDOM 0x2 169e9ac2743SConrad Meyer ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); 170e9ac2743SConrad Meyer 1714eeb4f04SMark Murray #endif /* _SYS_RANDOM_H_ */ 172