blob: 556d5ffe110612d9df956c342be78bc465a85601 [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>
38#include <linux/module.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
42struct rnd_state {
43 u32 s1, s2, s3;
44};
45
46static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
47
48static u32 __random32(struct rnd_state *state)
49{
50#define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
51
52 state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
53 state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
54 state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
55
56 return (state->s1 ^ state->s2 ^ state->s3);
57}
58
Stephen Hemminger697f8d02008-07-30 16:29:19 -070059/*
60 * Handle minimum values for seeds
61 */
62static inline u32 __seed(u32 x, u32 m)
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070063{
Stephen Hemminger697f8d02008-07-30 16:29:19 -070064 return (x < m) ? x + m : x;
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070065}
66
67/**
68 * random32 - pseudo random number generator
69 *
70 * A 32 bit pseudo-random number is generated using a fast
71 * algorithm suitable for simulation. This algorithm is NOT
72 * considered safe for cryptographic use.
73 */
74u32 random32(void)
75{
76 unsigned long r;
77 struct rnd_state *state = &get_cpu_var(net_rand_state);
78 r = __random32(state);
79 put_cpu_var(state);
80 return r;
81}
82EXPORT_SYMBOL(random32);
83
84/**
85 * srandom32 - add entropy to pseudo random number generator
86 * @seed: seed value
87 *
88 * Add some additional seeding to the random32() pool.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -070089 */
90void srandom32(u32 entropy)
91{
Andi Kleen61407f82008-04-03 14:07:02 -070092 int i;
93 /*
94 * No locking on the CPUs, but then somewhat random results are, well,
95 * expected.
96 */
97 for_each_possible_cpu (i) {
98 struct rnd_state *state = &per_cpu(net_rand_state, i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -070099 state->s1 = __seed(state->s1 ^ entropy, 1);
Andi Kleen61407f82008-04-03 14:07:02 -0700100 }
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700101}
102EXPORT_SYMBOL(srandom32);
103
104/*
105 * Generate some initially weak seeding values to allow
106 * to start the random32() engine.
107 */
108static int __init random32_init(void)
109{
110 int i;
111
112 for_each_possible_cpu(i) {
113 struct rnd_state *state = &per_cpu(net_rand_state,i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700114
115#define LCG(x) ((x) * 69069) /* super-duper LCG */
116 state->s1 = __seed(LCG(i + jiffies), 1);
117 state->s2 = __seed(LCG(state->s1), 7);
118 state->s3 = __seed(LCG(state->s2), 15);
119
120 /* "warm it up" */
121 __random32(state);
122 __random32(state);
123 __random32(state);
124 __random32(state);
125 __random32(state);
126 __random32(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700127 }
128 return 0;
129}
130core_initcall(random32_init);
131
132/*
133 * Generate better values after random number generator
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200134 * is fully initialized.
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700135 */
136static int __init random32_reseed(void)
137{
138 int i;
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700139
140 for_each_possible_cpu(i) {
141 struct rnd_state *state = &per_cpu(net_rand_state,i);
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700142 u32 seeds[3];
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700143
Stephen Hemminger697f8d02008-07-30 16:29:19 -0700144 get_random_bytes(&seeds, sizeof(seeds));
145 state->s1 = __seed(seeds[0], 1);
146 state->s2 = __seed(seeds[1], 7);
147 state->s3 = __seed(seeds[2], 15);
148
149 /* mix it in */
150 __random32(state);
Stephen Hemmingeraaa248f2006-10-17 00:09:42 -0700151 }
152 return 0;
153}
154late_initcall(random32_reseed);