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