Initial import
[openvswitch] / datapath / linux-2.4 / compat-2.4 / random32.c
1 /*
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/module.h>
38 #include <linux/jiffies.h>
39 #include <linux/random.h>
40 #include <linux/threads.h>
41 #include <linux/smp.h>
42
43 #include "compat24.h"
44
45 struct rnd_state {
46         u32 s1, s2, s3;
47 };
48
49 static struct rnd_state net_rand_state[NR_CPUS];
50
51 static u32 __random32(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 }
61
62 static void __set_random32(struct rnd_state *state, unsigned long s)
63 {
64         if (s == 0)
65                 s = 1;      /* default seed is 1 */
66
67 #define LCG(n) (69069 * n)
68         state->s1 = LCG(s);
69         state->s2 = LCG(state->s1);
70         state->s3 = LCG(state->s2);
71
72         /* "warm it up" */
73         __random32(state);
74         __random32(state);
75         __random32(state);
76         __random32(state);
77         __random32(state);
78         __random32(state);
79 }
80
81 /**
82  *      random32 - pseudo random number generator
83  *
84  *      A 32 bit pseudo-random number is generated using a fast
85  *      algorithm suitable for simulation. This algorithm is NOT
86  *      considered safe for cryptographic use.
87  */
88 u32 random32(void)
89 {
90         return __random32(&net_rand_state[smp_processor_id()]);
91 }
92 EXPORT_SYMBOL(random32);
93
94 /**
95  *      srandom32 - add entropy to pseudo random number generator
96  *      @seed: seed value
97  *
98  *      Add some additional seeding to the random32() pool.
99  *      Note: this pool is per cpu so it only affects current CPU.
100  */
101 void srandom32(u32 entropy)
102 {
103         struct rnd_state *state = &net_rand_state[smp_processor_id()];
104         __set_random32(state, state->s1 ^ entropy);
105 }
106 EXPORT_SYMBOL(srandom32);
107
108 static int __init random32_reseed(void);
109
110 /*
111  *      Generate some initially weak seeding values to allow
112  *      to start the random32() engine.
113  */
114 int __init random32_init(void)
115 {
116         int i;
117
118         for (i = 0; i < NR_CPUS; i++) {
119                 struct rnd_state *state = &net_rand_state[i];
120                 __set_random32(state, i + jiffies);
121         }
122         random32_reseed();
123         return 0;
124 }
125
126 /*
127  *      Generate better values after random number generator
128  *      is fully initalized.
129  */
130 static int __init random32_reseed(void)
131 {
132         int i;
133         unsigned long seed;
134
135         for (i = 0; i < NR_CPUS; i++) {
136                 struct rnd_state *state = &net_rand_state[i];
137
138                 get_random_bytes(&seed, sizeof(seed));
139                 __set_random32(state, seed);
140         }
141         return 0;
142 }