random.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * include/linux/random.h
  3. *
  4. * Include file for the random number generator.
  5. */
  6. #ifndef _LINUX_RANDOM_H
  7. #define _LINUX_RANDOM_H
  8. #include <linux/types.h>
  9. #include <linux/ioctl.h>
  10. #include <linux/irqnr.h>
  11. /* ioctl()'s for the random number generator */
  12. /* Get the entropy count. */
  13. #define RNDGETENTCNT _IOR( 'R', 0x00, int )
  14. /* Add to (or subtract from) the entropy count. (Superuser only.) */
  15. #define RNDADDTOENTCNT _IOW( 'R', 0x01, int )
  16. /* Get the contents of the entropy pool. (Superuser only.) */
  17. #define RNDGETPOOL _IOR( 'R', 0x02, int [2] )
  18. /*
  19. * Write bytes into the entropy pool and add to the entropy count.
  20. * (Superuser only.)
  21. */
  22. #define RNDADDENTROPY _IOW( 'R', 0x03, int [2] )
  23. /* Clear entropy count to 0. (Superuser only.) */
  24. #define RNDZAPENTCNT _IO( 'R', 0x04 )
  25. /* Clear the entropy pool and associated counters. (Superuser only.) */
  26. #define RNDCLEARPOOL _IO( 'R', 0x06 )
  27. struct rand_pool_info {
  28. int entropy_count;
  29. int buf_size;
  30. __u32 buf[0];
  31. };
  32. struct rnd_state {
  33. __u32 s1, s2, s3;
  34. };
  35. /* Exported functions */
  36. /*
  37. * Flags for getrandom(2)
  38. *
  39. * GRND_NONBLOCK Don't block and return EAGAIN instead
  40. * GRND_RANDOM Use the /dev/random pool instead of /dev/urandom
  41. */
  42. #define GRND_NONBLOCK 0x0001
  43. #define GRND_RANDOM 0x0002
  44. #ifdef __KERNEL__
  45. extern void add_device_randomness(const void *, unsigned int);
  46. extern void add_input_randomness(unsigned int type, unsigned int code,
  47. unsigned int value);
  48. extern void add_interrupt_randomness(int irq, int irq_flags);
  49. extern void get_random_bytes(void *buf, int nbytes);
  50. extern void get_random_bytes_arch(void *buf, int nbytes);
  51. void generate_random_uuid(unsigned char uuid_out[16]);
  52. extern int random_int_secret_init(void);
  53. #ifndef MODULE
  54. extern const struct file_operations random_fops, urandom_fops;
  55. #endif
  56. unsigned int get_random_int(void);
  57. unsigned long get_random_long(void);
  58. unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len);
  59. u32 prandom_u32(void);
  60. void prandom_bytes(void *buf, int nbytes);
  61. void prandom_seed(u32 seed);
  62. void prandom_reseed_late(void);
  63. /*
  64. * These macros are preserved for backward compatibility and should be
  65. * removed as soon as a transition is finished.
  66. */
  67. #define random32() prandom_u32()
  68. #define srandom32(seed) prandom_seed(seed)
  69. u32 prandom_u32_state(struct rnd_state *);
  70. void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes);
  71. /*
  72. * Handle minimum values for seeds
  73. */
  74. static inline u32 __seed(u32 x, u32 m)
  75. {
  76. return (x < m) ? x + m : x;
  77. }
  78. /**
  79. * prandom_seed_state - set seed for prandom_u32_state().
  80. * @state: pointer to state structure to receive the seed.
  81. * @seed: arbitrary 64-bit value to use as a seed.
  82. */
  83. static inline void prandom_seed_state(struct rnd_state *state, u64 seed)
  84. {
  85. u32 i = (seed >> 32) ^ (seed << 10) ^ seed;
  86. state->s1 = __seed(i, 2);
  87. state->s2 = __seed(i, 8);
  88. state->s3 = __seed(i, 16);
  89. }
  90. #ifdef CONFIG_ARCH_RANDOM
  91. # include <asm/archrandom.h>
  92. #else
  93. static inline int arch_get_random_long(unsigned long *v)
  94. {
  95. return 0;
  96. }
  97. static inline int arch_get_random_int(unsigned int *v)
  98. {
  99. return 0;
  100. }
  101. #endif
  102. #endif /* __KERNEL___ */
  103. #endif /* _LINUX_RANDOM_H */