blob: 938bde5876ac43aa4b1cb316b4e074f66ee2271f [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/**
45 * prandom32 - seeded pseudo-random number generator.
46 * @state: pointer to state structure holding seeded state.
47 *
48 * This is used for pseudo-randomness with no outside seeding.
49 * For more random results, use random32().
50 */
51u32 prandom32(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}
Joe Eykholt59601642010-05-26 14:44:13 -070061EXPORT_SYMBOL(prandom32);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070062
63/**
64 * random32 - pseudo random number generator
65 *
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 */
70u32 random32(void)
71{
72 unsigned long r;
73 struct rnd_state *state = &get_cpu_var(net_rand_state);
Joe Eykholt59601642010-05-26 14:44:13 -070074 r = prandom32(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070075 put_cpu_var(state);
76 return r;
77}
78EXPORT_SYMBOL(random32);
79
80/**
81 * srandom32 - add entropy to pseudo random number generator
82 * @seed: seed value
83 *
84 * Add some additional seeding to the random32() pool.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070085 */
86void srandom32(u32 entropy)
87{
Andi Kleen61407f82008-04-03 14:07:02 -070088 int i;
89 /*
90 * No locking on the CPUs, but then somewhat random results are, well,
91 * expected.
92 */
93 for_each_possible_cpu (i) {
94 struct rnd_state *state = &per_cpu(net_rand_state, i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -070095 state->s1 = __seed(state->s1 ^ entropy, 1);
Andi Kleen61407f82008-04-03 14:07:02 -070096 }
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070097}
98EXPORT_SYMBOL(srandom32);
99
100/*
101 * Generate some initially weak seeding values to allow
102 * to start the random32() engine.
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);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700110
111#define LCG(x) ((x) * 69069) /* super-duper LCG */
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 /* "warm it up" */
Joe Eykholt59601642010-05-26 14:44:13 -0700117 prandom32(state);
118 prandom32(state);
119 prandom32(state);
120 prandom32(state);
121 prandom32(state);
122 prandom32(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700123 }
124 return 0;
125}
126core_initcall(random32_init);
127
128/*
129 * Generate better values after random number generator
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200130 * is fully initialized.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700131 */
132static int __init random32_reseed(void)
133{
134 int i;
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700135
136 for_each_possible_cpu(i) {
137 struct rnd_state *state = &per_cpu(net_rand_state,i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700138 u32 seeds[3];
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700139
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700140 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 /* mix it in */
Joe Eykholt59601642010-05-26 14:44:13 -0700146 prandom32(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700147 }
148 return 0;
149}
150late_initcall(random32_reseed);