| .. | .. |
|---|
| 1 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
|---|
| 2 | | -/* |
|---|
| 3 | | - * include/linux/random.h |
|---|
| 4 | | - * |
|---|
| 5 | | - * Include file for the random number generator. |
|---|
| 6 | | - */ |
|---|
| 2 | + |
|---|
| 7 | 3 | #ifndef _LINUX_RANDOM_H |
|---|
| 8 | 4 | #define _LINUX_RANDOM_H |
|---|
| 9 | 5 | |
|---|
| 6 | +#include <linux/bug.h> |
|---|
| 7 | +#include <linux/kernel.h> |
|---|
| 10 | 8 | #include <linux/list.h> |
|---|
| 11 | 9 | #include <linux/once.h> |
|---|
| 12 | 10 | |
|---|
| 13 | 11 | #include <uapi/linux/random.h> |
|---|
| 14 | 12 | |
|---|
| 15 | | -struct random_ready_callback { |
|---|
| 16 | | - struct list_head list; |
|---|
| 17 | | - void (*func)(struct random_ready_callback *rdy); |
|---|
| 18 | | - struct module *owner; |
|---|
| 19 | | -}; |
|---|
| 13 | +struct notifier_block; |
|---|
| 20 | 14 | |
|---|
| 21 | | -extern void add_device_randomness(const void *, unsigned int); |
|---|
| 22 | | -extern void add_bootloader_randomness(const void *, unsigned int); |
|---|
| 15 | +void add_device_randomness(const void *buf, unsigned int len); |
|---|
| 16 | +void 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); |
|---|
| 23 | 21 | |
|---|
| 24 | | -#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) && !defined(__CHECKER__) |
|---|
| 25 | 22 | static inline void add_latent_entropy(void) |
|---|
| 26 | 23 | { |
|---|
| 27 | | - add_device_randomness((const void *)&latent_entropy, |
|---|
| 28 | | - sizeof(latent_entropy)); |
|---|
| 29 | | -} |
|---|
| 24 | +#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__) |
|---|
| 25 | + add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy)); |
|---|
| 30 | 26 | #else |
|---|
| 31 | | -static inline void add_latent_entropy(void) {} |
|---|
| 27 | + add_device_randomness(NULL, 0); |
|---|
| 32 | 28 | #endif |
|---|
| 29 | +} |
|---|
| 33 | 30 | |
|---|
| 34 | | -extern void add_input_randomness(unsigned int type, unsigned int code, |
|---|
| 35 | | - unsigned int value) __latent_entropy; |
|---|
| 36 | | -extern void add_interrupt_randomness(int irq, int irq_flags, __u64 ip) __latent_entropy; |
|---|
| 37 | | - |
|---|
| 38 | | -extern void get_random_bytes(void *buf, int nbytes); |
|---|
| 39 | | -extern int wait_for_random_bytes(void); |
|---|
| 40 | | -extern int __init rand_initialize(void); |
|---|
| 41 | | -extern bool rng_is_initialized(void); |
|---|
| 42 | | -extern int add_random_ready_callback(struct random_ready_callback *rdy); |
|---|
| 43 | | -extern void del_random_ready_callback(struct random_ready_callback *rdy); |
|---|
| 44 | | -extern int __must_check get_random_bytes_arch(void *buf, int nbytes); |
|---|
| 45 | | - |
|---|
| 46 | | -#ifndef MODULE |
|---|
| 47 | | -extern const struct file_operations random_fops, urandom_fops; |
|---|
| 48 | | -#endif |
|---|
| 49 | | - |
|---|
| 31 | +void get_random_bytes(void *buf, int len); |
|---|
| 32 | +int __must_check get_random_bytes_arch(void *buf, int len); |
|---|
| 50 | 33 | u32 get_random_u32(void); |
|---|
| 51 | 34 | u64 get_random_u64(void); |
|---|
| 52 | 35 | static inline unsigned int get_random_int(void) |
|---|
| .. | .. |
|---|
| 78 | 61 | |
|---|
| 79 | 62 | static inline unsigned long get_random_canary(void) |
|---|
| 80 | 63 | { |
|---|
| 81 | | - unsigned long val = get_random_long(); |
|---|
| 82 | | - |
|---|
| 83 | | - return val & CANARY_MASK; |
|---|
| 64 | + return get_random_long() & CANARY_MASK; |
|---|
| 84 | 65 | } |
|---|
| 66 | + |
|---|
| 67 | +int __init random_init(const char *command_line); |
|---|
| 68 | +bool rng_is_initialized(void); |
|---|
| 69 | +int wait_for_random_bytes(void); |
|---|
| 70 | +int register_random_ready_notifier(struct notifier_block *nb); |
|---|
| 71 | +int unregister_random_ready_notifier(struct notifier_block *nb); |
|---|
| 85 | 72 | |
|---|
| 86 | 73 | /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes). |
|---|
| 87 | 74 | * Returns the result of the call to wait_for_random_bytes. */ |
|---|
| 88 | | -static inline int get_random_bytes_wait(void *buf, int nbytes) |
|---|
| 75 | +static inline int get_random_bytes_wait(void *buf, size_t nbytes) |
|---|
| 89 | 76 | { |
|---|
| 90 | 77 | int ret = wait_for_random_bytes(); |
|---|
| 91 | 78 | get_random_bytes(buf, nbytes); |
|---|
| 92 | 79 | return ret; |
|---|
| 93 | 80 | } |
|---|
| 94 | 81 | |
|---|
| 95 | | -#define declare_get_random_var_wait(var) \ |
|---|
| 96 | | - static inline int get_random_ ## var ## _wait(var *out) { \ |
|---|
| 82 | +#define declare_get_random_var_wait(name, ret_type) \ |
|---|
| 83 | + static inline int get_random_ ## name ## _wait(ret_type *out) { \ |
|---|
| 97 | 84 | int ret = wait_for_random_bytes(); \ |
|---|
| 98 | 85 | if (unlikely(ret)) \ |
|---|
| 99 | 86 | return ret; \ |
|---|
| 100 | | - *out = get_random_ ## var(); \ |
|---|
| 87 | + *out = get_random_ ## name(); \ |
|---|
| 101 | 88 | return 0; \ |
|---|
| 102 | 89 | } |
|---|
| 103 | | -declare_get_random_var_wait(u32) |
|---|
| 104 | | -declare_get_random_var_wait(u64) |
|---|
| 105 | | -declare_get_random_var_wait(int) |
|---|
| 106 | | -declare_get_random_var_wait(long) |
|---|
| 90 | +declare_get_random_var_wait(u32, u32) |
|---|
| 91 | +declare_get_random_var_wait(u64, u32) |
|---|
| 92 | +declare_get_random_var_wait(int, unsigned int) |
|---|
| 93 | +declare_get_random_var_wait(long, unsigned long) |
|---|
| 107 | 94 | #undef declare_get_random_var |
|---|
| 108 | | - |
|---|
| 109 | | -unsigned long randomize_page(unsigned long start, unsigned long range); |
|---|
| 110 | 95 | |
|---|
| 111 | 96 | /* |
|---|
| 112 | 97 | * This is designed to be standalone for just prandom |
|---|
| .. | .. |
|---|
| 118 | 103 | #ifdef CONFIG_ARCH_RANDOM |
|---|
| 119 | 104 | # include <asm/archrandom.h> |
|---|
| 120 | 105 | #else |
|---|
| 121 | | -static inline bool __must_check arch_get_random_long(unsigned long *v) |
|---|
| 106 | +static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; } |
|---|
| 107 | +static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; } |
|---|
| 108 | +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; } |
|---|
| 109 | +static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; } |
|---|
| 110 | +#endif |
|---|
| 111 | + |
|---|
| 112 | +/* |
|---|
| 113 | + * Called from the boot CPU during startup; not valid to call once |
|---|
| 114 | + * secondary CPUs are up and preemption is possible. |
|---|
| 115 | + */ |
|---|
| 116 | +#ifndef arch_get_random_seed_long_early |
|---|
| 117 | +static inline bool __init arch_get_random_seed_long_early(unsigned long *v) |
|---|
| 122 | 118 | { |
|---|
| 123 | | - return false; |
|---|
| 124 | | -} |
|---|
| 125 | | -static inline bool __must_check arch_get_random_int(unsigned int *v) |
|---|
| 126 | | -{ |
|---|
| 127 | | - return false; |
|---|
| 128 | | -} |
|---|
| 129 | | -static inline bool __must_check arch_get_random_seed_long(unsigned long *v) |
|---|
| 130 | | -{ |
|---|
| 131 | | - return false; |
|---|
| 132 | | -} |
|---|
| 133 | | -static inline bool __must_check arch_get_random_seed_int(unsigned int *v) |
|---|
| 134 | | -{ |
|---|
| 135 | | - return false; |
|---|
| 119 | + WARN_ON(system_state != SYSTEM_BOOTING); |
|---|
| 120 | + return arch_get_random_seed_long(v); |
|---|
| 136 | 121 | } |
|---|
| 137 | 122 | #endif |
|---|
| 138 | 123 | |
|---|
| 124 | +#ifndef arch_get_random_long_early |
|---|
| 125 | +static inline bool __init arch_get_random_long_early(unsigned long *v) |
|---|
| 126 | +{ |
|---|
| 127 | + WARN_ON(system_state != SYSTEM_BOOTING); |
|---|
| 128 | + return arch_get_random_long(v); |
|---|
| 129 | +} |
|---|
| 130 | +#endif |
|---|
| 131 | + |
|---|
| 132 | +#ifdef CONFIG_SMP |
|---|
| 133 | +int random_prepare_cpu(unsigned int cpu); |
|---|
| 134 | +int random_online_cpu(unsigned int cpu); |
|---|
| 135 | +#endif |
|---|
| 136 | + |
|---|
| 137 | +#ifndef MODULE |
|---|
| 138 | +extern const struct file_operations random_fops, urandom_fops; |
|---|
| 139 | +#endif |
|---|
| 140 | + |
|---|
| 141 | +/* |
|---|
| 142 | + * Android KABI fixups |
|---|
| 143 | + * Added back the following structure and calls to preserve the ABI for |
|---|
| 144 | + * out-of-tree drivers that were using them. |
|---|
| 145 | + */ |
|---|
| 146 | +struct random_ready_callback { |
|---|
| 147 | + struct list_head list; |
|---|
| 148 | + void (*func)(struct random_ready_callback *rdy); |
|---|
| 149 | + struct module *owner; |
|---|
| 150 | +}; |
|---|
| 151 | +extern int add_random_ready_callback(struct random_ready_callback *rdy); |
|---|
| 152 | +extern void del_random_ready_callback(struct random_ready_callback *rdy); |
|---|
| 153 | + |
|---|
| 139 | 154 | #endif /* _LINUX_RANDOM_H */ |
|---|