14eeb4f04SMark Murray /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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 */
281bb2d314SMark Murray
29a3b693c9SBruce Evans #ifndef _SYS_RANDOM_H_
30a3b693c9SBruce Evans #define _SYS_RANDOM_H_
311bb2d314SMark Murray
32707d98feSEd Schouten #include <sys/types.h>
33707d98feSEd Schouten
34e9ac2743SConrad Meyer #ifdef _KERNEL
35e9ac2743SConrad Meyer
36707d98feSEd Schouten struct uio;
37707d98feSEd Schouten
383ee1d5bbSConrad Meyer /*
393ee1d5bbSConrad Meyer * In the loadable random world, there are set of dangling pointers left in the
403ee1d5bbSConrad Meyer * core kernel:
413ee1d5bbSConrad Meyer * * read_random, read_random_uio, is_random_seeded are function pointers,
423ee1d5bbSConrad Meyer * rather than functions.
433ee1d5bbSConrad Meyer * * p_random_alg_context is a true pointer in loadable random kernels.
443ee1d5bbSConrad Meyer *
453ee1d5bbSConrad Meyer * These are initialized at SI_SUB_RANDOM:SI_ORDER_SECOND during boot. The
463ee1d5bbSConrad Meyer * read-type pointers are initialized by random_alg_context_init() in
473ee1d5bbSConrad Meyer * randomdev.c and p_random_alg_context in the algorithm, e.g., fortuna.c's
483ee1d5bbSConrad Meyer * random_fortuna_init_alg(). The nice thing about function pointers is they
493ee1d5bbSConrad Meyer * have a similar calling convention to ordinary functions.
503ee1d5bbSConrad Meyer *
513ee1d5bbSConrad Meyer * (In !loadable, the read_random, etc, routines are just plain functions;
523ee1d5bbSConrad Meyer * p_random_alg_context is a macro for the public visibility
533ee1d5bbSConrad Meyer * &random_alg_context.)
543ee1d5bbSConrad Meyer */
553ee1d5bbSConrad Meyer #if defined(RANDOM_LOADABLE)
563ee1d5bbSConrad Meyer extern void (*_read_random)(void *, u_int);
573ee1d5bbSConrad Meyer extern int (*_read_random_uio)(struct uio *, bool);
583ee1d5bbSConrad Meyer extern bool (*_is_random_seeded)(void);
593ee1d5bbSConrad Meyer #define read_random(a, b) (*_read_random)(a, b)
603ee1d5bbSConrad Meyer #define read_random_uio(a, b) (*_read_random_uio)(a, b)
613ee1d5bbSConrad Meyer #define is_random_seeded() (*_is_random_seeded)()
623ee1d5bbSConrad Meyer #else
6313774e82SConrad Meyer void read_random(void *, u_int);
64707d98feSEd Schouten int read_random_uio(struct uio *, bool);
65f3d2512dSConrad Meyer bool is_random_seeded(void);
663ee1d5bbSConrad Meyer #endif
671bb2d314SMark Murray
68a9befd40SRobert Watson /*
69f04c11c3SConrad Meyer * Note: if you add or remove members of random_entropy_source, remember to
70f04c11c3SConrad Meyer * also update the strings in the static array random_source_descr[] in
71f04c11c3SConrad Meyer * random_harvestq.c.
72a9befd40SRobert Watson */
7310cb2424SMark Murray enum random_entropy_source {
74e1199601SMark Murray RANDOM_START = 0,
751a3c1f06SMark Murray RANDOM_CACHED = 0,
7610cb2424SMark Murray /* Environmental sources */
778cc20a46SMark Murray RANDOM_ATTACH,
78e1199601SMark Murray RANDOM_KEYBOARD,
79e1199601SMark Murray RANDOM_MOUSE,
80c495c935SMark Murray RANDOM_NET_TUN,
81c495c935SMark Murray RANDOM_NET_ETHER,
82c495c935SMark Murray RANDOM_NET_NG,
83e1199601SMark Murray RANDOM_INTERRUPT,
84c495c935SMark Murray RANDOM_SWI,
85d1b06863SMark Murray RANDOM_FS_ATIME,
86e866d8f0SMark Murray RANDOM_UMA, /* Special!! UMA/SLAB Allocator */
87642701abSKyle Evans RANDOM_CALLOUT,
88642701abSKyle Evans RANDOM_ENVIRONMENTAL_END = RANDOM_CALLOUT,
89d1b06863SMark Murray /* Fast hardware random-number sources from here on. */
90095db7e6SConrad Meyer RANDOM_PURE_START,
91095db7e6SConrad Meyer RANDOM_PURE_OCTEON = RANDOM_PURE_START,
92f02e47dcSMark Murray RANDOM_PURE_SAFE,
93f02e47dcSMark Murray RANDOM_PURE_GLXSB,
94f02e47dcSMark Murray RANDOM_PURE_HIFN,
95f02e47dcSMark Murray RANDOM_PURE_RDRAND,
96f02e47dcSMark Murray RANDOM_PURE_NEHEMIAH,
9769160622SDag-Erling Smørgrav RANDOM_PURE_RNDTEST,
9810c40180SBryan Venteicher RANDOM_PURE_VIRTIO,
99a1acc06fSMark Murray RANDOM_PURE_BROADCOM,
100e1700905SConrad Meyer RANDOM_PURE_CCP,
101b14959daSJustin Hibbits RANDOM_PURE_DARN,
1024ee7d3b0SMarcin Wojtas RANDOM_PURE_TPM,
103767991d2SConrad Meyer RANDOM_PURE_VMGENID,
1041492c8c0SAdrian Chadd RANDOM_PURE_QUALCOMM,
1059eecef05SAndrew Turner RANDOM_PURE_ARMV8,
106*b2f8b2dcSAndrew Turner RANDOM_PURE_ARM_TRNG,
107e1199601SMark Murray ENTROPYSOURCE
108e1199601SMark Murray };
109b0dee75eSConrad Meyer _Static_assert(ENTROPYSOURCE <= 32,
110b0dee75eSConrad Meyer "hardcoded assumption that values fit in a typical word-sized bitset");
111d1b06863SMark Murray
112150890b0SMark Murray #define RANDOM_CACHED_BOOT_ENTROPY_MODULE "boot_entropy_cache"
1135c73b3e0SColin Percival #define RANDOM_PLATFORM_BOOT_ENTROPY_MODULE "boot_entropy_platform"
114150890b0SMark Murray
115a6bc59f2SMatt Macy extern u_int hc_source_mask;
11619fa89e9SMark Murray void random_harvest_queue_(const void *, u_int, enum random_entropy_source);
11719fa89e9SMark Murray void random_harvest_fast_(const void *, u_int);
11819fa89e9SMark Murray void random_harvest_direct_(const void *, u_int, enum random_entropy_source);
119a6bc59f2SMatt Macy
120a6bc59f2SMatt Macy static __inline void
random_harvest_queue(const void * entropy,u_int size,enum random_entropy_source origin)12119fa89e9SMark Murray random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin)
122a6bc59f2SMatt Macy {
123a6bc59f2SMatt Macy
124a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin))
12519fa89e9SMark Murray random_harvest_queue_(entropy, size, origin);
126a6bc59f2SMatt Macy }
127a6bc59f2SMatt Macy
128a6bc59f2SMatt Macy static __inline void
random_harvest_fast(const void * entropy,u_int size,enum random_entropy_source origin)12919fa89e9SMark Murray random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin)
130a6bc59f2SMatt Macy {
131a6bc59f2SMatt Macy
132a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin))
13319fa89e9SMark Murray random_harvest_fast_(entropy, size);
134a6bc59f2SMatt Macy }
135a6bc59f2SMatt Macy
136a6bc59f2SMatt Macy static __inline void
random_harvest_direct(const void * entropy,u_int size,enum random_entropy_source origin)13719fa89e9SMark Murray random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin)
138a6bc59f2SMatt Macy {
139a6bc59f2SMatt Macy
140a6bc59f2SMatt Macy if (hc_source_mask & (1 << origin))
14119fa89e9SMark Murray random_harvest_direct_(entropy, size, origin);
142a6bc59f2SMatt Macy }
143a6bc59f2SMatt Macy
144095db7e6SConrad Meyer void random_harvest_register_source(enum random_entropy_source);
145095db7e6SConrad Meyer void random_harvest_deregister_source(enum random_entropy_source);
14614636c3bSMark Murray
147e866d8f0SMark Murray #if defined(RANDOM_ENABLE_UMA)
14819fa89e9SMark Murray #define random_harvest_fast_uma(a, b, c) random_harvest_fast(a, b, c)
149e866d8f0SMark Murray #else /* !defined(RANDOM_ENABLE_UMA) */
15019fa89e9SMark Murray #define random_harvest_fast_uma(a, b, c) do {} while (0)
151e866d8f0SMark Murray #endif /* defined(RANDOM_ENABLE_UMA) */
152e866d8f0SMark Murray
153a6bc59f2SMatt Macy #if defined(RANDOM_ENABLE_ETHER)
15419fa89e9SMark Murray #define random_harvest_queue_ether(a, b) random_harvest_queue(a, b, RANDOM_NET_ETHER)
155a6bc59f2SMatt Macy #else /* !defined(RANDOM_ENABLE_ETHER) */
15619fa89e9SMark Murray #define random_harvest_queue_ether(a, b) do {} while (0)
157a6bc59f2SMatt Macy #endif /* defined(RANDOM_ENABLE_ETHER) */
158a6bc59f2SMatt Macy
1594a7cdfd7SMark Murray #endif /* _KERNEL */
160da3fb6b4SMark Murray
161e9ac2743SConrad Meyer #define GRND_NONBLOCK 0x1
162e9ac2743SConrad Meyer #define GRND_RANDOM 0x2
16386def3dcSConrad Meyer #define GRND_INSECURE 0x4
1641fa054c1SConrad Meyer
165062d9380SKyle Evans #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
166062d9380SKyle Evans #include <ssp/random.h>
167062d9380SKyle Evans #endif
168062d9380SKyle Evans
1691fa054c1SConrad Meyer __BEGIN_DECLS
170e9ac2743SConrad Meyer ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
1711fa054c1SConrad Meyer __END_DECLS
172e9ac2743SConrad Meyer
1734eeb4f04SMark Murray #endif /* _SYS_RANDOM_H_ */
174