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