random32.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
  31. /**
  32. * prandom_u32_state - seeded pseudo-random number generator.
  33. * @state: pointer to state structure holding seeded state.
  34. *
  35. * This is used for pseudo-randomness with no outside seeding.
  36. * For more random results, use prandom_u32().
  37. */
  38. u32 prandom_u32_state(struct rnd_state *state)
  39. {
  40. #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
  41. state->s1 = TAUSWORTHE(state->s1, 13, 19, 4294967294UL, 12);
  42. state->s2 = TAUSWORTHE(state->s2, 2, 25, 4294967288UL, 4);
  43. state->s3 = TAUSWORTHE(state->s3, 3, 11, 4294967280UL, 17);
  44. return (state->s1 ^ state->s2 ^ state->s3);
  45. }
  46. EXPORT_SYMBOL(prandom_u32_state);
  47. /**
  48. * prandom_u32 - pseudo random number generator
  49. *
  50. * A 32 bit pseudo-random number is generated using a fast
  51. * algorithm suitable for simulation. This algorithm is NOT
  52. * considered safe for cryptographic use.
  53. */
  54. u32 prandom_u32(void)
  55. {
  56. unsigned long r;
  57. struct rnd_state *state = &get_cpu_var(net_rand_state);
  58. r = prandom_u32_state(state);
  59. put_cpu_var(state);
  60. return r;
  61. }
  62. EXPORT_SYMBOL(prandom_u32);
  63. /*
  64. * prandom_bytes_state - get the requested number of pseudo-random bytes
  65. *
  66. * @state: pointer to state structure holding seeded state.
  67. * @buf: where to copy the pseudo-random bytes to
  68. * @bytes: the requested number of bytes
  69. *
  70. * This is used for pseudo-randomness with no outside seeding.
  71. * For more random results, use prandom_bytes().
  72. */
  73. void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
  74. {
  75. unsigned char *p = buf;
  76. int i;
  77. for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
  78. u32 random = prandom_u32_state(state);
  79. int j;
  80. for (j = 0; j < sizeof(u32); j++) {
  81. p[i + j] = random;
  82. random >>= BITS_PER_BYTE;
  83. }
  84. }
  85. if (i < bytes) {
  86. u32 random = prandom_u32_state(state);
  87. for (; i < bytes; i++) {
  88. p[i] = random;
  89. random >>= BITS_PER_BYTE;
  90. }
  91. }
  92. }
  93. EXPORT_SYMBOL(prandom_bytes_state);
  94. /**
  95. * prandom_bytes - get the requested number of pseudo-random bytes
  96. * @buf: where to copy the pseudo-random bytes to
  97. * @bytes: the requested number of bytes
  98. */
  99. void prandom_bytes(void *buf, int bytes)
  100. {
  101. struct rnd_state *state = &get_cpu_var(net_rand_state);
  102. prandom_bytes_state(state, buf, bytes);
  103. put_cpu_var(state);
  104. }
  105. EXPORT_SYMBOL(prandom_bytes);
  106. /**
  107. * prandom_seed - add entropy to pseudo random number generator
  108. * @seed: seed value
  109. *
  110. * Add some additional seeding to the prandom pool.
  111. */
  112. void prandom_seed(u32 entropy)
  113. {
  114. int i;
  115. /*
  116. * No locking on the CPUs, but then somewhat random results are, well,
  117. * expected.
  118. */
  119. for_each_possible_cpu (i) {
  120. struct rnd_state *state = &per_cpu(net_rand_state, i);
  121. state->s1 = __seed(state->s1 ^ entropy, 1);
  122. }
  123. }
  124. EXPORT_SYMBOL(prandom_seed);
  125. /*
  126. * Generate some initially weak seeding values to allow
  127. * to start the prandom_u32() engine.
  128. */
  129. static int __init prandom_init(void)
  130. {
  131. int i;
  132. for_each_possible_cpu(i) {
  133. struct rnd_state *state = &per_cpu(net_rand_state,i);
  134. #define LCG(x) ((x) * 69069) /* super-duper LCG */
  135. state->s1 = __seed(LCG(i + jiffies), 1);
  136. state->s2 = __seed(LCG(state->s1), 7);
  137. state->s3 = __seed(LCG(state->s2), 15);
  138. /* "warm it up" */
  139. prandom_u32_state(state);
  140. prandom_u32_state(state);
  141. prandom_u32_state(state);
  142. prandom_u32_state(state);
  143. prandom_u32_state(state);
  144. prandom_u32_state(state);
  145. }
  146. return 0;
  147. }
  148. core_initcall(prandom_init);
  149. /*
  150. * Generate better values after random number generator
  151. * is fully initialized.
  152. */
  153. static int __init prandom_reseed(void)
  154. {
  155. int i;
  156. for_each_possible_cpu(i) {
  157. struct rnd_state *state = &per_cpu(net_rand_state,i);
  158. u32 seeds[3];
  159. get_random_bytes(&seeds, sizeof(seeds));
  160. state->s1 = __seed(seeds[0], 1);
  161. state->s2 = __seed(seeds[1], 7);
  162. state->s3 = __seed(seeds[2], 15);
  163. /* mix it in */
  164. prandom_u32_state(state);
  165. }
  166. return 0;
  167. }
  168. late_initcall(prandom_reseed);