2 This is a maximally equidistributed combined Tausworthe generator
3 based on code from GNU Scientific Library 1.5 (30 Jun 2004)
5 x_n = (s1_n ^ s2_n ^ s3_n)
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))
11 The period of this generator is about 2^88.
13 From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
14 Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
16 This is available on the net from L'Ecuyer's home page,
18 http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19 ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
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
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
31 This affects the seeding procedure by imposing the requirement
32 s1 > 1, s2 > 7, s3 > 15.
36 #include <linux/types.h>
37 #include <linux/percpu.h>
38 #include <linux/export.h>
39 #include <linux/jiffies.h>
40 #include <linux/random.h>
42 static DEFINE_PER_CPU(struct rnd_state
, net_rand_state
);
45 * prandom_u32_state - seeded pseudo-random number generator.
46 * @state: pointer to state structure holding seeded state.
48 * This is used for pseudo-randomness with no outside seeding.
49 * For more random results, use prandom_u32().
51 u32
prandom_u32_state(struct rnd_state
*state
)
53 #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
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);
59 return (state
->s1
^ state
->s2
^ state
->s3
);
61 EXPORT_SYMBOL(prandom_u32_state
);
64 * prandom_u32 - pseudo random number generator
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.
73 struct rnd_state
*state
= &get_cpu_var(net_rand_state
);
74 r
= prandom_u32_state(state
);
78 EXPORT_SYMBOL(prandom_u32
);
81 * prandom_bytes_state - get the requested number of pseudo-random bytes
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
87 * This is used for pseudo-randomness with no outside seeding.
88 * For more random results, use prandom_bytes().
90 void prandom_bytes_state(struct rnd_state
*state
, void *buf
, int bytes
)
92 unsigned char *p
= buf
;
95 for (i
= 0; i
< round_down(bytes
, sizeof(u32
)); i
+= sizeof(u32
)) {
96 u32 random
= prandom_u32_state(state
);
99 for (j
= 0; j
< sizeof(u32
); j
++) {
101 random
>>= BITS_PER_BYTE
;
105 u32 random
= prandom_u32_state(state
);
107 for (; i
< bytes
; i
++) {
109 random
>>= BITS_PER_BYTE
;
113 EXPORT_SYMBOL(prandom_bytes_state
);
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
120 void prandom_bytes(void *buf
, int bytes
)
122 struct rnd_state
*state
= &get_cpu_var(net_rand_state
);
124 prandom_bytes_state(state
, buf
, bytes
);
127 EXPORT_SYMBOL(prandom_bytes
);
130 * prandom_seed - add entropy to pseudo random number generator
133 * Add some additional seeding to the prandom pool.
135 void prandom_seed(u32 entropy
)
139 * No locking on the CPUs, but then somewhat random results are, well,
142 for_each_possible_cpu (i
) {
143 struct rnd_state
*state
= &per_cpu(net_rand_state
, i
);
144 state
->s1
= __seed(state
->s1
^ entropy
, 2);
145 prandom_u32_state(state
);
148 EXPORT_SYMBOL(prandom_seed
);
151 * Generate some initially weak seeding values to allow
152 * to start the prandom_u32() engine.
154 static int __init
prandom_init(void)
158 for_each_possible_cpu(i
) {
159 struct rnd_state
*state
= &per_cpu(net_rand_state
,i
);
161 #define LCG(x) ((x) * 69069) /* super-duper LCG */
162 state
->s1
= __seed(LCG(i
+ jiffies
), 2);
163 state
->s2
= __seed(LCG(state
->s1
), 8);
164 state
->s3
= __seed(LCG(state
->s2
), 16);
167 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
);
176 core_initcall(prandom_init
);
178 static void __prandom_timer(unsigned long dontcare
);
179 static DEFINE_TIMER(seed_timer
, __prandom_timer
, 0, 0);
181 static void __prandom_timer(unsigned long dontcare
)
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
);
192 static void prandom_start_seed_timer(void)
194 set_timer_slack(&seed_timer
, HZ
);
195 seed_timer
.expires
= jiffies
+ 40 * HZ
;
196 add_timer(&seed_timer
);
200 * Generate better values after random number generator
201 * is fully initialized.
203 static void __prandom_reseed(bool late
)
207 static bool latch
= false;
208 static DEFINE_SPINLOCK(lock
);
210 /* only allow initial seeding (late == false) once */
211 spin_lock_irqsave(&lock
, flags
);
216 for_each_possible_cpu(i
) {
217 struct rnd_state
*state
= &per_cpu(net_rand_state
,i
);
220 get_random_bytes(&seeds
, sizeof(seeds
));
221 state
->s1
= __seed(seeds
[0], 2);
222 state
->s2
= __seed(seeds
[1], 8);
223 state
->s3
= __seed(seeds
[2], 16);
226 prandom_u32_state(state
);
229 spin_unlock_irqrestore(&lock
, flags
);
232 void prandom_reseed_late(void)
234 __prandom_reseed(true);
237 static int __init
prandom_reseed(void)
239 __prandom_reseed(false);
240 prandom_start_seed_timer();
243 late_initcall(prandom_reseed
);
This page took 0.046307 seconds and 6 git commands to generate.