random.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifdef __KERNEL__
  37. extern void rand_initialize_irq(int irq);
  38. extern void add_input_randomness(unsigned int type, unsigned int code,
  39. unsigned int value);
  40. extern void add_interrupt_randomness(int irq, int irq_flags);
  41. extern void get_random_bytes(void *buf, int nbytes);
  42. void generate_random_uuid(unsigned char uuid_out[16]);
  43. #ifndef MODULE
  44. extern const struct file_operations random_fops, urandom_fops;
  45. #endif
  46. unsigned int get_random_int(void);
  47. unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len);
  48. u32 random32(void);
  49. void srandom32(u32 seed);
  50. u32 prandom32(struct rnd_state *);
  51. /*
  52. * Handle minimum values for seeds
  53. */
  54. static inline u32 __seed(u32 x, u32 m)
  55. {
  56. return (x < m) ? x + m : x;
  57. }
  58. /**
  59. * prandom32_seed - set seed for prandom32().
  60. * @state: pointer to state structure to receive the seed.
  61. * @seed: arbitrary 64-bit value to use as a seed.
  62. */
  63. static inline void prandom32_seed(struct rnd_state *state, u64 seed)
  64. {
  65. u32 i = (seed >> 32) ^ (seed << 10) ^ seed;
  66. state->s1 = __seed(i, 1);
  67. state->s2 = __seed(i, 7);
  68. state->s3 = __seed(i, 15);
  69. }
  70. #ifdef CONFIG_ARCH_RANDOM
  71. # include <asm/archrandom.h>
  72. #else
  73. static inline int arch_get_random_long(unsigned long *v)
  74. {
  75. return 0;
  76. }
  77. static inline int arch_get_random_int(unsigned int *v)
  78. {
  79. return 0;
  80. }
  81. #endif
  82. #endif /* __KERNEL___ */
  83. #endif /* _LINUX_RANDOM_H */