| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * include/linux/random.h | 
|  | 3 | * | 
|  | 4 | * Include file for the random number generator. | 
|  | 5 | */ | 
|  | 6 |  | 
|  | 7 | #ifndef _LINUX_RANDOM_H | 
|  | 8 | #define _LINUX_RANDOM_H | 
|  | 9 |  | 
| Jaswinder Singh Rajput | 68622c6 | 2009-01-30 22:11:32 +0530 | [diff] [blame] | 10 | #include <linux/types.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/ioctl.h> | 
| Ingo Molnar | 0ebb26e | 2008-12-12 11:26:39 +0100 | [diff] [blame] | 12 | #include <linux/irqnr.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
|  | 14 | /* ioctl()'s for the random number generator */ | 
|  | 15 |  | 
|  | 16 | /* Get the entropy count. */ | 
|  | 17 | #define RNDGETENTCNT	_IOR( 'R', 0x00, int ) | 
|  | 18 |  | 
|  | 19 | /* Add to (or subtract from) the entropy count.  (Superuser only.) */ | 
|  | 20 | #define RNDADDTOENTCNT	_IOW( 'R', 0x01, int ) | 
|  | 21 |  | 
|  | 22 | /* Get the contents of the entropy pool.  (Superuser only.) */ | 
|  | 23 | #define RNDGETPOOL	_IOR( 'R', 0x02, int [2] ) | 
|  | 24 |  | 
|  | 25 | /* | 
|  | 26 | * Write bytes into the entropy pool and add to the entropy count. | 
|  | 27 | * (Superuser only.) | 
|  | 28 | */ | 
|  | 29 | #define RNDADDENTROPY	_IOW( 'R', 0x03, int [2] ) | 
|  | 30 |  | 
|  | 31 | /* Clear entropy count to 0.  (Superuser only.) */ | 
|  | 32 | #define RNDZAPENTCNT	_IO( 'R', 0x04 ) | 
|  | 33 |  | 
|  | 34 | /* Clear the entropy pool and associated counters.  (Superuser only.) */ | 
|  | 35 | #define RNDCLEARPOOL	_IO( 'R', 0x06 ) | 
|  | 36 |  | 
|  | 37 | struct rand_pool_info { | 
|  | 38 | int	entropy_count; | 
|  | 39 | int	buf_size; | 
|  | 40 | __u32	buf[0]; | 
|  | 41 | }; | 
|  | 42 |  | 
| Joe Eykholt | 5960164 | 2010-05-26 14:44:13 -0700 | [diff] [blame] | 43 | struct rnd_state { | 
|  | 44 | __u32 s1, s2, s3; | 
|  | 45 | }; | 
|  | 46 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* Exported functions */ | 
|  | 48 |  | 
|  | 49 | #ifdef __KERNEL__ | 
|  | 50 |  | 
| Linus Torvalds | 1d0eb35 | 2012-07-04 11:16:01 -0400 | [diff] [blame] | 51 | extern void add_device_randomness(const void *, unsigned int); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | extern void add_input_randomness(unsigned int type, unsigned int code, | 
|  | 53 | unsigned int value); | 
| Theodore Ts'o | 0110bbf | 2012-07-02 07:52:16 -0400 | [diff] [blame] | 54 | extern void add_interrupt_randomness(int irq, int irq_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | extern void get_random_bytes(void *buf, int nbytes); | 
| Theodore Ts'o | 29aef9d | 2012-07-05 10:35:23 -0400 | [diff] [blame] | 57 | extern void get_random_bytes_arch(void *buf, int nbytes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | void generate_random_uuid(unsigned char uuid_out[16]); | 
| Theodore Ts'o | c2f2710 | 2013-09-10 10:52:35 -0400 | [diff] [blame] | 59 | extern int random_int_secret_init(void); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #ifndef MODULE | 
| Arjan van de Ven | 5404732 | 2007-02-12 00:55:28 -0800 | [diff] [blame] | 62 | extern const struct file_operations random_fops, urandom_fops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #endif | 
|  | 64 |  | 
|  | 65 | unsigned int get_random_int(void); | 
|  | 66 | unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len); | 
|  | 67 |  | 
| Stephen Hemminger | aaa248f | 2006-10-17 00:09:42 -0700 | [diff] [blame] | 68 | u32 random32(void); | 
|  | 69 | void srandom32(u32 seed); | 
|  | 70 |  | 
| Joe Eykholt | 5960164 | 2010-05-26 14:44:13 -0700 | [diff] [blame] | 71 | u32 prandom32(struct rnd_state *); | 
|  | 72 |  | 
|  | 73 | /* | 
|  | 74 | * Handle minimum values for seeds | 
|  | 75 | */ | 
|  | 76 | static inline u32 __seed(u32 x, u32 m) | 
|  | 77 | { | 
|  | 78 | return (x < m) ? x + m : x; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | /** | 
|  | 82 | * prandom32_seed - set seed for prandom32(). | 
|  | 83 | * @state: pointer to state structure to receive the seed. | 
|  | 84 | * @seed: arbitrary 64-bit value to use as a seed. | 
|  | 85 | */ | 
|  | 86 | static inline void prandom32_seed(struct rnd_state *state, u64 seed) | 
|  | 87 | { | 
|  | 88 | u32 i = (seed >> 32) ^ (seed << 10) ^ seed; | 
|  | 89 |  | 
| Daniel Borkmann | 4dd7a52 | 2013-11-11 12:20:32 +0100 | [diff] [blame] | 90 | state->s1 = __seed(i, 2); | 
|  | 91 | state->s2 = __seed(i, 8); | 
|  | 92 | state->s3 = __seed(i, 16); | 
| Joe Eykholt | 5960164 | 2010-05-26 14:44:13 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| H. Peter Anvin | 63d7717 | 2011-07-31 13:54:50 -0700 | [diff] [blame] | 95 | #ifdef CONFIG_ARCH_RANDOM | 
|  | 96 | # include <asm/archrandom.h> | 
|  | 97 | #else | 
|  | 98 | static inline int arch_get_random_long(unsigned long *v) | 
|  | 99 | { | 
|  | 100 | return 0; | 
|  | 101 | } | 
|  | 102 | static inline int arch_get_random_int(unsigned int *v) | 
|  | 103 | { | 
|  | 104 | return 0; | 
|  | 105 | } | 
|  | 106 | #endif | 
|  | 107 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #endif /* __KERNEL___ */ | 
|  | 109 |  | 
|  | 110 | #endif /* _LINUX_RANDOM_H */ |