GNU Linux-libre 4.9.326-gnu1
[releases.git] / include / linux / random.h
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__)
23 static inline void add_latent_entropy(void)
24 {
25         add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
26 }
27 #else
28 static inline void add_latent_entropy(void) { }
29 #endif
30
31 void get_random_bytes(void *buf, size_t len);
32 size_t __must_check get_random_bytes_arch(void *buf, size_t len);
33 u32 get_random_u32(void);
34 u64 get_random_u64(void);
35 static inline unsigned int get_random_int(void)
36 {
37         return get_random_u32();
38 }
39 static inline unsigned long get_random_long(void)
40 {
41 #if BITS_PER_LONG == 64
42         return get_random_u64();
43 #else
44         return get_random_u32();
45 #endif
46 }
47
48 int __init random_init(const char *command_line);
49 bool rng_is_initialized(void);
50 int wait_for_random_bytes(void);
51 int register_random_ready_notifier(struct notifier_block *nb);
52 int unregister_random_ready_notifier(struct notifier_block *nb);
53
54 /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
55  * Returns the result of the call to wait_for_random_bytes. */
56 static inline int get_random_bytes_wait(void *buf, size_t nbytes)
57 {
58         int ret = wait_for_random_bytes();
59         get_random_bytes(buf, nbytes);
60         return ret;
61 }
62
63 #define declare_get_random_var_wait(name, ret_type) \
64         static inline int get_random_ ## name ## _wait(ret_type *out) { \
65                 int ret = wait_for_random_bytes(); \
66                 if (unlikely(ret)) \
67                         return ret; \
68                 *out = get_random_ ## name(); \
69                 return 0; \
70         }
71 declare_get_random_var_wait(u32, u32)
72 declare_get_random_var_wait(u64, u32)
73 declare_get_random_var_wait(int, unsigned int)
74 declare_get_random_var_wait(long, unsigned long)
75 #undef declare_get_random_var
76
77 /*
78  * This is designed to be standalone for just prandom
79  * users, but for now we include it from <linux/random.h>
80  * for legacy reasons.
81  */
82 #include <linux/prandom.h>
83
84 #ifdef CONFIG_ARCH_RANDOM
85 # include <asm/archrandom.h>
86 #else
87 static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; }
88 static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; }
89 static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; }
90 static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; }
91 #endif
92
93 /*
94  * Called from the boot CPU during startup; not valid to call once
95  * secondary CPUs are up and preemption is possible.
96  */
97 #ifndef arch_get_random_seed_long_early
98 static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
99 {
100         WARN_ON(system_state != SYSTEM_BOOTING);
101         return arch_get_random_seed_long(v);
102 }
103 #endif
104
105 #ifndef arch_get_random_long_early
106 static inline bool __init arch_get_random_long_early(unsigned long *v)
107 {
108         WARN_ON(system_state != SYSTEM_BOOTING);
109         return arch_get_random_long(v);
110 }
111 #endif
112
113 #ifdef CONFIG_SMP
114 int random_prepare_cpu(unsigned int cpu);
115 int random_online_cpu(unsigned int cpu);
116 #endif
117
118 #ifndef MODULE
119 extern const struct file_operations random_fops, urandom_fops;
120 #endif
121
122 #endif /* _LINUX_RANDOM_H */