xref: /freebsd/sys/compat/linuxkpi/common/include/linux/random.h (revision 1dcd1a539275fe69173bfc4dfc32c963fc7dcdbe)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  * Copyright 2023 The FreeBSD Foundation
8  *
9  * Portions of this software was developed by Björn Zeeb
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice unmodified, this list of conditions, and the following
17  *    disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #ifndef _LINUXKPI_LINUX_RANDOM_H_
37 #define	_LINUXKPI_LINUX_RANDOM_H_
38 
39 #include <linux/types.h>
40 #include <sys/random.h>
41 #include <sys/libkern.h>
42 
43 static inline void
44 get_random_bytes(void *buf, int nbytes)
45 {
46 
47 	arc4random_buf(buf, nbytes);
48 }
49 
50 static inline u_int
51 get_random_int(void)
52 {
53 	u_int val;
54 
55 	get_random_bytes(&val, sizeof(val));
56 	return (val);
57 }
58 
59 #define	get_random_u32() get_random_int()
60 
61 /*
62  * See "Fast Random Integer Generation in an Interval" by Daniel Lemire
63  * [https://arxiv.org/pdf/1805.10941.pdf] for implementation insights.
64  */
65 static inline uint32_t
66 get_random_u32_inclusive(uint32_t floor, uint32_t ceil)
67 {
68 	uint64_t x;
69 	uint32_t t, v;
70 
71 	MPASS(ceil >= floor);
72 
73 	v = get_random_u32();
74 	t = ceil - floor + 1;
75 	x = (uint64_t)t * v;
76 	while (x < t)
77 		x = (uint64_t)t * get_random_u32();
78 	v = x >> 32;
79 
80 	return (floor + v);
81 }
82 
83 static inline u_long
84 get_random_long(void)
85 {
86 	u_long val;
87 
88 	get_random_bytes(&val, sizeof(val));
89 	return (val);
90 }
91 
92 static __inline uint32_t
93 prandom_u32(void)
94 {
95 	uint32_t val;
96 
97 	get_random_bytes(&val, sizeof(val));
98 	return (val);
99 }
100 
101 static inline u32
102 prandom_u32_max(u32 max)
103 {
104 	return (arc4random_uniform(max));
105 }
106 
107 #endif /* _LINUXKPI_LINUX_RANDOM_H_ */
108