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 void add_device_randomness(const void *buf, size_t len);
16 void __init add_bootloader_randomness(const void *buf, size_t len);
17 void add_input_randomness(unsigned int type, unsigned int code,
18 unsigned int value) __latent_entropy;
19 void add_interrupt_randomness(int irq) __latent_entropy;
20 void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
22 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
23 static inline void add_latent_entropy(void)
25 add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
28 static inline void add_latent_entropy(void) { }
31 #if IS_ENABLED(CONFIG_VMGENID)
32 void add_vmfork_randomness(const void *unique_vm_id, size_t len);
33 int register_random_vmfork_notifier(struct notifier_block *nb);
34 int unregister_random_vmfork_notifier(struct notifier_block *nb);
36 static inline int register_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
37 static inline int unregister_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
40 void get_random_bytes(void *buf, size_t len);
41 u8 get_random_u8(void);
42 u16 get_random_u16(void);
43 u32 get_random_u32(void);
44 u64 get_random_u64(void);
45 static inline unsigned int get_random_int(void)
47 return get_random_u32();
49 static inline unsigned long get_random_long(void)
51 #if BITS_PER_LONG == 64
52 return get_random_u64();
54 return get_random_u32();
59 * On 64-bit architectures, protect against non-terminated C string overflows
60 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
63 # ifdef __LITTLE_ENDIAN
64 # define CANARY_MASK 0xffffffffffffff00UL
65 # else /* big endian, 64 bits: */
66 # define CANARY_MASK 0x00ffffffffffffffUL
69 # define CANARY_MASK 0xffffffffUL
72 static inline unsigned long get_random_canary(void)
74 return get_random_long() & CANARY_MASK;
77 void __init random_init_early(const char *command_line);
78 void __init random_init(void);
79 bool rng_is_initialized(void);
80 int wait_for_random_bytes(void);
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(name, ret_type) \
92 static inline int get_random_ ## name ## _wait(ret_type *out) { \
93 int ret = wait_for_random_bytes(); \
96 *out = get_random_ ## name(); \
99 declare_get_random_var_wait(u8, u8)
100 declare_get_random_var_wait(u16, u16)
101 declare_get_random_var_wait(u32, u32)
102 declare_get_random_var_wait(u64, u32)
103 declare_get_random_var_wait(int, unsigned int)
104 declare_get_random_var_wait(long, unsigned long)
105 #undef declare_get_random_var
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 #include <asm/archrandom.h>
117 * Called from the boot CPU during startup; not valid to call once
118 * secondary CPUs are up and preemption is possible.
120 #ifndef arch_get_random_seed_longs_early
121 static inline size_t __init arch_get_random_seed_longs_early(unsigned long *v, size_t max_longs)
123 WARN_ON(system_state != SYSTEM_BOOTING);
124 return arch_get_random_seed_longs(v, max_longs);
128 #ifndef arch_get_random_longs_early
129 static inline bool __init arch_get_random_longs_early(unsigned long *v, size_t max_longs)
131 WARN_ON(system_state != SYSTEM_BOOTING);
132 return arch_get_random_longs(v, max_longs);
137 int random_prepare_cpu(unsigned int cpu);
138 int random_online_cpu(unsigned int cpu);
142 extern const struct file_operations random_fops, urandom_fops;
145 #endif /* _LINUX_RANDOM_H */