xref: /linux-6.15/include/linux/random.h (revision 7782cfec)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef _LINUX_RANDOM_H
4 #define _LINUX_RANDOM_H
5 
6 #include <linux/bug.h>
7 #include <linux/kernel.h>
8 #include <linux/list.h>
9 #include <linux/once.h>
10 
11 #include <uapi/linux/random.h>
12 
13 struct notifier_block;
14 
15 void add_device_randomness(const void *, size_t);
16 void add_bootloader_randomness(const void *, size_t);
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 *buffer, size_t count, size_t entropy);
21 
22 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
23 static inline void add_latent_entropy(void)
24 {
25 	add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
26 }
27 #else
28 static inline void add_latent_entropy(void) { }
29 #endif
30 
31 #if IS_ENABLED(CONFIG_VMGENID)
32 void add_vmfork_randomness(const void *unique_vm_id, size_t size);
33 int register_random_vmfork_notifier(struct notifier_block *nb);
34 int unregister_random_vmfork_notifier(struct notifier_block *nb);
35 #else
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; }
38 #endif
39 
40 void get_random_bytes(void *buf, size_t nbytes);
41 size_t __must_check get_random_bytes_arch(void *buf, size_t nbytes);
42 u32 get_random_u32(void);
43 u64 get_random_u64(void);
44 static inline unsigned int get_random_int(void)
45 {
46 	return get_random_u32();
47 }
48 static inline unsigned long get_random_long(void)
49 {
50 #if BITS_PER_LONG == 64
51 	return get_random_u64();
52 #else
53 	return get_random_u32();
54 #endif
55 }
56 
57 /*
58  * On 64-bit architectures, protect against non-terminated C string overflows
59  * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
60  */
61 #ifdef CONFIG_64BIT
62 # ifdef __LITTLE_ENDIAN
63 #  define CANARY_MASK 0xffffffffffffff00UL
64 # else /* big endian, 64 bits: */
65 #  define CANARY_MASK 0x00ffffffffffffffUL
66 # endif
67 #else /* 32 bits: */
68 # define CANARY_MASK 0xffffffffUL
69 #endif
70 
71 static inline unsigned long get_random_canary(void)
72 {
73 	return get_random_long() & CANARY_MASK;
74 }
75 
76 unsigned long randomize_page(unsigned long start, unsigned long range);
77 
78 int __init random_init(const char *command_line);
79 bool rng_is_initialized(void);
80 int wait_for_random_bytes(void);
81 int register_random_ready_notifier(struct notifier_block *nb);
82 int unregister_random_ready_notifier(struct notifier_block *nb);
83 
84 /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
85  * Returns the result of the call to wait_for_random_bytes. */
86 static inline int get_random_bytes_wait(void *buf, size_t nbytes)
87 {
88 	int ret = wait_for_random_bytes();
89 	get_random_bytes(buf, nbytes);
90 	return ret;
91 }
92 
93 #define declare_get_random_var_wait(var) \
94 	static inline int get_random_ ## var ## _wait(var *out) { \
95 		int ret = wait_for_random_bytes(); \
96 		if (unlikely(ret)) \
97 			return ret; \
98 		*out = get_random_ ## var(); \
99 		return 0; \
100 	}
101 declare_get_random_var_wait(u32)
102 declare_get_random_var_wait(u64)
103 declare_get_random_var_wait(int)
104 declare_get_random_var_wait(long)
105 #undef declare_get_random_var
106 
107 /*
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.
111  */
112 #include <linux/prandom.h>
113 
114 #ifdef CONFIG_ARCH_RANDOM
115 # include <asm/archrandom.h>
116 #else
117 static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; }
118 static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; }
119 static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; }
120 static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; }
121 #endif
122 
123 /*
124  * Called from the boot CPU during startup; not valid to call once
125  * secondary CPUs are up and preemption is possible.
126  */
127 #ifndef arch_get_random_seed_long_early
128 static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
129 {
130 	WARN_ON(system_state != SYSTEM_BOOTING);
131 	return arch_get_random_seed_long(v);
132 }
133 #endif
134 
135 #ifndef arch_get_random_long_early
136 static inline bool __init arch_get_random_long_early(unsigned long *v)
137 {
138 	WARN_ON(system_state != SYSTEM_BOOTING);
139 	return arch_get_random_long(v);
140 }
141 #endif
142 
143 #ifdef CONFIG_SMP
144 int random_prepare_cpu(unsigned int cpu);
145 int random_online_cpu(unsigned int cpu);
146 #endif
147 
148 #ifndef MODULE
149 extern const struct file_operations random_fops, urandom_fops;
150 #endif
151 
152 #endif /* _LINUX_RANDOM_H */
153