162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Non-physical true random number generator based on timing jitter --
362306a36Sopenharmony_ci * Jitter RNG standalone code.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2023
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Design
862306a36Sopenharmony_ci * ======
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * See https://www.chronox.de/jent.html
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * License
1362306a36Sopenharmony_ci * =======
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
1662306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
1762306a36Sopenharmony_ci * are met:
1862306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1962306a36Sopenharmony_ci *    notice, and the entire permission notice in its entirety,
2062306a36Sopenharmony_ci *    including the disclaimer of warranties.
2162306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
2262306a36Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
2362306a36Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
2462306a36Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote
2562306a36Sopenharmony_ci *    products derived from this software without specific prior
2662306a36Sopenharmony_ci *    written permission.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * ALTERNATIVELY, this product may be distributed under the terms of
2962306a36Sopenharmony_ci * the GNU General Public License, in which case the provisions of the GPL2 are
3062306a36Sopenharmony_ci * required INSTEAD OF the above restrictions.  (This clause is
3162306a36Sopenharmony_ci * necessary due to a potential bad interaction between the GPL and
3262306a36Sopenharmony_ci * the restrictions contained in a BSD-style copyright.)
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
3562306a36Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3662306a36Sopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
3762306a36Sopenharmony_ci * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
3862306a36Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3962306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
4062306a36Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
4162306a36Sopenharmony_ci * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
4262306a36Sopenharmony_ci * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4362306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
4462306a36Sopenharmony_ci * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
4562306a36Sopenharmony_ci * DAMAGE.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/*
4962306a36Sopenharmony_ci * This Jitterentropy RNG is based on the jitterentropy library
5062306a36Sopenharmony_ci * version 3.4.0 provided at https://www.chronox.de/jent.html
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#ifdef __OPTIMIZE__
5462306a36Sopenharmony_ci #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
5562306a36Sopenharmony_ci#endif
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_citypedef	unsigned long long	__u64;
5862306a36Sopenharmony_citypedef	long long		__s64;
5962306a36Sopenharmony_citypedef	unsigned int		__u32;
6062306a36Sopenharmony_citypedef unsigned char		u8;
6162306a36Sopenharmony_ci#define NULL    ((void *) 0)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* The entropy pool */
6462306a36Sopenharmony_cistruct rand_data {
6562306a36Sopenharmony_ci	/* SHA3-256 is used as conditioner */
6662306a36Sopenharmony_ci#define DATA_SIZE_BITS 256
6762306a36Sopenharmony_ci	/* all data values that are vital to maintain the security
6862306a36Sopenharmony_ci	 * of the RNG are marked as SENSITIVE. A user must not
6962306a36Sopenharmony_ci	 * access that information while the RNG executes its loops to
7062306a36Sopenharmony_ci	 * calculate the next random value. */
7162306a36Sopenharmony_ci	void *hash_state;		/* SENSITIVE hash state entropy pool */
7262306a36Sopenharmony_ci	__u64 prev_time;		/* SENSITIVE Previous time stamp */
7362306a36Sopenharmony_ci	__u64 last_delta;		/* SENSITIVE stuck test */
7462306a36Sopenharmony_ci	__s64 last_delta2;		/* SENSITIVE stuck test */
7562306a36Sopenharmony_ci	unsigned int osr;		/* Oversample rate */
7662306a36Sopenharmony_ci#define JENT_MEMORY_BLOCKS 64
7762306a36Sopenharmony_ci#define JENT_MEMORY_BLOCKSIZE 32
7862306a36Sopenharmony_ci#define JENT_MEMORY_ACCESSLOOPS 128
7962306a36Sopenharmony_ci#define JENT_MEMORY_SIZE (JENT_MEMORY_BLOCKS*JENT_MEMORY_BLOCKSIZE)
8062306a36Sopenharmony_ci	unsigned char *mem;	/* Memory access location with size of
8162306a36Sopenharmony_ci				 * memblocks * memblocksize */
8262306a36Sopenharmony_ci	unsigned int memlocation; /* Pointer to byte in *mem */
8362306a36Sopenharmony_ci	unsigned int memblocks;	/* Number of memory blocks in *mem */
8462306a36Sopenharmony_ci	unsigned int memblocksize; /* Size of one memory block in bytes */
8562306a36Sopenharmony_ci	unsigned int memaccessloops; /* Number of memory accesses per random
8662306a36Sopenharmony_ci				      * bit generation */
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/* Repetition Count Test */
8962306a36Sopenharmony_ci	unsigned int rct_count;			/* Number of stuck values */
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/* Intermittent health test failure threshold of 2^-30 */
9262306a36Sopenharmony_ci	/* From an SP800-90B perspective, this RCT cutoff value is equal to 31. */
9362306a36Sopenharmony_ci	/* However, our RCT implementation starts at 1, so we subtract 1 here. */
9462306a36Sopenharmony_ci#define JENT_RCT_CUTOFF		(31 - 1)	/* Taken from SP800-90B sec 4.4.1 */
9562306a36Sopenharmony_ci#define JENT_APT_CUTOFF		325			/* Taken from SP800-90B sec 4.4.2 */
9662306a36Sopenharmony_ci	/* Permanent health test failure threshold of 2^-60 */
9762306a36Sopenharmony_ci	/* From an SP800-90B perspective, this RCT cutoff value is equal to 61. */
9862306a36Sopenharmony_ci	/* However, our RCT implementation starts at 1, so we subtract 1 here. */
9962306a36Sopenharmony_ci#define JENT_RCT_CUTOFF_PERMANENT	(61 - 1)
10062306a36Sopenharmony_ci#define JENT_APT_CUTOFF_PERMANENT	355
10162306a36Sopenharmony_ci#define JENT_APT_WINDOW_SIZE	512	/* Data window size */
10262306a36Sopenharmony_ci	/* LSB of time stamp to process */
10362306a36Sopenharmony_ci#define JENT_APT_LSB		16
10462306a36Sopenharmony_ci#define JENT_APT_WORD_MASK	(JENT_APT_LSB - 1)
10562306a36Sopenharmony_ci	unsigned int apt_observations;	/* Number of collected observations */
10662306a36Sopenharmony_ci	unsigned int apt_count;		/* APT counter */
10762306a36Sopenharmony_ci	unsigned int apt_base;		/* APT base reference */
10862306a36Sopenharmony_ci	unsigned int apt_base_set:1;	/* APT base reference set? */
10962306a36Sopenharmony_ci};
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/* Flags that can be used to initialize the RNG */
11262306a36Sopenharmony_ci#define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more
11362306a36Sopenharmony_ci					   * entropy, saves MEMORY_SIZE RAM for
11462306a36Sopenharmony_ci					   * entropy collector */
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/* -- error codes for init function -- */
11762306a36Sopenharmony_ci#define JENT_ENOTIME		1 /* Timer service not available */
11862306a36Sopenharmony_ci#define JENT_ECOARSETIME	2 /* Timer too coarse for RNG */
11962306a36Sopenharmony_ci#define JENT_ENOMONOTONIC	3 /* Timer is not monotonic increasing */
12062306a36Sopenharmony_ci#define JENT_EVARVAR		5 /* Timer does not produce variations of
12162306a36Sopenharmony_ci				   * variations (2nd derivation of time is
12262306a36Sopenharmony_ci				   * zero). */
12362306a36Sopenharmony_ci#define JENT_ESTUCK		8 /* Too many stuck results during init. */
12462306a36Sopenharmony_ci#define JENT_EHEALTH		9 /* Health test failed during initialization */
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/*
12762306a36Sopenharmony_ci * The output n bits can receive more than n bits of min entropy, of course,
12862306a36Sopenharmony_ci * but the fixed output of the conditioning function can only asymptotically
12962306a36Sopenharmony_ci * approach the output size bits of min entropy, not attain that bound. Random
13062306a36Sopenharmony_ci * maps will tend to have output collisions, which reduces the creditable
13162306a36Sopenharmony_ci * output entropy (that is what SP 800-90B Section 3.1.5.1.2 attempts to bound).
13262306a36Sopenharmony_ci *
13362306a36Sopenharmony_ci * The value "64" is justified in Appendix A.4 of the current 90C draft,
13462306a36Sopenharmony_ci * and aligns with NIST's in "epsilon" definition in this document, which is
13562306a36Sopenharmony_ci * that a string can be considered "full entropy" if you can bound the min
13662306a36Sopenharmony_ci * entropy in each bit of output to at least 1-epsilon, where epsilon is
13762306a36Sopenharmony_ci * required to be <= 2^(-32).
13862306a36Sopenharmony_ci */
13962306a36Sopenharmony_ci#define JENT_ENTROPY_SAFETY_FACTOR	64
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#include <linux/fips.h>
14262306a36Sopenharmony_ci#include "jitterentropy.h"
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/***************************************************************************
14562306a36Sopenharmony_ci * Adaptive Proportion Test
14662306a36Sopenharmony_ci *
14762306a36Sopenharmony_ci * This test complies with SP800-90B section 4.4.2.
14862306a36Sopenharmony_ci ***************************************************************************/
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci/*
15162306a36Sopenharmony_ci * Reset the APT counter
15262306a36Sopenharmony_ci *
15362306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_cistatic void jent_apt_reset(struct rand_data *ec, unsigned int delta_masked)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	/* Reset APT counter */
15862306a36Sopenharmony_ci	ec->apt_count = 0;
15962306a36Sopenharmony_ci	ec->apt_base = delta_masked;
16062306a36Sopenharmony_ci	ec->apt_observations = 0;
16162306a36Sopenharmony_ci}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci/*
16462306a36Sopenharmony_ci * Insert a new entropy event into APT
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
16762306a36Sopenharmony_ci * @delta_masked [in] Masked time delta to process
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_cistatic void jent_apt_insert(struct rand_data *ec, unsigned int delta_masked)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	/* Initialize the base reference */
17262306a36Sopenharmony_ci	if (!ec->apt_base_set) {
17362306a36Sopenharmony_ci		ec->apt_base = delta_masked;
17462306a36Sopenharmony_ci		ec->apt_base_set = 1;
17562306a36Sopenharmony_ci		return;
17662306a36Sopenharmony_ci	}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	if (delta_masked == ec->apt_base)
17962306a36Sopenharmony_ci		ec->apt_count++;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	ec->apt_observations++;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	if (ec->apt_observations >= JENT_APT_WINDOW_SIZE)
18462306a36Sopenharmony_ci		jent_apt_reset(ec, delta_masked);
18562306a36Sopenharmony_ci}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/* APT health test failure detection */
18862306a36Sopenharmony_cistatic int jent_apt_permanent_failure(struct rand_data *ec)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	return (ec->apt_count >= JENT_APT_CUTOFF_PERMANENT) ? 1 : 0;
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic int jent_apt_failure(struct rand_data *ec)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	return (ec->apt_count >= JENT_APT_CUTOFF) ? 1 : 0;
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci/***************************************************************************
19962306a36Sopenharmony_ci * Stuck Test and its use as Repetition Count Test
20062306a36Sopenharmony_ci *
20162306a36Sopenharmony_ci * The Jitter RNG uses an enhanced version of the Repetition Count Test
20262306a36Sopenharmony_ci * (RCT) specified in SP800-90B section 4.4.1. Instead of counting identical
20362306a36Sopenharmony_ci * back-to-back values, the input to the RCT is the counting of the stuck
20462306a36Sopenharmony_ci * values during the generation of one Jitter RNG output block.
20562306a36Sopenharmony_ci *
20662306a36Sopenharmony_ci * The RCT is applied with an alpha of 2^{-30} compliant to FIPS 140-2 IG 9.8.
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci * During the counting operation, the Jitter RNG always calculates the RCT
20962306a36Sopenharmony_ci * cut-off value of C. If that value exceeds the allowed cut-off value,
21062306a36Sopenharmony_ci * the Jitter RNG output block will be calculated completely but discarded at
21162306a36Sopenharmony_ci * the end. The caller of the Jitter RNG is informed with an error code.
21262306a36Sopenharmony_ci ***************************************************************************/
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci/*
21562306a36Sopenharmony_ci * Repetition Count Test as defined in SP800-90B section 4.4.1
21662306a36Sopenharmony_ci *
21762306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
21862306a36Sopenharmony_ci * @stuck [in] Indicator whether the value is stuck
21962306a36Sopenharmony_ci */
22062306a36Sopenharmony_cistatic void jent_rct_insert(struct rand_data *ec, int stuck)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	if (stuck) {
22362306a36Sopenharmony_ci		ec->rct_count++;
22462306a36Sopenharmony_ci	} else {
22562306a36Sopenharmony_ci		/* Reset RCT */
22662306a36Sopenharmony_ci		ec->rct_count = 0;
22762306a36Sopenharmony_ci	}
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cistatic inline __u64 jent_delta(__u64 prev, __u64 next)
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci#define JENT_UINT64_MAX		(__u64)(~((__u64) 0))
23362306a36Sopenharmony_ci	return (prev < next) ? (next - prev) :
23462306a36Sopenharmony_ci			       (JENT_UINT64_MAX - prev + 1 + next);
23562306a36Sopenharmony_ci}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci/*
23862306a36Sopenharmony_ci * Stuck test by checking the:
23962306a36Sopenharmony_ci * 	1st derivative of the jitter measurement (time delta)
24062306a36Sopenharmony_ci * 	2nd derivative of the jitter measurement (delta of time deltas)
24162306a36Sopenharmony_ci * 	3rd derivative of the jitter measurement (delta of delta of time deltas)
24262306a36Sopenharmony_ci *
24362306a36Sopenharmony_ci * All values must always be non-zero.
24462306a36Sopenharmony_ci *
24562306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
24662306a36Sopenharmony_ci * @current_delta [in] Jitter time delta
24762306a36Sopenharmony_ci *
24862306a36Sopenharmony_ci * @return
24962306a36Sopenharmony_ci * 	0 jitter measurement not stuck (good bit)
25062306a36Sopenharmony_ci * 	1 jitter measurement stuck (reject bit)
25162306a36Sopenharmony_ci */
25262306a36Sopenharmony_cistatic int jent_stuck(struct rand_data *ec, __u64 current_delta)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	__u64 delta2 = jent_delta(ec->last_delta, current_delta);
25562306a36Sopenharmony_ci	__u64 delta3 = jent_delta(ec->last_delta2, delta2);
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	ec->last_delta = current_delta;
25862306a36Sopenharmony_ci	ec->last_delta2 = delta2;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	/*
26162306a36Sopenharmony_ci	 * Insert the result of the comparison of two back-to-back time
26262306a36Sopenharmony_ci	 * deltas.
26362306a36Sopenharmony_ci	 */
26462306a36Sopenharmony_ci	jent_apt_insert(ec, current_delta);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	if (!current_delta || !delta2 || !delta3) {
26762306a36Sopenharmony_ci		/* RCT with a stuck bit */
26862306a36Sopenharmony_ci		jent_rct_insert(ec, 1);
26962306a36Sopenharmony_ci		return 1;
27062306a36Sopenharmony_ci	}
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	/* RCT with a non-stuck bit */
27362306a36Sopenharmony_ci	jent_rct_insert(ec, 0);
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	return 0;
27662306a36Sopenharmony_ci}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/* RCT health test failure detection */
27962306a36Sopenharmony_cistatic int jent_rct_permanent_failure(struct rand_data *ec)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	return (ec->rct_count >= JENT_RCT_CUTOFF_PERMANENT) ? 1 : 0;
28262306a36Sopenharmony_ci}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_cistatic int jent_rct_failure(struct rand_data *ec)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	return (ec->rct_count >= JENT_RCT_CUTOFF) ? 1 : 0;
28762306a36Sopenharmony_ci}
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci/* Report of health test failures */
29062306a36Sopenharmony_cistatic int jent_health_failure(struct rand_data *ec)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	return jent_rct_failure(ec) | jent_apt_failure(ec);
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cistatic int jent_permanent_health_failure(struct rand_data *ec)
29662306a36Sopenharmony_ci{
29762306a36Sopenharmony_ci	return jent_rct_permanent_failure(ec) | jent_apt_permanent_failure(ec);
29862306a36Sopenharmony_ci}
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci/***************************************************************************
30162306a36Sopenharmony_ci * Noise sources
30262306a36Sopenharmony_ci ***************************************************************************/
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/*
30562306a36Sopenharmony_ci * Update of the loop count used for the next round of
30662306a36Sopenharmony_ci * an entropy collection.
30762306a36Sopenharmony_ci *
30862306a36Sopenharmony_ci * Input:
30962306a36Sopenharmony_ci * @bits is the number of low bits of the timer to consider
31062306a36Sopenharmony_ci * @min is the number of bits we shift the timer value to the right at
31162306a36Sopenharmony_ci *	the end to make sure we have a guaranteed minimum value
31262306a36Sopenharmony_ci *
31362306a36Sopenharmony_ci * @return Newly calculated loop counter
31462306a36Sopenharmony_ci */
31562306a36Sopenharmony_cistatic __u64 jent_loop_shuffle(unsigned int bits, unsigned int min)
31662306a36Sopenharmony_ci{
31762306a36Sopenharmony_ci	__u64 time = 0;
31862306a36Sopenharmony_ci	__u64 shuffle = 0;
31962306a36Sopenharmony_ci	unsigned int i = 0;
32062306a36Sopenharmony_ci	unsigned int mask = (1<<bits) - 1;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	jent_get_nstime(&time);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	/*
32562306a36Sopenharmony_ci	 * We fold the time value as much as possible to ensure that as many
32662306a36Sopenharmony_ci	 * bits of the time stamp are included as possible.
32762306a36Sopenharmony_ci	 */
32862306a36Sopenharmony_ci	for (i = 0; ((DATA_SIZE_BITS + bits - 1) / bits) > i; i++) {
32962306a36Sopenharmony_ci		shuffle ^= time & mask;
33062306a36Sopenharmony_ci		time = time >> bits;
33162306a36Sopenharmony_ci	}
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	/*
33462306a36Sopenharmony_ci	 * We add a lower boundary value to ensure we have a minimum
33562306a36Sopenharmony_ci	 * RNG loop count.
33662306a36Sopenharmony_ci	 */
33762306a36Sopenharmony_ci	return (shuffle + (1<<min));
33862306a36Sopenharmony_ci}
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci/*
34162306a36Sopenharmony_ci * CPU Jitter noise source -- this is the noise source based on the CPU
34262306a36Sopenharmony_ci *			      execution time jitter
34362306a36Sopenharmony_ci *
34462306a36Sopenharmony_ci * This function injects the individual bits of the time value into the
34562306a36Sopenharmony_ci * entropy pool using a hash.
34662306a36Sopenharmony_ci *
34762306a36Sopenharmony_ci * ec [in] entropy collector
34862306a36Sopenharmony_ci * time [in] time stamp to be injected
34962306a36Sopenharmony_ci * stuck [in] Is the time stamp identified as stuck?
35062306a36Sopenharmony_ci *
35162306a36Sopenharmony_ci * Output:
35262306a36Sopenharmony_ci * updated hash context in the entropy collector or error code
35362306a36Sopenharmony_ci */
35462306a36Sopenharmony_cistatic int jent_condition_data(struct rand_data *ec, __u64 time, int stuck)
35562306a36Sopenharmony_ci{
35662306a36Sopenharmony_ci#define SHA3_HASH_LOOP (1<<3)
35762306a36Sopenharmony_ci	struct {
35862306a36Sopenharmony_ci		int rct_count;
35962306a36Sopenharmony_ci		unsigned int apt_observations;
36062306a36Sopenharmony_ci		unsigned int apt_count;
36162306a36Sopenharmony_ci		unsigned int apt_base;
36262306a36Sopenharmony_ci	} addtl = {
36362306a36Sopenharmony_ci		ec->rct_count,
36462306a36Sopenharmony_ci		ec->apt_observations,
36562306a36Sopenharmony_ci		ec->apt_count,
36662306a36Sopenharmony_ci		ec->apt_base
36762306a36Sopenharmony_ci	};
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	return jent_hash_time(ec->hash_state, time, (u8 *)&addtl, sizeof(addtl),
37062306a36Sopenharmony_ci			      SHA3_HASH_LOOP, stuck);
37162306a36Sopenharmony_ci}
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci/*
37462306a36Sopenharmony_ci * Memory Access noise source -- this is a noise source based on variations in
37562306a36Sopenharmony_ci *				 memory access times
37662306a36Sopenharmony_ci *
37762306a36Sopenharmony_ci * This function performs memory accesses which will add to the timing
37862306a36Sopenharmony_ci * variations due to an unknown amount of CPU wait states that need to be
37962306a36Sopenharmony_ci * added when accessing memory. The memory size should be larger than the L1
38062306a36Sopenharmony_ci * caches as outlined in the documentation and the associated testing.
38162306a36Sopenharmony_ci *
38262306a36Sopenharmony_ci * The L1 cache has a very high bandwidth, albeit its access rate is  usually
38362306a36Sopenharmony_ci * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
38462306a36Sopenharmony_ci * variations as the CPU has hardly to wait. Starting with L2, significant
38562306a36Sopenharmony_ci * variations are added because L2 typically does not belong to the CPU any more
38662306a36Sopenharmony_ci * and therefore a wider range of CPU wait states is necessary for accesses.
38762306a36Sopenharmony_ci * L3 and real memory accesses have even a wider range of wait states. However,
38862306a36Sopenharmony_ci * to reliably access either L3 or memory, the ec->mem memory must be quite
38962306a36Sopenharmony_ci * large which is usually not desirable.
39062306a36Sopenharmony_ci *
39162306a36Sopenharmony_ci * @ec [in] Reference to the entropy collector with the memory access data -- if
39262306a36Sopenharmony_ci *	    the reference to the memory block to be accessed is NULL, this noise
39362306a36Sopenharmony_ci *	    source is disabled
39462306a36Sopenharmony_ci * @loop_cnt [in] if a value not equal to 0 is set, use the given value
39562306a36Sopenharmony_ci *		  number of loops to perform the LFSR
39662306a36Sopenharmony_ci */
39762306a36Sopenharmony_cistatic void jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
39862306a36Sopenharmony_ci{
39962306a36Sopenharmony_ci	unsigned int wrap = 0;
40062306a36Sopenharmony_ci	__u64 i = 0;
40162306a36Sopenharmony_ci#define MAX_ACC_LOOP_BIT 7
40262306a36Sopenharmony_ci#define MIN_ACC_LOOP_BIT 0
40362306a36Sopenharmony_ci	__u64 acc_loop_cnt =
40462306a36Sopenharmony_ci		jent_loop_shuffle(MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	if (NULL == ec || NULL == ec->mem)
40762306a36Sopenharmony_ci		return;
40862306a36Sopenharmony_ci	wrap = ec->memblocksize * ec->memblocks;
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	/*
41162306a36Sopenharmony_ci	 * testing purposes -- allow test app to set the counter, not
41262306a36Sopenharmony_ci	 * needed during runtime
41362306a36Sopenharmony_ci	 */
41462306a36Sopenharmony_ci	if (loop_cnt)
41562306a36Sopenharmony_ci		acc_loop_cnt = loop_cnt;
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
41862306a36Sopenharmony_ci		unsigned char *tmpval = ec->mem + ec->memlocation;
41962306a36Sopenharmony_ci		/*
42062306a36Sopenharmony_ci		 * memory access: just add 1 to one byte,
42162306a36Sopenharmony_ci		 * wrap at 255 -- memory access implies read
42262306a36Sopenharmony_ci		 * from and write to memory location
42362306a36Sopenharmony_ci		 */
42462306a36Sopenharmony_ci		*tmpval = (*tmpval + 1) & 0xff;
42562306a36Sopenharmony_ci		/*
42662306a36Sopenharmony_ci		 * Addition of memblocksize - 1 to pointer
42762306a36Sopenharmony_ci		 * with wrap around logic to ensure that every
42862306a36Sopenharmony_ci		 * memory location is hit evenly
42962306a36Sopenharmony_ci		 */
43062306a36Sopenharmony_ci		ec->memlocation = ec->memlocation + ec->memblocksize - 1;
43162306a36Sopenharmony_ci		ec->memlocation = ec->memlocation % wrap;
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci}
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci/***************************************************************************
43662306a36Sopenharmony_ci * Start of entropy processing logic
43762306a36Sopenharmony_ci ***************************************************************************/
43862306a36Sopenharmony_ci/*
43962306a36Sopenharmony_ci * This is the heart of the entropy generation: calculate time deltas and
44062306a36Sopenharmony_ci * use the CPU jitter in the time deltas. The jitter is injected into the
44162306a36Sopenharmony_ci * entropy pool.
44262306a36Sopenharmony_ci *
44362306a36Sopenharmony_ci * WARNING: ensure that ->prev_time is primed before using the output
44462306a36Sopenharmony_ci *	    of this function! This can be done by calling this function
44562306a36Sopenharmony_ci *	    and not using its result.
44662306a36Sopenharmony_ci *
44762306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
44862306a36Sopenharmony_ci *
44962306a36Sopenharmony_ci * @return result of stuck test
45062306a36Sopenharmony_ci */
45162306a36Sopenharmony_cistatic int jent_measure_jitter(struct rand_data *ec)
45262306a36Sopenharmony_ci{
45362306a36Sopenharmony_ci	__u64 time = 0;
45462306a36Sopenharmony_ci	__u64 current_delta = 0;
45562306a36Sopenharmony_ci	int stuck;
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	/* Invoke one noise source before time measurement to add variations */
45862306a36Sopenharmony_ci	jent_memaccess(ec, 0);
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	/*
46162306a36Sopenharmony_ci	 * Get time stamp and calculate time delta to previous
46262306a36Sopenharmony_ci	 * invocation to measure the timing variations
46362306a36Sopenharmony_ci	 */
46462306a36Sopenharmony_ci	jent_get_nstime(&time);
46562306a36Sopenharmony_ci	current_delta = jent_delta(ec->prev_time, time);
46662306a36Sopenharmony_ci	ec->prev_time = time;
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	/* Check whether we have a stuck measurement. */
46962306a36Sopenharmony_ci	stuck = jent_stuck(ec, current_delta);
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	/* Now call the next noise sources which also injects the data */
47262306a36Sopenharmony_ci	if (jent_condition_data(ec, current_delta, stuck))
47362306a36Sopenharmony_ci		stuck = 1;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	return stuck;
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci/*
47962306a36Sopenharmony_ci * Generator of one 64 bit random number
48062306a36Sopenharmony_ci * Function fills rand_data->hash_state
48162306a36Sopenharmony_ci *
48262306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
48362306a36Sopenharmony_ci */
48462306a36Sopenharmony_cistatic void jent_gen_entropy(struct rand_data *ec)
48562306a36Sopenharmony_ci{
48662306a36Sopenharmony_ci	unsigned int k = 0, safety_factor = 0;
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	if (fips_enabled)
48962306a36Sopenharmony_ci		safety_factor = JENT_ENTROPY_SAFETY_FACTOR;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	/* priming of the ->prev_time value */
49262306a36Sopenharmony_ci	jent_measure_jitter(ec);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	while (!jent_health_failure(ec)) {
49562306a36Sopenharmony_ci		/* If a stuck measurement is received, repeat measurement */
49662306a36Sopenharmony_ci		if (jent_measure_jitter(ec))
49762306a36Sopenharmony_ci			continue;
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci		/*
50062306a36Sopenharmony_ci		 * We multiply the loop value with ->osr to obtain the
50162306a36Sopenharmony_ci		 * oversampling rate requested by the caller
50262306a36Sopenharmony_ci		 */
50362306a36Sopenharmony_ci		if (++k >= ((DATA_SIZE_BITS + safety_factor) * ec->osr))
50462306a36Sopenharmony_ci			break;
50562306a36Sopenharmony_ci	}
50662306a36Sopenharmony_ci}
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci/*
50962306a36Sopenharmony_ci * Entry function: Obtain entropy for the caller.
51062306a36Sopenharmony_ci *
51162306a36Sopenharmony_ci * This function invokes the entropy gathering logic as often to generate
51262306a36Sopenharmony_ci * as many bytes as requested by the caller. The entropy gathering logic
51362306a36Sopenharmony_ci * creates 64 bit per invocation.
51462306a36Sopenharmony_ci *
51562306a36Sopenharmony_ci * This function truncates the last 64 bit entropy value output to the exact
51662306a36Sopenharmony_ci * size specified by the caller.
51762306a36Sopenharmony_ci *
51862306a36Sopenharmony_ci * @ec [in] Reference to entropy collector
51962306a36Sopenharmony_ci * @data [in] pointer to buffer for storing random data -- buffer must already
52062306a36Sopenharmony_ci *	      exist
52162306a36Sopenharmony_ci * @len [in] size of the buffer, specifying also the requested number of random
52262306a36Sopenharmony_ci *	     in bytes
52362306a36Sopenharmony_ci *
52462306a36Sopenharmony_ci * @return 0 when request is fulfilled or an error
52562306a36Sopenharmony_ci *
52662306a36Sopenharmony_ci * The following error codes can occur:
52762306a36Sopenharmony_ci *	-1	entropy_collector is NULL or the generation failed
52862306a36Sopenharmony_ci *	-2	Intermittent health failure
52962306a36Sopenharmony_ci *	-3	Permanent health failure
53062306a36Sopenharmony_ci */
53162306a36Sopenharmony_ciint jent_read_entropy(struct rand_data *ec, unsigned char *data,
53262306a36Sopenharmony_ci		      unsigned int len)
53362306a36Sopenharmony_ci{
53462306a36Sopenharmony_ci	unsigned char *p = data;
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci	if (!ec)
53762306a36Sopenharmony_ci		return -1;
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci	while (len > 0) {
54062306a36Sopenharmony_ci		unsigned int tocopy;
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci		jent_gen_entropy(ec);
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci		if (jent_permanent_health_failure(ec)) {
54562306a36Sopenharmony_ci			/*
54662306a36Sopenharmony_ci			 * At this point, the Jitter RNG instance is considered
54762306a36Sopenharmony_ci			 * as a failed instance. There is no rerun of the
54862306a36Sopenharmony_ci			 * startup test any more, because the caller
54962306a36Sopenharmony_ci			 * is assumed to not further use this instance.
55062306a36Sopenharmony_ci			 */
55162306a36Sopenharmony_ci			return -3;
55262306a36Sopenharmony_ci		} else if (jent_health_failure(ec)) {
55362306a36Sopenharmony_ci			/*
55462306a36Sopenharmony_ci			 * Perform startup health tests and return permanent
55562306a36Sopenharmony_ci			 * error if it fails.
55662306a36Sopenharmony_ci			 */
55762306a36Sopenharmony_ci			if (jent_entropy_init(ec->hash_state))
55862306a36Sopenharmony_ci				return -3;
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci			return -2;
56162306a36Sopenharmony_ci		}
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci		if ((DATA_SIZE_BITS / 8) < len)
56462306a36Sopenharmony_ci			tocopy = (DATA_SIZE_BITS / 8);
56562306a36Sopenharmony_ci		else
56662306a36Sopenharmony_ci			tocopy = len;
56762306a36Sopenharmony_ci		if (jent_read_random_block(ec->hash_state, p, tocopy))
56862306a36Sopenharmony_ci			return -1;
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci		len -= tocopy;
57162306a36Sopenharmony_ci		p += tocopy;
57262306a36Sopenharmony_ci	}
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci	return 0;
57562306a36Sopenharmony_ci}
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci/***************************************************************************
57862306a36Sopenharmony_ci * Initialization logic
57962306a36Sopenharmony_ci ***************************************************************************/
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_cistruct rand_data *jent_entropy_collector_alloc(unsigned int osr,
58262306a36Sopenharmony_ci					       unsigned int flags,
58362306a36Sopenharmony_ci					       void *hash_state)
58462306a36Sopenharmony_ci{
58562306a36Sopenharmony_ci	struct rand_data *entropy_collector;
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci	entropy_collector = jent_zalloc(sizeof(struct rand_data));
58862306a36Sopenharmony_ci	if (!entropy_collector)
58962306a36Sopenharmony_ci		return NULL;
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
59262306a36Sopenharmony_ci		/* Allocate memory for adding variations based on memory
59362306a36Sopenharmony_ci		 * access
59462306a36Sopenharmony_ci		 */
59562306a36Sopenharmony_ci		entropy_collector->mem = jent_zalloc(JENT_MEMORY_SIZE);
59662306a36Sopenharmony_ci		if (!entropy_collector->mem) {
59762306a36Sopenharmony_ci			jent_zfree(entropy_collector);
59862306a36Sopenharmony_ci			return NULL;
59962306a36Sopenharmony_ci		}
60062306a36Sopenharmony_ci		entropy_collector->memblocksize = JENT_MEMORY_BLOCKSIZE;
60162306a36Sopenharmony_ci		entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
60262306a36Sopenharmony_ci		entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
60362306a36Sopenharmony_ci	}
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	/* verify and set the oversampling rate */
60662306a36Sopenharmony_ci	if (osr == 0)
60762306a36Sopenharmony_ci		osr = 1; /* minimum sampling rate is 1 */
60862306a36Sopenharmony_ci	entropy_collector->osr = osr;
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	entropy_collector->hash_state = hash_state;
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci	/* fill the data pad with non-zero values */
61362306a36Sopenharmony_ci	jent_gen_entropy(entropy_collector);
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci	return entropy_collector;
61662306a36Sopenharmony_ci}
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_civoid jent_entropy_collector_free(struct rand_data *entropy_collector)
61962306a36Sopenharmony_ci{
62062306a36Sopenharmony_ci	jent_zfree(entropy_collector->mem);
62162306a36Sopenharmony_ci	entropy_collector->mem = NULL;
62262306a36Sopenharmony_ci	jent_zfree(entropy_collector);
62362306a36Sopenharmony_ci}
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_ciint jent_entropy_init(void *hash_state)
62662306a36Sopenharmony_ci{
62762306a36Sopenharmony_ci	int i;
62862306a36Sopenharmony_ci	__u64 delta_sum = 0;
62962306a36Sopenharmony_ci	__u64 old_delta = 0;
63062306a36Sopenharmony_ci	unsigned int nonstuck = 0;
63162306a36Sopenharmony_ci	int time_backwards = 0;
63262306a36Sopenharmony_ci	int count_mod = 0;
63362306a36Sopenharmony_ci	int count_stuck = 0;
63462306a36Sopenharmony_ci	struct rand_data ec = { 0 };
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci	/* Required for RCT */
63762306a36Sopenharmony_ci	ec.osr = 1;
63862306a36Sopenharmony_ci	ec.hash_state = hash_state;
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	/* We could perform statistical tests here, but the problem is
64162306a36Sopenharmony_ci	 * that we only have a few loop counts to do testing. These
64262306a36Sopenharmony_ci	 * loop counts may show some slight skew and we produce
64362306a36Sopenharmony_ci	 * false positives.
64462306a36Sopenharmony_ci	 *
64562306a36Sopenharmony_ci	 * Moreover, only old systems show potentially problematic
64662306a36Sopenharmony_ci	 * jitter entropy that could potentially be caught here. But
64762306a36Sopenharmony_ci	 * the RNG is intended for hardware that is available or widely
64862306a36Sopenharmony_ci	 * used, but not old systems that are long out of favor. Thus,
64962306a36Sopenharmony_ci	 * no statistical tests.
65062306a36Sopenharmony_ci	 */
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci	/*
65362306a36Sopenharmony_ci	 * We could add a check for system capabilities such as clock_getres or
65462306a36Sopenharmony_ci	 * check for CONFIG_X86_TSC, but it does not make much sense as the
65562306a36Sopenharmony_ci	 * following sanity checks verify that we have a high-resolution
65662306a36Sopenharmony_ci	 * timer.
65762306a36Sopenharmony_ci	 */
65862306a36Sopenharmony_ci	/*
65962306a36Sopenharmony_ci	 * TESTLOOPCOUNT needs some loops to identify edge systems. 100 is
66062306a36Sopenharmony_ci	 * definitely too little.
66162306a36Sopenharmony_ci	 *
66262306a36Sopenharmony_ci	 * SP800-90B requires at least 1024 initial test cycles.
66362306a36Sopenharmony_ci	 */
66462306a36Sopenharmony_ci#define TESTLOOPCOUNT 1024
66562306a36Sopenharmony_ci#define CLEARCACHE 100
66662306a36Sopenharmony_ci	for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
66762306a36Sopenharmony_ci		__u64 time = 0;
66862306a36Sopenharmony_ci		__u64 time2 = 0;
66962306a36Sopenharmony_ci		__u64 delta = 0;
67062306a36Sopenharmony_ci		unsigned int lowdelta = 0;
67162306a36Sopenharmony_ci		int stuck;
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci		/* Invoke core entropy collection logic */
67462306a36Sopenharmony_ci		jent_get_nstime(&time);
67562306a36Sopenharmony_ci		ec.prev_time = time;
67662306a36Sopenharmony_ci		jent_condition_data(&ec, time, 0);
67762306a36Sopenharmony_ci		jent_get_nstime(&time2);
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci		/* test whether timer works */
68062306a36Sopenharmony_ci		if (!time || !time2)
68162306a36Sopenharmony_ci			return JENT_ENOTIME;
68262306a36Sopenharmony_ci		delta = jent_delta(time, time2);
68362306a36Sopenharmony_ci		/*
68462306a36Sopenharmony_ci		 * test whether timer is fine grained enough to provide
68562306a36Sopenharmony_ci		 * delta even when called shortly after each other -- this
68662306a36Sopenharmony_ci		 * implies that we also have a high resolution timer
68762306a36Sopenharmony_ci		 */
68862306a36Sopenharmony_ci		if (!delta)
68962306a36Sopenharmony_ci			return JENT_ECOARSETIME;
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci		stuck = jent_stuck(&ec, delta);
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci		/*
69462306a36Sopenharmony_ci		 * up to here we did not modify any variable that will be
69562306a36Sopenharmony_ci		 * evaluated later, but we already performed some work. Thus we
69662306a36Sopenharmony_ci		 * already have had an impact on the caches, branch prediction,
69762306a36Sopenharmony_ci		 * etc. with the goal to clear it to get the worst case
69862306a36Sopenharmony_ci		 * measurements.
69962306a36Sopenharmony_ci		 */
70062306a36Sopenharmony_ci		if (i < CLEARCACHE)
70162306a36Sopenharmony_ci			continue;
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci		if (stuck)
70462306a36Sopenharmony_ci			count_stuck++;
70562306a36Sopenharmony_ci		else {
70662306a36Sopenharmony_ci			nonstuck++;
70762306a36Sopenharmony_ci
70862306a36Sopenharmony_ci			/*
70962306a36Sopenharmony_ci			 * Ensure that the APT succeeded.
71062306a36Sopenharmony_ci			 *
71162306a36Sopenharmony_ci			 * With the check below that count_stuck must be less
71262306a36Sopenharmony_ci			 * than 10% of the overall generated raw entropy values
71362306a36Sopenharmony_ci			 * it is guaranteed that the APT is invoked at
71462306a36Sopenharmony_ci			 * floor((TESTLOOPCOUNT * 0.9) / 64) == 14 times.
71562306a36Sopenharmony_ci			 */
71662306a36Sopenharmony_ci			if ((nonstuck % JENT_APT_WINDOW_SIZE) == 0) {
71762306a36Sopenharmony_ci				jent_apt_reset(&ec,
71862306a36Sopenharmony_ci					       delta & JENT_APT_WORD_MASK);
71962306a36Sopenharmony_ci			}
72062306a36Sopenharmony_ci		}
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci		/* Validate health test result */
72362306a36Sopenharmony_ci		if (jent_health_failure(&ec))
72462306a36Sopenharmony_ci			return JENT_EHEALTH;
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci		/* test whether we have an increasing timer */
72762306a36Sopenharmony_ci		if (!(time2 > time))
72862306a36Sopenharmony_ci			time_backwards++;
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci		/* use 32 bit value to ensure compilation on 32 bit arches */
73162306a36Sopenharmony_ci		lowdelta = time2 - time;
73262306a36Sopenharmony_ci		if (!(lowdelta % 100))
73362306a36Sopenharmony_ci			count_mod++;
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci		/*
73662306a36Sopenharmony_ci		 * ensure that we have a varying delta timer which is necessary
73762306a36Sopenharmony_ci		 * for the calculation of entropy -- perform this check
73862306a36Sopenharmony_ci		 * only after the first loop is executed as we need to prime
73962306a36Sopenharmony_ci		 * the old_data value
74062306a36Sopenharmony_ci		 */
74162306a36Sopenharmony_ci		if (delta > old_delta)
74262306a36Sopenharmony_ci			delta_sum += (delta - old_delta);
74362306a36Sopenharmony_ci		else
74462306a36Sopenharmony_ci			delta_sum += (old_delta - delta);
74562306a36Sopenharmony_ci		old_delta = delta;
74662306a36Sopenharmony_ci	}
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	/*
74962306a36Sopenharmony_ci	 * we allow up to three times the time running backwards.
75062306a36Sopenharmony_ci	 * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
75162306a36Sopenharmony_ci	 * if such an operation just happens to interfere with our test, it
75262306a36Sopenharmony_ci	 * should not fail. The value of 3 should cover the NTP case being
75362306a36Sopenharmony_ci	 * performed during our test run.
75462306a36Sopenharmony_ci	 */
75562306a36Sopenharmony_ci	if (time_backwards > 3)
75662306a36Sopenharmony_ci		return JENT_ENOMONOTONIC;
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci	/*
75962306a36Sopenharmony_ci	 * Variations of deltas of time must on average be larger
76062306a36Sopenharmony_ci	 * than 1 to ensure the entropy estimation
76162306a36Sopenharmony_ci	 * implied with 1 is preserved
76262306a36Sopenharmony_ci	 */
76362306a36Sopenharmony_ci	if ((delta_sum) <= 1)
76462306a36Sopenharmony_ci		return JENT_EVARVAR;
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ci	/*
76762306a36Sopenharmony_ci	 * Ensure that we have variations in the time stamp below 10 for at
76862306a36Sopenharmony_ci	 * least 10% of all checks -- on some platforms, the counter increments
76962306a36Sopenharmony_ci	 * in multiples of 100, but not always
77062306a36Sopenharmony_ci	 */
77162306a36Sopenharmony_ci	if ((TESTLOOPCOUNT/10 * 9) < count_mod)
77262306a36Sopenharmony_ci		return JENT_ECOARSETIME;
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	/*
77562306a36Sopenharmony_ci	 * If we have more than 90% stuck results, then this Jitter RNG is
77662306a36Sopenharmony_ci	 * likely to not work well.
77762306a36Sopenharmony_ci	 */
77862306a36Sopenharmony_ci	if ((TESTLOOPCOUNT/10 * 9) < count_stuck)
77962306a36Sopenharmony_ci		return JENT_ESTUCK;
78062306a36Sopenharmony_ci
78162306a36Sopenharmony_ci	return 0;
78262306a36Sopenharmony_ci}
783