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 *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);
21
22 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
add_latent_entropy(void)23 static inline void add_latent_entropy(void)
24 {
25 add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
26 }
27 #else
add_latent_entropy(void)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 len);
33 int register_random_vmfork_notifier(struct notifier_block *nb);
34 int unregister_random_vmfork_notifier(struct notifier_block *nb);
35 #else
register_random_vmfork_notifier(struct notifier_block * nb)36 static inline int register_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
unregister_random_vmfork_notifier(struct notifier_block * nb)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 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);
get_random_long(void)45 static inline unsigned long get_random_long(void)
46 {
47 #if BITS_PER_LONG == 64
48 return get_random_u64();
49 #else
50 return get_random_u32();
51 #endif
52 }
53
54 /*
55 * On 64-bit architectures, protect against non-terminated C string overflows
56 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
57 */
58 #ifdef CONFIG_64BIT
59 # ifdef __LITTLE_ENDIAN
60 # define CANARY_MASK 0xffffffffffffff00UL
61 # else /* big endian, 64 bits: */
62 # define CANARY_MASK 0x00ffffffffffffffUL
63 # endif
64 #else /* 32 bits: */
65 # define CANARY_MASK 0xffffffffUL
66 #endif
67
get_random_canary(void)68 static inline unsigned long get_random_canary(void)
69 {
70 return get_random_long() & CANARY_MASK;
71 }
72
73 void __init random_init_early(const char *command_line);
74 void __init random_init(void);
75 bool rng_is_initialized(void);
76 int wait_for_random_bytes(void);
77
78 /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
79 * Returns the result of the call to wait_for_random_bytes. */
get_random_bytes_wait(void * buf,size_t nbytes)80 static inline int get_random_bytes_wait(void *buf, size_t nbytes)
81 {
82 int ret = wait_for_random_bytes();
83 get_random_bytes(buf, nbytes);
84 return ret;
85 }
86
87 #define declare_get_random_var_wait(name, ret_type) \
88 static inline int get_random_ ## name ## _wait(ret_type *out) { \
89 int ret = wait_for_random_bytes(); \
90 if (unlikely(ret)) \
91 return ret; \
92 *out = get_random_ ## name(); \
93 return 0; \
94 }
declare_get_random_var_wait(u8,u8)95 declare_get_random_var_wait(u8, u8)
96 declare_get_random_var_wait(u16, u16)
97 declare_get_random_var_wait(u32, u32)
98 declare_get_random_var_wait(u64, u32)
99 declare_get_random_var_wait(long, unsigned long)
100 #undef declare_get_random_var
101
102 /*
103 * This is designed to be standalone for just prandom
104 * users, but for now we include it from <linux/random.h>
105 * for legacy reasons.
106 */
107 #include <linux/prandom.h>
108
109 #include <asm/archrandom.h>
110
111 /*
112 * Called from the boot CPU during startup; not valid to call once
113 * secondary CPUs are up and preemption is possible.
114 */
115 #ifndef arch_get_random_seed_longs_early
116 static inline size_t __init arch_get_random_seed_longs_early(unsigned long *v, size_t max_longs)
117 {
118 WARN_ON(system_state != SYSTEM_BOOTING);
119 return arch_get_random_seed_longs(v, max_longs);
120 }
121 #endif
122
123 #ifndef arch_get_random_longs_early
arch_get_random_longs_early(unsigned long * v,size_t max_longs)124 static inline bool __init arch_get_random_longs_early(unsigned long *v, size_t max_longs)
125 {
126 WARN_ON(system_state != SYSTEM_BOOTING);
127 return arch_get_random_longs(v, max_longs);
128 }
129 #endif
130
131 #ifdef CONFIG_SMP
132 int random_prepare_cpu(unsigned int cpu);
133 int random_online_cpu(unsigned int cpu);
134 #endif
135
136 #ifndef MODULE
137 extern const struct file_operations random_fops, urandom_fops;
138 #endif
139
140 #endif /* _LINUX_RANDOM_H */
141