xref: /linux-6.15/include/linux/random.h (revision e5a52fd2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * include/linux/random.h
4  *
5  * Include file for the random number generator.
6  */
7 #ifndef _LINUX_RANDOM_H
8 #define _LINUX_RANDOM_H
9 
10 #include <linux/bug.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/once.h>
14 #include <asm/percpu.h>
15 
16 #include <uapi/linux/random.h>
17 
18 struct random_ready_callback {
19 	struct list_head list;
20 	void (*func)(struct random_ready_callback *rdy);
21 	struct module *owner;
22 };
23 
24 extern void add_device_randomness(const void *, unsigned int);
25 extern void add_bootloader_randomness(const void *, unsigned int);
26 
27 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
28 static inline void add_latent_entropy(void)
29 {
30 	add_device_randomness((const void *)&latent_entropy,
31 			      sizeof(latent_entropy));
32 }
33 #else
34 static inline void add_latent_entropy(void) {}
35 #endif
36 
37 extern void add_input_randomness(unsigned int type, unsigned int code,
38 				 unsigned int value) __latent_entropy;
39 extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
40 
41 extern void get_random_bytes(void *buf, int nbytes);
42 extern int wait_for_random_bytes(void);
43 extern int __init rand_initialize(void);
44 extern bool rng_is_initialized(void);
45 extern int add_random_ready_callback(struct random_ready_callback *rdy);
46 extern void del_random_ready_callback(struct random_ready_callback *rdy);
47 extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
48 
49 #ifndef MODULE
50 extern const struct file_operations random_fops, urandom_fops;
51 #endif
52 
53 u32 get_random_u32(void);
54 u64 get_random_u64(void);
55 static inline unsigned int get_random_int(void)
56 {
57 	return get_random_u32();
58 }
59 static inline unsigned long get_random_long(void)
60 {
61 #if BITS_PER_LONG == 64
62 	return get_random_u64();
63 #else
64 	return get_random_u32();
65 #endif
66 }
67 
68 /*
69  * On 64-bit architectures, protect against non-terminated C string overflows
70  * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
71  */
72 #ifdef CONFIG_64BIT
73 # ifdef __LITTLE_ENDIAN
74 #  define CANARY_MASK 0xffffffffffffff00UL
75 # else /* big endian, 64 bits: */
76 #  define CANARY_MASK 0x00ffffffffffffffUL
77 # endif
78 #else /* 32 bits: */
79 # define CANARY_MASK 0xffffffffUL
80 #endif
81 
82 static inline unsigned long get_random_canary(void)
83 {
84 	unsigned long val = get_random_long();
85 
86 	return val & CANARY_MASK;
87 }
88 
89 /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
90  * Returns the result of the call to wait_for_random_bytes. */
91 static inline int get_random_bytes_wait(void *buf, int nbytes)
92 {
93 	int ret = wait_for_random_bytes();
94 	get_random_bytes(buf, nbytes);
95 	return ret;
96 }
97 
98 #define declare_get_random_var_wait(var) \
99 	static inline int get_random_ ## var ## _wait(var *out) { \
100 		int ret = wait_for_random_bytes(); \
101 		if (unlikely(ret)) \
102 			return ret; \
103 		*out = get_random_ ## var(); \
104 		return 0; \
105 	}
106 declare_get_random_var_wait(u32)
107 declare_get_random_var_wait(u64)
108 declare_get_random_var_wait(int)
109 declare_get_random_var_wait(long)
110 #undef declare_get_random_var
111 
112 unsigned long randomize_page(unsigned long start, unsigned long range);
113 
114 u32 prandom_u32(void);
115 void prandom_bytes(void *buf, size_t nbytes);
116 void prandom_seed(u32 seed);
117 void prandom_reseed_late(void);
118 
119 struct rnd_state {
120 	__u32 s1, s2, s3, s4;
121 };
122 
123 DECLARE_PER_CPU(struct rnd_state, net_rand_state);
124 
125 u32 prandom_u32_state(struct rnd_state *state);
126 void prandom_bytes_state(struct rnd_state *state, void *buf, size_t nbytes);
127 void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state);
128 
129 #define prandom_init_once(pcpu_state)			\
130 	DO_ONCE(prandom_seed_full_state, (pcpu_state))
131 
132 /**
133  * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro)
134  * @ep_ro: right open interval endpoint
135  *
136  * Returns a pseudo-random number that is in interval [0, ep_ro). Note
137  * that the result depends on PRNG being well distributed in [0, ~0U]
138  * u32 space. Here we use maximally equidistributed combined Tausworthe
139  * generator, that is, prandom_u32(). This is useful when requesting a
140  * random index of an array containing ep_ro elements, for example.
141  *
142  * Returns: pseudo-random number in interval [0, ep_ro)
143  */
144 static inline u32 prandom_u32_max(u32 ep_ro)
145 {
146 	return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
147 }
148 
149 /*
150  * Handle minimum values for seeds
151  */
152 static inline u32 __seed(u32 x, u32 m)
153 {
154 	return (x < m) ? x + m : x;
155 }
156 
157 /**
158  * prandom_seed_state - set seed for prandom_u32_state().
159  * @state: pointer to state structure to receive the seed.
160  * @seed: arbitrary 64-bit value to use as a seed.
161  */
162 static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
163 {
164 	u32 i = (seed >> 32) ^ (seed << 10) ^ seed;
165 
166 	state->s1 = __seed(i,   2U);
167 	state->s2 = __seed(i,   8U);
168 	state->s3 = __seed(i,  16U);
169 	state->s4 = __seed(i, 128U);
170 }
171 
172 #ifdef CONFIG_ARCH_RANDOM
173 # include <asm/archrandom.h>
174 #else
175 static inline bool __must_check arch_get_random_long(unsigned long *v)
176 {
177 	return false;
178 }
179 static inline bool __must_check arch_get_random_int(unsigned int *v)
180 {
181 	return false;
182 }
183 static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
184 {
185 	return false;
186 }
187 static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
188 {
189 	return false;
190 }
191 #endif
192 
193 /*
194  * Called from the boot CPU during startup; not valid to call once
195  * secondary CPUs are up and preemption is possible.
196  */
197 #ifndef arch_get_random_seed_long_early
198 static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
199 {
200 	WARN_ON(system_state != SYSTEM_BOOTING);
201 	return arch_get_random_seed_long(v);
202 }
203 #endif
204 
205 #ifndef arch_get_random_long_early
206 static inline bool __init arch_get_random_long_early(unsigned long *v)
207 {
208 	WARN_ON(system_state != SYSTEM_BOOTING);
209 	return arch_get_random_long(v);
210 }
211 #endif
212 
213 /* Pseudo random number generator from numerical recipes. */
214 static inline u32 next_pseudo_random32(u32 seed)
215 {
216 	return seed * 1664525 + 1013904223;
217 }
218 
219 #endif /* _LINUX_RANDOM_H */
220