xref: /f-stack/freebsd/sys/random.h (revision 8640edf1)
1a9643ea8Slogwang /*-
222ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
322ce4affSfengbojiang  *
422ce4affSfengbojiang  * Copyright (c) 2000-2015, 2017 Mark R. V. Murray
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer
12a9643ea8Slogwang  *    in this position and unchanged.
13a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
15a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
16a9643ea8Slogwang  *
17a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18a9643ea8Slogwang  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19a9643ea8Slogwang  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20a9643ea8Slogwang  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21a9643ea8Slogwang  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22a9643ea8Slogwang  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a9643ea8Slogwang  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a9643ea8Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a9643ea8Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26a9643ea8Slogwang  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a9643ea8Slogwang  *
28a9643ea8Slogwang  * $FreeBSD$
29a9643ea8Slogwang  */
30a9643ea8Slogwang 
31a9643ea8Slogwang #ifndef	_SYS_RANDOM_H_
32a9643ea8Slogwang #define	_SYS_RANDOM_H_
33a9643ea8Slogwang 
34a9643ea8Slogwang #include <sys/types.h>
35a9643ea8Slogwang 
3622ce4affSfengbojiang #ifdef _KERNEL
37a9643ea8Slogwang 
38a9643ea8Slogwang struct uio;
39a9643ea8Slogwang 
4022ce4affSfengbojiang /*
4122ce4affSfengbojiang  * In the loadable random world, there are set of dangling pointers left in the
4222ce4affSfengbojiang  * core kernel:
4322ce4affSfengbojiang  *   * read_random, read_random_uio, is_random_seeded are function pointers,
4422ce4affSfengbojiang  *     rather than functions.
4522ce4affSfengbojiang  *   * p_random_alg_context is a true pointer in loadable random kernels.
4622ce4affSfengbojiang  *
4722ce4affSfengbojiang  * These are initialized at SI_SUB_RANDOM:SI_ORDER_SECOND during boot.  The
4822ce4affSfengbojiang  * read-type pointers are initialized by random_alg_context_init() in
4922ce4affSfengbojiang  * randomdev.c and p_random_alg_context in the algorithm, e.g., fortuna.c's
5022ce4affSfengbojiang  * random_fortuna_init_alg().  The nice thing about function pointers is they
5122ce4affSfengbojiang  * have a similar calling convention to ordinary functions.
5222ce4affSfengbojiang  *
5322ce4affSfengbojiang  * (In !loadable, the read_random, etc, routines are just plain functions;
5422ce4affSfengbojiang  * p_random_alg_context is a macro for the public visibility
5522ce4affSfengbojiang  * &random_alg_context.)
5622ce4affSfengbojiang  */
5722ce4affSfengbojiang #if defined(RANDOM_LOADABLE)
5822ce4affSfengbojiang extern void (*_read_random)(void *, u_int);
5922ce4affSfengbojiang extern int (*_read_random_uio)(struct uio *, bool);
6022ce4affSfengbojiang extern bool (*_is_random_seeded)(void);
6122ce4affSfengbojiang #define	read_random(a, b)	(*_read_random)(a, b)
6222ce4affSfengbojiang #define	read_random_uio(a, b)	(*_read_random_uio)(a, b)
6322ce4affSfengbojiang #define	is_random_seeded()	(*_is_random_seeded)()
64a9643ea8Slogwang #else
6522ce4affSfengbojiang void read_random(void *, u_int);
6622ce4affSfengbojiang int read_random_uio(struct uio *, bool);
6722ce4affSfengbojiang bool is_random_seeded(void);
68a9643ea8Slogwang #endif
69a9643ea8Slogwang 
70a9643ea8Slogwang /*
7122ce4affSfengbojiang  * Note: if you add or remove members of random_entropy_source, remember to
7222ce4affSfengbojiang  * also update the strings in the static array random_source_descr[] in
7322ce4affSfengbojiang  * random_harvestq.c.
74a9643ea8Slogwang  */
75a9643ea8Slogwang enum random_entropy_source {
76a9643ea8Slogwang 	RANDOM_START = 0,
77a9643ea8Slogwang 	RANDOM_CACHED = 0,
78a9643ea8Slogwang 	/* Environmental sources */
79a9643ea8Slogwang 	RANDOM_ATTACH,
80a9643ea8Slogwang 	RANDOM_KEYBOARD,
81a9643ea8Slogwang 	RANDOM_MOUSE,
82a9643ea8Slogwang 	RANDOM_NET_TUN,
83a9643ea8Slogwang 	RANDOM_NET_ETHER,
84a9643ea8Slogwang 	RANDOM_NET_NG,
85a9643ea8Slogwang 	RANDOM_INTERRUPT,
86a9643ea8Slogwang 	RANDOM_SWI,
87a9643ea8Slogwang 	RANDOM_FS_ATIME,
88a9643ea8Slogwang 	RANDOM_UMA,	/* Special!! UMA/SLAB Allocator */
89a9643ea8Slogwang 	RANDOM_ENVIRONMENTAL_END = RANDOM_UMA,
90a9643ea8Slogwang 	/* Fast hardware random-number sources from here on. */
9122ce4affSfengbojiang 	RANDOM_PURE_START,
9222ce4affSfengbojiang 	RANDOM_PURE_OCTEON = RANDOM_PURE_START,
93a9643ea8Slogwang 	RANDOM_PURE_SAFE,
94a9643ea8Slogwang 	RANDOM_PURE_GLXSB,
95a9643ea8Slogwang 	RANDOM_PURE_HIFN,
96a9643ea8Slogwang 	RANDOM_PURE_RDRAND,
97a9643ea8Slogwang 	RANDOM_PURE_NEHEMIAH,
98a9643ea8Slogwang 	RANDOM_PURE_RNDTEST,
99a9643ea8Slogwang 	RANDOM_PURE_VIRTIO,
10022ce4affSfengbojiang 	RANDOM_PURE_BROADCOM,
10122ce4affSfengbojiang 	RANDOM_PURE_CCP,
10222ce4affSfengbojiang 	RANDOM_PURE_DARN,
10322ce4affSfengbojiang 	RANDOM_PURE_TPM,
10422ce4affSfengbojiang 	RANDOM_PURE_VMGENID,
105a9643ea8Slogwang 	ENTROPYSOURCE
106a9643ea8Slogwang };
10722ce4affSfengbojiang _Static_assert(ENTROPYSOURCE <= 32,
10822ce4affSfengbojiang     "hardcoded assumption that values fit in a typical word-sized bitset");
109a9643ea8Slogwang 
11022ce4affSfengbojiang #define RANDOM_CACHED_BOOT_ENTROPY_MODULE	"boot_entropy_cache"
111a9643ea8Slogwang 
112*8640edf1Sfengbojiang #ifndef FSTACK
11322ce4affSfengbojiang extern u_int hc_source_mask;
11422ce4affSfengbojiang void random_harvest_queue_(const void *, u_int, enum random_entropy_source);
11522ce4affSfengbojiang void random_harvest_fast_(const void *, u_int);
11622ce4affSfengbojiang void random_harvest_direct_(const void *, u_int, enum random_entropy_source);
11722ce4affSfengbojiang 
11822ce4affSfengbojiang static __inline void
random_harvest_queue(const void * entropy,u_int size,enum random_entropy_source origin)11922ce4affSfengbojiang random_harvest_queue(const void *entropy, u_int size, enum random_entropy_source origin)
12022ce4affSfengbojiang {
12122ce4affSfengbojiang 
12222ce4affSfengbojiang 	if (hc_source_mask & (1 << origin))
12322ce4affSfengbojiang 		random_harvest_queue_(entropy, size, origin);
12422ce4affSfengbojiang }
12522ce4affSfengbojiang 
12622ce4affSfengbojiang static __inline void
random_harvest_fast(const void * entropy,u_int size,enum random_entropy_source origin)12722ce4affSfengbojiang random_harvest_fast(const void *entropy, u_int size, enum random_entropy_source origin)
12822ce4affSfengbojiang {
12922ce4affSfengbojiang 
13022ce4affSfengbojiang 	if (hc_source_mask & (1 << origin))
13122ce4affSfengbojiang 		random_harvest_fast_(entropy, size);
13222ce4affSfengbojiang }
13322ce4affSfengbojiang 
13422ce4affSfengbojiang static __inline void
random_harvest_direct(const void * entropy,u_int size,enum random_entropy_source origin)13522ce4affSfengbojiang random_harvest_direct(const void *entropy, u_int size, enum random_entropy_source origin)
13622ce4affSfengbojiang {
13722ce4affSfengbojiang 
13822ce4affSfengbojiang 	if (hc_source_mask & (1 << origin))
13922ce4affSfengbojiang 		random_harvest_direct_(entropy, size, origin);
14022ce4affSfengbojiang }
141*8640edf1Sfengbojiang #else
142*8640edf1Sfengbojiang #define random_harvest_queue(a, b, c) do {} while (0)
143*8640edf1Sfengbojiang #define random_harvest_fast(a, b, c) do {} while (0)
144*8640edf1Sfengbojiang #define random_harvest_direct(a, b, c) do {} while (0)
145*8640edf1Sfengbojiang #endif
14622ce4affSfengbojiang 
14722ce4affSfengbojiang void random_harvest_register_source(enum random_entropy_source);
14822ce4affSfengbojiang void random_harvest_deregister_source(enum random_entropy_source);
149a9643ea8Slogwang 
150a9643ea8Slogwang #if defined(RANDOM_ENABLE_UMA)
15122ce4affSfengbojiang #define random_harvest_fast_uma(a, b, c)	random_harvest_fast(a, b, c)
152a9643ea8Slogwang #else /* !defined(RANDOM_ENABLE_UMA) */
15322ce4affSfengbojiang #define random_harvest_fast_uma(a, b, c)	do {} while (0)
154a9643ea8Slogwang #endif /* defined(RANDOM_ENABLE_UMA) */
155a9643ea8Slogwang 
15622ce4affSfengbojiang #if defined(RANDOM_ENABLE_ETHER)
15722ce4affSfengbojiang #define random_harvest_queue_ether(a, b)	random_harvest_queue(a, b, RANDOM_NET_ETHER)
15822ce4affSfengbojiang #else /* !defined(RANDOM_ENABLE_ETHER) */
15922ce4affSfengbojiang #define random_harvest_queue_ether(a, b)	do {} while (0)
16022ce4affSfengbojiang #endif /* defined(RANDOM_ENABLE_ETHER) */
16122ce4affSfengbojiang 
162a9643ea8Slogwang #endif /* _KERNEL */
163a9643ea8Slogwang 
16422ce4affSfengbojiang #define GRND_NONBLOCK	0x1
16522ce4affSfengbojiang #define GRND_RANDOM	0x2
16622ce4affSfengbojiang #define GRND_INSECURE	0x4
16722ce4affSfengbojiang 
16822ce4affSfengbojiang __BEGIN_DECLS
16922ce4affSfengbojiang ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
17022ce4affSfengbojiang __END_DECLS
17122ce4affSfengbojiang 
172a9643ea8Slogwang #endif /* _SYS_RANDOM_H_ */
173