xref: /OK3568_Linux_fs/kernel/crypto/jitterentropy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Non-physical true random number generator based on timing jitter --
3*4882a593Smuzhiyun  * Jitter RNG standalone code.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2020
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Design
8*4882a593Smuzhiyun  * ======
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * See https://www.chronox.de/jent.html
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * License
13*4882a593Smuzhiyun  * =======
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
16*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
17*4882a593Smuzhiyun  * are met:
18*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
19*4882a593Smuzhiyun  *    notice, and the entire permission notice in its entirety,
20*4882a593Smuzhiyun  *    including the disclaimer of warranties.
21*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
22*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
23*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
24*4882a593Smuzhiyun  * 3. The name of the author may not be used to endorse or promote
25*4882a593Smuzhiyun  *    products derived from this software without specific prior
26*4882a593Smuzhiyun  *    written permission.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * ALTERNATIVELY, this product may be distributed under the terms of
29*4882a593Smuzhiyun  * the GNU General Public License, in which case the provisions of the GPL2 are
30*4882a593Smuzhiyun  * required INSTEAD OF the above restrictions.  (This clause is
31*4882a593Smuzhiyun  * necessary due to a potential bad interaction between the GPL and
32*4882a593Smuzhiyun  * the restrictions contained in a BSD-style copyright.)
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
35*4882a593Smuzhiyun  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36*4882a593Smuzhiyun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
37*4882a593Smuzhiyun  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
38*4882a593Smuzhiyun  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40*4882a593Smuzhiyun  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41*4882a593Smuzhiyun  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42*4882a593Smuzhiyun  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
44*4882a593Smuzhiyun  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
45*4882a593Smuzhiyun  * DAMAGE.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * This Jitterentropy RNG is based on the jitterentropy library
50*4882a593Smuzhiyun  * version 2.2.0 provided at https://www.chronox.de/jent.html
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #ifdef __OPTIMIZE__
54*4882a593Smuzhiyun  #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun typedef	unsigned long long	__u64;
58*4882a593Smuzhiyun typedef	long long		__s64;
59*4882a593Smuzhiyun typedef	unsigned int		__u32;
60*4882a593Smuzhiyun #define NULL    ((void *) 0)
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* The entropy pool */
63*4882a593Smuzhiyun struct rand_data {
64*4882a593Smuzhiyun 	/* all data values that are vital to maintain the security
65*4882a593Smuzhiyun 	 * of the RNG are marked as SENSITIVE. A user must not
66*4882a593Smuzhiyun 	 * access that information while the RNG executes its loops to
67*4882a593Smuzhiyun 	 * calculate the next random value. */
68*4882a593Smuzhiyun 	__u64 data;		/* SENSITIVE Actual random number */
69*4882a593Smuzhiyun 	__u64 old_data;		/* SENSITIVE Previous random number */
70*4882a593Smuzhiyun 	__u64 prev_time;	/* SENSITIVE Previous time stamp */
71*4882a593Smuzhiyun #define DATA_SIZE_BITS ((sizeof(__u64)) * 8)
72*4882a593Smuzhiyun 	__u64 last_delta;	/* SENSITIVE stuck test */
73*4882a593Smuzhiyun 	__s64 last_delta2;	/* SENSITIVE stuck test */
74*4882a593Smuzhiyun 	unsigned int osr;	/* Oversample rate */
75*4882a593Smuzhiyun #define JENT_MEMORY_BLOCKS 64
76*4882a593Smuzhiyun #define JENT_MEMORY_BLOCKSIZE 32
77*4882a593Smuzhiyun #define JENT_MEMORY_ACCESSLOOPS 128
78*4882a593Smuzhiyun #define JENT_MEMORY_SIZE (JENT_MEMORY_BLOCKS*JENT_MEMORY_BLOCKSIZE)
79*4882a593Smuzhiyun 	unsigned char *mem;	/* Memory access location with size of
80*4882a593Smuzhiyun 				 * memblocks * memblocksize */
81*4882a593Smuzhiyun 	unsigned int memlocation; /* Pointer to byte in *mem */
82*4882a593Smuzhiyun 	unsigned int memblocks;	/* Number of memory blocks in *mem */
83*4882a593Smuzhiyun 	unsigned int memblocksize; /* Size of one memory block in bytes */
84*4882a593Smuzhiyun 	unsigned int memaccessloops; /* Number of memory accesses per random
85*4882a593Smuzhiyun 				      * bit generation */
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	/* Repetition Count Test */
88*4882a593Smuzhiyun 	int rct_count;			/* Number of stuck values */
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Adaptive Proportion Test for a significance level of 2^-30 */
91*4882a593Smuzhiyun #define JENT_APT_CUTOFF		325	/* Taken from SP800-90B sec 4.4.2 */
92*4882a593Smuzhiyun #define JENT_APT_WINDOW_SIZE	512	/* Data window size */
93*4882a593Smuzhiyun 	/* LSB of time stamp to process */
94*4882a593Smuzhiyun #define JENT_APT_LSB		16
95*4882a593Smuzhiyun #define JENT_APT_WORD_MASK	(JENT_APT_LSB - 1)
96*4882a593Smuzhiyun 	unsigned int apt_observations;	/* Number of collected observations */
97*4882a593Smuzhiyun 	unsigned int apt_count;		/* APT counter */
98*4882a593Smuzhiyun 	unsigned int apt_base;		/* APT base reference */
99*4882a593Smuzhiyun 	unsigned int apt_base_set:1;	/* APT base reference set? */
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	unsigned int health_failure:1;	/* Permanent health failure */
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /* Flags that can be used to initialize the RNG */
105*4882a593Smuzhiyun #define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more
106*4882a593Smuzhiyun 					   * entropy, saves MEMORY_SIZE RAM for
107*4882a593Smuzhiyun 					   * entropy collector */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* -- error codes for init function -- */
110*4882a593Smuzhiyun #define JENT_ENOTIME		1 /* Timer service not available */
111*4882a593Smuzhiyun #define JENT_ECOARSETIME	2 /* Timer too coarse for RNG */
112*4882a593Smuzhiyun #define JENT_ENOMONOTONIC	3 /* Timer is not monotonic increasing */
113*4882a593Smuzhiyun #define JENT_EVARVAR		5 /* Timer does not produce variations of
114*4882a593Smuzhiyun 				   * variations (2nd derivation of time is
115*4882a593Smuzhiyun 				   * zero). */
116*4882a593Smuzhiyun #define JENT_ESTUCK		8 /* Too many stuck results during init. */
117*4882a593Smuzhiyun #define JENT_EHEALTH		9 /* Health test failed during initialization */
118*4882a593Smuzhiyun #define JENT_ERCT		10 /* RCT failed during initialization */
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun  * The output n bits can receive more than n bits of min entropy, of course,
122*4882a593Smuzhiyun  * but the fixed output of the conditioning function can only asymptotically
123*4882a593Smuzhiyun  * approach the output size bits of min entropy, not attain that bound. Random
124*4882a593Smuzhiyun  * maps will tend to have output collisions, which reduces the creditable
125*4882a593Smuzhiyun  * output entropy (that is what SP 800-90B Section 3.1.5.1.2 attempts to bound).
126*4882a593Smuzhiyun  *
127*4882a593Smuzhiyun  * The value "64" is justified in Appendix A.4 of the current 90C draft,
128*4882a593Smuzhiyun  * and aligns with NIST's in "epsilon" definition in this document, which is
129*4882a593Smuzhiyun  * that a string can be considered "full entropy" if you can bound the min
130*4882a593Smuzhiyun  * entropy in each bit of output to at least 1-epsilon, where epsilon is
131*4882a593Smuzhiyun  * required to be <= 2^(-32).
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun #define JENT_ENTROPY_SAFETY_FACTOR	64
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun #include <linux/fips.h>
136*4882a593Smuzhiyun #include "jitterentropy.h"
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /***************************************************************************
139*4882a593Smuzhiyun  * Adaptive Proportion Test
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * This test complies with SP800-90B section 4.4.2.
142*4882a593Smuzhiyun  ***************************************************************************/
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /**
145*4882a593Smuzhiyun  * Reset the APT counter
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
148*4882a593Smuzhiyun  */
jent_apt_reset(struct rand_data * ec,unsigned int delta_masked)149*4882a593Smuzhiyun static void jent_apt_reset(struct rand_data *ec, unsigned int delta_masked)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	/* Reset APT counter */
152*4882a593Smuzhiyun 	ec->apt_count = 0;
153*4882a593Smuzhiyun 	ec->apt_base = delta_masked;
154*4882a593Smuzhiyun 	ec->apt_observations = 0;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun  * Insert a new entropy event into APT
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
161*4882a593Smuzhiyun  * @delta_masked [in] Masked time delta to process
162*4882a593Smuzhiyun  */
jent_apt_insert(struct rand_data * ec,unsigned int delta_masked)163*4882a593Smuzhiyun static void jent_apt_insert(struct rand_data *ec, unsigned int delta_masked)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	/* Initialize the base reference */
166*4882a593Smuzhiyun 	if (!ec->apt_base_set) {
167*4882a593Smuzhiyun 		ec->apt_base = delta_masked;
168*4882a593Smuzhiyun 		ec->apt_base_set = 1;
169*4882a593Smuzhiyun 		return;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	if (delta_masked == ec->apt_base) {
173*4882a593Smuzhiyun 		ec->apt_count++;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		if (ec->apt_count >= JENT_APT_CUTOFF)
176*4882a593Smuzhiyun 			ec->health_failure = 1;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	ec->apt_observations++;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (ec->apt_observations >= JENT_APT_WINDOW_SIZE)
182*4882a593Smuzhiyun 		jent_apt_reset(ec, delta_masked);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /***************************************************************************
186*4882a593Smuzhiyun  * Stuck Test and its use as Repetition Count Test
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * The Jitter RNG uses an enhanced version of the Repetition Count Test
189*4882a593Smuzhiyun  * (RCT) specified in SP800-90B section 4.4.1. Instead of counting identical
190*4882a593Smuzhiyun  * back-to-back values, the input to the RCT is the counting of the stuck
191*4882a593Smuzhiyun  * values during the generation of one Jitter RNG output block.
192*4882a593Smuzhiyun  *
193*4882a593Smuzhiyun  * The RCT is applied with an alpha of 2^{-30} compliant to FIPS 140-2 IG 9.8.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * During the counting operation, the Jitter RNG always calculates the RCT
196*4882a593Smuzhiyun  * cut-off value of C. If that value exceeds the allowed cut-off value,
197*4882a593Smuzhiyun  * the Jitter RNG output block will be calculated completely but discarded at
198*4882a593Smuzhiyun  * the end. The caller of the Jitter RNG is informed with an error code.
199*4882a593Smuzhiyun  ***************************************************************************/
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun  * Repetition Count Test as defined in SP800-90B section 4.4.1
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
205*4882a593Smuzhiyun  * @stuck [in] Indicator whether the value is stuck
206*4882a593Smuzhiyun  */
jent_rct_insert(struct rand_data * ec,int stuck)207*4882a593Smuzhiyun static void jent_rct_insert(struct rand_data *ec, int stuck)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	/*
210*4882a593Smuzhiyun 	 * If we have a count less than zero, a previous RCT round identified
211*4882a593Smuzhiyun 	 * a failure. We will not overwrite it.
212*4882a593Smuzhiyun 	 */
213*4882a593Smuzhiyun 	if (ec->rct_count < 0)
214*4882a593Smuzhiyun 		return;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (stuck) {
217*4882a593Smuzhiyun 		ec->rct_count++;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 		/*
220*4882a593Smuzhiyun 		 * The cutoff value is based on the following consideration:
221*4882a593Smuzhiyun 		 * alpha = 2^-30 as recommended in FIPS 140-2 IG 9.8.
222*4882a593Smuzhiyun 		 * In addition, we require an entropy value H of 1/OSR as this
223*4882a593Smuzhiyun 		 * is the minimum entropy required to provide full entropy.
224*4882a593Smuzhiyun 		 * Note, we collect 64 * OSR deltas for inserting them into
225*4882a593Smuzhiyun 		 * the entropy pool which should then have (close to) 64 bits
226*4882a593Smuzhiyun 		 * of entropy.
227*4882a593Smuzhiyun 		 *
228*4882a593Smuzhiyun 		 * Note, ec->rct_count (which equals to value B in the pseudo
229*4882a593Smuzhiyun 		 * code of SP800-90B section 4.4.1) starts with zero. Hence
230*4882a593Smuzhiyun 		 * we need to subtract one from the cutoff value as calculated
231*4882a593Smuzhiyun 		 * following SP800-90B.
232*4882a593Smuzhiyun 		 */
233*4882a593Smuzhiyun 		if ((unsigned int)ec->rct_count >= (31 * ec->osr)) {
234*4882a593Smuzhiyun 			ec->rct_count = -1;
235*4882a593Smuzhiyun 			ec->health_failure = 1;
236*4882a593Smuzhiyun 		}
237*4882a593Smuzhiyun 	} else {
238*4882a593Smuzhiyun 		ec->rct_count = 0;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun  * Is there an RCT health test failure?
244*4882a593Smuzhiyun  *
245*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
246*4882a593Smuzhiyun  *
247*4882a593Smuzhiyun  * @return
248*4882a593Smuzhiyun  * 	0 No health test failure
249*4882a593Smuzhiyun  * 	1 Permanent health test failure
250*4882a593Smuzhiyun  */
jent_rct_failure(struct rand_data * ec)251*4882a593Smuzhiyun static int jent_rct_failure(struct rand_data *ec)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	if (ec->rct_count < 0)
254*4882a593Smuzhiyun 		return 1;
255*4882a593Smuzhiyun 	return 0;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
jent_delta(__u64 prev,__u64 next)258*4882a593Smuzhiyun static inline __u64 jent_delta(__u64 prev, __u64 next)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun #define JENT_UINT64_MAX		(__u64)(~((__u64) 0))
261*4882a593Smuzhiyun 	return (prev < next) ? (next - prev) :
262*4882a593Smuzhiyun 			       (JENT_UINT64_MAX - prev + 1 + next);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun /**
266*4882a593Smuzhiyun  * Stuck test by checking the:
267*4882a593Smuzhiyun  * 	1st derivative of the jitter measurement (time delta)
268*4882a593Smuzhiyun  * 	2nd derivative of the jitter measurement (delta of time deltas)
269*4882a593Smuzhiyun  * 	3rd derivative of the jitter measurement (delta of delta of time deltas)
270*4882a593Smuzhiyun  *
271*4882a593Smuzhiyun  * All values must always be non-zero.
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
274*4882a593Smuzhiyun  * @current_delta [in] Jitter time delta
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * @return
277*4882a593Smuzhiyun  * 	0 jitter measurement not stuck (good bit)
278*4882a593Smuzhiyun  * 	1 jitter measurement stuck (reject bit)
279*4882a593Smuzhiyun  */
jent_stuck(struct rand_data * ec,__u64 current_delta)280*4882a593Smuzhiyun static int jent_stuck(struct rand_data *ec, __u64 current_delta)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	__u64 delta2 = jent_delta(ec->last_delta, current_delta);
283*4882a593Smuzhiyun 	__u64 delta3 = jent_delta(ec->last_delta2, delta2);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	ec->last_delta = current_delta;
286*4882a593Smuzhiyun 	ec->last_delta2 = delta2;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/*
289*4882a593Smuzhiyun 	 * Insert the result of the comparison of two back-to-back time
290*4882a593Smuzhiyun 	 * deltas.
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	jent_apt_insert(ec, current_delta);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	if (!current_delta || !delta2 || !delta3) {
295*4882a593Smuzhiyun 		/* RCT with a stuck bit */
296*4882a593Smuzhiyun 		jent_rct_insert(ec, 1);
297*4882a593Smuzhiyun 		return 1;
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* RCT with a non-stuck bit */
301*4882a593Smuzhiyun 	jent_rct_insert(ec, 0);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun /**
307*4882a593Smuzhiyun  * Report any health test failures
308*4882a593Smuzhiyun  *
309*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
310*4882a593Smuzhiyun  *
311*4882a593Smuzhiyun  * @return
312*4882a593Smuzhiyun  * 	0 No health test failure
313*4882a593Smuzhiyun  * 	1 Permanent health test failure
314*4882a593Smuzhiyun  */
jent_health_failure(struct rand_data * ec)315*4882a593Smuzhiyun static int jent_health_failure(struct rand_data *ec)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	/* Test is only enabled in FIPS mode */
318*4882a593Smuzhiyun 	if (!jent_fips_enabled())
319*4882a593Smuzhiyun 		return 0;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	return ec->health_failure;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /***************************************************************************
325*4882a593Smuzhiyun  * Noise sources
326*4882a593Smuzhiyun  ***************************************************************************/
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /**
329*4882a593Smuzhiyun  * Update of the loop count used for the next round of
330*4882a593Smuzhiyun  * an entropy collection.
331*4882a593Smuzhiyun  *
332*4882a593Smuzhiyun  * Input:
333*4882a593Smuzhiyun  * @ec entropy collector struct -- may be NULL
334*4882a593Smuzhiyun  * @bits is the number of low bits of the timer to consider
335*4882a593Smuzhiyun  * @min is the number of bits we shift the timer value to the right at
336*4882a593Smuzhiyun  *	the end to make sure we have a guaranteed minimum value
337*4882a593Smuzhiyun  *
338*4882a593Smuzhiyun  * @return Newly calculated loop counter
339*4882a593Smuzhiyun  */
jent_loop_shuffle(struct rand_data * ec,unsigned int bits,unsigned int min)340*4882a593Smuzhiyun static __u64 jent_loop_shuffle(struct rand_data *ec,
341*4882a593Smuzhiyun 			       unsigned int bits, unsigned int min)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	__u64 time = 0;
344*4882a593Smuzhiyun 	__u64 shuffle = 0;
345*4882a593Smuzhiyun 	unsigned int i = 0;
346*4882a593Smuzhiyun 	unsigned int mask = (1<<bits) - 1;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	jent_get_nstime(&time);
349*4882a593Smuzhiyun 	/*
350*4882a593Smuzhiyun 	 * Mix the current state of the random number into the shuffle
351*4882a593Smuzhiyun 	 * calculation to balance that shuffle a bit more.
352*4882a593Smuzhiyun 	 */
353*4882a593Smuzhiyun 	if (ec)
354*4882a593Smuzhiyun 		time ^= ec->data;
355*4882a593Smuzhiyun 	/*
356*4882a593Smuzhiyun 	 * We fold the time value as much as possible to ensure that as many
357*4882a593Smuzhiyun 	 * bits of the time stamp are included as possible.
358*4882a593Smuzhiyun 	 */
359*4882a593Smuzhiyun 	for (i = 0; ((DATA_SIZE_BITS + bits - 1) / bits) > i; i++) {
360*4882a593Smuzhiyun 		shuffle ^= time & mask;
361*4882a593Smuzhiyun 		time = time >> bits;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	/*
365*4882a593Smuzhiyun 	 * We add a lower boundary value to ensure we have a minimum
366*4882a593Smuzhiyun 	 * RNG loop count.
367*4882a593Smuzhiyun 	 */
368*4882a593Smuzhiyun 	return (shuffle + (1<<min));
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun /**
372*4882a593Smuzhiyun  * CPU Jitter noise source -- this is the noise source based on the CPU
373*4882a593Smuzhiyun  *			      execution time jitter
374*4882a593Smuzhiyun  *
375*4882a593Smuzhiyun  * This function injects the individual bits of the time value into the
376*4882a593Smuzhiyun  * entropy pool using an LFSR.
377*4882a593Smuzhiyun  *
378*4882a593Smuzhiyun  * The code is deliberately inefficient with respect to the bit shifting
379*4882a593Smuzhiyun  * and shall stay that way. This function is the root cause why the code
380*4882a593Smuzhiyun  * shall be compiled without optimization. This function not only acts as
381*4882a593Smuzhiyun  * folding operation, but this function's execution is used to measure
382*4882a593Smuzhiyun  * the CPU execution time jitter. Any change to the loop in this function
383*4882a593Smuzhiyun  * implies that careful retesting must be done.
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * @ec [in] entropy collector struct
386*4882a593Smuzhiyun  * @time [in] time stamp to be injected
387*4882a593Smuzhiyun  * @loop_cnt [in] if a value not equal to 0 is set, use the given value as
388*4882a593Smuzhiyun  *		  number of loops to perform the folding
389*4882a593Smuzhiyun  * @stuck [in] Is the time stamp identified as stuck?
390*4882a593Smuzhiyun  *
391*4882a593Smuzhiyun  * Output:
392*4882a593Smuzhiyun  * updated ec->data
393*4882a593Smuzhiyun  *
394*4882a593Smuzhiyun  * @return Number of loops the folding operation is performed
395*4882a593Smuzhiyun  */
jent_lfsr_time(struct rand_data * ec,__u64 time,__u64 loop_cnt,int stuck)396*4882a593Smuzhiyun static void jent_lfsr_time(struct rand_data *ec, __u64 time, __u64 loop_cnt,
397*4882a593Smuzhiyun 			   int stuck)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	unsigned int i;
400*4882a593Smuzhiyun 	__u64 j = 0;
401*4882a593Smuzhiyun 	__u64 new = 0;
402*4882a593Smuzhiyun #define MAX_FOLD_LOOP_BIT 4
403*4882a593Smuzhiyun #define MIN_FOLD_LOOP_BIT 0
404*4882a593Smuzhiyun 	__u64 fold_loop_cnt =
405*4882a593Smuzhiyun 		jent_loop_shuffle(ec, MAX_FOLD_LOOP_BIT, MIN_FOLD_LOOP_BIT);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/*
408*4882a593Smuzhiyun 	 * testing purposes -- allow test app to set the counter, not
409*4882a593Smuzhiyun 	 * needed during runtime
410*4882a593Smuzhiyun 	 */
411*4882a593Smuzhiyun 	if (loop_cnt)
412*4882a593Smuzhiyun 		fold_loop_cnt = loop_cnt;
413*4882a593Smuzhiyun 	for (j = 0; j < fold_loop_cnt; j++) {
414*4882a593Smuzhiyun 		new = ec->data;
415*4882a593Smuzhiyun 		for (i = 1; (DATA_SIZE_BITS) >= i; i++) {
416*4882a593Smuzhiyun 			__u64 tmp = time << (DATA_SIZE_BITS - i);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 			tmp = tmp >> (DATA_SIZE_BITS - 1);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 			/*
421*4882a593Smuzhiyun 			* Fibonacci LSFR with polynomial of
422*4882a593Smuzhiyun 			*  x^64 + x^61 + x^56 + x^31 + x^28 + x^23 + 1 which is
423*4882a593Smuzhiyun 			*  primitive according to
424*4882a593Smuzhiyun 			*   http://poincare.matf.bg.ac.rs/~ezivkovm/publications/primpol1.pdf
425*4882a593Smuzhiyun 			* (the shift values are the polynomial values minus one
426*4882a593Smuzhiyun 			* due to counting bits from 0 to 63). As the current
427*4882a593Smuzhiyun 			* position is always the LSB, the polynomial only needs
428*4882a593Smuzhiyun 			* to shift data in from the left without wrap.
429*4882a593Smuzhiyun 			*/
430*4882a593Smuzhiyun 			tmp ^= ((new >> 63) & 1);
431*4882a593Smuzhiyun 			tmp ^= ((new >> 60) & 1);
432*4882a593Smuzhiyun 			tmp ^= ((new >> 55) & 1);
433*4882a593Smuzhiyun 			tmp ^= ((new >> 30) & 1);
434*4882a593Smuzhiyun 			tmp ^= ((new >> 27) & 1);
435*4882a593Smuzhiyun 			tmp ^= ((new >> 22) & 1);
436*4882a593Smuzhiyun 			new <<= 1;
437*4882a593Smuzhiyun 			new ^= tmp;
438*4882a593Smuzhiyun 		}
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	/*
442*4882a593Smuzhiyun 	 * If the time stamp is stuck, do not finally insert the value into
443*4882a593Smuzhiyun 	 * the entropy pool. Although this operation should not do any harm
444*4882a593Smuzhiyun 	 * even when the time stamp has no entropy, SP800-90B requires that
445*4882a593Smuzhiyun 	 * any conditioning operation (SP800-90B considers the LFSR to be a
446*4882a593Smuzhiyun 	 * conditioning operation) to have an identical amount of input
447*4882a593Smuzhiyun 	 * data according to section 3.1.5.
448*4882a593Smuzhiyun 	 */
449*4882a593Smuzhiyun 	if (!stuck)
450*4882a593Smuzhiyun 		ec->data = new;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun /**
454*4882a593Smuzhiyun  * Memory Access noise source -- this is a noise source based on variations in
455*4882a593Smuzhiyun  *				 memory access times
456*4882a593Smuzhiyun  *
457*4882a593Smuzhiyun  * This function performs memory accesses which will add to the timing
458*4882a593Smuzhiyun  * variations due to an unknown amount of CPU wait states that need to be
459*4882a593Smuzhiyun  * added when accessing memory. The memory size should be larger than the L1
460*4882a593Smuzhiyun  * caches as outlined in the documentation and the associated testing.
461*4882a593Smuzhiyun  *
462*4882a593Smuzhiyun  * The L1 cache has a very high bandwidth, albeit its access rate is  usually
463*4882a593Smuzhiyun  * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
464*4882a593Smuzhiyun  * variations as the CPU has hardly to wait. Starting with L2, significant
465*4882a593Smuzhiyun  * variations are added because L2 typically does not belong to the CPU any more
466*4882a593Smuzhiyun  * and therefore a wider range of CPU wait states is necessary for accesses.
467*4882a593Smuzhiyun  * L3 and real memory accesses have even a wider range of wait states. However,
468*4882a593Smuzhiyun  * to reliably access either L3 or memory, the ec->mem memory must be quite
469*4882a593Smuzhiyun  * large which is usually not desirable.
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  * @ec [in] Reference to the entropy collector with the memory access data -- if
472*4882a593Smuzhiyun  *	    the reference to the memory block to be accessed is NULL, this noise
473*4882a593Smuzhiyun  *	    source is disabled
474*4882a593Smuzhiyun  * @loop_cnt [in] if a value not equal to 0 is set, use the given value
475*4882a593Smuzhiyun  *		  number of loops to perform the LFSR
476*4882a593Smuzhiyun  */
jent_memaccess(struct rand_data * ec,__u64 loop_cnt)477*4882a593Smuzhiyun static void jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun 	unsigned int wrap = 0;
480*4882a593Smuzhiyun 	__u64 i = 0;
481*4882a593Smuzhiyun #define MAX_ACC_LOOP_BIT 7
482*4882a593Smuzhiyun #define MIN_ACC_LOOP_BIT 0
483*4882a593Smuzhiyun 	__u64 acc_loop_cnt =
484*4882a593Smuzhiyun 		jent_loop_shuffle(ec, MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (NULL == ec || NULL == ec->mem)
487*4882a593Smuzhiyun 		return;
488*4882a593Smuzhiyun 	wrap = ec->memblocksize * ec->memblocks;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	/*
491*4882a593Smuzhiyun 	 * testing purposes -- allow test app to set the counter, not
492*4882a593Smuzhiyun 	 * needed during runtime
493*4882a593Smuzhiyun 	 */
494*4882a593Smuzhiyun 	if (loop_cnt)
495*4882a593Smuzhiyun 		acc_loop_cnt = loop_cnt;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
498*4882a593Smuzhiyun 		unsigned char *tmpval = ec->mem + ec->memlocation;
499*4882a593Smuzhiyun 		/*
500*4882a593Smuzhiyun 		 * memory access: just add 1 to one byte,
501*4882a593Smuzhiyun 		 * wrap at 255 -- memory access implies read
502*4882a593Smuzhiyun 		 * from and write to memory location
503*4882a593Smuzhiyun 		 */
504*4882a593Smuzhiyun 		*tmpval = (*tmpval + 1) & 0xff;
505*4882a593Smuzhiyun 		/*
506*4882a593Smuzhiyun 		 * Addition of memblocksize - 1 to pointer
507*4882a593Smuzhiyun 		 * with wrap around logic to ensure that every
508*4882a593Smuzhiyun 		 * memory location is hit evenly
509*4882a593Smuzhiyun 		 */
510*4882a593Smuzhiyun 		ec->memlocation = ec->memlocation + ec->memblocksize - 1;
511*4882a593Smuzhiyun 		ec->memlocation = ec->memlocation % wrap;
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun /***************************************************************************
516*4882a593Smuzhiyun  * Start of entropy processing logic
517*4882a593Smuzhiyun  ***************************************************************************/
518*4882a593Smuzhiyun /**
519*4882a593Smuzhiyun  * This is the heart of the entropy generation: calculate time deltas and
520*4882a593Smuzhiyun  * use the CPU jitter in the time deltas. The jitter is injected into the
521*4882a593Smuzhiyun  * entropy pool.
522*4882a593Smuzhiyun  *
523*4882a593Smuzhiyun  * WARNING: ensure that ->prev_time is primed before using the output
524*4882a593Smuzhiyun  *	    of this function! This can be done by calling this function
525*4882a593Smuzhiyun  *	    and not using its result.
526*4882a593Smuzhiyun  *
527*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
528*4882a593Smuzhiyun  *
529*4882a593Smuzhiyun  * @return result of stuck test
530*4882a593Smuzhiyun  */
jent_measure_jitter(struct rand_data * ec)531*4882a593Smuzhiyun static int jent_measure_jitter(struct rand_data *ec)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	__u64 time = 0;
534*4882a593Smuzhiyun 	__u64 current_delta = 0;
535*4882a593Smuzhiyun 	int stuck;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	/* Invoke one noise source before time measurement to add variations */
538*4882a593Smuzhiyun 	jent_memaccess(ec, 0);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/*
541*4882a593Smuzhiyun 	 * Get time stamp and calculate time delta to previous
542*4882a593Smuzhiyun 	 * invocation to measure the timing variations
543*4882a593Smuzhiyun 	 */
544*4882a593Smuzhiyun 	jent_get_nstime(&time);
545*4882a593Smuzhiyun 	current_delta = jent_delta(ec->prev_time, time);
546*4882a593Smuzhiyun 	ec->prev_time = time;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	/* Check whether we have a stuck measurement. */
549*4882a593Smuzhiyun 	stuck = jent_stuck(ec, current_delta);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	/* Now call the next noise sources which also injects the data */
552*4882a593Smuzhiyun 	jent_lfsr_time(ec, current_delta, 0, stuck);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	return stuck;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /**
558*4882a593Smuzhiyun  * Generator of one 64 bit random number
559*4882a593Smuzhiyun  * Function fills rand_data->data
560*4882a593Smuzhiyun  *
561*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
562*4882a593Smuzhiyun  */
jent_gen_entropy(struct rand_data * ec)563*4882a593Smuzhiyun static void jent_gen_entropy(struct rand_data *ec)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun 	unsigned int k = 0, safety_factor = 0;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	if (fips_enabled)
568*4882a593Smuzhiyun 		safety_factor = JENT_ENTROPY_SAFETY_FACTOR;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	/* priming of the ->prev_time value */
571*4882a593Smuzhiyun 	jent_measure_jitter(ec);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	while (1) {
574*4882a593Smuzhiyun 		/* If a stuck measurement is received, repeat measurement */
575*4882a593Smuzhiyun 		if (jent_measure_jitter(ec))
576*4882a593Smuzhiyun 			continue;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 		/*
579*4882a593Smuzhiyun 		 * We multiply the loop value with ->osr to obtain the
580*4882a593Smuzhiyun 		 * oversampling rate requested by the caller
581*4882a593Smuzhiyun 		 */
582*4882a593Smuzhiyun 		if (++k >= ((DATA_SIZE_BITS + safety_factor) * ec->osr))
583*4882a593Smuzhiyun 			break;
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /**
588*4882a593Smuzhiyun  * Entry function: Obtain entropy for the caller.
589*4882a593Smuzhiyun  *
590*4882a593Smuzhiyun  * This function invokes the entropy gathering logic as often to generate
591*4882a593Smuzhiyun  * as many bytes as requested by the caller. The entropy gathering logic
592*4882a593Smuzhiyun  * creates 64 bit per invocation.
593*4882a593Smuzhiyun  *
594*4882a593Smuzhiyun  * This function truncates the last 64 bit entropy value output to the exact
595*4882a593Smuzhiyun  * size specified by the caller.
596*4882a593Smuzhiyun  *
597*4882a593Smuzhiyun  * @ec [in] Reference to entropy collector
598*4882a593Smuzhiyun  * @data [in] pointer to buffer for storing random data -- buffer must already
599*4882a593Smuzhiyun  *	      exist
600*4882a593Smuzhiyun  * @len [in] size of the buffer, specifying also the requested number of random
601*4882a593Smuzhiyun  *	     in bytes
602*4882a593Smuzhiyun  *
603*4882a593Smuzhiyun  * @return 0 when request is fulfilled or an error
604*4882a593Smuzhiyun  *
605*4882a593Smuzhiyun  * The following error codes can occur:
606*4882a593Smuzhiyun  *	-1	entropy_collector is NULL
607*4882a593Smuzhiyun  *	-2	RCT failed
608*4882a593Smuzhiyun  *	-3	APT test failed
609*4882a593Smuzhiyun  */
jent_read_entropy(struct rand_data * ec,unsigned char * data,unsigned int len)610*4882a593Smuzhiyun int jent_read_entropy(struct rand_data *ec, unsigned char *data,
611*4882a593Smuzhiyun 		      unsigned int len)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun 	unsigned char *p = data;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (!ec)
616*4882a593Smuzhiyun 		return -1;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	while (0 < len) {
619*4882a593Smuzhiyun 		unsigned int tocopy;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 		jent_gen_entropy(ec);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 		if (jent_health_failure(ec)) {
624*4882a593Smuzhiyun 			int ret;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 			if (jent_rct_failure(ec))
627*4882a593Smuzhiyun 				ret = -2;
628*4882a593Smuzhiyun 			else
629*4882a593Smuzhiyun 				ret = -3;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 			/*
632*4882a593Smuzhiyun 			 * Re-initialize the noise source
633*4882a593Smuzhiyun 			 *
634*4882a593Smuzhiyun 			 * If the health test fails, the Jitter RNG remains
635*4882a593Smuzhiyun 			 * in failure state and will return a health failure
636*4882a593Smuzhiyun 			 * during next invocation.
637*4882a593Smuzhiyun 			 */
638*4882a593Smuzhiyun 			if (jent_entropy_init())
639*4882a593Smuzhiyun 				return ret;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 			/* Set APT to initial state */
642*4882a593Smuzhiyun 			jent_apt_reset(ec, 0);
643*4882a593Smuzhiyun 			ec->apt_base_set = 0;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 			/* Set RCT to initial state */
646*4882a593Smuzhiyun 			ec->rct_count = 0;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 			/* Re-enable Jitter RNG */
649*4882a593Smuzhiyun 			ec->health_failure = 0;
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 			/*
652*4882a593Smuzhiyun 			 * Return the health test failure status to the
653*4882a593Smuzhiyun 			 * caller as the generated value is not appropriate.
654*4882a593Smuzhiyun 			 */
655*4882a593Smuzhiyun 			return ret;
656*4882a593Smuzhiyun 		}
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 		if ((DATA_SIZE_BITS / 8) < len)
659*4882a593Smuzhiyun 			tocopy = (DATA_SIZE_BITS / 8);
660*4882a593Smuzhiyun 		else
661*4882a593Smuzhiyun 			tocopy = len;
662*4882a593Smuzhiyun 		jent_memcpy(p, &ec->data, tocopy);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 		len -= tocopy;
665*4882a593Smuzhiyun 		p += tocopy;
666*4882a593Smuzhiyun 	}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	return 0;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun /***************************************************************************
672*4882a593Smuzhiyun  * Initialization logic
673*4882a593Smuzhiyun  ***************************************************************************/
674*4882a593Smuzhiyun 
jent_entropy_collector_alloc(unsigned int osr,unsigned int flags)675*4882a593Smuzhiyun struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
676*4882a593Smuzhiyun 					       unsigned int flags)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	struct rand_data *entropy_collector;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	entropy_collector = jent_zalloc(sizeof(struct rand_data));
681*4882a593Smuzhiyun 	if (!entropy_collector)
682*4882a593Smuzhiyun 		return NULL;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
685*4882a593Smuzhiyun 		/* Allocate memory for adding variations based on memory
686*4882a593Smuzhiyun 		 * access
687*4882a593Smuzhiyun 		 */
688*4882a593Smuzhiyun 		entropy_collector->mem = jent_zalloc(JENT_MEMORY_SIZE);
689*4882a593Smuzhiyun 		if (!entropy_collector->mem) {
690*4882a593Smuzhiyun 			jent_zfree(entropy_collector);
691*4882a593Smuzhiyun 			return NULL;
692*4882a593Smuzhiyun 		}
693*4882a593Smuzhiyun 		entropy_collector->memblocksize = JENT_MEMORY_BLOCKSIZE;
694*4882a593Smuzhiyun 		entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
695*4882a593Smuzhiyun 		entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
696*4882a593Smuzhiyun 	}
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	/* verify and set the oversampling rate */
699*4882a593Smuzhiyun 	if (0 == osr)
700*4882a593Smuzhiyun 		osr = 1; /* minimum sampling rate is 1 */
701*4882a593Smuzhiyun 	entropy_collector->osr = osr;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	/* fill the data pad with non-zero values */
704*4882a593Smuzhiyun 	jent_gen_entropy(entropy_collector);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	return entropy_collector;
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun 
jent_entropy_collector_free(struct rand_data * entropy_collector)709*4882a593Smuzhiyun void jent_entropy_collector_free(struct rand_data *entropy_collector)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun 	jent_zfree(entropy_collector->mem);
712*4882a593Smuzhiyun 	entropy_collector->mem = NULL;
713*4882a593Smuzhiyun 	jent_zfree(entropy_collector);
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun 
jent_entropy_init(void)716*4882a593Smuzhiyun int jent_entropy_init(void)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun 	int i;
719*4882a593Smuzhiyun 	__u64 delta_sum = 0;
720*4882a593Smuzhiyun 	__u64 old_delta = 0;
721*4882a593Smuzhiyun 	unsigned int nonstuck = 0;
722*4882a593Smuzhiyun 	int time_backwards = 0;
723*4882a593Smuzhiyun 	int count_mod = 0;
724*4882a593Smuzhiyun 	int count_stuck = 0;
725*4882a593Smuzhiyun 	struct rand_data ec = { 0 };
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	/* Required for RCT */
728*4882a593Smuzhiyun 	ec.osr = 1;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	/* We could perform statistical tests here, but the problem is
731*4882a593Smuzhiyun 	 * that we only have a few loop counts to do testing. These
732*4882a593Smuzhiyun 	 * loop counts may show some slight skew and we produce
733*4882a593Smuzhiyun 	 * false positives.
734*4882a593Smuzhiyun 	 *
735*4882a593Smuzhiyun 	 * Moreover, only old systems show potentially problematic
736*4882a593Smuzhiyun 	 * jitter entropy that could potentially be caught here. But
737*4882a593Smuzhiyun 	 * the RNG is intended for hardware that is available or widely
738*4882a593Smuzhiyun 	 * used, but not old systems that are long out of favor. Thus,
739*4882a593Smuzhiyun 	 * no statistical tests.
740*4882a593Smuzhiyun 	 */
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	/*
743*4882a593Smuzhiyun 	 * We could add a check for system capabilities such as clock_getres or
744*4882a593Smuzhiyun 	 * check for CONFIG_X86_TSC, but it does not make much sense as the
745*4882a593Smuzhiyun 	 * following sanity checks verify that we have a high-resolution
746*4882a593Smuzhiyun 	 * timer.
747*4882a593Smuzhiyun 	 */
748*4882a593Smuzhiyun 	/*
749*4882a593Smuzhiyun 	 * TESTLOOPCOUNT needs some loops to identify edge systems. 100 is
750*4882a593Smuzhiyun 	 * definitely too little.
751*4882a593Smuzhiyun 	 *
752*4882a593Smuzhiyun 	 * SP800-90B requires at least 1024 initial test cycles.
753*4882a593Smuzhiyun 	 */
754*4882a593Smuzhiyun #define TESTLOOPCOUNT 1024
755*4882a593Smuzhiyun #define CLEARCACHE 100
756*4882a593Smuzhiyun 	for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
757*4882a593Smuzhiyun 		__u64 time = 0;
758*4882a593Smuzhiyun 		__u64 time2 = 0;
759*4882a593Smuzhiyun 		__u64 delta = 0;
760*4882a593Smuzhiyun 		unsigned int lowdelta = 0;
761*4882a593Smuzhiyun 		int stuck;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 		/* Invoke core entropy collection logic */
764*4882a593Smuzhiyun 		jent_get_nstime(&time);
765*4882a593Smuzhiyun 		ec.prev_time = time;
766*4882a593Smuzhiyun 		jent_lfsr_time(&ec, time, 0, 0);
767*4882a593Smuzhiyun 		jent_get_nstime(&time2);
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 		/* test whether timer works */
770*4882a593Smuzhiyun 		if (!time || !time2)
771*4882a593Smuzhiyun 			return JENT_ENOTIME;
772*4882a593Smuzhiyun 		delta = jent_delta(time, time2);
773*4882a593Smuzhiyun 		/*
774*4882a593Smuzhiyun 		 * test whether timer is fine grained enough to provide
775*4882a593Smuzhiyun 		 * delta even when called shortly after each other -- this
776*4882a593Smuzhiyun 		 * implies that we also have a high resolution timer
777*4882a593Smuzhiyun 		 */
778*4882a593Smuzhiyun 		if (!delta)
779*4882a593Smuzhiyun 			return JENT_ECOARSETIME;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 		stuck = jent_stuck(&ec, delta);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		/*
784*4882a593Smuzhiyun 		 * up to here we did not modify any variable that will be
785*4882a593Smuzhiyun 		 * evaluated later, but we already performed some work. Thus we
786*4882a593Smuzhiyun 		 * already have had an impact on the caches, branch prediction,
787*4882a593Smuzhiyun 		 * etc. with the goal to clear it to get the worst case
788*4882a593Smuzhiyun 		 * measurements.
789*4882a593Smuzhiyun 		 */
790*4882a593Smuzhiyun 		if (CLEARCACHE > i)
791*4882a593Smuzhiyun 			continue;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 		if (stuck)
794*4882a593Smuzhiyun 			count_stuck++;
795*4882a593Smuzhiyun 		else {
796*4882a593Smuzhiyun 			nonstuck++;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 			/*
799*4882a593Smuzhiyun 			 * Ensure that the APT succeeded.
800*4882a593Smuzhiyun 			 *
801*4882a593Smuzhiyun 			 * With the check below that count_stuck must be less
802*4882a593Smuzhiyun 			 * than 10% of the overall generated raw entropy values
803*4882a593Smuzhiyun 			 * it is guaranteed that the APT is invoked at
804*4882a593Smuzhiyun 			 * floor((TESTLOOPCOUNT * 0.9) / 64) == 14 times.
805*4882a593Smuzhiyun 			 */
806*4882a593Smuzhiyun 			if ((nonstuck % JENT_APT_WINDOW_SIZE) == 0) {
807*4882a593Smuzhiyun 				jent_apt_reset(&ec,
808*4882a593Smuzhiyun 					       delta & JENT_APT_WORD_MASK);
809*4882a593Smuzhiyun 				if (jent_health_failure(&ec))
810*4882a593Smuzhiyun 					return JENT_EHEALTH;
811*4882a593Smuzhiyun 			}
812*4882a593Smuzhiyun 		}
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 		/* Validate RCT */
815*4882a593Smuzhiyun 		if (jent_rct_failure(&ec))
816*4882a593Smuzhiyun 			return JENT_ERCT;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 		/* test whether we have an increasing timer */
819*4882a593Smuzhiyun 		if (!(time2 > time))
820*4882a593Smuzhiyun 			time_backwards++;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 		/* use 32 bit value to ensure compilation on 32 bit arches */
823*4882a593Smuzhiyun 		lowdelta = time2 - time;
824*4882a593Smuzhiyun 		if (!(lowdelta % 100))
825*4882a593Smuzhiyun 			count_mod++;
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 		/*
828*4882a593Smuzhiyun 		 * ensure that we have a varying delta timer which is necessary
829*4882a593Smuzhiyun 		 * for the calculation of entropy -- perform this check
830*4882a593Smuzhiyun 		 * only after the first loop is executed as we need to prime
831*4882a593Smuzhiyun 		 * the old_data value
832*4882a593Smuzhiyun 		 */
833*4882a593Smuzhiyun 		if (delta > old_delta)
834*4882a593Smuzhiyun 			delta_sum += (delta - old_delta);
835*4882a593Smuzhiyun 		else
836*4882a593Smuzhiyun 			delta_sum += (old_delta - delta);
837*4882a593Smuzhiyun 		old_delta = delta;
838*4882a593Smuzhiyun 	}
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	/*
841*4882a593Smuzhiyun 	 * we allow up to three times the time running backwards.
842*4882a593Smuzhiyun 	 * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
843*4882a593Smuzhiyun 	 * if such an operation just happens to interfere with our test, it
844*4882a593Smuzhiyun 	 * should not fail. The value of 3 should cover the NTP case being
845*4882a593Smuzhiyun 	 * performed during our test run.
846*4882a593Smuzhiyun 	 */
847*4882a593Smuzhiyun 	if (3 < time_backwards)
848*4882a593Smuzhiyun 		return JENT_ENOMONOTONIC;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	/*
851*4882a593Smuzhiyun 	 * Variations of deltas of time must on average be larger
852*4882a593Smuzhiyun 	 * than 1 to ensure the entropy estimation
853*4882a593Smuzhiyun 	 * implied with 1 is preserved
854*4882a593Smuzhiyun 	 */
855*4882a593Smuzhiyun 	if ((delta_sum) <= 1)
856*4882a593Smuzhiyun 		return JENT_EVARVAR;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	/*
859*4882a593Smuzhiyun 	 * Ensure that we have variations in the time stamp below 10 for at
860*4882a593Smuzhiyun 	 * least 10% of all checks -- on some platforms, the counter increments
861*4882a593Smuzhiyun 	 * in multiples of 100, but not always
862*4882a593Smuzhiyun 	 */
863*4882a593Smuzhiyun 	if ((TESTLOOPCOUNT/10 * 9) < count_mod)
864*4882a593Smuzhiyun 		return JENT_ECOARSETIME;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	/*
867*4882a593Smuzhiyun 	 * If we have more than 90% stuck results, then this Jitter RNG is
868*4882a593Smuzhiyun 	 * likely to not work well.
869*4882a593Smuzhiyun 	 */
870*4882a593Smuzhiyun 	if ((TESTLOOPCOUNT/10 * 9) < count_stuck)
871*4882a593Smuzhiyun 		return JENT_ESTUCK;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	return 0;
874*4882a593Smuzhiyun }
875