linux/include/linux/random.h
<<
>>
Prefs
   1/* 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
  10#include <linux/list.h>
  11#include <linux/once.h>
  12#include <linux/percpu.h>
  13
  14#include <uapi/linux/random.h>
  15
  16struct random_extrng {
  17        ssize_t (*extrng_read)(void *buf, size_t buflen);
  18        struct module *owner;
  19};
  20
  21struct random_ready_callback {
  22        struct list_head list;
  23        void (*func)(struct random_ready_callback *rdy);
  24        struct module *owner;
  25};
  26
  27extern void add_device_randomness(const void *, unsigned int);
  28
  29#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) && !defined(__CHECKER__)
  30static inline void add_latent_entropy(void)
  31{
  32        add_device_randomness((const void *)&latent_entropy,
  33                              sizeof(latent_entropy));
  34}
  35#else
  36static inline void add_latent_entropy(void) {}
  37#endif
  38
  39extern void add_input_randomness(unsigned int type, unsigned int code,
  40                                 unsigned int value) __latent_entropy;
  41extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
  42
  43extern void get_random_bytes(void *buf, int nbytes);
  44extern int wait_for_random_bytes(void);
  45extern int __init rand_initialize(void);
  46extern bool rng_is_initialized(void);
  47extern int add_random_ready_callback(struct random_ready_callback *rdy);
  48extern void del_random_ready_callback(struct random_ready_callback *rdy);
  49extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
  50void random_register_extrng(const struct random_extrng *rng);
  51void random_unregister_extrng(void);
  52
  53#ifndef MODULE
  54extern const struct file_operations random_fops, urandom_fops;
  55#endif
  56
  57u32 get_random_u32(void);
  58u64 get_random_u64(void);
  59static inline unsigned int get_random_int(void)
  60{
  61        return get_random_u32();
  62}
  63static inline unsigned long get_random_long(void)
  64{
  65#if BITS_PER_LONG == 64
  66        return get_random_u64();
  67#else
  68        return get_random_u32();
  69#endif
  70}
  71
  72/*
  73 * On 64-bit architectures, protect against non-terminated C string overflows
  74 * by zeroing out the first byte of the canary; this leaves 56 bits of entropy.
  75 */
  76#ifdef CONFIG_64BIT
  77# ifdef __LITTLE_ENDIAN
  78#  define CANARY_MASK 0xffffffffffffff00UL
  79# else /* big endian, 64 bits: */
  80#  define CANARY_MASK 0x00ffffffffffffffUL
  81# endif
  82#else /* 32 bits: */
  83# define CANARY_MASK 0xffffffffUL
  84#endif
  85
  86static inline unsigned long get_random_canary(void)
  87{
  88        unsigned long val = get_random_long();
  89
  90        return val & CANARY_MASK;
  91}
  92
  93/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
  94 * Returns the result of the call to wait_for_random_bytes. */
  95static inline int get_random_bytes_wait(void *buf, int nbytes)
  96{
  97        int ret = wait_for_random_bytes();
  98        get_random_bytes(buf, nbytes);
  99        return ret;
 100}
 101
 102#define declare_get_random_var_wait(var) \
 103        static inline int get_random_ ## var ## _wait(var *out) { \
 104                int ret = wait_for_random_bytes(); \
 105                if (unlikely(ret)) \
 106                        return ret; \
 107                *out = get_random_ ## var(); \
 108                return 0; \
 109        }
 110declare_get_random_var_wait(u32)
 111declare_get_random_var_wait(u64)
 112declare_get_random_var_wait(int)
 113declare_get_random_var_wait(long)
 114#undef declare_get_random_var
 115
 116unsigned long randomize_page(unsigned long start, unsigned long range);
 117
 118u32 prandom_u32(void);
 119void prandom_bytes(void *buf, size_t nbytes);
 120void prandom_seed(u32 seed);
 121void prandom_reseed_late(void);
 122
 123struct rnd_state {
 124        __u32 s1, s2, s3, s4;
 125};
 126
 127DECLARE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy;
 128
 129u32 prandom_u32_state(struct rnd_state *state);
 130void prandom_bytes_state(struct rnd_state *state, void *buf, size_t nbytes);
 131void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state);
 132
 133#define prandom_init_once(pcpu_state)                   \
 134        DO_ONCE(prandom_seed_full_state, (pcpu_state))
 135
 136/**
 137 * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro)
 138 * @ep_ro: right open interval endpoint
 139 *
 140 * Returns a pseudo-random number that is in interval [0, ep_ro). Note
 141 * that the result depends on PRNG being well distributed in [0, ~0U]
 142 * u32 space. Here we use maximally equidistributed combined Tausworthe
 143 * generator, that is, prandom_u32(). This is useful when requesting a
 144 * random index of an array containing ep_ro elements, for example.
 145 *
 146 * Returns: pseudo-random number in interval [0, ep_ro)
 147 */
 148static inline u32 prandom_u32_max(u32 ep_ro)
 149{
 150        return (u32)(((u64) prandom_u32() * ep_ro) >> 32);
 151}
 152
 153/*
 154 * Handle minimum values for seeds
 155 */
 156static inline u32 __seed(u32 x, u32 m)
 157{
 158        return (x < m) ? x + m : x;
 159}
 160
 161/**
 162 * prandom_seed_state - set seed for prandom_u32_state().
 163 * @state: pointer to state structure to receive the seed.
 164 * @seed: arbitrary 64-bit value to use as a seed.
 165 */
 166static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
 167{
 168        u32 i = (seed >> 32) ^ (seed << 10) ^ seed;
 169
 170        state->s1 = __seed(i,   2U);
 171        state->s2 = __seed(i,   8U);
 172        state->s3 = __seed(i,  16U);
 173        state->s4 = __seed(i, 128U);
 174}
 175
 176#ifdef CONFIG_ARCH_RANDOM
 177# include <asm/archrandom.h>
 178#else
 179static inline bool arch_get_random_long(unsigned long *v)
 180{
 181        return 0;
 182}
 183static inline bool arch_get_random_int(unsigned int *v)
 184{
 185        return 0;
 186}
 187static inline bool arch_has_random(void)
 188{
 189        return 0;
 190}
 191static inline bool arch_get_random_seed_long(unsigned long *v)
 192{
 193        return 0;
 194}
 195static inline bool arch_get_random_seed_int(unsigned int *v)
 196{
 197        return 0;
 198}
 199static inline bool arch_has_random_seed(void)
 200{
 201        return 0;
 202}
 203#endif
 204
 205/* Pseudo random number generator from numerical recipes. */
 206static inline u32 next_pseudo_random32(u32 seed)
 207{
 208        return seed * 1664525 + 1013904223;
 209}
 210
 211#endif /* _LINUX_RANDOM_H */
 212