1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/types.h>
37#include <linux/percpu.h>
38#include <linux/module.h>
39#include <linux/jiffies.h>
40#include <linux/random.h>
41
42static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
43
44
45
46
47
48
49
50
51u32 prandom32(struct rnd_state *state)
52{
53#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
54
55 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
56 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
57 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
58
59 return (state->s1 ^ state->s2 ^ state->s3);
60}
61EXPORT_SYMBOL(prandom32);
62
63
64
65
66
67
68
69
70u32 random32(void)
71{
72 unsigned long r;
73 struct rnd_state *state = &get_cpu_var(net_rand_state);
74 r = prandom32(state);
75 put_cpu_var(state);
76 return r;
77}
78EXPORT_SYMBOL(random32);
79
80
81
82
83
84
85
86void srandom32(u32 entropy)
87{
88 int i;
89
90
91
92
93 for_each_possible_cpu (i) {
94 struct rnd_state *state = &per_cpu(net_rand_state, i);
95 state->s1 = __seed(state->s1 ^ entropy, 1);
96 }
97}
98EXPORT_SYMBOL(srandom32);
99
100
101
102
103
104static int __init random32_init(void)
105{
106 int i;
107
108 for_each_possible_cpu(i) {
109 struct rnd_state *state = &per_cpu(net_rand_state,i);
110
111#define LCG(x) ((x) * 69069)
112 state->s1 = __seed(LCG(i + jiffies), 1);
113 state->s2 = __seed(LCG(state->s1), 7);
114 state->s3 = __seed(LCG(state->s2), 15);
115
116
117 prandom32(state);
118 prandom32(state);
119 prandom32(state);
120 prandom32(state);
121 prandom32(state);
122 prandom32(state);
123 }
124 return 0;
125}
126core_initcall(random32_init);
127
128
129
130
131
132static int __init random32_reseed(void)
133{
134 int i;
135
136 for_each_possible_cpu(i) {
137 struct rnd_state *state = &per_cpu(net_rand_state,i);
138 u32 seeds[3];
139
140 get_random_bytes(&seeds, sizeof(seeds));
141 state->s1 = __seed(seeds[0], 1);
142 state->s2 = __seed(seeds[1], 7);
143 state->s3 = __seed(seeds[2], 15);
144
145
146 prandom32(state);
147 }
148 return 0;
149}
150late_initcall(random32_reseed);
151