blob: 12215df701e87441bc0b8b09340dac815594463a [file] [log] [blame]
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -07001/*
2 This is a maximally equidistributed combined Tausworthe generator
3 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
4
5 x_n = (s1_n ^ s2_n ^ s3_n)
6
7 s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
8 s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
9 s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
10
11 The period of this generator is about 2^88.
12
13 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
14 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
15
16 This is available on the net from L'Ecuyer's home page,
17
18 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
20
21 There is an erratum in the paper "Tables of Maximally
22 Equidistributed Combined LFSR Generators", Mathematics of
23 Computation, 68, 225 (1999), 261--269:
24 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
25
26 ... the k_j most significant bits of z_j must be non-
27 zero, for each j. (Note: this restriction also applies to the
28 computer code given in [4], but was mistakenly not mentioned in
29 that paper.)
30
31 This affects the seeding procedure by imposing the requirement
32 s1 > 1, s2 > 7, s3 > 15.
33
34*/
35
36#include <linux/types.h>
37#include <linux/percpu.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050038#include <linux/export.h>
Al Virof6a57032006-10-18 01:47:25 -040039#include <linux/jiffies.h>
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070040#include <linux/random.h>
41
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070042static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
43
Joe Eykholt59601642010-05-26 14:44:13 -070044/**
Akinobu Mita496f2f92012-12-17 16:04:23 -080045 * prandom_u32_state - seeded pseudo-random number generator.
Joe Eykholt59601642010-05-26 14:44:13 -070046 * @state: pointer to state structure holding seeded state.
47 *
48 * This is used for pseudo-randomness with no outside seeding.
Akinobu Mita496f2f92012-12-17 16:04:23 -080049 * For more random results, use prandom_u32().
Joe Eykholt59601642010-05-26 14:44:13 -070050 */
Akinobu Mita496f2f92012-12-17 16:04:23 -080051u32 prandom_u32_state(struct rnd_state *state)
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070052{
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}
Akinobu Mita496f2f92012-12-17 16:04:23 -080061EXPORT_SYMBOL(prandom_u32_state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070062
63/**
Akinobu Mita496f2f92012-12-17 16:04:23 -080064 * prandom_u32 - pseudo random number generator
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070065 *
66 * A 32 bit pseudo-random number is generated using a fast
67 * algorithm suitable for simulation. This algorithm is NOT
68 * considered safe for cryptographic use.
69 */
Akinobu Mita496f2f92012-12-17 16:04:23 -080070u32 prandom_u32(void)
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070071{
72 unsigned long r;
73 struct rnd_state *state = &get_cpu_var(net_rand_state);
Akinobu Mita496f2f92012-12-17 16:04:23 -080074 r = prandom_u32_state(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070075 put_cpu_var(state);
76 return r;
77}
Akinobu Mita496f2f92012-12-17 16:04:23 -080078EXPORT_SYMBOL(prandom_u32);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070079
Akinobu Mita6582c662012-12-17 16:04:25 -080080/*
81 * prandom_bytes_state - get the requested number of pseudo-random bytes
82 *
83 * @state: pointer to state structure holding seeded state.
84 * @buf: where to copy the pseudo-random bytes to
85 * @bytes: the requested number of bytes
86 *
87 * This is used for pseudo-randomness with no outside seeding.
88 * For more random results, use prandom_bytes().
89 */
90void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
91{
92 unsigned char *p = buf;
93 int i;
94
95 for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
96 u32 random = prandom_u32_state(state);
97 int j;
98
99 for (j = 0; j < sizeof(u32); j++) {
100 p[i + j] = random;
101 random >>= BITS_PER_BYTE;
102 }
103 }
104 if (i < bytes) {
105 u32 random = prandom_u32_state(state);
106
107 for (; i < bytes; i++) {
108 p[i] = random;
109 random >>= BITS_PER_BYTE;
110 }
111 }
112}
113EXPORT_SYMBOL(prandom_bytes_state);
114
115/**
116 * prandom_bytes - get the requested number of pseudo-random bytes
117 * @buf: where to copy the pseudo-random bytes to
118 * @bytes: the requested number of bytes
119 */
120void prandom_bytes(void *buf, int bytes)
121{
122 struct rnd_state *state = &get_cpu_var(net_rand_state);
123
124 prandom_bytes_state(state, buf, bytes);
125 put_cpu_var(state);
126}
127EXPORT_SYMBOL(prandom_bytes);
128
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700129/**
Akinobu Mita496f2f92012-12-17 16:04:23 -0800130 * prandom_seed - add entropy to pseudo random number generator
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700131 * @seed: seed value
132 *
Akinobu Mita496f2f92012-12-17 16:04:23 -0800133 * Add some additional seeding to the prandom pool.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700134 */
Akinobu Mita496f2f92012-12-17 16:04:23 -0800135void prandom_seed(u32 entropy)
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700136{
Andi Kleen61407f82008-04-03 14:07:02 -0700137 int i;
138 /*
139 * No locking on the CPUs, but then somewhat random results are, well,
140 * expected.
141 */
142 for_each_possible_cpu (i) {
143 struct rnd_state *state = &per_cpu(net_rand_state, i);
Daniel Borkmann51c37a72013-11-11 12:20:32 +0100144 state->s1 = __seed(state->s1 ^ entropy, 2);
Hannes Frederic Sowa6d319202013-11-11 12:20:33 +0100145 prandom_u32_state(state);
Andi Kleen61407f82008-04-03 14:07:02 -0700146 }
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700147}
Akinobu Mita496f2f92012-12-17 16:04:23 -0800148EXPORT_SYMBOL(prandom_seed);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700149
150/*
151 * Generate some initially weak seeding values to allow
Akinobu Mita496f2f92012-12-17 16:04:23 -0800152 * to start the prandom_u32() engine.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700153 */
Akinobu Mita496f2f92012-12-17 16:04:23 -0800154static int __init prandom_init(void)
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700155{
156 int i;
157
158 for_each_possible_cpu(i) {
159 struct rnd_state *state = &per_cpu(net_rand_state,i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700160
161#define LCG(x) ((x) * 69069) /* super-duper LCG */
Daniel Borkmann51c37a72013-11-11 12:20:32 +0100162 state->s1 = __seed(LCG(i + jiffies), 2);
163 state->s2 = __seed(LCG(state->s1), 8);
164 state->s3 = __seed(LCG(state->s2), 16);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700165
166 /* "warm it up" */
Akinobu Mita496f2f92012-12-17 16:04:23 -0800167 prandom_u32_state(state);
168 prandom_u32_state(state);
169 prandom_u32_state(state);
170 prandom_u32_state(state);
171 prandom_u32_state(state);
172 prandom_u32_state(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700173 }
174 return 0;
175}
Akinobu Mita496f2f92012-12-17 16:04:23 -0800176core_initcall(prandom_init);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700177
Hannes Frederic Sowa6d319202013-11-11 12:20:33 +0100178static void __prandom_timer(unsigned long dontcare);
179static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
180
181static void __prandom_timer(unsigned long dontcare)
182{
183 u32 entropy;
184
185 get_random_bytes(&entropy, sizeof(entropy));
186 prandom_seed(entropy);
187 /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
188 seed_timer.expires = jiffies + (40 * HZ + (prandom_u32() % (40 * HZ)));
189 add_timer(&seed_timer);
190}
191
192static void prandom_start_seed_timer(void)
193{
194 set_timer_slack(&seed_timer, HZ);
195 seed_timer.expires = jiffies + 40 * HZ;
196 add_timer(&seed_timer);
197}
198
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700199/*
200 * Generate better values after random number generator
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200201 * is fully initialized.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700202 */
Akinobu Mita496f2f92012-12-17 16:04:23 -0800203static int __init prandom_reseed(void)
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700204{
205 int i;
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700206
207 for_each_possible_cpu(i) {
208 struct rnd_state *state = &per_cpu(net_rand_state,i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700209 u32 seeds[3];
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700210
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700211 get_random_bytes(&seeds, sizeof(seeds));
Daniel Borkmann51c37a72013-11-11 12:20:32 +0100212 state->s1 = __seed(seeds[0], 2);
213 state->s2 = __seed(seeds[1], 8);
214 state->s3 = __seed(seeds[2], 16);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700215
216 /* mix it in */
Akinobu Mita496f2f92012-12-17 16:04:23 -0800217 prandom_u32_state(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700218 }
Hannes Frederic Sowa6d319202013-11-11 12:20:33 +0100219 prandom_start_seed_timer();
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700220 return 0;
221}
Akinobu Mita496f2f92012-12-17 16:04:23 -0800222late_initcall(prandom_reseed);