1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2aaa248f6SStephen Hemminger /* 3d3d47eb2SDaniel Borkmann * This is a maximally equidistributed combined Tausworthe generator 4d3d47eb2SDaniel Borkmann * based on code from GNU Scientific Library 1.5 (30 Jun 2004) 5d3d47eb2SDaniel Borkmann * 6d3d47eb2SDaniel Borkmann * lfsr113 version: 7d3d47eb2SDaniel Borkmann * 8d3d47eb2SDaniel Borkmann * x_n = (s1_n ^ s2_n ^ s3_n ^ s4_n) 9d3d47eb2SDaniel Borkmann * 10d3d47eb2SDaniel Borkmann * s1_{n+1} = (((s1_n & 4294967294) << 18) ^ (((s1_n << 6) ^ s1_n) >> 13)) 11d3d47eb2SDaniel Borkmann * s2_{n+1} = (((s2_n & 4294967288) << 2) ^ (((s2_n << 2) ^ s2_n) >> 27)) 12d3d47eb2SDaniel Borkmann * s3_{n+1} = (((s3_n & 4294967280) << 7) ^ (((s3_n << 13) ^ s3_n) >> 21)) 13d3d47eb2SDaniel Borkmann * s4_{n+1} = (((s4_n & 4294967168) << 13) ^ (((s4_n << 3) ^ s4_n) >> 12)) 14d3d47eb2SDaniel Borkmann * 15d3d47eb2SDaniel Borkmann * The period of this generator is about 2^113 (see erratum paper). 16d3d47eb2SDaniel Borkmann * 17d3d47eb2SDaniel Borkmann * From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe 18d3d47eb2SDaniel Borkmann * Generators", Mathematics of Computation, 65, 213 (1996), 203--213: 19d3d47eb2SDaniel Borkmann * http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps 20d3d47eb2SDaniel Borkmann * ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps 21d3d47eb2SDaniel Borkmann * 22d3d47eb2SDaniel Borkmann * There is an erratum in the paper "Tables of Maximally Equidistributed 23d3d47eb2SDaniel Borkmann * Combined LFSR Generators", Mathematics of Computation, 68, 225 (1999), 24d3d47eb2SDaniel Borkmann * 261--269: http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps 25d3d47eb2SDaniel Borkmann * 26d3d47eb2SDaniel Borkmann * ... the k_j most significant bits of z_j must be non-zero, 27d3d47eb2SDaniel Borkmann * for each j. (Note: this restriction also applies to the 28d3d47eb2SDaniel Borkmann * computer code given in [4], but was mistakenly not mentioned 29d3d47eb2SDaniel Borkmann * in that paper.) 30d3d47eb2SDaniel Borkmann * 31d3d47eb2SDaniel Borkmann * This affects the seeding procedure by imposing the requirement 32d3d47eb2SDaniel Borkmann * s1 > 1, s2 > 7, s3 > 15, s4 > 127. 33aaa248f6SStephen Hemminger */ 34aaa248f6SStephen Hemminger 35aaa248f6SStephen Hemminger #include <linux/types.h> 36aaa248f6SStephen Hemminger #include <linux/percpu.h> 378bc3bcc9SPaul Gortmaker #include <linux/export.h> 38f6a57033SAl Viro #include <linux/jiffies.h> 39aaa248f6SStephen Hemminger #include <linux/random.h> 40a6a9c0f1SDaniel Borkmann #include <linux/sched.h> 41a98406e2SDaniel Borkmann #include <asm/unaligned.h> 42a6a9c0f1SDaniel Borkmann 43a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST 44a6a9c0f1SDaniel Borkmann static void __init prandom_state_selftest(void); 454ada97abSHannes Frederic Sowa #else 464ada97abSHannes Frederic Sowa static inline void prandom_state_selftest(void) 474ada97abSHannes Frederic Sowa { 484ada97abSHannes Frederic Sowa } 49a6a9c0f1SDaniel Borkmann #endif 50aaa248f6SStephen Hemminger 510766f788SEmese Revfy static DEFINE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy; 52aaa248f6SStephen Hemminger 535960164fSJoe Eykholt /** 54496f2f93SAkinobu Mita * prandom_u32_state - seeded pseudo-random number generator. 555960164fSJoe Eykholt * @state: pointer to state structure holding seeded state. 565960164fSJoe Eykholt * 575960164fSJoe Eykholt * This is used for pseudo-randomness with no outside seeding. 58496f2f93SAkinobu Mita * For more random results, use prandom_u32(). 595960164fSJoe Eykholt */ 60496f2f93SAkinobu Mita u32 prandom_u32_state(struct rnd_state *state) 61aaa248f6SStephen Hemminger { 62aaa248f6SStephen Hemminger #define TAUSWORTHE(s, a, b, c, d) ((s & c) << d) ^ (((s << a) ^ s) >> b) 63a98814ceSDaniel Borkmann state->s1 = TAUSWORTHE(state->s1, 6U, 13U, 4294967294U, 18U); 64a98814ceSDaniel Borkmann state->s2 = TAUSWORTHE(state->s2, 2U, 27U, 4294967288U, 2U); 65a98814ceSDaniel Borkmann state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U, 7U); 66a98814ceSDaniel Borkmann state->s4 = TAUSWORTHE(state->s4, 3U, 12U, 4294967168U, 13U); 67aaa248f6SStephen Hemminger 68a98814ceSDaniel Borkmann return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4); 69aaa248f6SStephen Hemminger } 70496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_u32_state); 71aaa248f6SStephen Hemminger 72aaa248f6SStephen Hemminger /** 73496f2f93SAkinobu Mita * prandom_u32 - pseudo random number generator 74aaa248f6SStephen Hemminger * 75aaa248f6SStephen Hemminger * A 32 bit pseudo-random number is generated using a fast 76aaa248f6SStephen Hemminger * algorithm suitable for simulation. This algorithm is NOT 77aaa248f6SStephen Hemminger * considered safe for cryptographic use. 78aaa248f6SStephen Hemminger */ 79496f2f93SAkinobu Mita u32 prandom_u32(void) 80aaa248f6SStephen Hemminger { 81aaa248f6SStephen Hemminger struct rnd_state *state = &get_cpu_var(net_rand_state); 82d3d47eb2SDaniel Borkmann u32 res; 83d3d47eb2SDaniel Borkmann 84d3d47eb2SDaniel Borkmann res = prandom_u32_state(state); 853796c3cbSShaohua Li put_cpu_var(net_rand_state); 86d3d47eb2SDaniel Borkmann 87d3d47eb2SDaniel Borkmann return res; 88aaa248f6SStephen Hemminger } 89496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_u32); 90aaa248f6SStephen Hemminger 91d3d47eb2SDaniel Borkmann /** 926582c665SAkinobu Mita * prandom_bytes_state - get the requested number of pseudo-random bytes 936582c665SAkinobu Mita * 946582c665SAkinobu Mita * @state: pointer to state structure holding seeded state. 956582c665SAkinobu Mita * @buf: where to copy the pseudo-random bytes to 966582c665SAkinobu Mita * @bytes: the requested number of bytes 976582c665SAkinobu Mita * 986582c665SAkinobu Mita * This is used for pseudo-randomness with no outside seeding. 996582c665SAkinobu Mita * For more random results, use prandom_bytes(). 1006582c665SAkinobu Mita */ 101a98406e2SDaniel Borkmann void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes) 1026582c665SAkinobu Mita { 103a98406e2SDaniel Borkmann u8 *ptr = buf; 1046582c665SAkinobu Mita 105a98406e2SDaniel Borkmann while (bytes >= sizeof(u32)) { 106a98406e2SDaniel Borkmann put_unaligned(prandom_u32_state(state), (u32 *) ptr); 107a98406e2SDaniel Borkmann ptr += sizeof(u32); 108a98406e2SDaniel Borkmann bytes -= sizeof(u32); 109a98406e2SDaniel Borkmann } 1106582c665SAkinobu Mita 111a98406e2SDaniel Borkmann if (bytes > 0) { 112a98406e2SDaniel Borkmann u32 rem = prandom_u32_state(state); 113a98406e2SDaniel Borkmann do { 114a98406e2SDaniel Borkmann *ptr++ = (u8) rem; 115a98406e2SDaniel Borkmann bytes--; 116a98406e2SDaniel Borkmann rem >>= BITS_PER_BYTE; 117a98406e2SDaniel Borkmann } while (bytes > 0); 1186582c665SAkinobu Mita } 1196582c665SAkinobu Mita } 1206582c665SAkinobu Mita EXPORT_SYMBOL(prandom_bytes_state); 1216582c665SAkinobu Mita 1226582c665SAkinobu Mita /** 1236582c665SAkinobu Mita * prandom_bytes - get the requested number of pseudo-random bytes 1246582c665SAkinobu Mita * @buf: where to copy the pseudo-random bytes to 1256582c665SAkinobu Mita * @bytes: the requested number of bytes 1266582c665SAkinobu Mita */ 127a98406e2SDaniel Borkmann void prandom_bytes(void *buf, size_t bytes) 1286582c665SAkinobu Mita { 1296582c665SAkinobu Mita struct rnd_state *state = &get_cpu_var(net_rand_state); 1306582c665SAkinobu Mita 1316582c665SAkinobu Mita prandom_bytes_state(state, buf, bytes); 1323796c3cbSShaohua Li put_cpu_var(net_rand_state); 1336582c665SAkinobu Mita } 1346582c665SAkinobu Mita EXPORT_SYMBOL(prandom_bytes); 1356582c665SAkinobu Mita 136a98814ceSDaniel Borkmann static void prandom_warmup(struct rnd_state *state) 137a98814ceSDaniel Borkmann { 138a98406e2SDaniel Borkmann /* Calling RNG ten times to satisfy recurrence condition */ 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 prandom_u32_state(state); 149a98814ceSDaniel Borkmann } 150a98814ceSDaniel Borkmann 1514ada97abSHannes Frederic Sowa static u32 __extract_hwseed(void) 152a6a9c0f1SDaniel Borkmann { 153a98406e2SDaniel Borkmann unsigned int val = 0; 1544ada97abSHannes Frederic Sowa 1554ada97abSHannes Frederic Sowa (void)(arch_get_random_seed_int(&val) || 1564ada97abSHannes Frederic Sowa arch_get_random_int(&val)); 1574ada97abSHannes Frederic Sowa 1584ada97abSHannes Frederic Sowa return val; 1594ada97abSHannes Frederic Sowa } 1604ada97abSHannes Frederic Sowa 1614ada97abSHannes Frederic Sowa static void prandom_seed_early(struct rnd_state *state, u32 seed, 1624ada97abSHannes Frederic Sowa bool mix_with_hwseed) 1634ada97abSHannes Frederic Sowa { 164a6a9c0f1SDaniel Borkmann #define LCG(x) ((x) * 69069U) /* super-duper LCG */ 1654ada97abSHannes Frederic Sowa #define HWSEED() (mix_with_hwseed ? __extract_hwseed() : 0) 1664ada97abSHannes Frederic Sowa state->s1 = __seed(HWSEED() ^ LCG(seed), 2U); 1674ada97abSHannes Frederic Sowa state->s2 = __seed(HWSEED() ^ LCG(state->s1), 8U); 1684ada97abSHannes Frederic Sowa state->s3 = __seed(HWSEED() ^ LCG(state->s2), 16U); 1694ada97abSHannes Frederic Sowa state->s4 = __seed(HWSEED() ^ LCG(state->s3), 128U); 170a6a9c0f1SDaniel Borkmann } 171a6a9c0f1SDaniel Borkmann 172aaa248f6SStephen Hemminger /** 173496f2f93SAkinobu Mita * prandom_seed - add entropy to pseudo random number generator 174aaa248f6SStephen Hemminger * @seed: seed value 175aaa248f6SStephen Hemminger * 176496f2f93SAkinobu Mita * Add some additional seeding to the prandom pool. 177aaa248f6SStephen Hemminger */ 178496f2f93SAkinobu Mita void prandom_seed(u32 entropy) 179aaa248f6SStephen Hemminger { 18061407f80SAndi Kleen int i; 18161407f80SAndi Kleen /* 18261407f80SAndi Kleen * No locking on the CPUs, but then somewhat random results are, well, 18361407f80SAndi Kleen * expected. 18461407f80SAndi Kleen */ 18561407f80SAndi Kleen for_each_possible_cpu(i) { 18661407f80SAndi Kleen struct rnd_state *state = &per_cpu(net_rand_state, i); 187a98814ceSDaniel Borkmann 188a98814ceSDaniel Borkmann state->s1 = __seed(state->s1 ^ entropy, 2U); 189a98814ceSDaniel Borkmann prandom_warmup(state); 19061407f80SAndi Kleen } 191aaa248f6SStephen Hemminger } 192496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_seed); 193aaa248f6SStephen Hemminger 194aaa248f6SStephen Hemminger /* 195aaa248f6SStephen Hemminger * Generate some initially weak seeding values to allow 196496f2f93SAkinobu Mita * to start the prandom_u32() engine. 197aaa248f6SStephen Hemminger */ 198496f2f93SAkinobu Mita static int __init prandom_init(void) 199aaa248f6SStephen Hemminger { 200aaa248f6SStephen Hemminger int i; 201aaa248f6SStephen Hemminger 202a6a9c0f1SDaniel Borkmann prandom_state_selftest(); 203a6a9c0f1SDaniel Borkmann 204aaa248f6SStephen Hemminger for_each_possible_cpu(i) { 205aaa248f6SStephen Hemminger struct rnd_state *state = &per_cpu(net_rand_state, i); 2064ada97abSHannes Frederic Sowa u32 weak_seed = (i + jiffies) ^ random_get_entropy(); 207697f8d03SStephen Hemminger 2084ada97abSHannes Frederic Sowa prandom_seed_early(state, weak_seed, true); 209a98814ceSDaniel Borkmann prandom_warmup(state); 210aaa248f6SStephen Hemminger } 211d3d47eb2SDaniel Borkmann 212aaa248f6SStephen Hemminger return 0; 213aaa248f6SStephen Hemminger } 214496f2f93SAkinobu Mita core_initcall(prandom_init); 215aaa248f6SStephen Hemminger 2166d319202SHannes Frederic Sowa static void __prandom_timer(unsigned long dontcare); 2174ada97abSHannes Frederic Sowa 2186d319202SHannes Frederic Sowa static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0); 2196d319202SHannes Frederic Sowa 2206d319202SHannes Frederic Sowa static void __prandom_timer(unsigned long dontcare) 2216d319202SHannes Frederic Sowa { 2226d319202SHannes Frederic Sowa u32 entropy; 2230125737aSDaniel Borkmann unsigned long expires; 2246d319202SHannes Frederic Sowa 2256d319202SHannes Frederic Sowa get_random_bytes(&entropy, sizeof(entropy)); 2266d319202SHannes Frederic Sowa prandom_seed(entropy); 2270125737aSDaniel Borkmann 2286d319202SHannes Frederic Sowa /* reseed every ~60 seconds, in [40 .. 80) interval with slack */ 229a98406e2SDaniel Borkmann expires = 40 + prandom_u32_max(40); 2300125737aSDaniel Borkmann seed_timer.expires = jiffies + msecs_to_jiffies(expires * MSEC_PER_SEC); 2310125737aSDaniel Borkmann 2326d319202SHannes Frederic Sowa add_timer(&seed_timer); 2336d319202SHannes Frederic Sowa } 2346d319202SHannes Frederic Sowa 23566b25142SDaniel Borkmann static void __init __prandom_start_seed_timer(void) 2366d319202SHannes Frederic Sowa { 2370125737aSDaniel Borkmann seed_timer.expires = jiffies + msecs_to_jiffies(40 * MSEC_PER_SEC); 2386d319202SHannes Frederic Sowa add_timer(&seed_timer); 2396d319202SHannes Frederic Sowa } 2406d319202SHannes Frederic Sowa 241897ece56SDaniel Borkmann void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state) 2420dd50d1bSDaniel Borkmann { 2430dd50d1bSDaniel Borkmann int i; 2440dd50d1bSDaniel Borkmann 2450dd50d1bSDaniel Borkmann for_each_possible_cpu(i) { 2460dd50d1bSDaniel Borkmann struct rnd_state *state = per_cpu_ptr(pcpu_state, i); 2470dd50d1bSDaniel Borkmann u32 seeds[4]; 2480dd50d1bSDaniel Borkmann 2490dd50d1bSDaniel Borkmann get_random_bytes(&seeds, sizeof(seeds)); 2500dd50d1bSDaniel Borkmann state->s1 = __seed(seeds[0], 2U); 2510dd50d1bSDaniel Borkmann state->s2 = __seed(seeds[1], 8U); 2520dd50d1bSDaniel Borkmann state->s3 = __seed(seeds[2], 16U); 2530dd50d1bSDaniel Borkmann state->s4 = __seed(seeds[3], 128U); 2540dd50d1bSDaniel Borkmann 2550dd50d1bSDaniel Borkmann prandom_warmup(state); 2560dd50d1bSDaniel Borkmann } 2570dd50d1bSDaniel Borkmann } 258b07edbe1SFlorian Westphal EXPORT_SYMBOL(prandom_seed_full_state); 2590dd50d1bSDaniel Borkmann 260aaa248f6SStephen Hemminger /* 261aaa248f6SStephen Hemminger * Generate better values after random number generator 262421f91d2SUwe Kleine-König * is fully initialized. 263aaa248f6SStephen Hemminger */ 2644af712e8SHannes Frederic Sowa static void __prandom_reseed(bool late) 265aaa248f6SStephen Hemminger { 2664af712e8SHannes Frederic Sowa unsigned long flags; 2674af712e8SHannes Frederic Sowa static bool latch = false; 2684af712e8SHannes Frederic Sowa static DEFINE_SPINLOCK(lock); 2694af712e8SHannes Frederic Sowa 27005efa8c9SSasha Levin /* Asking for random bytes might result in bytes getting 27105efa8c9SSasha Levin * moved into the nonblocking pool and thus marking it 27205efa8c9SSasha Levin * as initialized. In this case we would double back into 27305efa8c9SSasha Levin * this function and attempt to do a late reseed. 27405efa8c9SSasha Levin * Ignore the pointless attempt to reseed again if we're 27505efa8c9SSasha Levin * already waiting for bytes when the nonblocking pool 27605efa8c9SSasha Levin * got initialized. 27705efa8c9SSasha Levin */ 27805efa8c9SSasha Levin 2794af712e8SHannes Frederic Sowa /* only allow initial seeding (late == false) once */ 28005efa8c9SSasha Levin if (!spin_trylock_irqsave(&lock, flags)) 28105efa8c9SSasha Levin return; 28205efa8c9SSasha Levin 2834af712e8SHannes Frederic Sowa if (latch && !late) 2844af712e8SHannes Frederic Sowa goto out; 285d3d47eb2SDaniel Borkmann 2864af712e8SHannes Frederic Sowa latch = true; 2870dd50d1bSDaniel Borkmann prandom_seed_full_state(&net_rand_state); 2884af712e8SHannes Frederic Sowa out: 2894af712e8SHannes Frederic Sowa spin_unlock_irqrestore(&lock, flags); 2904af712e8SHannes Frederic Sowa } 2914af712e8SHannes Frederic Sowa 2924af712e8SHannes Frederic Sowa void prandom_reseed_late(void) 2934af712e8SHannes Frederic Sowa { 2944af712e8SHannes Frederic Sowa __prandom_reseed(true); 2954af712e8SHannes Frederic Sowa } 2964af712e8SHannes Frederic Sowa 2974af712e8SHannes Frederic Sowa static int __init prandom_reseed(void) 2984af712e8SHannes Frederic Sowa { 2994af712e8SHannes Frederic Sowa __prandom_reseed(false); 30066b25142SDaniel Borkmann __prandom_start_seed_timer(); 301aaa248f6SStephen Hemminger return 0; 302aaa248f6SStephen Hemminger } 303496f2f93SAkinobu Mita late_initcall(prandom_reseed); 304a6a9c0f1SDaniel Borkmann 305a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST 306a6a9c0f1SDaniel Borkmann static struct prandom_test1 { 307a6a9c0f1SDaniel Borkmann u32 seed; 308a6a9c0f1SDaniel Borkmann u32 result; 309a6a9c0f1SDaniel Borkmann } test1[] = { 310a6a9c0f1SDaniel Borkmann { 1U, 3484351685U }, 311a6a9c0f1SDaniel Borkmann { 2U, 2623130059U }, 312a6a9c0f1SDaniel Borkmann { 3U, 3125133893U }, 313a6a9c0f1SDaniel Borkmann { 4U, 984847254U }, 314a6a9c0f1SDaniel Borkmann }; 315a6a9c0f1SDaniel Borkmann 316a6a9c0f1SDaniel Borkmann static struct prandom_test2 { 317a6a9c0f1SDaniel Borkmann u32 seed; 318a6a9c0f1SDaniel Borkmann u32 iteration; 319a6a9c0f1SDaniel Borkmann u32 result; 320a6a9c0f1SDaniel Borkmann } test2[] = { 321a6a9c0f1SDaniel Borkmann /* Test cases against taus113 from GSL library. */ 322a6a9c0f1SDaniel Borkmann { 931557656U, 959U, 2975593782U }, 323a6a9c0f1SDaniel Borkmann { 1339693295U, 876U, 3887776532U }, 324a6a9c0f1SDaniel Borkmann { 1545556285U, 961U, 1615538833U }, 325a6a9c0f1SDaniel Borkmann { 601730776U, 723U, 1776162651U }, 326a6a9c0f1SDaniel Borkmann { 1027516047U, 687U, 511983079U }, 327a6a9c0f1SDaniel Borkmann { 416526298U, 700U, 916156552U }, 328a6a9c0f1SDaniel Borkmann { 1395522032U, 652U, 2222063676U }, 329a6a9c0f1SDaniel Borkmann { 366221443U, 617U, 2992857763U }, 330a6a9c0f1SDaniel Borkmann { 1539836965U, 714U, 3783265725U }, 331a6a9c0f1SDaniel Borkmann { 556206671U, 994U, 799626459U }, 332a6a9c0f1SDaniel Borkmann { 684907218U, 799U, 367789491U }, 333a6a9c0f1SDaniel Borkmann { 2121230701U, 931U, 2115467001U }, 334a6a9c0f1SDaniel Borkmann { 1668516451U, 644U, 3620590685U }, 335a6a9c0f1SDaniel Borkmann { 768046066U, 883U, 2034077390U }, 336a6a9c0f1SDaniel Borkmann { 1989159136U, 833U, 1195767305U }, 337a6a9c0f1SDaniel Borkmann { 536585145U, 996U, 3577259204U }, 338a6a9c0f1SDaniel Borkmann { 1008129373U, 642U, 1478080776U }, 339a6a9c0f1SDaniel Borkmann { 1740775604U, 939U, 1264980372U }, 340a6a9c0f1SDaniel Borkmann { 1967883163U, 508U, 10734624U }, 341a6a9c0f1SDaniel Borkmann { 1923019697U, 730U, 3821419629U }, 342a6a9c0f1SDaniel Borkmann { 442079932U, 560U, 3440032343U }, 343a6a9c0f1SDaniel Borkmann { 1961302714U, 845U, 841962572U }, 344a6a9c0f1SDaniel Borkmann { 2030205964U, 962U, 1325144227U }, 345a6a9c0f1SDaniel Borkmann { 1160407529U, 507U, 240940858U }, 346a6a9c0f1SDaniel Borkmann { 635482502U, 779U, 4200489746U }, 347a6a9c0f1SDaniel Borkmann { 1252788931U, 699U, 867195434U }, 348a6a9c0f1SDaniel Borkmann { 1961817131U, 719U, 668237657U }, 349a6a9c0f1SDaniel Borkmann { 1071468216U, 983U, 917876630U }, 350a6a9c0f1SDaniel Borkmann { 1281848367U, 932U, 1003100039U }, 351a6a9c0f1SDaniel Borkmann { 582537119U, 780U, 1127273778U }, 352a6a9c0f1SDaniel Borkmann { 1973672777U, 853U, 1071368872U }, 353a6a9c0f1SDaniel Borkmann { 1896756996U, 762U, 1127851055U }, 354a6a9c0f1SDaniel Borkmann { 847917054U, 500U, 1717499075U }, 355a6a9c0f1SDaniel Borkmann { 1240520510U, 951U, 2849576657U }, 356a6a9c0f1SDaniel Borkmann { 1685071682U, 567U, 1961810396U }, 357a6a9c0f1SDaniel Borkmann { 1516232129U, 557U, 3173877U }, 358a6a9c0f1SDaniel Borkmann { 1208118903U, 612U, 1613145022U }, 359a6a9c0f1SDaniel Borkmann { 1817269927U, 693U, 4279122573U }, 360a6a9c0f1SDaniel Borkmann { 1510091701U, 717U, 638191229U }, 361a6a9c0f1SDaniel Borkmann { 365916850U, 807U, 600424314U }, 362a6a9c0f1SDaniel Borkmann { 399324359U, 702U, 1803598116U }, 363a6a9c0f1SDaniel Borkmann { 1318480274U, 779U, 2074237022U }, 364a6a9c0f1SDaniel Borkmann { 697758115U, 840U, 1483639402U }, 365a6a9c0f1SDaniel Borkmann { 1696507773U, 840U, 577415447U }, 366a6a9c0f1SDaniel Borkmann { 2081979121U, 981U, 3041486449U }, 367a6a9c0f1SDaniel Borkmann { 955646687U, 742U, 3846494357U }, 368a6a9c0f1SDaniel Borkmann { 1250683506U, 749U, 836419859U }, 369a6a9c0f1SDaniel Borkmann { 595003102U, 534U, 366794109U }, 370a6a9c0f1SDaniel Borkmann { 47485338U, 558U, 3521120834U }, 371a6a9c0f1SDaniel Borkmann { 619433479U, 610U, 3991783875U }, 372a6a9c0f1SDaniel Borkmann { 704096520U, 518U, 4139493852U }, 373a6a9c0f1SDaniel Borkmann { 1712224984U, 606U, 2393312003U }, 374a6a9c0f1SDaniel Borkmann { 1318233152U, 922U, 3880361134U }, 375a6a9c0f1SDaniel Borkmann { 855572992U, 761U, 1472974787U }, 376a6a9c0f1SDaniel Borkmann { 64721421U, 703U, 683860550U }, 377a6a9c0f1SDaniel Borkmann { 678931758U, 840U, 380616043U }, 378a6a9c0f1SDaniel Borkmann { 692711973U, 778U, 1382361947U }, 379a6a9c0f1SDaniel Borkmann { 677703619U, 530U, 2826914161U }, 380a6a9c0f1SDaniel Borkmann { 92393223U, 586U, 1522128471U }, 381a6a9c0f1SDaniel Borkmann { 1222592920U, 743U, 3466726667U }, 382a6a9c0f1SDaniel Borkmann { 358288986U, 695U, 1091956998U }, 383a6a9c0f1SDaniel Borkmann { 1935056945U, 958U, 514864477U }, 384a6a9c0f1SDaniel Borkmann { 735675993U, 990U, 1294239989U }, 385a6a9c0f1SDaniel Borkmann { 1560089402U, 897U, 2238551287U }, 386a6a9c0f1SDaniel Borkmann { 70616361U, 829U, 22483098U }, 387a6a9c0f1SDaniel Borkmann { 368234700U, 731U, 2913875084U }, 388a6a9c0f1SDaniel Borkmann { 20221190U, 879U, 1564152970U }, 389a6a9c0f1SDaniel Borkmann { 539444654U, 682U, 1835141259U }, 390a6a9c0f1SDaniel Borkmann { 1314987297U, 840U, 1801114136U }, 391a6a9c0f1SDaniel Borkmann { 2019295544U, 645U, 3286438930U }, 392a6a9c0f1SDaniel Borkmann { 469023838U, 716U, 1637918202U }, 393a6a9c0f1SDaniel Borkmann { 1843754496U, 653U, 2562092152U }, 394a6a9c0f1SDaniel Borkmann { 400672036U, 809U, 4264212785U }, 395a6a9c0f1SDaniel Borkmann { 404722249U, 965U, 2704116999U }, 396a6a9c0f1SDaniel Borkmann { 600702209U, 758U, 584979986U }, 397a6a9c0f1SDaniel Borkmann { 519953954U, 667U, 2574436237U }, 398a6a9c0f1SDaniel Borkmann { 1658071126U, 694U, 2214569490U }, 399a6a9c0f1SDaniel Borkmann { 420480037U, 749U, 3430010866U }, 400a6a9c0f1SDaniel Borkmann { 690103647U, 969U, 3700758083U }, 401a6a9c0f1SDaniel Borkmann { 1029424799U, 937U, 3787746841U }, 402a6a9c0f1SDaniel Borkmann { 2012608669U, 506U, 3362628973U }, 403a6a9c0f1SDaniel Borkmann { 1535432887U, 998U, 42610943U }, 404a6a9c0f1SDaniel Borkmann { 1330635533U, 857U, 3040806504U }, 405a6a9c0f1SDaniel Borkmann { 1223800550U, 539U, 3954229517U }, 406a6a9c0f1SDaniel Borkmann { 1322411537U, 680U, 3223250324U }, 407a6a9c0f1SDaniel Borkmann { 1877847898U, 945U, 2915147143U }, 408a6a9c0f1SDaniel Borkmann { 1646356099U, 874U, 965988280U }, 409a6a9c0f1SDaniel Borkmann { 805687536U, 744U, 4032277920U }, 410a6a9c0f1SDaniel Borkmann { 1948093210U, 633U, 1346597684U }, 411a6a9c0f1SDaniel Borkmann { 392609744U, 783U, 1636083295U }, 412a6a9c0f1SDaniel Borkmann { 690241304U, 770U, 1201031298U }, 413a6a9c0f1SDaniel Borkmann { 1360302965U, 696U, 1665394461U }, 414a6a9c0f1SDaniel Borkmann { 1220090946U, 780U, 1316922812U }, 415a6a9c0f1SDaniel Borkmann { 447092251U, 500U, 3438743375U }, 416a6a9c0f1SDaniel Borkmann { 1613868791U, 592U, 828546883U }, 417a6a9c0f1SDaniel Borkmann { 523430951U, 548U, 2552392304U }, 418a6a9c0f1SDaniel Borkmann { 726692899U, 810U, 1656872867U }, 419a6a9c0f1SDaniel Borkmann { 1364340021U, 836U, 3710513486U }, 420a6a9c0f1SDaniel Borkmann { 1986257729U, 931U, 935013962U }, 421a6a9c0f1SDaniel Borkmann { 407983964U, 921U, 728767059U }, 422a6a9c0f1SDaniel Borkmann }; 423a6a9c0f1SDaniel Borkmann 424a6a9c0f1SDaniel Borkmann static void __init prandom_state_selftest(void) 425a6a9c0f1SDaniel Borkmann { 426a6a9c0f1SDaniel Borkmann int i, j, errors = 0, runs = 0; 427a6a9c0f1SDaniel Borkmann bool error = false; 428a6a9c0f1SDaniel Borkmann 429a6a9c0f1SDaniel Borkmann for (i = 0; i < ARRAY_SIZE(test1); i++) { 430a6a9c0f1SDaniel Borkmann struct rnd_state state; 431a6a9c0f1SDaniel Borkmann 4324ada97abSHannes Frederic Sowa prandom_seed_early(&state, test1[i].seed, false); 433a6a9c0f1SDaniel Borkmann prandom_warmup(&state); 434a6a9c0f1SDaniel Borkmann 435a6a9c0f1SDaniel Borkmann if (test1[i].result != prandom_u32_state(&state)) 436a6a9c0f1SDaniel Borkmann error = true; 437a6a9c0f1SDaniel Borkmann } 438a6a9c0f1SDaniel Borkmann 439a6a9c0f1SDaniel Borkmann if (error) 440a6a9c0f1SDaniel Borkmann pr_warn("prandom: seed boundary self test failed\n"); 441a6a9c0f1SDaniel Borkmann else 442a6a9c0f1SDaniel Borkmann pr_info("prandom: seed boundary self test passed\n"); 443a6a9c0f1SDaniel Borkmann 444a6a9c0f1SDaniel Borkmann for (i = 0; i < ARRAY_SIZE(test2); i++) { 445a6a9c0f1SDaniel Borkmann struct rnd_state state; 446a6a9c0f1SDaniel Borkmann 4474ada97abSHannes Frederic Sowa prandom_seed_early(&state, test2[i].seed, false); 448a6a9c0f1SDaniel Borkmann prandom_warmup(&state); 449a6a9c0f1SDaniel Borkmann 450a6a9c0f1SDaniel Borkmann for (j = 0; j < test2[i].iteration - 1; j++) 451a6a9c0f1SDaniel Borkmann prandom_u32_state(&state); 452a6a9c0f1SDaniel Borkmann 453a6a9c0f1SDaniel Borkmann if (test2[i].result != prandom_u32_state(&state)) 454a6a9c0f1SDaniel Borkmann errors++; 455a6a9c0f1SDaniel Borkmann 456a6a9c0f1SDaniel Borkmann runs++; 457a6a9c0f1SDaniel Borkmann cond_resched(); 458a6a9c0f1SDaniel Borkmann } 459a6a9c0f1SDaniel Borkmann 460a6a9c0f1SDaniel Borkmann if (errors) 461a6a9c0f1SDaniel Borkmann pr_warn("prandom: %d/%d self tests failed\n", errors, runs); 462a6a9c0f1SDaniel Borkmann else 463a6a9c0f1SDaniel Borkmann pr_info("prandom: %d self tests passed\n", runs); 464a6a9c0f1SDaniel Borkmann } 465a6a9c0f1SDaniel Borkmann #endif 466