random32.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. This is a maximally equidistributed combined Tausworthe generator
  3. based on code from GNU Scientific Library 1.5 (30 Jun 2004)
  4. x_n = (s1_n ^ s2_n ^ s3_n)
  5. s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  6. s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  7. s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  8. The period of this generator is about 2^88.
  9. From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  10. Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  11. This is available on the net from L'Ecuyer's home page,
  12. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  13. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
  14. There is an erratum in the paper "Tables of Maximally
  15. Equidistributed Combined LFSR Generators", Mathematics of
  16. Computation, 68, 225 (1999), 261--269:
  17. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  18. ... the k_j most significant bits of z_j must be non-
  19. zero, for each j. (Note: this restriction also applies to the
  20. computer code given in [4], but was mistakenly not mentioned in
  21. that paper.)
  22. This affects the seeding procedure by imposing the requirement
  23. s1 > 1, s2 > 7, s3 > 15.
  24. */
  25. #include <linux/types.h>
  26. #include <linux/percpu.h>
  27. #include <linux/export.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/random.h>
  30. #include <linux/timer.h>
  31. static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
  32. /**
  33. * prandom_u32_state - seeded pseudo-random number generator.
  34. * @state: pointer to state structure holding seeded state.
  35. *
  36. * This is used for pseudo-randomness with no outside seeding.
  37. * For more random results, use prandom_u32().
  38. */
  39. u32 prandom_u32_state(struct rnd_state *state)
  40. {
  41. #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
  42. state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
  43. state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
  44. state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
  45. return (state->s1 ^ state->s2 ^ state->s3);
  46. }
  47. EXPORT_SYMBOL(prandom_u32_state);
  48. /**
  49. * prandom_u32 - pseudo random number generator
  50. *
  51. * A 32 bit pseudo-random number is generated using a fast
  52. * algorithm suitable for simulation. This algorithm is NOT
  53. * considered safe for cryptographic use.
  54. */
  55. u32 prandom_u32(void)
  56. {
  57. unsigned long r;
  58. struct rnd_state *state = &get_cpu_var(net_rand_state);
  59. r = prandom_u32_state(state);
  60. put_cpu_var(state);
  61. return r;
  62. }
  63. EXPORT_SYMBOL(prandom_u32);
  64. /*
  65. * prandom_bytes_state - get the requested number of pseudo-random bytes
  66. *
  67. * @state: pointer to state structure holding seeded state.
  68. * @buf: where to copy the pseudo-random bytes to
  69. * @bytes: the requested number of bytes
  70. *
  71. * This is used for pseudo-randomness with no outside seeding.
  72. * For more random results, use prandom_bytes().
  73. */
  74. void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
  75. {
  76. unsigned char *p = buf;
  77. int i;
  78. for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
  79. u32 random = prandom_u32_state(state);
  80. int j;
  81. for (j = 0; j < sizeof(u32); j++) {
  82. p[i + j] = random;
  83. random >>= BITS_PER_BYTE;
  84. }
  85. }
  86. if (i < bytes) {
  87. u32 random = prandom_u32_state(state);
  88. for (; i < bytes; i++) {
  89. p[i] = random;
  90. random >>= BITS_PER_BYTE;
  91. }
  92. }
  93. }
  94. EXPORT_SYMBOL(prandom_bytes_state);
  95. /**
  96. * prandom_bytes - get the requested number of pseudo-random bytes
  97. * @buf: where to copy the pseudo-random bytes to
  98. * @bytes: the requested number of bytes
  99. */
  100. void prandom_bytes(void *buf, int bytes)
  101. {
  102. struct rnd_state *state = &get_cpu_var(net_rand_state);
  103. prandom_bytes_state(state, buf, bytes);
  104. put_cpu_var(state);
  105. }
  106. EXPORT_SYMBOL(prandom_bytes);
  107. /**
  108. * prandom_seed - add entropy to pseudo random number generator
  109. * @seed: seed value
  110. *
  111. * Add some additional seeding to the prandom pool.
  112. */
  113. void prandom_seed(u32 entropy)
  114. {
  115. int i;
  116. /*
  117. * No locking on the CPUs, but then somewhat random results are, well,
  118. * expected.
  119. */
  120. for_each_possible_cpu (i) {
  121. struct rnd_state *state = &per_cpu(net_rand_state, i);
  122. state->s1 = __seed(state->s1 ^ entropy, 2);
  123. prandom_u32_state(state);
  124. }
  125. }
  126. EXPORT_SYMBOL(prandom_seed);
  127. /*
  128. * Generate some initially weak seeding values to allow
  129. * to start the prandom_u32() engine.
  130. */
  131. static int __init prandom_init(void)
  132. {
  133. int i;
  134. for_each_possible_cpu(i) {
  135. struct rnd_state *state = &per_cpu(net_rand_state,i);
  136. #define LCG(x) ((x) * 69069) /* super-duper LCG */
  137. state->s1 = __seed(LCG(i + jiffies), 2);
  138. state->s2 = __seed(LCG(state->s1), 8);
  139. state->s3 = __seed(LCG(state->s2), 16);
  140. /* "warm it up" */
  141. prandom_u32_state(state);
  142. prandom_u32_state(state);
  143. prandom_u32_state(state);
  144. prandom_u32_state(state);
  145. prandom_u32_state(state);
  146. prandom_u32_state(state);
  147. }
  148. return 0;
  149. }
  150. core_initcall(prandom_init);
  151. static void __prandom_timer(unsigned long dontcare);
  152. static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
  153. static void __prandom_timer(unsigned long dontcare)
  154. {
  155. u32 entropy;
  156. get_random_bytes(&entropy, sizeof(entropy));
  157. prandom_seed(entropy);
  158. /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
  159. seed_timer.expires = jiffies + (40 * HZ + (prandom_u32() % (40 * HZ)));
  160. add_timer(&seed_timer);
  161. }
  162. static void prandom_start_seed_timer(void)
  163. {
  164. set_timer_slack(&seed_timer, HZ);
  165. seed_timer.expires = jiffies + 40 * HZ;
  166. add_timer(&seed_timer);
  167. }
  168. /*
  169. * Generate better values after random number generator
  170. * is fully initialized.
  171. */
  172. static void __prandom_reseed(bool late)
  173. {
  174. int i;
  175. unsigned long flags;
  176. static bool latch = false;
  177. static DEFINE_SPINLOCK(lock);
  178. /* only allow initial seeding (late == false) once */
  179. spin_lock_irqsave(&lock, flags);
  180. if (latch && !late)
  181. goto out;
  182. latch = true;
  183. for_each_possible_cpu(i) {
  184. struct rnd_state *state = &per_cpu(net_rand_state,i);
  185. u32 seeds[3];
  186. get_random_bytes(&seeds, sizeof(seeds));
  187. state->s1 = __seed(seeds[0], 2);
  188. state->s2 = __seed(seeds[1], 8);
  189. state->s3 = __seed(seeds[2], 16);
  190. /* mix it in */
  191. prandom_u32_state(state);
  192. }
  193. out:
  194. spin_unlock_irqrestore(&lock, flags);
  195. }
  196. void prandom_reseed_late(void)
  197. {
  198. __prandom_reseed(true);
  199. }
  200. static int __init prandom_reseed(void)
  201. {
  202. __prandom_reseed(false);
  203. prandom_start_seed_timer();
  204. return 0;
  205. }
  206. late_initcall(prandom_reseed);