blob: f45b8be3e3c4e0d730834f307bbc6ca597e5ddfd [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * include/linux/random.h
4 *
5 * Include file for the random number generator.
6 */
7#ifndef _LINUX_RANDOM_H
8#define _LINUX_RANDOM_H
9
Olivier Deprez157378f2022-04-04 15:47:50 +020010#include <linux/bug.h>
11#include <linux/kernel.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/list.h>
13#include <linux/once.h>
14
15#include <uapi/linux/random.h>
16
17struct random_ready_callback {
18 struct list_head list;
19 void (*func)(struct random_ready_callback *rdy);
20 struct module *owner;
21};
22
23extern void add_device_randomness(const void *, unsigned int);
David Brazdil0f672f62019-12-10 10:32:29 +000024extern void add_bootloader_randomness(const void *, unsigned int);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025
David Brazdil0f672f62019-12-10 10:32:29 +000026#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027static inline void add_latent_entropy(void)
28{
29 add_device_randomness((const void *)&latent_entropy,
30 sizeof(latent_entropy));
31}
32#else
33static inline void add_latent_entropy(void) {}
34#endif
35
36extern void add_input_randomness(unsigned int type, unsigned int code,
37 unsigned int value) __latent_entropy;
38extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
39
40extern void get_random_bytes(void *buf, int nbytes);
41extern int wait_for_random_bytes(void);
David Brazdil0f672f62019-12-10 10:32:29 +000042extern int __init rand_initialize(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043extern bool rng_is_initialized(void);
44extern int add_random_ready_callback(struct random_ready_callback *rdy);
45extern void del_random_ready_callback(struct random_ready_callback *rdy);
46extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
47
48#ifndef MODULE
49extern const struct file_operations random_fops, urandom_fops;
50#endif
51
52u32 get_random_u32(void);
53u64 get_random_u64(void);
54static inline unsigned int get_random_int(void)
55{
56 return get_random_u32();
57}
58static inline unsigned long get_random_long(void)
59{
60#if BITS_PER_LONG == 64
61 return get_random_u64();
62#else
63 return get_random_u32();
64#endif
65}
66
67/*
68 * On 64-bit architectures, protect against non-terminated C string overflows
69 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
70 */
71#ifdef CONFIG_64BIT
72# ifdef __LITTLE_ENDIAN
73# define CANARY_MASK 0xffffffffffffff00UL
74# else /* big endian, 64 bits: */
75# define CANARY_MASK 0x00ffffffffffffffUL
76# endif
77#else /* 32 bits: */
78# define CANARY_MASK 0xffffffffUL
79#endif
80
81static inline unsigned long get_random_canary(void)
82{
83 unsigned long val = get_random_long();
84
85 return val & CANARY_MASK;
86}
87
88/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
89 * Returns the result of the call to wait_for_random_bytes. */
90static inline int get_random_bytes_wait(void *buf, int nbytes)
91{
92 int ret = wait_for_random_bytes();
93 get_random_bytes(buf, nbytes);
94 return ret;
95}
96
97#define declare_get_random_var_wait(var) \
98 static inline int get_random_ ## var ## _wait(var *out) { \
99 int ret = wait_for_random_bytes(); \
100 if (unlikely(ret)) \
101 return ret; \
102 *out = get_random_ ## var(); \
103 return 0; \
104 }
105declare_get_random_var_wait(u32)
106declare_get_random_var_wait(u64)
107declare_get_random_var_wait(int)
108declare_get_random_var_wait(long)
109#undef declare_get_random_var
110
111unsigned long randomize_page(unsigned long start, unsigned long range);
112
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113/*
Olivier Deprez0e641232021-09-23 10:07:05 +0200114 * This is designed to be standalone for just prandom
115 * users, but for now we include it from <linux/random.h>
116 * for legacy reasons.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200118#include <linux/prandom.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119
120#ifdef CONFIG_ARCH_RANDOM
121# include <asm/archrandom.h>
122#else
Olivier Deprez157378f2022-04-04 15:47:50 +0200123static inline bool __must_check arch_get_random_long(unsigned long *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124{
Olivier Deprez157378f2022-04-04 15:47:50 +0200125 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126}
Olivier Deprez157378f2022-04-04 15:47:50 +0200127static inline bool __must_check arch_get_random_int(unsigned int *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128{
Olivier Deprez157378f2022-04-04 15:47:50 +0200129 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000130}
Olivier Deprez157378f2022-04-04 15:47:50 +0200131static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132{
Olivier Deprez157378f2022-04-04 15:47:50 +0200133 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134}
Olivier Deprez157378f2022-04-04 15:47:50 +0200135static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136{
Olivier Deprez157378f2022-04-04 15:47:50 +0200137 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138}
Olivier Deprez157378f2022-04-04 15:47:50 +0200139#endif
140
141/*
142 * Called from the boot CPU during startup; not valid to call once
143 * secondary CPUs are up and preemption is possible.
144 */
145#ifndef arch_get_random_seed_long_early
146static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000147{
Olivier Deprez157378f2022-04-04 15:47:50 +0200148 WARN_ON(system_state != SYSTEM_BOOTING);
149 return arch_get_random_seed_long(v);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150}
Olivier Deprez157378f2022-04-04 15:47:50 +0200151#endif
152
153#ifndef arch_get_random_long_early
154static inline bool __init arch_get_random_long_early(unsigned long *v)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000155{
Olivier Deprez157378f2022-04-04 15:47:50 +0200156 WARN_ON(system_state != SYSTEM_BOOTING);
157 return arch_get_random_long(v);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158}
159#endif
160
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161#endif /* _LINUX_RANDOM_H */