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