Back to home page

LXR

 
 

    


0001 /*
0002  * This is a maximally equidistributed combined Tausworthe generator
0003  * based on code from GNU Scientific Library 1.5 (30 Jun 2004)
0004  *
0005  * lfsr113 version:
0006  *
0007  * x_n = (s1_n ^ s2_n ^ s3_n ^ s4_n)
0008  *
0009  * s1_{n+1} = (((s1_n & 4294967294) << 18) ^ (((s1_n <<  6) ^ s1_n) >> 13))
0010  * s2_{n+1} = (((s2_n & 4294967288) <<  2) ^ (((s2_n <<  2) ^ s2_n) >> 27))
0011  * s3_{n+1} = (((s3_n & 4294967280) <<  7) ^ (((s3_n << 13) ^ s3_n) >> 21))
0012  * s4_{n+1} = (((s4_n & 4294967168) << 13) ^ (((s4_n <<  3) ^ s4_n) >> 12))
0013  *
0014  * The period of this generator is about 2^113 (see erratum paper).
0015  *
0016  * From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
0017  * Generators", Mathematics of Computation, 65, 213 (1996), 203--213:
0018  * http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
0019  * ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
0020  *
0021  * There is an erratum in the paper "Tables of Maximally Equidistributed
0022  * Combined LFSR Generators", Mathematics of Computation, 68, 225 (1999),
0023  * 261--269: http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
0024  *
0025  *      ... the k_j most significant bits of z_j must be non-zero,
0026  *      for each j. (Note: this restriction also applies to the
0027  *      computer code given in [4], but was mistakenly not mentioned
0028  *      in that paper.)
0029  *
0030  * This affects the seeding procedure by imposing the requirement
0031  * s1 > 1, s2 > 7, s3 > 15, s4 > 127.
0032  */
0033 
0034 #include <linux/types.h>
0035 #include <linux/percpu.h>
0036 #include <linux/export.h>
0037 #include <linux/jiffies.h>
0038 #include <linux/random.h>
0039 #include <linux/sched.h>
0040 #include <asm/unaligned.h>
0041 
0042 #ifdef CONFIG_RANDOM32_SELFTEST
0043 static void __init prandom_state_selftest(void);
0044 #else
0045 static inline void prandom_state_selftest(void)
0046 {
0047 }
0048 #endif
0049 
0050 static DEFINE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy;
0051 
0052 /**
0053  *  prandom_u32_state - seeded pseudo-random number generator.
0054  *  @state: pointer to state structure holding seeded state.
0055  *
0056  *  This is used for pseudo-randomness with no outside seeding.
0057  *  For more random results, use prandom_u32().
0058  */
0059 u32 prandom_u32_state(struct rnd_state *state)
0060 {
0061 #define TAUSWORTHE(s, a, b, c, d) ((s & c) << d) ^ (((s << a) ^ s) >> b)
0062     state->s1 = TAUSWORTHE(state->s1,  6U, 13U, 4294967294U, 18U);
0063     state->s2 = TAUSWORTHE(state->s2,  2U, 27U, 4294967288U,  2U);
0064     state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U,  7U);
0065     state->s4 = TAUSWORTHE(state->s4,  3U, 12U, 4294967168U, 13U);
0066 
0067     return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4);
0068 }
0069 EXPORT_SYMBOL(prandom_u32_state);
0070 
0071 /**
0072  *  prandom_u32 - pseudo random number generator
0073  *
0074  *  A 32 bit pseudo-random number is generated using a fast
0075  *  algorithm suitable for simulation. This algorithm is NOT
0076  *  considered safe for cryptographic use.
0077  */
0078 u32 prandom_u32(void)
0079 {
0080     struct rnd_state *state = &get_cpu_var(net_rand_state);
0081     u32 res;
0082 
0083     res = prandom_u32_state(state);
0084     put_cpu_var(net_rand_state);
0085 
0086     return res;
0087 }
0088 EXPORT_SYMBOL(prandom_u32);
0089 
0090 /**
0091  *  prandom_bytes_state - get the requested number of pseudo-random bytes
0092  *
0093  *  @state: pointer to state structure holding seeded state.
0094  *  @buf: where to copy the pseudo-random bytes to
0095  *  @bytes: the requested number of bytes
0096  *
0097  *  This is used for pseudo-randomness with no outside seeding.
0098  *  For more random results, use prandom_bytes().
0099  */
0100 void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes)
0101 {
0102     u8 *ptr = buf;
0103 
0104     while (bytes >= sizeof(u32)) {
0105         put_unaligned(prandom_u32_state(state), (u32 *) ptr);
0106         ptr += sizeof(u32);
0107         bytes -= sizeof(u32);
0108     }
0109 
0110     if (bytes > 0) {
0111         u32 rem = prandom_u32_state(state);
0112         do {
0113             *ptr++ = (u8) rem;
0114             bytes--;
0115             rem >>= BITS_PER_BYTE;
0116         } while (bytes > 0);
0117     }
0118 }
0119 EXPORT_SYMBOL(prandom_bytes_state);
0120 
0121 /**
0122  *  prandom_bytes - get the requested number of pseudo-random bytes
0123  *  @buf: where to copy the pseudo-random bytes to
0124  *  @bytes: the requested number of bytes
0125  */
0126 void prandom_bytes(void *buf, size_t bytes)
0127 {
0128     struct rnd_state *state = &get_cpu_var(net_rand_state);
0129 
0130     prandom_bytes_state(state, buf, bytes);
0131     put_cpu_var(net_rand_state);
0132 }
0133 EXPORT_SYMBOL(prandom_bytes);
0134 
0135 static void prandom_warmup(struct rnd_state *state)
0136 {
0137     /* Calling RNG ten times to satisfy recurrence condition */
0138     prandom_u32_state(state);
0139     prandom_u32_state(state);
0140     prandom_u32_state(state);
0141     prandom_u32_state(state);
0142     prandom_u32_state(state);
0143     prandom_u32_state(state);
0144     prandom_u32_state(state);
0145     prandom_u32_state(state);
0146     prandom_u32_state(state);
0147     prandom_u32_state(state);
0148 }
0149 
0150 static u32 __extract_hwseed(void)
0151 {
0152     unsigned int val = 0;
0153 
0154     (void)(arch_get_random_seed_int(&val) ||
0155            arch_get_random_int(&val));
0156 
0157     return val;
0158 }
0159 
0160 static void prandom_seed_early(struct rnd_state *state, u32 seed,
0161                    bool mix_with_hwseed)
0162 {
0163 #define LCG(x)   ((x) * 69069U) /* super-duper LCG */
0164 #define HWSEED() (mix_with_hwseed ? __extract_hwseed() : 0)
0165     state->s1 = __seed(HWSEED() ^ LCG(seed),        2U);
0166     state->s2 = __seed(HWSEED() ^ LCG(state->s1),   8U);
0167     state->s3 = __seed(HWSEED() ^ LCG(state->s2),  16U);
0168     state->s4 = __seed(HWSEED() ^ LCG(state->s3), 128U);
0169 }
0170 
0171 /**
0172  *  prandom_seed - add entropy to pseudo random number generator
0173  *  @seed: seed value
0174  *
0175  *  Add some additional seeding to the prandom pool.
0176  */
0177 void prandom_seed(u32 entropy)
0178 {
0179     int i;
0180     /*
0181      * No locking on the CPUs, but then somewhat random results are, well,
0182      * expected.
0183      */
0184     for_each_possible_cpu(i) {
0185         struct rnd_state *state = &per_cpu(net_rand_state, i);
0186 
0187         state->s1 = __seed(state->s1 ^ entropy, 2U);
0188         prandom_warmup(state);
0189     }
0190 }
0191 EXPORT_SYMBOL(prandom_seed);
0192 
0193 /*
0194  *  Generate some initially weak seeding values to allow
0195  *  to start the prandom_u32() engine.
0196  */
0197 static int __init prandom_init(void)
0198 {
0199     int i;
0200 
0201     prandom_state_selftest();
0202 
0203     for_each_possible_cpu(i) {
0204         struct rnd_state *state = &per_cpu(net_rand_state, i);
0205         u32 weak_seed = (i + jiffies) ^ random_get_entropy();
0206 
0207         prandom_seed_early(state, weak_seed, true);
0208         prandom_warmup(state);
0209     }
0210 
0211     return 0;
0212 }
0213 core_initcall(prandom_init);
0214 
0215 static void __prandom_timer(unsigned long dontcare);
0216 
0217 static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
0218 
0219 static void __prandom_timer(unsigned long dontcare)
0220 {
0221     u32 entropy;
0222     unsigned long expires;
0223 
0224     get_random_bytes(&entropy, sizeof(entropy));
0225     prandom_seed(entropy);
0226 
0227     /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
0228     expires = 40 + prandom_u32_max(40);
0229     seed_timer.expires = jiffies + msecs_to_jiffies(expires * MSEC_PER_SEC);
0230 
0231     add_timer(&seed_timer);
0232 }
0233 
0234 static void __init __prandom_start_seed_timer(void)
0235 {
0236     seed_timer.expires = jiffies + msecs_to_jiffies(40 * MSEC_PER_SEC);
0237     add_timer(&seed_timer);
0238 }
0239 
0240 void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state)
0241 {
0242     int i;
0243 
0244     for_each_possible_cpu(i) {
0245         struct rnd_state *state = per_cpu_ptr(pcpu_state, i);
0246         u32 seeds[4];
0247 
0248         get_random_bytes(&seeds, sizeof(seeds));
0249         state->s1 = __seed(seeds[0],   2U);
0250         state->s2 = __seed(seeds[1],   8U);
0251         state->s3 = __seed(seeds[2],  16U);
0252         state->s4 = __seed(seeds[3], 128U);
0253 
0254         prandom_warmup(state);
0255     }
0256 }
0257 EXPORT_SYMBOL(prandom_seed_full_state);
0258 
0259 /*
0260  *  Generate better values after random number generator
0261  *  is fully initialized.
0262  */
0263 static void __prandom_reseed(bool late)
0264 {
0265     unsigned long flags;
0266     static bool latch = false;
0267     static DEFINE_SPINLOCK(lock);
0268 
0269     /* Asking for random bytes might result in bytes getting
0270      * moved into the nonblocking pool and thus marking it
0271      * as initialized. In this case we would double back into
0272      * this function and attempt to do a late reseed.
0273      * Ignore the pointless attempt to reseed again if we're
0274      * already waiting for bytes when the nonblocking pool
0275      * got initialized.
0276      */
0277 
0278     /* only allow initial seeding (late == false) once */
0279     if (!spin_trylock_irqsave(&lock, flags))
0280         return;
0281 
0282     if (latch && !late)
0283         goto out;
0284 
0285     latch = true;
0286     prandom_seed_full_state(&net_rand_state);
0287 out:
0288     spin_unlock_irqrestore(&lock, flags);
0289 }
0290 
0291 void prandom_reseed_late(void)
0292 {
0293     __prandom_reseed(true);
0294 }
0295 
0296 static int __init prandom_reseed(void)
0297 {
0298     __prandom_reseed(false);
0299     __prandom_start_seed_timer();
0300     return 0;
0301 }
0302 late_initcall(prandom_reseed);
0303 
0304 #ifdef CONFIG_RANDOM32_SELFTEST
0305 static struct prandom_test1 {
0306     u32 seed;
0307     u32 result;
0308 } test1[] = {
0309     { 1U, 3484351685U },
0310     { 2U, 2623130059U },
0311     { 3U, 3125133893U },
0312     { 4U,  984847254U },
0313 };
0314 
0315 static struct prandom_test2 {
0316     u32 seed;
0317     u32 iteration;
0318     u32 result;
0319 } test2[] = {
0320     /* Test cases against taus113 from GSL library. */
0321     {  931557656U, 959U, 2975593782U },
0322     { 1339693295U, 876U, 3887776532U },
0323     { 1545556285U, 961U, 1615538833U },
0324     {  601730776U, 723U, 1776162651U },
0325     { 1027516047U, 687U,  511983079U },
0326     {  416526298U, 700U,  916156552U },
0327     { 1395522032U, 652U, 2222063676U },
0328     {  366221443U, 617U, 2992857763U },
0329     { 1539836965U, 714U, 3783265725U },
0330     {  556206671U, 994U,  799626459U },
0331     {  684907218U, 799U,  367789491U },
0332     { 2121230701U, 931U, 2115467001U },
0333     { 1668516451U, 644U, 3620590685U },
0334     {  768046066U, 883U, 2034077390U },
0335     { 1989159136U, 833U, 1195767305U },
0336     {  536585145U, 996U, 3577259204U },
0337     { 1008129373U, 642U, 1478080776U },
0338     { 1740775604U, 939U, 1264980372U },
0339     { 1967883163U, 508U,   10734624U },
0340     { 1923019697U, 730U, 3821419629U },
0341     {  442079932U, 560U, 3440032343U },
0342     { 1961302714U, 845U,  841962572U },
0343     { 2030205964U, 962U, 1325144227U },
0344     { 1160407529U, 507U,  240940858U },
0345     {  635482502U, 779U, 4200489746U },
0346     { 1252788931U, 699U,  867195434U },
0347     { 1961817131U, 719U,  668237657U },
0348     { 1071468216U, 983U,  917876630U },
0349     { 1281848367U, 932U, 1003100039U },
0350     {  582537119U, 780U, 1127273778U },
0351     { 1973672777U, 853U, 1071368872U },
0352     { 1896756996U, 762U, 1127851055U },
0353     {  847917054U, 500U, 1717499075U },
0354     { 1240520510U, 951U, 2849576657U },
0355     { 1685071682U, 567U, 1961810396U },
0356     { 1516232129U, 557U,    3173877U },
0357     { 1208118903U, 612U, 1613145022U },
0358     { 1817269927U, 693U, 4279122573U },
0359     { 1510091701U, 717U,  638191229U },
0360     {  365916850U, 807U,  600424314U },
0361     {  399324359U, 702U, 1803598116U },
0362     { 1318480274U, 779U, 2074237022U },
0363     {  697758115U, 840U, 1483639402U },
0364     { 1696507773U, 840U,  577415447U },
0365     { 2081979121U, 981U, 3041486449U },
0366     {  955646687U, 742U, 3846494357U },
0367     { 1250683506U, 749U,  836419859U },
0368     {  595003102U, 534U,  366794109U },
0369     {   47485338U, 558U, 3521120834U },
0370     {  619433479U, 610U, 3991783875U },
0371     {  704096520U, 518U, 4139493852U },
0372     { 1712224984U, 606U, 2393312003U },
0373     { 1318233152U, 922U, 3880361134U },
0374     {  855572992U, 761U, 1472974787U },
0375     {   64721421U, 703U,  683860550U },
0376     {  678931758U, 840U,  380616043U },
0377     {  692711973U, 778U, 1382361947U },
0378     {  677703619U, 530U, 2826914161U },
0379     {   92393223U, 586U, 1522128471U },
0380     { 1222592920U, 743U, 3466726667U },
0381     {  358288986U, 695U, 1091956998U },
0382     { 1935056945U, 958U,  514864477U },
0383     {  735675993U, 990U, 1294239989U },
0384     { 1560089402U, 897U, 2238551287U },
0385     {   70616361U, 829U,   22483098U },
0386     {  368234700U, 731U, 2913875084U },
0387     {   20221190U, 879U, 1564152970U },
0388     {  539444654U, 682U, 1835141259U },
0389     { 1314987297U, 840U, 1801114136U },
0390     { 2019295544U, 645U, 3286438930U },
0391     {  469023838U, 716U, 1637918202U },
0392     { 1843754496U, 653U, 2562092152U },
0393     {  400672036U, 809U, 4264212785U },
0394     {  404722249U, 965U, 2704116999U },
0395     {  600702209U, 758U,  584979986U },
0396     {  519953954U, 667U, 2574436237U },
0397     { 1658071126U, 694U, 2214569490U },
0398     {  420480037U, 749U, 3430010866U },
0399     {  690103647U, 969U, 3700758083U },
0400     { 1029424799U, 937U, 3787746841U },
0401     { 2012608669U, 506U, 3362628973U },
0402     { 1535432887U, 998U,   42610943U },
0403     { 1330635533U, 857U, 3040806504U },
0404     { 1223800550U, 539U, 3954229517U },
0405     { 1322411537U, 680U, 3223250324U },
0406     { 1877847898U, 945U, 2915147143U },
0407     { 1646356099U, 874U,  965988280U },
0408     {  805687536U, 744U, 4032277920U },
0409     { 1948093210U, 633U, 1346597684U },
0410     {  392609744U, 783U, 1636083295U },
0411     {  690241304U, 770U, 1201031298U },
0412     { 1360302965U, 696U, 1665394461U },
0413     { 1220090946U, 780U, 1316922812U },
0414     {  447092251U, 500U, 3438743375U },
0415     { 1613868791U, 592U,  828546883U },
0416     {  523430951U, 548U, 2552392304U },
0417     {  726692899U, 810U, 1656872867U },
0418     { 1364340021U, 836U, 3710513486U },
0419     { 1986257729U, 931U,  935013962U },
0420     {  407983964U, 921U,  728767059U },
0421 };
0422 
0423 static void __init prandom_state_selftest(void)
0424 {
0425     int i, j, errors = 0, runs = 0;
0426     bool error = false;
0427 
0428     for (i = 0; i < ARRAY_SIZE(test1); i++) {
0429         struct rnd_state state;
0430 
0431         prandom_seed_early(&state, test1[i].seed, false);
0432         prandom_warmup(&state);
0433 
0434         if (test1[i].result != prandom_u32_state(&state))
0435             error = true;
0436     }
0437 
0438     if (error)
0439         pr_warn("prandom: seed boundary self test failed\n");
0440     else
0441         pr_info("prandom: seed boundary self test passed\n");
0442 
0443     for (i = 0; i < ARRAY_SIZE(test2); i++) {
0444         struct rnd_state state;
0445 
0446         prandom_seed_early(&state, test2[i].seed, false);
0447         prandom_warmup(&state);
0448 
0449         for (j = 0; j < test2[i].iteration - 1; j++)
0450             prandom_u32_state(&state);
0451 
0452         if (test2[i].result != prandom_u32_state(&state))
0453             errors++;
0454 
0455         runs++;
0456         cond_resched();
0457     }
0458 
0459     if (errors)
0460         pr_warn("prandom: %d/%d self tests failed\n", errors, runs);
0461     else
0462         pr_info("prandom: %d self tests passed\n", runs);
0463 }
0464 #endif