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 38707d98feSEd Schouten struct uio; 39707d98feSEd Schouten 40*3ee1d5bbSConrad Meyer /* 41*3ee1d5bbSConrad Meyer * In the loadable random world, there are set of dangling pointers left in the 42*3ee1d5bbSConrad Meyer * core kernel: 43*3ee1d5bbSConrad Meyer * * read_random, read_random_uio, is_random_seeded are function pointers, 44*3ee1d5bbSConrad Meyer * rather than functions. 45*3ee1d5bbSConrad Meyer * * p_random_alg_context is a true pointer in loadable random kernels. 46*3ee1d5bbSConrad Meyer * 47*3ee1d5bbSConrad Meyer * These are initialized at SI_SUB_RANDOM:SI_ORDER_SECOND during boot. The 48*3ee1d5bbSConrad Meyer * read-type pointers are initialized by random_alg_context_init() in 49*3ee1d5bbSConrad Meyer * randomdev.c and p_random_alg_context in the algorithm, e.g., fortuna.c's 50*3ee1d5bbSConrad Meyer * random_fortuna_init_alg(). The nice thing about function pointers is they 51*3ee1d5bbSConrad Meyer * have a similar calling convention to ordinary functions. 52*3ee1d5bbSConrad Meyer * 53*3ee1d5bbSConrad Meyer * (In !loadable, the read_random, etc, routines are just plain functions; 54*3ee1d5bbSConrad Meyer * p_random_alg_context is a macro for the public visibility 55*3ee1d5bbSConrad Meyer * &random_alg_context.) 56*3ee1d5bbSConrad Meyer */ 57*3ee1d5bbSConrad Meyer #if defined(RANDOM_LOADABLE) 58*3ee1d5bbSConrad Meyer extern void (*_read_random)(void *, u_int); 59*3ee1d5bbSConrad Meyer extern int (*_read_random_uio)(struct uio *, bool); 60*3ee1d5bbSConrad Meyer extern bool (*_is_random_seeded)(void); 61*3ee1d5bbSConrad Meyer #define read_random(a, b) (*_read_random)(a, b) 62*3ee1d5bbSConrad Meyer #define read_random_uio(a, b) (*_read_random_uio)(a, b) 63*3ee1d5bbSConrad Meyer #define is_random_seeded() (*_is_random_seeded)() 64*3ee1d5bbSConrad Meyer #else 6513774e82SConrad Meyer void read_random(void *, u_int); 66707d98feSEd Schouten int read_random_uio(struct uio *, bool); 67f3d2512dSConrad Meyer bool is_random_seeded(void); 68*3ee1d5bbSConrad Meyer #endif 691bb2d314SMark Murray 70a9befd40SRobert Watson /* 71f04c11c3SConrad Meyer * Note: if you add or remove members of random_entropy_source, remember to 72f04c11c3SConrad Meyer * also update the strings in the static array random_source_descr[] in 73f04c11c3SConrad Meyer * random_harvestq.c. 74a9befd40SRobert Watson */ 7510cb2424SMark Murray enum random_entropy_source { 76e1199601SMark Murray RANDOM_START = 0, 771a3c1f06SMark Murray RANDOM_CACHED = 0, 7810cb2424SMark Murray /* Environmental sources */ 798cc20a46SMark Murray RANDOM_ATTACH, 80e1199601SMark Murray RANDOM_KEYBOARD, 81e1199601SMark Murray RANDOM_MOUSE, 82c495c935SMark Murray RANDOM_NET_TUN, 83c495c935SMark Murray RANDOM_NET_ETHER, 84c495c935SMark Murray RANDOM_NET_NG, 85e1199601SMark Murray RANDOM_INTERRUPT, 86c495c935SMark Murray RANDOM_SWI, 87d1b06863SMark Murray RANDOM_FS_ATIME, 88e866d8f0SMark Murray RANDOM_UMA, /* Special!! UMA/SLAB Allocator */ 89e866d8f0SMark Murray RANDOM_ENVIRONMENTAL_END = RANDOM_UMA, 90d1b06863SMark Murray /* Fast hardware random-number sources from here on. */ 91095db7e6SConrad Meyer RANDOM_PURE_START, 92095db7e6SConrad Meyer RANDOM_PURE_OCTEON = RANDOM_PURE_START, 93f02e47dcSMark Murray RANDOM_PURE_SAFE, 94f02e47dcSMark Murray RANDOM_PURE_GLXSB, 95f02e47dcSMark Murray RANDOM_PURE_UBSEC, 96f02e47dcSMark Murray RANDOM_PURE_HIFN, 97f02e47dcSMark Murray RANDOM_PURE_RDRAND, 98f02e47dcSMark Murray RANDOM_PURE_NEHEMIAH, 9969160622SDag-Erling Smørgrav RANDOM_PURE_RNDTEST, 10010c40180SBryan Venteicher RANDOM_PURE_VIRTIO, 101a1acc06fSMark Murray RANDOM_PURE_BROADCOM, 102e1700905SConrad Meyer RANDOM_PURE_CCP, 103b14959daSJustin Hibbits RANDOM_PURE_DARN, 1044ee7d3b0SMarcin Wojtas RANDOM_PURE_TPM, 105e1199601SMark Murray ENTROPYSOURCE 106e1199601SMark Murray }; 107b0dee75eSConrad Meyer _Static_assert(ENTROPYSOURCE <= 32, 108b0dee75eSConrad Meyer "hardcoded assumption that values fit in a typical word-sized bitset"); 109d1b06863SMark Murray 110150890b0SMark Murray #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache" 111150890b0SMark Murray 112a6bc59f2SMatt Macy extern u_int hc_source_mask; 11319fa89e9SMark Murray void random_harvest_queue_(const void *, u_int, enum random_entropy_source); 11419fa89e9SMark Murray void random_harvest_fast_(const void *, u_int); 11519fa89e9SMark Murray void random_harvest_direct_(const void *, u_int, enum random_entropy_source); 116a6bc59f2SMatt Macy 117a6bc59f2SMatt Macy static __inline void 11819fa89e9SMark Murray random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin) 119a6bc59f2SMatt Macy { 120a6bc59f2SMatt Macy 121a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin)) 12219fa89e9SMark Murray random_harvest_queue_(entropy, size, origin); 123a6bc59f2SMatt Macy } 124a6bc59f2SMatt Macy 125a6bc59f2SMatt Macy static __inline void 12619fa89e9SMark Murray random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin) 127a6bc59f2SMatt Macy { 128a6bc59f2SMatt Macy 129a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin)) 13019fa89e9SMark Murray random_harvest_fast_(entropy, size); 131a6bc59f2SMatt Macy } 132a6bc59f2SMatt Macy 133a6bc59f2SMatt Macy static __inline void 13419fa89e9SMark Murray random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin) 135a6bc59f2SMatt Macy { 136a6bc59f2SMatt Macy 137a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin)) 13819fa89e9SMark Murray random_harvest_direct_(entropy, size, origin); 139a6bc59f2SMatt Macy } 140a6bc59f2SMatt Macy 141095db7e6SConrad Meyer void random_harvest_register_source(enum random_entropy_source); 142095db7e6SConrad Meyer void random_harvest_deregister_source(enum random_entropy_source); 14314636c3bSMark Murray 144e866d8f0SMark Murray #if defined(RANDOM_ENABLE_UMA) 14519fa89e9SMark Murray #define random_harvest_fast_uma(a, b, c) random_harvest_fast(a, b, c) 146e866d8f0SMark Murray #else /* !defined(RANDOM_ENABLE_UMA) */ 14719fa89e9SMark Murray #define random_harvest_fast_uma(a, b, c) do {} while (0) 148e866d8f0SMark Murray #endif /* defined(RANDOM_ENABLE_UMA) */ 149e866d8f0SMark Murray 150a6bc59f2SMatt Macy #if defined(RANDOM_ENABLE_ETHER) 15119fa89e9SMark Murray #define random_harvest_queue_ether(a, b) random_harvest_queue(a, b, RANDOM_NET_ETHER) 152a6bc59f2SMatt Macy #else /* !defined(RANDOM_ENABLE_ETHER) */ 15319fa89e9SMark Murray #define random_harvest_queue_ether(a, b) do {} while (0) 154a6bc59f2SMatt Macy #endif /* defined(RANDOM_ENABLE_ETHER) */ 155a6bc59f2SMatt Macy 156a6bc59f2SMatt Macy 1574a7cdfd7SMark Murray #endif /* _KERNEL */ 158da3fb6b4SMark Murray 159e9ac2743SConrad Meyer #define GRND_NONBLOCK 0x1 160e9ac2743SConrad Meyer #define GRND_RANDOM 0x2 1611fa054c1SConrad Meyer 1621fa054c1SConrad Meyer __BEGIN_DECLS 163e9ac2743SConrad Meyer ssize_t getrandom(void *buf, size_t buflen, unsigned int flags); 1641fa054c1SConrad Meyer __END_DECLS 165e9ac2743SConrad Meyer 1664eeb4f04SMark Murray #endif /* _SYS_RANDOM_H_ */ 167