xref: /linux/lib/random32.c (revision aaa248f6c9c81b2683db7dbb0689cd5ed1c86d88)
1*aaa248f6SStephen Hemminger /*
2*aaa248f6SStephen Hemminger   This is a maximally equidistributed combined Tausworthe generator
3*aaa248f6SStephen Hemminger   based on code from GNU Scientific Library 1.5 (30 Jun 2004)
4*aaa248f6SStephen Hemminger 
5*aaa248f6SStephen Hemminger    x_n = (s1_n ^ s2_n ^ s3_n)
6*aaa248f6SStephen Hemminger 
7*aaa248f6SStephen Hemminger    s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
8*aaa248f6SStephen Hemminger    s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
9*aaa248f6SStephen Hemminger    s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
10*aaa248f6SStephen Hemminger 
11*aaa248f6SStephen Hemminger    The period of this generator is about 2^88.
12*aaa248f6SStephen Hemminger 
13*aaa248f6SStephen Hemminger    From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
14*aaa248f6SStephen Hemminger    Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
15*aaa248f6SStephen Hemminger 
16*aaa248f6SStephen Hemminger    This is available on the net from L'Ecuyer's home page,
17*aaa248f6SStephen Hemminger 
18*aaa248f6SStephen Hemminger    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19*aaa248f6SStephen Hemminger    ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
20*aaa248f6SStephen Hemminger 
21*aaa248f6SStephen Hemminger    There is an erratum in the paper "Tables of Maximally
22*aaa248f6SStephen Hemminger    Equidistributed Combined LFSR Generators", Mathematics of
23*aaa248f6SStephen Hemminger    Computation, 68, 225 (1999), 261--269:
24*aaa248f6SStephen Hemminger    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
25*aaa248f6SStephen Hemminger 
26*aaa248f6SStephen Hemminger         ... the k_j most significant bits of z_j must be non-
27*aaa248f6SStephen Hemminger         zero, for each j. (Note: this restriction also applies to the
28*aaa248f6SStephen Hemminger         computer code given in [4], but was mistakenly not mentioned in
29*aaa248f6SStephen Hemminger         that paper.)
30*aaa248f6SStephen Hemminger 
31*aaa248f6SStephen Hemminger    This affects the seeding procedure by imposing the requirement
32*aaa248f6SStephen Hemminger    s1 > 1, s2 > 7, s3 > 15.
33*aaa248f6SStephen Hemminger 
34*aaa248f6SStephen Hemminger */
35*aaa248f6SStephen Hemminger 
36*aaa248f6SStephen Hemminger #include <linux/types.h>
37*aaa248f6SStephen Hemminger #include <linux/percpu.h>
38*aaa248f6SStephen Hemminger #include <linux/module.h>
39*aaa248f6SStephen Hemminger #include <linux/random.h>
40*aaa248f6SStephen Hemminger 
41*aaa248f6SStephen Hemminger struct rnd_state {
42*aaa248f6SStephen Hemminger 	u32 s1, s2, s3;
43*aaa248f6SStephen Hemminger };
44*aaa248f6SStephen Hemminger 
45*aaa248f6SStephen Hemminger static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
46*aaa248f6SStephen Hemminger 
47*aaa248f6SStephen Hemminger static u32 __random32(struct rnd_state *state)
48*aaa248f6SStephen Hemminger {
49*aaa248f6SStephen Hemminger #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
50*aaa248f6SStephen Hemminger 
51*aaa248f6SStephen Hemminger 	state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
52*aaa248f6SStephen Hemminger 	state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
53*aaa248f6SStephen Hemminger 	state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
54*aaa248f6SStephen Hemminger 
55*aaa248f6SStephen Hemminger 	return (state->s1 ^ state->s2 ^ state->s3);
56*aaa248f6SStephen Hemminger }
57*aaa248f6SStephen Hemminger 
58*aaa248f6SStephen Hemminger static void __set_random32(struct rnd_state *state, unsigned long s)
59*aaa248f6SStephen Hemminger {
60*aaa248f6SStephen Hemminger 	if (s == 0)
61*aaa248f6SStephen Hemminger 		s = 1;      /* default seed is 1 */
62*aaa248f6SStephen Hemminger 
63*aaa248f6SStephen Hemminger #define LCG(n) (69069 * n)
64*aaa248f6SStephen Hemminger 	state->s1 = LCG(s);
65*aaa248f6SStephen Hemminger 	state->s2 = LCG(state->s1);
66*aaa248f6SStephen Hemminger 	state->s3 = LCG(state->s2);
67*aaa248f6SStephen Hemminger 
68*aaa248f6SStephen Hemminger 	/* "warm it up" */
69*aaa248f6SStephen Hemminger 	__random32(state);
70*aaa248f6SStephen Hemminger 	__random32(state);
71*aaa248f6SStephen Hemminger 	__random32(state);
72*aaa248f6SStephen Hemminger 	__random32(state);
73*aaa248f6SStephen Hemminger 	__random32(state);
74*aaa248f6SStephen Hemminger 	__random32(state);
75*aaa248f6SStephen Hemminger }
76*aaa248f6SStephen Hemminger 
77*aaa248f6SStephen Hemminger /**
78*aaa248f6SStephen Hemminger  *	random32 - pseudo random number generator
79*aaa248f6SStephen Hemminger  *
80*aaa248f6SStephen Hemminger  *	A 32 bit pseudo-random number is generated using a fast
81*aaa248f6SStephen Hemminger  *	algorithm suitable for simulation. This algorithm is NOT
82*aaa248f6SStephen Hemminger  *	considered safe for cryptographic use.
83*aaa248f6SStephen Hemminger  */
84*aaa248f6SStephen Hemminger u32 random32(void)
85*aaa248f6SStephen Hemminger {
86*aaa248f6SStephen Hemminger 	unsigned long r;
87*aaa248f6SStephen Hemminger 	struct rnd_state *state = &get_cpu_var(net_rand_state);
88*aaa248f6SStephen Hemminger 	r = __random32(state);
89*aaa248f6SStephen Hemminger 	put_cpu_var(state);
90*aaa248f6SStephen Hemminger 	return r;
91*aaa248f6SStephen Hemminger }
92*aaa248f6SStephen Hemminger EXPORT_SYMBOL(random32);
93*aaa248f6SStephen Hemminger 
94*aaa248f6SStephen Hemminger /**
95*aaa248f6SStephen Hemminger  *	srandom32 - add entropy to pseudo random number generator
96*aaa248f6SStephen Hemminger  *	@seed: seed value
97*aaa248f6SStephen Hemminger  *
98*aaa248f6SStephen Hemminger  *	Add some additional seeding to the random32() pool.
99*aaa248f6SStephen Hemminger  *	Note: this pool is per cpu so it only affects current CPU.
100*aaa248f6SStephen Hemminger  */
101*aaa248f6SStephen Hemminger void srandom32(u32 entropy)
102*aaa248f6SStephen Hemminger {
103*aaa248f6SStephen Hemminger 	struct rnd_state *state = &get_cpu_var(net_rand_state);
104*aaa248f6SStephen Hemminger 	__set_random32(state, state->s1 ^ entropy);
105*aaa248f6SStephen Hemminger 	put_cpu_var(state);
106*aaa248f6SStephen Hemminger }
107*aaa248f6SStephen Hemminger EXPORT_SYMBOL(srandom32);
108*aaa248f6SStephen Hemminger 
109*aaa248f6SStephen Hemminger /*
110*aaa248f6SStephen Hemminger  *	Generate some initially weak seeding values to allow
111*aaa248f6SStephen Hemminger  *	to start the random32() engine.
112*aaa248f6SStephen Hemminger  */
113*aaa248f6SStephen Hemminger static int __init random32_init(void)
114*aaa248f6SStephen Hemminger {
115*aaa248f6SStephen Hemminger 	int i;
116*aaa248f6SStephen Hemminger 
117*aaa248f6SStephen Hemminger 	for_each_possible_cpu(i) {
118*aaa248f6SStephen Hemminger 		struct rnd_state *state = &per_cpu(net_rand_state,i);
119*aaa248f6SStephen Hemminger 		__set_random32(state, i + jiffies);
120*aaa248f6SStephen Hemminger 	}
121*aaa248f6SStephen Hemminger 	return 0;
122*aaa248f6SStephen Hemminger }
123*aaa248f6SStephen Hemminger core_initcall(random32_init);
124*aaa248f6SStephen Hemminger 
125*aaa248f6SStephen Hemminger /*
126*aaa248f6SStephen Hemminger  *	Generate better values after random number generator
127*aaa248f6SStephen Hemminger  *	is fully initalized.
128*aaa248f6SStephen Hemminger  */
129*aaa248f6SStephen Hemminger static int __init random32_reseed(void)
130*aaa248f6SStephen Hemminger {
131*aaa248f6SStephen Hemminger 	int i;
132*aaa248f6SStephen Hemminger 	unsigned long seed;
133*aaa248f6SStephen Hemminger 
134*aaa248f6SStephen Hemminger 	for_each_possible_cpu(i) {
135*aaa248f6SStephen Hemminger 		struct rnd_state *state = &per_cpu(net_rand_state,i);
136*aaa248f6SStephen Hemminger 
137*aaa248f6SStephen Hemminger 		get_random_bytes(&seed, sizeof(seed));
138*aaa248f6SStephen Hemminger 		__set_random32(state, seed);
139*aaa248f6SStephen Hemminger 	}
140*aaa248f6SStephen Hemminger 	return 0;
141*aaa248f6SStephen Hemminger }
142*aaa248f6SStephen Hemminger late_initcall(random32_reseed);
143