random.h 5.1 KB

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