0001
0002
0003 #ifndef _LINUX_RANDOM_H
0004 #define _LINUX_RANDOM_H
0005
0006 #include <linux/bug.h>
0007 #include <linux/kernel.h>
0008 #include <linux/list.h>
0009 #include <linux/once.h>
0010
0011 #include <uapi/linux/random.h>
0012
0013 struct notifier_block;
0014
0015 void add_device_randomness(const void *buf, size_t len);
0016 void __init add_bootloader_randomness(const void *buf, size_t len);
0017 void add_input_randomness(unsigned int type, unsigned int code,
0018 unsigned int value) __latent_entropy;
0019 void add_interrupt_randomness(int irq) __latent_entropy;
0020 void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
0021
0022 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
0023 static inline void add_latent_entropy(void)
0024 {
0025 add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
0026 }
0027 #else
0028 static inline void add_latent_entropy(void) { }
0029 #endif
0030
0031 #if IS_ENABLED(CONFIG_VMGENID)
0032 void add_vmfork_randomness(const void *unique_vm_id, size_t len);
0033 int register_random_vmfork_notifier(struct notifier_block *nb);
0034 int unregister_random_vmfork_notifier(struct notifier_block *nb);
0035 #else
0036 static inline int register_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
0037 static inline int unregister_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
0038 #endif
0039
0040 void get_random_bytes(void *buf, size_t len);
0041 u32 get_random_u32(void);
0042 u64 get_random_u64(void);
0043 static inline unsigned int get_random_int(void)
0044 {
0045 return get_random_u32();
0046 }
0047 static inline unsigned long get_random_long(void)
0048 {
0049 #if BITS_PER_LONG == 64
0050 return get_random_u64();
0051 #else
0052 return get_random_u32();
0053 #endif
0054 }
0055
0056
0057
0058
0059
0060 #ifdef CONFIG_64BIT
0061 # ifdef __LITTLE_ENDIAN
0062 # define CANARY_MASK 0xffffffffffffff00UL
0063 # else
0064 # define CANARY_MASK 0x00ffffffffffffffUL
0065 # endif
0066 #else
0067 # define CANARY_MASK 0xffffffffUL
0068 #endif
0069
0070 static inline unsigned long get_random_canary(void)
0071 {
0072 return get_random_long() & CANARY_MASK;
0073 }
0074
0075 int __init random_init(const char *command_line);
0076 bool rng_is_initialized(void);
0077 int wait_for_random_bytes(void);
0078
0079
0080
0081 static inline int get_random_bytes_wait(void *buf, size_t nbytes)
0082 {
0083 int ret = wait_for_random_bytes();
0084 get_random_bytes(buf, nbytes);
0085 return ret;
0086 }
0087
0088 #define declare_get_random_var_wait(name, ret_type) \
0089 static inline int get_random_ ## name ## _wait(ret_type *out) { \
0090 int ret = wait_for_random_bytes(); \
0091 if (unlikely(ret)) \
0092 return ret; \
0093 *out = get_random_ ## name(); \
0094 return 0; \
0095 }
0096 declare_get_random_var_wait(u32, u32)
0097 declare_get_random_var_wait(u64, u32)
0098 declare_get_random_var_wait(int, unsigned int)
0099 declare_get_random_var_wait(long, unsigned long)
0100 #undef declare_get_random_var
0101
0102
0103
0104
0105
0106
0107 #include <linux/prandom.h>
0108
0109 #include <asm/archrandom.h>
0110
0111
0112
0113
0114
0115 #ifndef arch_get_random_seed_longs_early
0116 static inline size_t __init arch_get_random_seed_longs_early(unsigned long *v, size_t max_longs)
0117 {
0118 WARN_ON(system_state != SYSTEM_BOOTING);
0119 return arch_get_random_seed_longs(v, max_longs);
0120 }
0121 #endif
0122
0123 #ifndef arch_get_random_longs_early
0124 static inline bool __init arch_get_random_longs_early(unsigned long *v, size_t max_longs)
0125 {
0126 WARN_ON(system_state != SYSTEM_BOOTING);
0127 return arch_get_random_longs(v, max_longs);
0128 }
0129 #endif
0130
0131 #ifdef CONFIG_SMP
0132 int random_prepare_cpu(unsigned int cpu);
0133 int random_online_cpu(unsigned int cpu);
0134 #endif
0135
0136 #ifndef MODULE
0137 extern const struct file_operations random_fops, urandom_fops;
0138 #endif
0139
0140 #endif