1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _LINUX_RANDOM_H
4 #define _LINUX_RANDOM_H
7 #include <linux/kernel.h>
8 #include <linux/list.h>
9 #include <linux/once.h>
11 #include <uapi/linux/random.h>
13 struct notifier_block;
15 extern void add_device_randomness(const void *, size_t);
16 extern void add_bootloader_randomness(const void *, size_t);
18 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
19 static inline void add_latent_entropy(void)
21 add_device_randomness((const void *)&latent_entropy,
22 sizeof(latent_entropy));
25 static inline void add_latent_entropy(void) {}
28 extern void add_input_randomness(unsigned int type, unsigned int code,
29 unsigned int value) __latent_entropy;
30 extern void add_interrupt_randomness(int irq) __latent_entropy;
31 extern void add_hwgenerator_randomness(const void *buffer, size_t count,
34 extern void get_random_bytes(void *buf, size_t nbytes);
35 extern int wait_for_random_bytes(void);
36 extern int __init rand_initialize(void);
37 extern bool rng_is_initialized(void);
38 extern int register_random_ready_notifier(struct notifier_block *nb);
39 extern int unregister_random_ready_notifier(struct notifier_block *nb);
40 extern size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes);
43 extern const struct file_operations random_fops, urandom_fops;
46 u32 get_random_u32(void);
47 u64 get_random_u64(void);
48 static inline unsigned int get_random_int(void)
50 return get_random_u32();
52 static inline unsigned long get_random_long(void)
54 #if BITS_PER_LONG == 64
55 return get_random_u64();
57 return get_random_u32();
62 * On 64-bit architectures, protect against non-terminated C string overflows
63 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
66 # ifdef __LITTLE_ENDIAN
67 # define CANARY_MASK 0xffffffffffffff00UL
68 # else /* big endian, 64 bits: */
69 # define CANARY_MASK 0x00ffffffffffffffUL
72 # define CANARY_MASK 0xffffffffUL
75 static inline unsigned long get_random_canary(void)
77 unsigned long val = get_random_long();
79 return val & CANARY_MASK;
82 /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
83 * Returns the result of the call to wait_for_random_bytes. */
84 static inline int get_random_bytes_wait(void *buf, size_t nbytes)
86 int ret = wait_for_random_bytes();
87 get_random_bytes(buf, nbytes);
91 #define declare_get_random_var_wait(var) \
92 static inline int get_random_ ## var ## _wait(var *out) { \
93 int ret = wait_for_random_bytes(); \
96 *out = get_random_ ## var(); \
99 declare_get_random_var_wait(u32)
100 declare_get_random_var_wait(u64)
101 declare_get_random_var_wait(int)
102 declare_get_random_var_wait(long)
103 #undef declare_get_random_var
105 unsigned long randomize_page(unsigned long start, unsigned long range);
108 * This is designed to be standalone for just prandom
109 * users, but for now we include it from <linux/random.h>
110 * for legacy reasons.
112 #include <linux/prandom.h>
114 #ifdef CONFIG_ARCH_RANDOM
115 # include <asm/archrandom.h>
117 static inline bool __must_check arch_get_random_long(unsigned long *v)
121 static inline bool __must_check arch_get_random_int(unsigned int *v)
125 static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
129 static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
136 * Called from the boot CPU during startup; not valid to call once
137 * secondary CPUs are up and preemption is possible.
139 #ifndef arch_get_random_seed_long_early
140 static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
142 WARN_ON(system_state != SYSTEM_BOOTING);
143 return arch_get_random_seed_long(v);
147 #ifndef arch_get_random_long_early
148 static inline bool __init arch_get_random_long_early(unsigned long *v)
150 WARN_ON(system_state != SYSTEM_BOOTING);
151 return arch_get_random_long(v);
156 extern int random_prepare_cpu(unsigned int cpu);
157 extern int random_online_cpu(unsigned int cpu);
160 #endif /* _LINUX_RANDOM_H */