xref: /linux/lib/random32.c (revision d3d47eb265c2bc6f4a6f1bc6971b84d27035b964)
1aaa248f6SStephen Hemminger /*
2*d3d47eb2SDaniel Borkmann  * This is a maximally equidistributed combined Tausworthe generator
3*d3d47eb2SDaniel Borkmann  * based on code from GNU Scientific Library 1.5 (30 Jun 2004)
4*d3d47eb2SDaniel Borkmann  *
5*d3d47eb2SDaniel Borkmann  * lfsr113 version:
6*d3d47eb2SDaniel Borkmann  *
7*d3d47eb2SDaniel Borkmann  * x_n = (s1_n ^ s2_n ^ s3_n ^ s4_n)
8*d3d47eb2SDaniel Borkmann  *
9*d3d47eb2SDaniel Borkmann  * s1_{n+1} = (((s1_n & 4294967294) << 18) ^ (((s1_n <<  6) ^ s1_n) >> 13))
10*d3d47eb2SDaniel Borkmann  * s2_{n+1} = (((s2_n & 4294967288) <<  2) ^ (((s2_n <<  2) ^ s2_n) >> 27))
11*d3d47eb2SDaniel Borkmann  * s3_{n+1} = (((s3_n & 4294967280) <<  7) ^ (((s3_n << 13) ^ s3_n) >> 21))
12*d3d47eb2SDaniel Borkmann  * s4_{n+1} = (((s4_n & 4294967168) << 13) ^ (((s4_n <<  3) ^ s4_n) >> 12))
13*d3d47eb2SDaniel Borkmann  *
14*d3d47eb2SDaniel Borkmann  * The period of this generator is about 2^113 (see erratum paper).
15*d3d47eb2SDaniel Borkmann  *
16*d3d47eb2SDaniel Borkmann  * From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
17*d3d47eb2SDaniel Borkmann  * Generators", Mathematics of Computation, 65, 213 (1996), 203--213:
18*d3d47eb2SDaniel Borkmann  * http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
19*d3d47eb2SDaniel Borkmann  * ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
20*d3d47eb2SDaniel Borkmann  *
21*d3d47eb2SDaniel Borkmann  * There is an erratum in the paper "Tables of Maximally Equidistributed
22*d3d47eb2SDaniel Borkmann  * Combined LFSR Generators", Mathematics of Computation, 68, 225 (1999),
23*d3d47eb2SDaniel Borkmann  * 261--269: http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
24*d3d47eb2SDaniel Borkmann  *
25*d3d47eb2SDaniel Borkmann  *      ... the k_j most significant bits of z_j must be non-zero,
26*d3d47eb2SDaniel Borkmann  *      for each j. (Note: this restriction also applies to the
27*d3d47eb2SDaniel Borkmann  *      computer code given in [4], but was mistakenly not mentioned
28*d3d47eb2SDaniel Borkmann  *      in that paper.)
29*d3d47eb2SDaniel Borkmann  *
30*d3d47eb2SDaniel Borkmann  * This affects the seeding procedure by imposing the requirement
31*d3d47eb2SDaniel 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>
40a6a9c0f1SDaniel Borkmann 
41a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST
42a6a9c0f1SDaniel Borkmann static void __init prandom_state_selftest(void);
43a6a9c0f1SDaniel Borkmann #endif
44aaa248f6SStephen Hemminger 
45aaa248f6SStephen Hemminger static DEFINE_PER_CPU(struct rnd_state, net_rand_state);
46aaa248f6SStephen Hemminger 
475960164fSJoe Eykholt /**
48496f2f93SAkinobu Mita  *	prandom_u32_state - seeded pseudo-random number generator.
495960164fSJoe Eykholt  *	@state: pointer to state structure holding seeded state.
505960164fSJoe Eykholt  *
515960164fSJoe Eykholt  *	This is used for pseudo-randomness with no outside seeding.
52496f2f93SAkinobu Mita  *	For more random results, use prandom_u32().
535960164fSJoe Eykholt  */
54496f2f93SAkinobu Mita u32 prandom_u32_state(struct rnd_state *state)
55aaa248f6SStephen Hemminger {
56aaa248f6SStephen Hemminger #define TAUSWORTHE(s,a,b,c,d) ((s&c)<<d) ^ (((s <<a) ^ s)>>b)
57aaa248f6SStephen Hemminger 
58a98814ceSDaniel Borkmann 	state->s1 = TAUSWORTHE(state->s1,  6U, 13U, 4294967294U, 18U);
59a98814ceSDaniel Borkmann 	state->s2 = TAUSWORTHE(state->s2,  2U, 27U, 4294967288U,  2U);
60a98814ceSDaniel Borkmann 	state->s3 = TAUSWORTHE(state->s3, 13U, 21U, 4294967280U,  7U);
61a98814ceSDaniel Borkmann 	state->s4 = TAUSWORTHE(state->s4,  3U, 12U, 4294967168U, 13U);
62aaa248f6SStephen Hemminger 
63a98814ceSDaniel Borkmann 	return (state->s1 ^ state->s2 ^ state->s3 ^ state->s4);
64aaa248f6SStephen Hemminger }
65496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_u32_state);
66aaa248f6SStephen Hemminger 
67aaa248f6SStephen Hemminger /**
68496f2f93SAkinobu Mita  *	prandom_u32 - pseudo random number generator
69aaa248f6SStephen Hemminger  *
70aaa248f6SStephen Hemminger  *	A 32 bit pseudo-random number is generated using a fast
71aaa248f6SStephen Hemminger  *	algorithm suitable for simulation. This algorithm is NOT
72aaa248f6SStephen Hemminger  *	considered safe for cryptographic use.
73aaa248f6SStephen Hemminger  */
74496f2f93SAkinobu Mita u32 prandom_u32(void)
75aaa248f6SStephen Hemminger {
76aaa248f6SStephen Hemminger 	struct rnd_state *state = &get_cpu_var(net_rand_state);
77*d3d47eb2SDaniel Borkmann 	u32 res;
78*d3d47eb2SDaniel Borkmann 
79*d3d47eb2SDaniel Borkmann 	res = prandom_u32_state(state);
80aaa248f6SStephen Hemminger 	put_cpu_var(state);
81*d3d47eb2SDaniel Borkmann 
82*d3d47eb2SDaniel Borkmann 	return res;
83aaa248f6SStephen Hemminger }
84496f2f93SAkinobu Mita EXPORT_SYMBOL(prandom_u32);
85aaa248f6SStephen Hemminger 
86*d3d47eb2SDaniel Borkmann /**
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 
150a6a9c0f1SDaniel Borkmann static void prandom_seed_very_weak(struct rnd_state *state, u32 seed)
151a6a9c0f1SDaniel Borkmann {
152a6a9c0f1SDaniel Borkmann 	/* Note: This sort of seeding is ONLY used in test cases and
153a6a9c0f1SDaniel Borkmann 	 * during boot at the time from core_initcall until late_initcall
154a6a9c0f1SDaniel Borkmann 	 * as we don't have a stronger entropy source available yet.
155a6a9c0f1SDaniel Borkmann 	 * After late_initcall, we reseed entire state, we have to (!),
156a6a9c0f1SDaniel Borkmann 	 * otherwise an attacker just needs to search 32 bit space to
157a6a9c0f1SDaniel Borkmann 	 * probe for our internal 128 bit state if he knows a couple
158a6a9c0f1SDaniel Borkmann 	 * of prandom32 outputs!
159a6a9c0f1SDaniel Borkmann 	 */
160a6a9c0f1SDaniel Borkmann #define LCG(x)	((x) * 69069U)	/* super-duper LCG */
161a6a9c0f1SDaniel Borkmann 	state->s1 = __seed(LCG(seed),        2U);
162a6a9c0f1SDaniel Borkmann 	state->s2 = __seed(LCG(state->s1),   8U);
163a6a9c0f1SDaniel Borkmann 	state->s3 = __seed(LCG(state->s2),  16U);
164a6a9c0f1SDaniel Borkmann 	state->s4 = __seed(LCG(state->s3), 128U);
165a6a9c0f1SDaniel Borkmann }
166a6a9c0f1SDaniel 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 
197a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST
198a6a9c0f1SDaniel Borkmann 	prandom_state_selftest();
199a6a9c0f1SDaniel Borkmann #endif
200a6a9c0f1SDaniel Borkmann 
201aaa248f6SStephen Hemminger 	for_each_possible_cpu(i) {
202aaa248f6SStephen Hemminger 		struct rnd_state *state = &per_cpu(net_rand_state,i);
203697f8d03SStephen Hemminger 
204a6a9c0f1SDaniel Borkmann 		prandom_seed_very_weak(state, (i + jiffies) ^ random_get_entropy());
205a98814ceSDaniel Borkmann 		prandom_warmup(state);
206aaa248f6SStephen Hemminger 	}
207*d3d47eb2SDaniel Borkmann 
208aaa248f6SStephen Hemminger 	return 0;
209aaa248f6SStephen Hemminger }
210496f2f93SAkinobu Mita core_initcall(prandom_init);
211aaa248f6SStephen Hemminger 
2126d319202SHannes Frederic Sowa static void __prandom_timer(unsigned long dontcare);
2136d319202SHannes Frederic Sowa static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
2146d319202SHannes Frederic Sowa 
2156d319202SHannes Frederic Sowa static void __prandom_timer(unsigned long dontcare)
2166d319202SHannes Frederic Sowa {
2176d319202SHannes Frederic Sowa 	u32 entropy;
2180125737aSDaniel Borkmann 	unsigned long expires;
2196d319202SHannes Frederic Sowa 
2206d319202SHannes Frederic Sowa 	get_random_bytes(&entropy, sizeof(entropy));
2216d319202SHannes Frederic Sowa 	prandom_seed(entropy);
2220125737aSDaniel Borkmann 
2236d319202SHannes Frederic Sowa 	/* reseed every ~60 seconds, in [40 .. 80) interval with slack */
2240125737aSDaniel Borkmann 	expires = 40 + (prandom_u32() % 40);
2250125737aSDaniel Borkmann 	seed_timer.expires = jiffies + msecs_to_jiffies(expires * MSEC_PER_SEC);
2260125737aSDaniel Borkmann 
2276d319202SHannes Frederic Sowa 	add_timer(&seed_timer);
2286d319202SHannes Frederic Sowa }
2296d319202SHannes Frederic Sowa 
23066b25142SDaniel Borkmann static void __init __prandom_start_seed_timer(void)
2316d319202SHannes Frederic Sowa {
2326d319202SHannes Frederic Sowa 	set_timer_slack(&seed_timer, HZ);
2330125737aSDaniel Borkmann 	seed_timer.expires = jiffies + msecs_to_jiffies(40 * MSEC_PER_SEC);
2346d319202SHannes Frederic Sowa 	add_timer(&seed_timer);
2356d319202SHannes Frederic Sowa }
2366d319202SHannes Frederic Sowa 
237aaa248f6SStephen Hemminger /*
238aaa248f6SStephen Hemminger  *	Generate better values after random number generator
239421f91d2SUwe Kleine-König  *	is fully initialized.
240aaa248f6SStephen Hemminger  */
2414af712e8SHannes Frederic Sowa static void __prandom_reseed(bool late)
242aaa248f6SStephen Hemminger {
243aaa248f6SStephen Hemminger 	int i;
2444af712e8SHannes Frederic Sowa 	unsigned long flags;
2454af712e8SHannes Frederic Sowa 	static bool latch = false;
2464af712e8SHannes Frederic Sowa 	static DEFINE_SPINLOCK(lock);
2474af712e8SHannes Frederic Sowa 
24805efa8c9SSasha Levin 	/* Asking for random bytes might result in bytes getting
24905efa8c9SSasha Levin 	 * moved into the nonblocking pool and thus marking it
25005efa8c9SSasha Levin 	 * as initialized. In this case we would double back into
25105efa8c9SSasha Levin 	 * this function and attempt to do a late reseed.
25205efa8c9SSasha Levin 	 * Ignore the pointless attempt to reseed again if we're
25305efa8c9SSasha Levin 	 * already waiting for bytes when the nonblocking pool
25405efa8c9SSasha Levin 	 * got initialized.
25505efa8c9SSasha Levin 	 */
25605efa8c9SSasha Levin 
2574af712e8SHannes Frederic Sowa 	/* only allow initial seeding (late == false) once */
25805efa8c9SSasha Levin 	if (!spin_trylock_irqsave(&lock, flags))
25905efa8c9SSasha Levin 		return;
26005efa8c9SSasha Levin 
2614af712e8SHannes Frederic Sowa 	if (latch && !late)
2624af712e8SHannes Frederic Sowa 		goto out;
263*d3d47eb2SDaniel Borkmann 
2644af712e8SHannes Frederic Sowa 	latch = true;
265aaa248f6SStephen Hemminger 
266aaa248f6SStephen Hemminger 	for_each_possible_cpu(i) {
267aaa248f6SStephen Hemminger 		struct rnd_state *state = &per_cpu(net_rand_state,i);
268a98814ceSDaniel Borkmann 		u32 seeds[4];
269aaa248f6SStephen Hemminger 
270697f8d03SStephen Hemminger 		get_random_bytes(&seeds, sizeof(seeds));
271a98814ceSDaniel Borkmann 		state->s1 = __seed(seeds[0],   2U);
272a98814ceSDaniel Borkmann 		state->s2 = __seed(seeds[1],   8U);
273a98814ceSDaniel Borkmann 		state->s3 = __seed(seeds[2],  16U);
274a98814ceSDaniel Borkmann 		state->s4 = __seed(seeds[3], 128U);
275697f8d03SStephen Hemminger 
276a98814ceSDaniel Borkmann 		prandom_warmup(state);
277aaa248f6SStephen Hemminger 	}
2784af712e8SHannes Frederic Sowa out:
2794af712e8SHannes Frederic Sowa 	spin_unlock_irqrestore(&lock, flags);
2804af712e8SHannes Frederic Sowa }
2814af712e8SHannes Frederic Sowa 
2824af712e8SHannes Frederic Sowa void prandom_reseed_late(void)
2834af712e8SHannes Frederic Sowa {
2844af712e8SHannes Frederic Sowa 	__prandom_reseed(true);
2854af712e8SHannes Frederic Sowa }
2864af712e8SHannes Frederic Sowa 
2874af712e8SHannes Frederic Sowa static int __init prandom_reseed(void)
2884af712e8SHannes Frederic Sowa {
2894af712e8SHannes Frederic Sowa 	__prandom_reseed(false);
29066b25142SDaniel Borkmann 	__prandom_start_seed_timer();
291aaa248f6SStephen Hemminger 	return 0;
292aaa248f6SStephen Hemminger }
293496f2f93SAkinobu Mita late_initcall(prandom_reseed);
294a6a9c0f1SDaniel Borkmann 
295a6a9c0f1SDaniel Borkmann #ifdef CONFIG_RANDOM32_SELFTEST
296a6a9c0f1SDaniel Borkmann static struct prandom_test1 {
297a6a9c0f1SDaniel Borkmann 	u32 seed;
298a6a9c0f1SDaniel Borkmann 	u32 result;
299a6a9c0f1SDaniel Borkmann } test1[] = {
300a6a9c0f1SDaniel Borkmann 	{ 1U, 3484351685U },
301a6a9c0f1SDaniel Borkmann 	{ 2U, 2623130059U },
302a6a9c0f1SDaniel Borkmann 	{ 3U, 3125133893U },
303a6a9c0f1SDaniel Borkmann 	{ 4U,  984847254U },
304a6a9c0f1SDaniel Borkmann };
305a6a9c0f1SDaniel Borkmann 
306a6a9c0f1SDaniel Borkmann static struct prandom_test2 {
307a6a9c0f1SDaniel Borkmann 	u32 seed;
308a6a9c0f1SDaniel Borkmann 	u32 iteration;
309a6a9c0f1SDaniel Borkmann 	u32 result;
310a6a9c0f1SDaniel Borkmann } test2[] = {
311a6a9c0f1SDaniel Borkmann 	/* Test cases against taus113 from GSL library. */
312a6a9c0f1SDaniel Borkmann 	{  931557656U, 959U, 2975593782U },
313a6a9c0f1SDaniel Borkmann 	{ 1339693295U, 876U, 3887776532U },
314a6a9c0f1SDaniel Borkmann 	{ 1545556285U, 961U, 1615538833U },
315a6a9c0f1SDaniel Borkmann 	{  601730776U, 723U, 1776162651U },
316a6a9c0f1SDaniel Borkmann 	{ 1027516047U, 687U,  511983079U },
317a6a9c0f1SDaniel Borkmann 	{  416526298U, 700U,  916156552U },
318a6a9c0f1SDaniel Borkmann 	{ 1395522032U, 652U, 2222063676U },
319a6a9c0f1SDaniel Borkmann 	{  366221443U, 617U, 2992857763U },
320a6a9c0f1SDaniel Borkmann 	{ 1539836965U, 714U, 3783265725U },
321a6a9c0f1SDaniel Borkmann 	{  556206671U, 994U,  799626459U },
322a6a9c0f1SDaniel Borkmann 	{  684907218U, 799U,  367789491U },
323a6a9c0f1SDaniel Borkmann 	{ 2121230701U, 931U, 2115467001U },
324a6a9c0f1SDaniel Borkmann 	{ 1668516451U, 644U, 3620590685U },
325a6a9c0f1SDaniel Borkmann 	{  768046066U, 883U, 2034077390U },
326a6a9c0f1SDaniel Borkmann 	{ 1989159136U, 833U, 1195767305U },
327a6a9c0f1SDaniel Borkmann 	{  536585145U, 996U, 3577259204U },
328a6a9c0f1SDaniel Borkmann 	{ 1008129373U, 642U, 1478080776U },
329a6a9c0f1SDaniel Borkmann 	{ 1740775604U, 939U, 1264980372U },
330a6a9c0f1SDaniel Borkmann 	{ 1967883163U, 508U,   10734624U },
331a6a9c0f1SDaniel Borkmann 	{ 1923019697U, 730U, 3821419629U },
332a6a9c0f1SDaniel Borkmann 	{  442079932U, 560U, 3440032343U },
333a6a9c0f1SDaniel Borkmann 	{ 1961302714U, 845U,  841962572U },
334a6a9c0f1SDaniel Borkmann 	{ 2030205964U, 962U, 1325144227U },
335a6a9c0f1SDaniel Borkmann 	{ 1160407529U, 507U,  240940858U },
336a6a9c0f1SDaniel Borkmann 	{  635482502U, 779U, 4200489746U },
337a6a9c0f1SDaniel Borkmann 	{ 1252788931U, 699U,  867195434U },
338a6a9c0f1SDaniel Borkmann 	{ 1961817131U, 719U,  668237657U },
339a6a9c0f1SDaniel Borkmann 	{ 1071468216U, 983U,  917876630U },
340a6a9c0f1SDaniel Borkmann 	{ 1281848367U, 932U, 1003100039U },
341a6a9c0f1SDaniel Borkmann 	{  582537119U, 780U, 1127273778U },
342a6a9c0f1SDaniel Borkmann 	{ 1973672777U, 853U, 1071368872U },
343a6a9c0f1SDaniel Borkmann 	{ 1896756996U, 762U, 1127851055U },
344a6a9c0f1SDaniel Borkmann 	{  847917054U, 500U, 1717499075U },
345a6a9c0f1SDaniel Borkmann 	{ 1240520510U, 951U, 2849576657U },
346a6a9c0f1SDaniel Borkmann 	{ 1685071682U, 567U, 1961810396U },
347a6a9c0f1SDaniel Borkmann 	{ 1516232129U, 557U,    3173877U },
348a6a9c0f1SDaniel Borkmann 	{ 1208118903U, 612U, 1613145022U },
349a6a9c0f1SDaniel Borkmann 	{ 1817269927U, 693U, 4279122573U },
350a6a9c0f1SDaniel Borkmann 	{ 1510091701U, 717U,  638191229U },
351a6a9c0f1SDaniel Borkmann 	{  365916850U, 807U,  600424314U },
352a6a9c0f1SDaniel Borkmann 	{  399324359U, 702U, 1803598116U },
353a6a9c0f1SDaniel Borkmann 	{ 1318480274U, 779U, 2074237022U },
354a6a9c0f1SDaniel Borkmann 	{  697758115U, 840U, 1483639402U },
355a6a9c0f1SDaniel Borkmann 	{ 1696507773U, 840U,  577415447U },
356a6a9c0f1SDaniel Borkmann 	{ 2081979121U, 981U, 3041486449U },
357a6a9c0f1SDaniel Borkmann 	{  955646687U, 742U, 3846494357U },
358a6a9c0f1SDaniel Borkmann 	{ 1250683506U, 749U,  836419859U },
359a6a9c0f1SDaniel Borkmann 	{  595003102U, 534U,  366794109U },
360a6a9c0f1SDaniel Borkmann 	{   47485338U, 558U, 3521120834U },
361a6a9c0f1SDaniel Borkmann 	{  619433479U, 610U, 3991783875U },
362a6a9c0f1SDaniel Borkmann 	{  704096520U, 518U, 4139493852U },
363a6a9c0f1SDaniel Borkmann 	{ 1712224984U, 606U, 2393312003U },
364a6a9c0f1SDaniel Borkmann 	{ 1318233152U, 922U, 3880361134U },
365a6a9c0f1SDaniel Borkmann 	{  855572992U, 761U, 1472974787U },
366a6a9c0f1SDaniel Borkmann 	{   64721421U, 703U,  683860550U },
367a6a9c0f1SDaniel Borkmann 	{  678931758U, 840U,  380616043U },
368a6a9c0f1SDaniel Borkmann 	{  692711973U, 778U, 1382361947U },
369a6a9c0f1SDaniel Borkmann 	{  677703619U, 530U, 2826914161U },
370a6a9c0f1SDaniel Borkmann 	{   92393223U, 586U, 1522128471U },
371a6a9c0f1SDaniel Borkmann 	{ 1222592920U, 743U, 3466726667U },
372a6a9c0f1SDaniel Borkmann 	{  358288986U, 695U, 1091956998U },
373a6a9c0f1SDaniel Borkmann 	{ 1935056945U, 958U,  514864477U },
374a6a9c0f1SDaniel Borkmann 	{  735675993U, 990U, 1294239989U },
375a6a9c0f1SDaniel Borkmann 	{ 1560089402U, 897U, 2238551287U },
376a6a9c0f1SDaniel Borkmann 	{   70616361U, 829U,   22483098U },
377a6a9c0f1SDaniel Borkmann 	{  368234700U, 731U, 2913875084U },
378a6a9c0f1SDaniel Borkmann 	{   20221190U, 879U, 1564152970U },
379a6a9c0f1SDaniel Borkmann 	{  539444654U, 682U, 1835141259U },
380a6a9c0f1SDaniel Borkmann 	{ 1314987297U, 840U, 1801114136U },
381a6a9c0f1SDaniel Borkmann 	{ 2019295544U, 645U, 3286438930U },
382a6a9c0f1SDaniel Borkmann 	{  469023838U, 716U, 1637918202U },
383a6a9c0f1SDaniel Borkmann 	{ 1843754496U, 653U, 2562092152U },
384a6a9c0f1SDaniel Borkmann 	{  400672036U, 809U, 4264212785U },
385a6a9c0f1SDaniel Borkmann 	{  404722249U, 965U, 2704116999U },
386a6a9c0f1SDaniel Borkmann 	{  600702209U, 758U,  584979986U },
387a6a9c0f1SDaniel Borkmann 	{  519953954U, 667U, 2574436237U },
388a6a9c0f1SDaniel Borkmann 	{ 1658071126U, 694U, 2214569490U },
389a6a9c0f1SDaniel Borkmann 	{  420480037U, 749U, 3430010866U },
390a6a9c0f1SDaniel Borkmann 	{  690103647U, 969U, 3700758083U },
391a6a9c0f1SDaniel Borkmann 	{ 1029424799U, 937U, 3787746841U },
392a6a9c0f1SDaniel Borkmann 	{ 2012608669U, 506U, 3362628973U },
393a6a9c0f1SDaniel Borkmann 	{ 1535432887U, 998U,   42610943U },
394a6a9c0f1SDaniel Borkmann 	{ 1330635533U, 857U, 3040806504U },
395a6a9c0f1SDaniel Borkmann 	{ 1223800550U, 539U, 3954229517U },
396a6a9c0f1SDaniel Borkmann 	{ 1322411537U, 680U, 3223250324U },
397a6a9c0f1SDaniel Borkmann 	{ 1877847898U, 945U, 2915147143U },
398a6a9c0f1SDaniel Borkmann 	{ 1646356099U, 874U,  965988280U },
399a6a9c0f1SDaniel Borkmann 	{  805687536U, 744U, 4032277920U },
400a6a9c0f1SDaniel Borkmann 	{ 1948093210U, 633U, 1346597684U },
401a6a9c0f1SDaniel Borkmann 	{  392609744U, 783U, 1636083295U },
402a6a9c0f1SDaniel Borkmann 	{  690241304U, 770U, 1201031298U },
403a6a9c0f1SDaniel Borkmann 	{ 1360302965U, 696U, 1665394461U },
404a6a9c0f1SDaniel Borkmann 	{ 1220090946U, 780U, 1316922812U },
405a6a9c0f1SDaniel Borkmann 	{  447092251U, 500U, 3438743375U },
406a6a9c0f1SDaniel Borkmann 	{ 1613868791U, 592U,  828546883U },
407a6a9c0f1SDaniel Borkmann 	{  523430951U, 548U, 2552392304U },
408a6a9c0f1SDaniel Borkmann 	{  726692899U, 810U, 1656872867U },
409a6a9c0f1SDaniel Borkmann 	{ 1364340021U, 836U, 3710513486U },
410a6a9c0f1SDaniel Borkmann 	{ 1986257729U, 931U,  935013962U },
411a6a9c0f1SDaniel Borkmann 	{  407983964U, 921U,  728767059U },
412a6a9c0f1SDaniel Borkmann };
413a6a9c0f1SDaniel Borkmann 
414a6a9c0f1SDaniel Borkmann static void __init prandom_state_selftest(void)
415a6a9c0f1SDaniel Borkmann {
416a6a9c0f1SDaniel Borkmann 	int i, j, errors = 0, runs = 0;
417a6a9c0f1SDaniel Borkmann 	bool error = false;
418a6a9c0f1SDaniel Borkmann 
419a6a9c0f1SDaniel Borkmann 	for (i = 0; i < ARRAY_SIZE(test1); i++) {
420a6a9c0f1SDaniel Borkmann 		struct rnd_state state;
421a6a9c0f1SDaniel Borkmann 
422a6a9c0f1SDaniel Borkmann 		prandom_seed_very_weak(&state, test1[i].seed);
423a6a9c0f1SDaniel Borkmann 		prandom_warmup(&state);
424a6a9c0f1SDaniel Borkmann 
425a6a9c0f1SDaniel Borkmann 		if (test1[i].result != prandom_u32_state(&state))
426a6a9c0f1SDaniel Borkmann 			error = true;
427a6a9c0f1SDaniel Borkmann 	}
428a6a9c0f1SDaniel Borkmann 
429a6a9c0f1SDaniel Borkmann 	if (error)
430a6a9c0f1SDaniel Borkmann 		pr_warn("prandom: seed boundary self test failed\n");
431a6a9c0f1SDaniel Borkmann 	else
432a6a9c0f1SDaniel Borkmann 		pr_info("prandom: seed boundary self test passed\n");
433a6a9c0f1SDaniel Borkmann 
434a6a9c0f1SDaniel Borkmann 	for (i = 0; i < ARRAY_SIZE(test2); i++) {
435a6a9c0f1SDaniel Borkmann 		struct rnd_state state;
436a6a9c0f1SDaniel Borkmann 
437a6a9c0f1SDaniel Borkmann 		prandom_seed_very_weak(&state, test2[i].seed);
438a6a9c0f1SDaniel Borkmann 		prandom_warmup(&state);
439a6a9c0f1SDaniel Borkmann 
440a6a9c0f1SDaniel Borkmann 		for (j = 0; j < test2[i].iteration - 1; j++)
441a6a9c0f1SDaniel Borkmann 			prandom_u32_state(&state);
442a6a9c0f1SDaniel Borkmann 
443a6a9c0f1SDaniel Borkmann 		if (test2[i].result != prandom_u32_state(&state))
444a6a9c0f1SDaniel Borkmann 			errors++;
445a6a9c0f1SDaniel Borkmann 
446a6a9c0f1SDaniel Borkmann 		runs++;
447a6a9c0f1SDaniel Borkmann 		cond_resched();
448a6a9c0f1SDaniel Borkmann 	}
449a6a9c0f1SDaniel Borkmann 
450a6a9c0f1SDaniel Borkmann 	if (errors)
451a6a9c0f1SDaniel Borkmann 		pr_warn("prandom: %d/%d self tests failed\n", errors, runs);
452a6a9c0f1SDaniel Borkmann 	else
453a6a9c0f1SDaniel Borkmann 		pr_info("prandom: %d self tests passed\n", runs);
454a6a9c0f1SDaniel Borkmann }
455a6a9c0f1SDaniel Borkmann #endif
456