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/percpu.h> 38 #include <linux/module.h> 39 #include <linux/jiffies.h> 40 #include <linux/random.h> 41 42 struct rnd_state { 43 u32 s1, s2, s3; 44 }; 45 46 static DEFINE_PER_CPU(struct rnd_state, net_rand_state); 47 48 static 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 59 /* 60 * Handle minimum values for seeds 61 */ 62 static inline u32 __seed(u32 x, u32 m) 63 { 64 return (x < m) ? x + m : x; 65 } 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 */ 74 u32 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 } 82 EXPORT_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. 89 */ 90 void srandom32(u32 entropy) 91 { 92 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); 99 state->s1 = __seed(state->s1 ^ entropy, 1); 100 } 101 } 102 EXPORT_SYMBOL(srandom32); 103 104 /* 105 * Generate some initially weak seeding values to allow 106 * to start the random32() engine. 107 */ 108 static 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); 114 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); 127 } 128 return 0; 129 } 130 core_initcall(random32_init); 131 132 /* 133 * Generate better values after random number generator 134 * is fully initalized. 135 */ 136 static int __init random32_reseed(void) 137 { 138 int i; 139 140 for_each_possible_cpu(i) { 141 struct rnd_state *state = &per_cpu(net_rand_state,i); 142 u32 seeds[3]; 143 144 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); 151 } 152 return 0; 153 } 154 late_initcall(random32_reseed); 155