blob: ce29a040c8dce1fa5a0d2f17ff2ce529f77887c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Rajput68622c62009-01-30 22:11:32 +053010#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/ioctl.h>
Ingo Molnar0ebb26e2008-12-12 11:26:39 +010012#include <linux/irqnr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
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
37struct rand_pool_info {
38 int entropy_count;
39 int buf_size;
40 __u32 buf[0];
41};
42
Joe Eykholt59601642010-05-26 14:44:13 -070043struct rnd_state {
44 __u32 s1, s2, s3;
45};
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* Exported functions */
48
49#ifdef __KERNEL__
50
51extern void rand_initialize_irq(int irq);
52
53extern void add_input_randomness(unsigned int type, unsigned int code,
54 unsigned int value);
55extern void add_interrupt_randomness(int irq);
56
57extern void get_random_bytes(void *buf, int nbytes);
58void generate_random_uuid(unsigned char uuid_out[16]);
59
Al Virob09b845c2006-11-14 20:52:19 -080060extern __u32 secure_ip_id(__be32 daddr);
Eric Dumazet87c48fa2011-07-21 21:25:58 -070061extern __u32 secure_ipv6_id(const __be32 daddr[4]);
Al Virob09b845c2006-11-14 20:52:19 -080062extern u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport);
63extern u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
64 __be16 dport);
65extern __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
66 __be16 sport, __be16 dport);
67extern __u32 secure_tcpv6_sequence_number(__be32 *saddr, __be32 *daddr,
68 __be16 sport, __be16 dport);
69extern u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
70 __be16 sport, __be16 dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#ifndef MODULE
Arjan van de Ven54047322007-02-12 00:55:28 -080073extern const struct file_operations random_fops, urandom_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75
76unsigned int get_random_int(void);
77unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len);
78
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070079u32 random32(void);
80void srandom32(u32 seed);
81
Joe Eykholt59601642010-05-26 14:44:13 -070082u32 prandom32(struct rnd_state *);
83
84/*
85 * Handle minimum values for seeds
86 */
87static inline u32 __seed(u32 x, u32 m)
88{
89 return (x < m) ? x + m : x;
90}
91
92/**
93 * prandom32_seed - set seed for prandom32().
94 * @state: pointer to state structure to receive the seed.
95 * @seed: arbitrary 64-bit value to use as a seed.
96 */
97static inline void prandom32_seed(struct rnd_state *state, u64 seed)
98{
99 u32 i = (seed >> 32) ^ (seed << 10) ^ seed;
100
101 state->s1 = __seed(i, 1);
102 state->s2 = __seed(i, 7);
103 state->s3 = __seed(i, 15);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif /* __KERNEL___ */
107
108#endif /* _LINUX_RANDOM_H */