1aaa248f6SStephen Hemminger /* 2aaa248f6SStephen Hemminger This is a maximally equidistributed combined Tausworthe generator 3aaa248f6SStephen Hemminger based on code from GNU Scientific Library 1.5 (30 Jun 2004) 4aaa248f6SStephen Hemminger 5a98814ceSDaniel Borkmann lfsr113 version: 6aaa248f6SStephen Hemminger 7a98814ceSDaniel Borkmann x_n = (s1_n ^ s2_n ^ s3_n ^ s4_n) 8aaa248f6SStephen Hemminger 9a98814ceSDaniel Borkmann s1_{n+1} = (((s1_n & 4294967294) << 18) ^ (((s1_n << 6) ^ s1_n) >> 13)) 10a98814ceSDaniel Borkmann s2_{n+1} = (((s2_n & 4294967288) << 2) ^ (((s2_n << 2) ^ s2_n) >> 27)) 11a98814ceSDaniel Borkmann s3_{n+1} = (((s3_n & 4294967280) << 7) ^ (((s3_n << 13) ^ s3_n) >> 21)) 12a98814ceSDaniel Borkmann s4_{n+1} = (((s4_n & 4294967168) << 13) ^ (((s4_n << 3) ^ s4_n) >> 12)) 13a98814ceSDaniel Borkmann 14a98814ceSDaniel Borkmann The period of this generator is about 2^113 (see erratum paper). 15aaa248f6SStephen Hemminger 16aaa248f6SStephen Hemminger From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe 17a98814ceSDaniel Borkmann Generators", Mathematics of Computation, 65, 213 (1996), 203--213: 18aaa248f6SStephen Hemminger http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps 19aaa248f6SStephen Hemminger ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps 20aaa248f6SStephen Hemminger 21aaa248f6SStephen Hemminger There is an erratum in the paper "Tables of Maximally 22aaa248f6SStephen Hemminger Equidistributed Combined LFSR Generators", Mathematics of 23aaa248f6SStephen Hemminger Computation, 68, 225 (1999), 261--269: 24aaa248f6SStephen Hemminger http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 25aaa248f6SStephen Hemminger 26aaa248f6SStephen Hemminger ... the k_j most significant bits of z_j must be non- 27aaa248f6SStephen Hemminger zero, for each j. (Note: this restriction also applies to the 28aaa248f6SStephen Hemminger computer code given in [4], but was mistakenly not mentioned in 29aaa248f6SStephen Hemminger that paper.) 30aaa248f6SStephen Hemminger 31aaa248f6SStephen Hemminger This affects the seeding procedure by imposing the requirement 32a98814ceSDaniel Borkmann s1 > 1, s2 > 7, s3 > 15, s4 > 127. 33aaa248f6SStephen Hemminger 34aaa248f6SStephen Hemminger */ 35aaa248f6SStephen Hemminger 36aaa248f6SStephen Hemminger #include <linux/types.h> 37aaa248f6SStephen Hemminger #include <linux/percpu.h> 388bc3bcc9SPaul Gortmaker #include <linux/export.h> 39f6a57033SAl Viro #include <linux/jiffies.h> 40aaa248f6SStephen Hemminger #include <linux/random.h> 41*a6a9c0f1SDaniel Borkmann #include <linux/sched.h> 42*a6a9c0f1SDaniel Borkmann 43*a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST 44*a6a9c0f1SDaniel Borkmann static void __init prandom_state_selftest(void); 45*a6a9c0f1SDaniel Borkmann #endif 46aaa248f6SStephen Hemminger 47aaa248f6SStephen Hemminger static DEFINE_PER_CPU(struct rnd_state, net_rand_state); 48aaa248f6SStephen Hemminger 495960164fSJoe Eykholt /** 50496f2f93SAkinobu Mita * prandom_u32_state - seeded pseudo-random number generator. 515960164fSJoe Eykholt * @state: pointer to state structure holding seeded state. 525960164fSJoe Eykholt * 535960164fSJoe Eykholt * This is used for pseudo-randomness with no outside seeding. 54496f2f93SAkinobu Mita * For more random results, use prandom_u32(). 555960164fSJoe Eykholt */ 56496f2f93SAkinobu Mita u32 prandom_u32_state(struct rnd_state *state) 57aaa248f6SStephen Hemminger { 58aaa248f6SStephen Hemminger #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b) 59aaa248f6SStephen Hemminger 60a98814ceSDaniel Borkmann state->s1 = TAUSWORTHE(state->s1, 6U, 13U, 4294967294U, 18U); 61a98814ceSDaniel Borkmann state->s2 = TAUSWORTHE(state->s2, 2U, 27U, 4294967288U, 2U); 62a98814ceSDaniel Borkmann state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U, 7U); 63a98814ceSDaniel Borkmann state->s4 = TAUSWORTHE(state->s4, 3U, 12U, 4294967168U, 13U); 64aaa248f6SStephen Hemminger 65a98814ceSDaniel Borkmann return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4); 66aaa248f6SStephen Hemminger } 67496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_u32_state); 68aaa248f6SStephen Hemminger 69aaa248f6SStephen Hemminger /** 70496f2f93SAkinobu Mita * prandom_u32 - pseudo random number generator 71aaa248f6SStephen Hemminger * 72aaa248f6SStephen Hemminger * A 32 bit pseudo-random number is generated using a fast 73aaa248f6SStephen Hemminger * algorithm suitable for simulation. This algorithm is NOT 74aaa248f6SStephen Hemminger * considered safe for cryptographic use. 75aaa248f6SStephen Hemminger */ 76496f2f93SAkinobu Mita u32 prandom_u32(void) 77aaa248f6SStephen Hemminger { 78aaa248f6SStephen Hemminger unsigned long r; 79aaa248f6SStephen Hemminger struct rnd_state *state = &get_cpu_var(net_rand_state); 80496f2f93SAkinobu Mita r = prandom_u32_state(state); 81aaa248f6SStephen Hemminger put_cpu_var(state); 82aaa248f6SStephen Hemminger return r; 83aaa248f6SStephen Hemminger } 84496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_u32); 85aaa248f6SStephen Hemminger 866582c665SAkinobu Mita /* 876582c665SAkinobu Mita * prandom_bytes_state - get the requested number of pseudo-random bytes 886582c665SAkinobu Mita * 896582c665SAkinobu Mita * @state: pointer to state structure holding seeded state. 906582c665SAkinobu Mita * @buf: where to copy the pseudo-random bytes to 916582c665SAkinobu Mita * @bytes: the requested number of bytes 926582c665SAkinobu Mita * 936582c665SAkinobu Mita * This is used for pseudo-randomness with no outside seeding. 946582c665SAkinobu Mita * For more random results, use prandom_bytes(). 956582c665SAkinobu Mita */ 966582c665SAkinobu Mita void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes) 976582c665SAkinobu Mita { 986582c665SAkinobu Mita unsigned char *p = buf; 996582c665SAkinobu Mita int i; 1006582c665SAkinobu Mita 1016582c665SAkinobu Mita for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) { 1026582c665SAkinobu Mita u32 random = prandom_u32_state(state); 1036582c665SAkinobu Mita int j; 1046582c665SAkinobu Mita 1056582c665SAkinobu Mita for (j = 0; j < sizeof(u32); j++) { 1066582c665SAkinobu Mita p[i + j] = random; 1076582c665SAkinobu Mita random >>= BITS_PER_BYTE; 1086582c665SAkinobu Mita } 1096582c665SAkinobu Mita } 1106582c665SAkinobu Mita if (i < bytes) { 1116582c665SAkinobu Mita u32 random = prandom_u32_state(state); 1126582c665SAkinobu Mita 1136582c665SAkinobu Mita for (; i < bytes; i++) { 1146582c665SAkinobu Mita p[i] = random; 1156582c665SAkinobu Mita random >>= BITS_PER_BYTE; 1166582c665SAkinobu Mita } 1176582c665SAkinobu Mita } 1186582c665SAkinobu Mita } 1196582c665SAkinobu Mita EXPORT_SYMBOL(prandom_bytes_state); 1206582c665SAkinobu Mita 1216582c665SAkinobu Mita /** 1226582c665SAkinobu Mita * prandom_bytes - get the requested number of pseudo-random bytes 1236582c665SAkinobu Mita * @buf: where to copy the pseudo-random bytes to 1246582c665SAkinobu Mita * @bytes: the requested number of bytes 1256582c665SAkinobu Mita */ 1266582c665SAkinobu Mita void prandom_bytes(void *buf, int bytes) 1276582c665SAkinobu Mita { 1286582c665SAkinobu Mita struct rnd_state *state = &get_cpu_var(net_rand_state); 1296582c665SAkinobu Mita 1306582c665SAkinobu Mita prandom_bytes_state(state, buf, bytes); 1316582c665SAkinobu Mita put_cpu_var(state); 1326582c665SAkinobu Mita } 1336582c665SAkinobu Mita EXPORT_SYMBOL(prandom_bytes); 1346582c665SAkinobu Mita 135a98814ceSDaniel Borkmann static void prandom_warmup(struct rnd_state *state) 136a98814ceSDaniel Borkmann { 137a98814ceSDaniel Borkmann /* Calling RNG ten times to satify recurrence condition */ 138a98814ceSDaniel Borkmann prandom_u32_state(state); 139a98814ceSDaniel Borkmann prandom_u32_state(state); 140a98814ceSDaniel Borkmann prandom_u32_state(state); 141a98814ceSDaniel Borkmann prandom_u32_state(state); 142a98814ceSDaniel Borkmann prandom_u32_state(state); 143a98814ceSDaniel Borkmann prandom_u32_state(state); 144a98814ceSDaniel Borkmann prandom_u32_state(state); 145a98814ceSDaniel Borkmann prandom_u32_state(state); 146a98814ceSDaniel Borkmann prandom_u32_state(state); 147a98814ceSDaniel Borkmann prandom_u32_state(state); 148a98814ceSDaniel Borkmann } 149a98814ceSDaniel Borkmann 150*a6a9c0f1SDaniel Borkmann static void prandom_seed_very_weak(struct rnd_state *state, u32 seed) 151*a6a9c0f1SDaniel Borkmann { 152*a6a9c0f1SDaniel Borkmann /* Note: This sort of seeding is ONLY used in test cases and 153*a6a9c0f1SDaniel Borkmann * during boot at the time from core_initcall until late_initcall 154*a6a9c0f1SDaniel Borkmann * as we don't have a stronger entropy source available yet. 155*a6a9c0f1SDaniel Borkmann * After late_initcall, we reseed entire state, we have to (!), 156*a6a9c0f1SDaniel Borkmann * otherwise an attacker just needs to search 32 bit space to 157*a6a9c0f1SDaniel Borkmann * probe for our internal 128 bit state if he knows a couple 158*a6a9c0f1SDaniel Borkmann * of prandom32 outputs! 159*a6a9c0f1SDaniel Borkmann */ 160*a6a9c0f1SDaniel Borkmann #define LCG(x) ((x) * 69069U) /* super-duper LCG */ 161*a6a9c0f1SDaniel Borkmann state->s1 = __seed(LCG(seed), 2U); 162*a6a9c0f1SDaniel Borkmann state->s2 = __seed(LCG(state->s1), 8U); 163*a6a9c0f1SDaniel Borkmann state->s3 = __seed(LCG(state->s2), 16U); 164*a6a9c0f1SDaniel Borkmann state->s4 = __seed(LCG(state->s3), 128U); 165*a6a9c0f1SDaniel Borkmann } 166*a6a9c0f1SDaniel Borkmann 167aaa248f6SStephen Hemminger /** 168496f2f93SAkinobu Mita * prandom_seed - add entropy to pseudo random number generator 169aaa248f6SStephen Hemminger * @seed: seed value 170aaa248f6SStephen Hemminger * 171496f2f93SAkinobu Mita * Add some additional seeding to the prandom pool. 172aaa248f6SStephen Hemminger */ 173496f2f93SAkinobu Mita void prandom_seed(u32 entropy) 174aaa248f6SStephen Hemminger { 17561407f80SAndi Kleen int i; 17661407f80SAndi Kleen /* 17761407f80SAndi Kleen * No locking on the CPUs, but then somewhat random results are, well, 17861407f80SAndi Kleen * expected. 17961407f80SAndi Kleen */ 18061407f80SAndi Kleen for_each_possible_cpu (i) { 18161407f80SAndi Kleen struct rnd_state *state = &per_cpu(net_rand_state, i); 182a98814ceSDaniel Borkmann 183a98814ceSDaniel Borkmann state->s1 = __seed(state->s1 ^ entropy, 2U); 184a98814ceSDaniel Borkmann prandom_warmup(state); 18561407f80SAndi Kleen } 186aaa248f6SStephen Hemminger } 187496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_seed); 188aaa248f6SStephen Hemminger 189aaa248f6SStephen Hemminger /* 190aaa248f6SStephen Hemminger * Generate some initially weak seeding values to allow 191496f2f93SAkinobu Mita * to start the prandom_u32() engine. 192aaa248f6SStephen Hemminger */ 193496f2f93SAkinobu Mita static int __init prandom_init(void) 194aaa248f6SStephen Hemminger { 195aaa248f6SStephen Hemminger int i; 196aaa248f6SStephen Hemminger 197*a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST 198*a6a9c0f1SDaniel Borkmann prandom_state_selftest(); 199*a6a9c0f1SDaniel Borkmann #endif 200*a6a9c0f1SDaniel Borkmann 201aaa248f6SStephen Hemminger for_each_possible_cpu(i) { 202aaa248f6SStephen Hemminger struct rnd_state *state = &per_cpu(net_rand_state,i); 203697f8d03SStephen Hemminger 204*a6a9c0f1SDaniel Borkmann prandom_seed_very_weak(state, (i + jiffies) ^ random_get_entropy()); 205a98814ceSDaniel Borkmann prandom_warmup(state); 206aaa248f6SStephen Hemminger } 207aaa248f6SStephen Hemminger return 0; 208aaa248f6SStephen Hemminger } 209496f2f93SAkinobu Mita core_initcall(prandom_init); 210aaa248f6SStephen Hemminger 2116d319202SHannes Frederic Sowa static void __prandom_timer(unsigned long dontcare); 2126d319202SHannes Frederic Sowa static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0); 2136d319202SHannes Frederic Sowa 2146d319202SHannes Frederic Sowa static void __prandom_timer(unsigned long dontcare) 2156d319202SHannes Frederic Sowa { 2166d319202SHannes Frederic Sowa u32 entropy; 2176d319202SHannes Frederic Sowa 2186d319202SHannes Frederic Sowa get_random_bytes(&entropy, sizeof(entropy)); 2196d319202SHannes Frederic Sowa prandom_seed(entropy); 2206d319202SHannes Frederic Sowa /* reseed every ~60 seconds, in [40 .. 80) interval with slack */ 2216d319202SHannes Frederic Sowa seed_timer.expires = jiffies + (40 * HZ + (prandom_u32() % (40 * HZ))); 2226d319202SHannes Frederic Sowa add_timer(&seed_timer); 2236d319202SHannes Frederic Sowa } 2246d319202SHannes Frederic Sowa 2256d319202SHannes Frederic Sowa static void prandom_start_seed_timer(void) 2266d319202SHannes Frederic Sowa { 2276d319202SHannes Frederic Sowa set_timer_slack(&seed_timer, HZ); 2286d319202SHannes Frederic Sowa seed_timer.expires = jiffies + 40 * HZ; 2296d319202SHannes Frederic Sowa add_timer(&seed_timer); 2306d319202SHannes Frederic Sowa } 2316d319202SHannes Frederic Sowa 232aaa248f6SStephen Hemminger /* 233aaa248f6SStephen Hemminger * Generate better values after random number generator 234421f91d2SUwe Kleine-König * is fully initialized. 235aaa248f6SStephen Hemminger */ 2364af712e8SHannes Frederic Sowa static void __prandom_reseed(bool late) 237aaa248f6SStephen Hemminger { 238aaa248f6SStephen Hemminger int i; 2394af712e8SHannes Frederic Sowa unsigned long flags; 2404af712e8SHannes Frederic Sowa static bool latch = false; 2414af712e8SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 2424af712e8SHannes Frederic Sowa 2434af712e8SHannes Frederic Sowa /* only allow initial seeding (late == false) once */ 2444af712e8SHannes Frederic Sowa spin_lock_irqsave(&lock, flags); 2454af712e8SHannes Frederic Sowa if (latch && !late) 2464af712e8SHannes Frederic Sowa goto out; 2474af712e8SHannes Frederic Sowa latch = true; 248aaa248f6SStephen Hemminger 249aaa248f6SStephen Hemminger for_each_possible_cpu(i) { 250aaa248f6SStephen Hemminger struct rnd_state *state = &per_cpu(net_rand_state,i); 251a98814ceSDaniel Borkmann u32 seeds[4]; 252aaa248f6SStephen Hemminger 253697f8d03SStephen Hemminger get_random_bytes(&seeds, sizeof(seeds)); 254a98814ceSDaniel Borkmann state->s1 = __seed(seeds[0], 2U); 255a98814ceSDaniel Borkmann state->s2 = __seed(seeds[1], 8U); 256a98814ceSDaniel Borkmann state->s3 = __seed(seeds[2], 16U); 257a98814ceSDaniel Borkmann state->s4 = __seed(seeds[3], 128U); 258697f8d03SStephen Hemminger 259a98814ceSDaniel Borkmann prandom_warmup(state); 260aaa248f6SStephen Hemminger } 2614af712e8SHannes Frederic Sowa out: 2624af712e8SHannes Frederic Sowa spin_unlock_irqrestore(&lock, flags); 2634af712e8SHannes Frederic Sowa } 2644af712e8SHannes Frederic Sowa 2654af712e8SHannes Frederic Sowa void prandom_reseed_late(void) 2664af712e8SHannes Frederic Sowa { 2674af712e8SHannes Frederic Sowa __prandom_reseed(true); 2684af712e8SHannes Frederic Sowa } 2694af712e8SHannes Frederic Sowa 2704af712e8SHannes Frederic Sowa static int __init prandom_reseed(void) 2714af712e8SHannes Frederic Sowa { 2724af712e8SHannes Frederic Sowa __prandom_reseed(false); 2736d319202SHannes Frederic Sowa prandom_start_seed_timer(); 274aaa248f6SStephen Hemminger return 0; 275aaa248f6SStephen Hemminger } 276496f2f93SAkinobu Mita late_initcall(prandom_reseed); 277*a6a9c0f1SDaniel Borkmann 278*a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST 279*a6a9c0f1SDaniel Borkmann static struct prandom_test1 { 280*a6a9c0f1SDaniel Borkmann u32 seed; 281*a6a9c0f1SDaniel Borkmann u32 result; 282*a6a9c0f1SDaniel Borkmann } test1[] = { 283*a6a9c0f1SDaniel Borkmann { 1U, 3484351685U }, 284*a6a9c0f1SDaniel Borkmann { 2U, 2623130059U }, 285*a6a9c0f1SDaniel Borkmann { 3U, 3125133893U }, 286*a6a9c0f1SDaniel Borkmann { 4U, 984847254U }, 287*a6a9c0f1SDaniel Borkmann }; 288*a6a9c0f1SDaniel Borkmann 289*a6a9c0f1SDaniel Borkmann static struct prandom_test2 { 290*a6a9c0f1SDaniel Borkmann u32 seed; 291*a6a9c0f1SDaniel Borkmann u32 iteration; 292*a6a9c0f1SDaniel Borkmann u32 result; 293*a6a9c0f1SDaniel Borkmann } test2[] = { 294*a6a9c0f1SDaniel Borkmann /* Test cases against taus113 from GSL library. */ 295*a6a9c0f1SDaniel Borkmann { 931557656U, 959U, 2975593782U }, 296*a6a9c0f1SDaniel Borkmann { 1339693295U, 876U, 3887776532U }, 297*a6a9c0f1SDaniel Borkmann { 1545556285U, 961U, 1615538833U }, 298*a6a9c0f1SDaniel Borkmann { 601730776U, 723U, 1776162651U }, 299*a6a9c0f1SDaniel Borkmann { 1027516047U, 687U, 511983079U }, 300*a6a9c0f1SDaniel Borkmann { 416526298U, 700U, 916156552U }, 301*a6a9c0f1SDaniel Borkmann { 1395522032U, 652U, 2222063676U }, 302*a6a9c0f1SDaniel Borkmann { 366221443U, 617U, 2992857763U }, 303*a6a9c0f1SDaniel Borkmann { 1539836965U, 714U, 3783265725U }, 304*a6a9c0f1SDaniel Borkmann { 556206671U, 994U, 799626459U }, 305*a6a9c0f1SDaniel Borkmann { 684907218U, 799U, 367789491U }, 306*a6a9c0f1SDaniel Borkmann { 2121230701U, 931U, 2115467001U }, 307*a6a9c0f1SDaniel Borkmann { 1668516451U, 644U, 3620590685U }, 308*a6a9c0f1SDaniel Borkmann { 768046066U, 883U, 2034077390U }, 309*a6a9c0f1SDaniel Borkmann { 1989159136U, 833U, 1195767305U }, 310*a6a9c0f1SDaniel Borkmann { 536585145U, 996U, 3577259204U }, 311*a6a9c0f1SDaniel Borkmann { 1008129373U, 642U, 1478080776U }, 312*a6a9c0f1SDaniel Borkmann { 1740775604U, 939U, 1264980372U }, 313*a6a9c0f1SDaniel Borkmann { 1967883163U, 508U, 10734624U }, 314*a6a9c0f1SDaniel Borkmann { 1923019697U, 730U, 3821419629U }, 315*a6a9c0f1SDaniel Borkmann { 442079932U, 560U, 3440032343U }, 316*a6a9c0f1SDaniel Borkmann { 1961302714U, 845U, 841962572U }, 317*a6a9c0f1SDaniel Borkmann { 2030205964U, 962U, 1325144227U }, 318*a6a9c0f1SDaniel Borkmann { 1160407529U, 507U, 240940858U }, 319*a6a9c0f1SDaniel Borkmann { 635482502U, 779U, 4200489746U }, 320*a6a9c0f1SDaniel Borkmann { 1252788931U, 699U, 867195434U }, 321*a6a9c0f1SDaniel Borkmann { 1961817131U, 719U, 668237657U }, 322*a6a9c0f1SDaniel Borkmann { 1071468216U, 983U, 917876630U }, 323*a6a9c0f1SDaniel Borkmann { 1281848367U, 932U, 1003100039U }, 324*a6a9c0f1SDaniel Borkmann { 582537119U, 780U, 1127273778U }, 325*a6a9c0f1SDaniel Borkmann { 1973672777U, 853U, 1071368872U }, 326*a6a9c0f1SDaniel Borkmann { 1896756996U, 762U, 1127851055U }, 327*a6a9c0f1SDaniel Borkmann { 847917054U, 500U, 1717499075U }, 328*a6a9c0f1SDaniel Borkmann { 1240520510U, 951U, 2849576657U }, 329*a6a9c0f1SDaniel Borkmann { 1685071682U, 567U, 1961810396U }, 330*a6a9c0f1SDaniel Borkmann { 1516232129U, 557U, 3173877U }, 331*a6a9c0f1SDaniel Borkmann { 1208118903U, 612U, 1613145022U }, 332*a6a9c0f1SDaniel Borkmann { 1817269927U, 693U, 4279122573U }, 333*a6a9c0f1SDaniel Borkmann { 1510091701U, 717U, 638191229U }, 334*a6a9c0f1SDaniel Borkmann { 365916850U, 807U, 600424314U }, 335*a6a9c0f1SDaniel Borkmann { 399324359U, 702U, 1803598116U }, 336*a6a9c0f1SDaniel Borkmann { 1318480274U, 779U, 2074237022U }, 337*a6a9c0f1SDaniel Borkmann { 697758115U, 840U, 1483639402U }, 338*a6a9c0f1SDaniel Borkmann { 1696507773U, 840U, 577415447U }, 339*a6a9c0f1SDaniel Borkmann { 2081979121U, 981U, 3041486449U }, 340*a6a9c0f1SDaniel Borkmann { 955646687U, 742U, 3846494357U }, 341*a6a9c0f1SDaniel Borkmann { 1250683506U, 749U, 836419859U }, 342*a6a9c0f1SDaniel Borkmann { 595003102U, 534U, 366794109U }, 343*a6a9c0f1SDaniel Borkmann { 47485338U, 558U, 3521120834U }, 344*a6a9c0f1SDaniel Borkmann { 619433479U, 610U, 3991783875U }, 345*a6a9c0f1SDaniel Borkmann { 704096520U, 518U, 4139493852U }, 346*a6a9c0f1SDaniel Borkmann { 1712224984U, 606U, 2393312003U }, 347*a6a9c0f1SDaniel Borkmann { 1318233152U, 922U, 3880361134U }, 348*a6a9c0f1SDaniel Borkmann { 855572992U, 761U, 1472974787U }, 349*a6a9c0f1SDaniel Borkmann { 64721421U, 703U, 683860550U }, 350*a6a9c0f1SDaniel Borkmann { 678931758U, 840U, 380616043U }, 351*a6a9c0f1SDaniel Borkmann { 692711973U, 778U, 1382361947U }, 352*a6a9c0f1SDaniel Borkmann { 677703619U, 530U, 2826914161U }, 353*a6a9c0f1SDaniel Borkmann { 92393223U, 586U, 1522128471U }, 354*a6a9c0f1SDaniel Borkmann { 1222592920U, 743U, 3466726667U }, 355*a6a9c0f1SDaniel Borkmann { 358288986U, 695U, 1091956998U }, 356*a6a9c0f1SDaniel Borkmann { 1935056945U, 958U, 514864477U }, 357*a6a9c0f1SDaniel Borkmann { 735675993U, 990U, 1294239989U }, 358*a6a9c0f1SDaniel Borkmann { 1560089402U, 897U, 2238551287U }, 359*a6a9c0f1SDaniel Borkmann { 70616361U, 829U, 22483098U }, 360*a6a9c0f1SDaniel Borkmann { 368234700U, 731U, 2913875084U }, 361*a6a9c0f1SDaniel Borkmann { 20221190U, 879U, 1564152970U }, 362*a6a9c0f1SDaniel Borkmann { 539444654U, 682U, 1835141259U }, 363*a6a9c0f1SDaniel Borkmann { 1314987297U, 840U, 1801114136U }, 364*a6a9c0f1SDaniel Borkmann { 2019295544U, 645U, 3286438930U }, 365*a6a9c0f1SDaniel Borkmann { 469023838U, 716U, 1637918202U }, 366*a6a9c0f1SDaniel Borkmann { 1843754496U, 653U, 2562092152U }, 367*a6a9c0f1SDaniel Borkmann { 400672036U, 809U, 4264212785U }, 368*a6a9c0f1SDaniel Borkmann { 404722249U, 965U, 2704116999U }, 369*a6a9c0f1SDaniel Borkmann { 600702209U, 758U, 584979986U }, 370*a6a9c0f1SDaniel Borkmann { 519953954U, 667U, 2574436237U }, 371*a6a9c0f1SDaniel Borkmann { 1658071126U, 694U, 2214569490U }, 372*a6a9c0f1SDaniel Borkmann { 420480037U, 749U, 3430010866U }, 373*a6a9c0f1SDaniel Borkmann { 690103647U, 969U, 3700758083U }, 374*a6a9c0f1SDaniel Borkmann { 1029424799U, 937U, 3787746841U }, 375*a6a9c0f1SDaniel Borkmann { 2012608669U, 506U, 3362628973U }, 376*a6a9c0f1SDaniel Borkmann { 1535432887U, 998U, 42610943U }, 377*a6a9c0f1SDaniel Borkmann { 1330635533U, 857U, 3040806504U }, 378*a6a9c0f1SDaniel Borkmann { 1223800550U, 539U, 3954229517U }, 379*a6a9c0f1SDaniel Borkmann { 1322411537U, 680U, 3223250324U }, 380*a6a9c0f1SDaniel Borkmann { 1877847898U, 945U, 2915147143U }, 381*a6a9c0f1SDaniel Borkmann { 1646356099U, 874U, 965988280U }, 382*a6a9c0f1SDaniel Borkmann { 805687536U, 744U, 4032277920U }, 383*a6a9c0f1SDaniel Borkmann { 1948093210U, 633U, 1346597684U }, 384*a6a9c0f1SDaniel Borkmann { 392609744U, 783U, 1636083295U }, 385*a6a9c0f1SDaniel Borkmann { 690241304U, 770U, 1201031298U }, 386*a6a9c0f1SDaniel Borkmann { 1360302965U, 696U, 1665394461U }, 387*a6a9c0f1SDaniel Borkmann { 1220090946U, 780U, 1316922812U }, 388*a6a9c0f1SDaniel Borkmann { 447092251U, 500U, 3438743375U }, 389*a6a9c0f1SDaniel Borkmann { 1613868791U, 592U, 828546883U }, 390*a6a9c0f1SDaniel Borkmann { 523430951U, 548U, 2552392304U }, 391*a6a9c0f1SDaniel Borkmann { 726692899U, 810U, 1656872867U }, 392*a6a9c0f1SDaniel Borkmann { 1364340021U, 836U, 3710513486U }, 393*a6a9c0f1SDaniel Borkmann { 1986257729U, 931U, 935013962U }, 394*a6a9c0f1SDaniel Borkmann { 407983964U, 921U, 728767059U }, 395*a6a9c0f1SDaniel Borkmann }; 396*a6a9c0f1SDaniel Borkmann 397*a6a9c0f1SDaniel Borkmann static void __init prandom_state_selftest(void) 398*a6a9c0f1SDaniel Borkmann { 399*a6a9c0f1SDaniel Borkmann int i, j, errors = 0, runs = 0; 400*a6a9c0f1SDaniel Borkmann bool error = false; 401*a6a9c0f1SDaniel Borkmann 402*a6a9c0f1SDaniel Borkmann for (i = 0; i < ARRAY_SIZE(test1); i++) { 403*a6a9c0f1SDaniel Borkmann struct rnd_state state; 404*a6a9c0f1SDaniel Borkmann 405*a6a9c0f1SDaniel Borkmann prandom_seed_very_weak(&state, test1[i].seed); 406*a6a9c0f1SDaniel Borkmann prandom_warmup(&state); 407*a6a9c0f1SDaniel Borkmann 408*a6a9c0f1SDaniel Borkmann if (test1[i].result != prandom_u32_state(&state)) 409*a6a9c0f1SDaniel Borkmann error = true; 410*a6a9c0f1SDaniel Borkmann } 411*a6a9c0f1SDaniel Borkmann 412*a6a9c0f1SDaniel Borkmann if (error) 413*a6a9c0f1SDaniel Borkmann pr_warn("prandom: seed boundary self test failed\n"); 414*a6a9c0f1SDaniel Borkmann else 415*a6a9c0f1SDaniel Borkmann pr_info("prandom: seed boundary self test passed\n"); 416*a6a9c0f1SDaniel Borkmann 417*a6a9c0f1SDaniel Borkmann for (i = 0; i < ARRAY_SIZE(test2); i++) { 418*a6a9c0f1SDaniel Borkmann struct rnd_state state; 419*a6a9c0f1SDaniel Borkmann 420*a6a9c0f1SDaniel Borkmann prandom_seed_very_weak(&state, test2[i].seed); 421*a6a9c0f1SDaniel Borkmann prandom_warmup(&state); 422*a6a9c0f1SDaniel Borkmann 423*a6a9c0f1SDaniel Borkmann for (j = 0; j < test2[i].iteration - 1; j++) 424*a6a9c0f1SDaniel Borkmann prandom_u32_state(&state); 425*a6a9c0f1SDaniel Borkmann 426*a6a9c0f1SDaniel Borkmann if (test2[i].result != prandom_u32_state(&state)) 427*a6a9c0f1SDaniel Borkmann errors++; 428*a6a9c0f1SDaniel Borkmann 429*a6a9c0f1SDaniel Borkmann runs++; 430*a6a9c0f1SDaniel Borkmann cond_resched(); 431*a6a9c0f1SDaniel Borkmann } 432*a6a9c0f1SDaniel Borkmann 433*a6a9c0f1SDaniel Borkmann if (errors) 434*a6a9c0f1SDaniel Borkmann pr_warn("prandom: %d/%d self tests failed\n", errors, runs); 435*a6a9c0f1SDaniel Borkmann else 436*a6a9c0f1SDaniel Borkmann pr_info("prandom: %d self tests passed\n", runs); 437*a6a9c0f1SDaniel Borkmann } 438*a6a9c0f1SDaniel Borkmann #endif 439