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