18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Non-physical true random number generator based on timing jitter --
38c2ecf20Sopenharmony_ci * Jitter RNG standalone code.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2020
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Design
88c2ecf20Sopenharmony_ci * ======
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * See https://www.chronox.de/jent.html
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * License
138c2ecf20Sopenharmony_ci * =======
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
168c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
178c2ecf20Sopenharmony_ci * are met:
188c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
198c2ecf20Sopenharmony_ci *    notice, and the entire permission notice in its entirety,
208c2ecf20Sopenharmony_ci *    including the disclaimer of warranties.
218c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
228c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
238c2ecf20Sopenharmony_ci *    documentation and/or other materials provided with the distribution.
248c2ecf20Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote
258c2ecf20Sopenharmony_ci *    products derived from this software without specific prior
268c2ecf20Sopenharmony_ci *    written permission.
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * ALTERNATIVELY, this product may be distributed under the terms of
298c2ecf20Sopenharmony_ci * the GNU General Public License, in which case the provisions of the GPL2 are
308c2ecf20Sopenharmony_ci * required INSTEAD OF the above restrictions.  (This clause is
318c2ecf20Sopenharmony_ci * necessary due to a potential bad interaction between the GPL and
328c2ecf20Sopenharmony_ci * the restrictions contained in a BSD-style copyright.)
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
358c2ecf20Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
368c2ecf20Sopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
378c2ecf20Sopenharmony_ci * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
388c2ecf20Sopenharmony_ci * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
398c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
408c2ecf20Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
418c2ecf20Sopenharmony_ci * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
428c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
448c2ecf20Sopenharmony_ci * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
458c2ecf20Sopenharmony_ci * DAMAGE.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * This Jitterentropy RNG is based on the jitterentropy library
508c2ecf20Sopenharmony_ci * version 2.2.0 provided at https://www.chronox.de/jent.html
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#ifdef __OPTIMIZE__
548c2ecf20Sopenharmony_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."
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_citypedef	unsigned long long	__u64;
588c2ecf20Sopenharmony_citypedef	long long		__s64;
598c2ecf20Sopenharmony_citypedef	unsigned int		__u32;
608c2ecf20Sopenharmony_ci#define NULL    ((void *) 0)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/* The entropy pool */
638c2ecf20Sopenharmony_cistruct rand_data {
648c2ecf20Sopenharmony_ci	/* all data values that are vital to maintain the security
658c2ecf20Sopenharmony_ci	 * of the RNG are marked as SENSITIVE. A user must not
668c2ecf20Sopenharmony_ci	 * access that information while the RNG executes its loops to
678c2ecf20Sopenharmony_ci	 * calculate the next random value. */
688c2ecf20Sopenharmony_ci	__u64 data;		/* SENSITIVE Actual random number */
698c2ecf20Sopenharmony_ci	__u64 old_data;		/* SENSITIVE Previous random number */
708c2ecf20Sopenharmony_ci	__u64 prev_time;	/* SENSITIVE Previous time stamp */
718c2ecf20Sopenharmony_ci#define DATA_SIZE_BITS ((sizeof(__u64)) * 8)
728c2ecf20Sopenharmony_ci	__u64 last_delta;	/* SENSITIVE stuck test */
738c2ecf20Sopenharmony_ci	__s64 last_delta2;	/* SENSITIVE stuck test */
748c2ecf20Sopenharmony_ci	unsigned int osr;	/* Oversample rate */
758c2ecf20Sopenharmony_ci#define JENT_MEMORY_BLOCKS 64
768c2ecf20Sopenharmony_ci#define JENT_MEMORY_BLOCKSIZE 32
778c2ecf20Sopenharmony_ci#define JENT_MEMORY_ACCESSLOOPS 128
788c2ecf20Sopenharmony_ci#define JENT_MEMORY_SIZE (JENT_MEMORY_BLOCKS*JENT_MEMORY_BLOCKSIZE)
798c2ecf20Sopenharmony_ci	unsigned char *mem;	/* Memory access location with size of
808c2ecf20Sopenharmony_ci				 * memblocks * memblocksize */
818c2ecf20Sopenharmony_ci	unsigned int memlocation; /* Pointer to byte in *mem */
828c2ecf20Sopenharmony_ci	unsigned int memblocks;	/* Number of memory blocks in *mem */
838c2ecf20Sopenharmony_ci	unsigned int memblocksize; /* Size of one memory block in bytes */
848c2ecf20Sopenharmony_ci	unsigned int memaccessloops; /* Number of memory accesses per random
858c2ecf20Sopenharmony_ci				      * bit generation */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* Repetition Count Test */
888c2ecf20Sopenharmony_ci	int rct_count;			/* Number of stuck values */
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* Adaptive Proportion Test for a significance level of 2^-30 */
918c2ecf20Sopenharmony_ci#define JENT_APT_CUTOFF		325	/* Taken from SP800-90B sec 4.4.2 */
928c2ecf20Sopenharmony_ci#define JENT_APT_WINDOW_SIZE	512	/* Data window size */
938c2ecf20Sopenharmony_ci	/* LSB of time stamp to process */
948c2ecf20Sopenharmony_ci#define JENT_APT_LSB		16
958c2ecf20Sopenharmony_ci#define JENT_APT_WORD_MASK	(JENT_APT_LSB - 1)
968c2ecf20Sopenharmony_ci	unsigned int apt_observations;	/* Number of collected observations */
978c2ecf20Sopenharmony_ci	unsigned int apt_count;		/* APT counter */
988c2ecf20Sopenharmony_ci	unsigned int apt_base;		/* APT base reference */
998c2ecf20Sopenharmony_ci	unsigned int apt_base_set:1;	/* APT base reference set? */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	unsigned int health_failure:1;	/* Permanent health failure */
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* Flags that can be used to initialize the RNG */
1058c2ecf20Sopenharmony_ci#define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more
1068c2ecf20Sopenharmony_ci					   * entropy, saves MEMORY_SIZE RAM for
1078c2ecf20Sopenharmony_ci					   * entropy collector */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* -- error codes for init function -- */
1108c2ecf20Sopenharmony_ci#define JENT_ENOTIME		1 /* Timer service not available */
1118c2ecf20Sopenharmony_ci#define JENT_ECOARSETIME	2 /* Timer too coarse for RNG */
1128c2ecf20Sopenharmony_ci#define JENT_ENOMONOTONIC	3 /* Timer is not monotonic increasing */
1138c2ecf20Sopenharmony_ci#define JENT_EVARVAR		5 /* Timer does not produce variations of
1148c2ecf20Sopenharmony_ci				   * variations (2nd derivation of time is
1158c2ecf20Sopenharmony_ci				   * zero). */
1168c2ecf20Sopenharmony_ci#define JENT_ESTUCK		8 /* Too many stuck results during init. */
1178c2ecf20Sopenharmony_ci#define JENT_EHEALTH		9 /* Health test failed during initialization */
1188c2ecf20Sopenharmony_ci#define JENT_ERCT		10 /* RCT failed during initialization */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#include "jitterentropy.h"
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/***************************************************************************
1238c2ecf20Sopenharmony_ci * Adaptive Proportion Test
1248c2ecf20Sopenharmony_ci *
1258c2ecf20Sopenharmony_ci * This test complies with SP800-90B section 4.4.2.
1268c2ecf20Sopenharmony_ci ***************************************************************************/
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/**
1298c2ecf20Sopenharmony_ci * Reset the APT counter
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_cistatic void jent_apt_reset(struct rand_data *ec, unsigned int delta_masked)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	/* Reset APT counter */
1368c2ecf20Sopenharmony_ci	ec->apt_count = 0;
1378c2ecf20Sopenharmony_ci	ec->apt_base = delta_masked;
1388c2ecf20Sopenharmony_ci	ec->apt_observations = 0;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/**
1428c2ecf20Sopenharmony_ci * Insert a new entropy event into APT
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
1458c2ecf20Sopenharmony_ci * @delta_masked [in] Masked time delta to process
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistatic void jent_apt_insert(struct rand_data *ec, unsigned int delta_masked)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	/* Initialize the base reference */
1508c2ecf20Sopenharmony_ci	if (!ec->apt_base_set) {
1518c2ecf20Sopenharmony_ci		ec->apt_base = delta_masked;
1528c2ecf20Sopenharmony_ci		ec->apt_base_set = 1;
1538c2ecf20Sopenharmony_ci		return;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (delta_masked == ec->apt_base) {
1578c2ecf20Sopenharmony_ci		ec->apt_count++;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		if (ec->apt_count >= JENT_APT_CUTOFF)
1608c2ecf20Sopenharmony_ci			ec->health_failure = 1;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ec->apt_observations++;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (ec->apt_observations >= JENT_APT_WINDOW_SIZE)
1668c2ecf20Sopenharmony_ci		jent_apt_reset(ec, delta_masked);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/***************************************************************************
1708c2ecf20Sopenharmony_ci * Stuck Test and its use as Repetition Count Test
1718c2ecf20Sopenharmony_ci *
1728c2ecf20Sopenharmony_ci * The Jitter RNG uses an enhanced version of the Repetition Count Test
1738c2ecf20Sopenharmony_ci * (RCT) specified in SP800-90B section 4.4.1. Instead of counting identical
1748c2ecf20Sopenharmony_ci * back-to-back values, the input to the RCT is the counting of the stuck
1758c2ecf20Sopenharmony_ci * values during the generation of one Jitter RNG output block.
1768c2ecf20Sopenharmony_ci *
1778c2ecf20Sopenharmony_ci * The RCT is applied with an alpha of 2^{-30} compliant to FIPS 140-2 IG 9.8.
1788c2ecf20Sopenharmony_ci *
1798c2ecf20Sopenharmony_ci * During the counting operation, the Jitter RNG always calculates the RCT
1808c2ecf20Sopenharmony_ci * cut-off value of C. If that value exceeds the allowed cut-off value,
1818c2ecf20Sopenharmony_ci * the Jitter RNG output block will be calculated completely but discarded at
1828c2ecf20Sopenharmony_ci * the end. The caller of the Jitter RNG is informed with an error code.
1838c2ecf20Sopenharmony_ci ***************************************************************************/
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/**
1868c2ecf20Sopenharmony_ci * Repetition Count Test as defined in SP800-90B section 4.4.1
1878c2ecf20Sopenharmony_ci *
1888c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
1898c2ecf20Sopenharmony_ci * @stuck [in] Indicator whether the value is stuck
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_cistatic void jent_rct_insert(struct rand_data *ec, int stuck)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	/*
1948c2ecf20Sopenharmony_ci	 * If we have a count less than zero, a previous RCT round identified
1958c2ecf20Sopenharmony_ci	 * a failure. We will not overwrite it.
1968c2ecf20Sopenharmony_ci	 */
1978c2ecf20Sopenharmony_ci	if (ec->rct_count < 0)
1988c2ecf20Sopenharmony_ci		return;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (stuck) {
2018c2ecf20Sopenharmony_ci		ec->rct_count++;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		/*
2048c2ecf20Sopenharmony_ci		 * The cutoff value is based on the following consideration:
2058c2ecf20Sopenharmony_ci		 * alpha = 2^-30 as recommended in FIPS 140-2 IG 9.8.
2068c2ecf20Sopenharmony_ci		 * In addition, we require an entropy value H of 1/OSR as this
2078c2ecf20Sopenharmony_ci		 * is the minimum entropy required to provide full entropy.
2088c2ecf20Sopenharmony_ci		 * Note, we collect 64 * OSR deltas for inserting them into
2098c2ecf20Sopenharmony_ci		 * the entropy pool which should then have (close to) 64 bits
2108c2ecf20Sopenharmony_ci		 * of entropy.
2118c2ecf20Sopenharmony_ci		 *
2128c2ecf20Sopenharmony_ci		 * Note, ec->rct_count (which equals to value B in the pseudo
2138c2ecf20Sopenharmony_ci		 * code of SP800-90B section 4.4.1) starts with zero. Hence
2148c2ecf20Sopenharmony_ci		 * we need to subtract one from the cutoff value as calculated
2158c2ecf20Sopenharmony_ci		 * following SP800-90B.
2168c2ecf20Sopenharmony_ci		 */
2178c2ecf20Sopenharmony_ci		if ((unsigned int)ec->rct_count >= (31 * ec->osr)) {
2188c2ecf20Sopenharmony_ci			ec->rct_count = -1;
2198c2ecf20Sopenharmony_ci			ec->health_failure = 1;
2208c2ecf20Sopenharmony_ci		}
2218c2ecf20Sopenharmony_ci	} else {
2228c2ecf20Sopenharmony_ci		ec->rct_count = 0;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/**
2278c2ecf20Sopenharmony_ci * Is there an RCT health test failure?
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
2308c2ecf20Sopenharmony_ci *
2318c2ecf20Sopenharmony_ci * @return
2328c2ecf20Sopenharmony_ci * 	0 No health test failure
2338c2ecf20Sopenharmony_ci * 	1 Permanent health test failure
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_cistatic int jent_rct_failure(struct rand_data *ec)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	if (ec->rct_count < 0)
2388c2ecf20Sopenharmony_ci		return 1;
2398c2ecf20Sopenharmony_ci	return 0;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic inline __u64 jent_delta(__u64 prev, __u64 next)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci#define JENT_UINT64_MAX		(__u64)(~((__u64) 0))
2458c2ecf20Sopenharmony_ci	return (prev < next) ? (next - prev) :
2468c2ecf20Sopenharmony_ci			       (JENT_UINT64_MAX - prev + 1 + next);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci/**
2508c2ecf20Sopenharmony_ci * Stuck test by checking the:
2518c2ecf20Sopenharmony_ci * 	1st derivative of the jitter measurement (time delta)
2528c2ecf20Sopenharmony_ci * 	2nd derivative of the jitter measurement (delta of time deltas)
2538c2ecf20Sopenharmony_ci * 	3rd derivative of the jitter measurement (delta of delta of time deltas)
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * All values must always be non-zero.
2568c2ecf20Sopenharmony_ci *
2578c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
2588c2ecf20Sopenharmony_ci * @current_delta [in] Jitter time delta
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * @return
2618c2ecf20Sopenharmony_ci * 	0 jitter measurement not stuck (good bit)
2628c2ecf20Sopenharmony_ci * 	1 jitter measurement stuck (reject bit)
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_cistatic int jent_stuck(struct rand_data *ec, __u64 current_delta)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	__u64 delta2 = jent_delta(ec->last_delta, current_delta);
2678c2ecf20Sopenharmony_ci	__u64 delta3 = jent_delta(ec->last_delta2, delta2);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	ec->last_delta = current_delta;
2708c2ecf20Sopenharmony_ci	ec->last_delta2 = delta2;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/*
2738c2ecf20Sopenharmony_ci	 * Insert the result of the comparison of two back-to-back time
2748c2ecf20Sopenharmony_ci	 * deltas.
2758c2ecf20Sopenharmony_ci	 */
2768c2ecf20Sopenharmony_ci	jent_apt_insert(ec, current_delta);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (!current_delta || !delta2 || !delta3) {
2798c2ecf20Sopenharmony_ci		/* RCT with a stuck bit */
2808c2ecf20Sopenharmony_ci		jent_rct_insert(ec, 1);
2818c2ecf20Sopenharmony_ci		return 1;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* RCT with a non-stuck bit */
2858c2ecf20Sopenharmony_ci	jent_rct_insert(ec, 0);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	return 0;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci/**
2918c2ecf20Sopenharmony_ci * Report any health test failures
2928c2ecf20Sopenharmony_ci *
2938c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
2948c2ecf20Sopenharmony_ci *
2958c2ecf20Sopenharmony_ci * @return
2968c2ecf20Sopenharmony_ci * 	0 No health test failure
2978c2ecf20Sopenharmony_ci * 	1 Permanent health test failure
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistatic int jent_health_failure(struct rand_data *ec)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	/* Test is only enabled in FIPS mode */
3028c2ecf20Sopenharmony_ci	if (!jent_fips_enabled())
3038c2ecf20Sopenharmony_ci		return 0;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return ec->health_failure;
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/***************************************************************************
3098c2ecf20Sopenharmony_ci * Noise sources
3108c2ecf20Sopenharmony_ci ***************************************************************************/
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/**
3138c2ecf20Sopenharmony_ci * Update of the loop count used for the next round of
3148c2ecf20Sopenharmony_ci * an entropy collection.
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * Input:
3178c2ecf20Sopenharmony_ci * @ec entropy collector struct -- may be NULL
3188c2ecf20Sopenharmony_ci * @bits is the number of low bits of the timer to consider
3198c2ecf20Sopenharmony_ci * @min is the number of bits we shift the timer value to the right at
3208c2ecf20Sopenharmony_ci *	the end to make sure we have a guaranteed minimum value
3218c2ecf20Sopenharmony_ci *
3228c2ecf20Sopenharmony_ci * @return Newly calculated loop counter
3238c2ecf20Sopenharmony_ci */
3248c2ecf20Sopenharmony_cistatic __u64 jent_loop_shuffle(struct rand_data *ec,
3258c2ecf20Sopenharmony_ci			       unsigned int bits, unsigned int min)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	__u64 time = 0;
3288c2ecf20Sopenharmony_ci	__u64 shuffle = 0;
3298c2ecf20Sopenharmony_ci	unsigned int i = 0;
3308c2ecf20Sopenharmony_ci	unsigned int mask = (1<<bits) - 1;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	jent_get_nstime(&time);
3338c2ecf20Sopenharmony_ci	/*
3348c2ecf20Sopenharmony_ci	 * Mix the current state of the random number into the shuffle
3358c2ecf20Sopenharmony_ci	 * calculation to balance that shuffle a bit more.
3368c2ecf20Sopenharmony_ci	 */
3378c2ecf20Sopenharmony_ci	if (ec)
3388c2ecf20Sopenharmony_ci		time ^= ec->data;
3398c2ecf20Sopenharmony_ci	/*
3408c2ecf20Sopenharmony_ci	 * We fold the time value as much as possible to ensure that as many
3418c2ecf20Sopenharmony_ci	 * bits of the time stamp are included as possible.
3428c2ecf20Sopenharmony_ci	 */
3438c2ecf20Sopenharmony_ci	for (i = 0; ((DATA_SIZE_BITS + bits - 1) / bits) > i; i++) {
3448c2ecf20Sopenharmony_ci		shuffle ^= time & mask;
3458c2ecf20Sopenharmony_ci		time = time >> bits;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/*
3498c2ecf20Sopenharmony_ci	 * We add a lower boundary value to ensure we have a minimum
3508c2ecf20Sopenharmony_ci	 * RNG loop count.
3518c2ecf20Sopenharmony_ci	 */
3528c2ecf20Sopenharmony_ci	return (shuffle + (1<<min));
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci/**
3568c2ecf20Sopenharmony_ci * CPU Jitter noise source -- this is the noise source based on the CPU
3578c2ecf20Sopenharmony_ci *			      execution time jitter
3588c2ecf20Sopenharmony_ci *
3598c2ecf20Sopenharmony_ci * This function injects the individual bits of the time value into the
3608c2ecf20Sopenharmony_ci * entropy pool using an LFSR.
3618c2ecf20Sopenharmony_ci *
3628c2ecf20Sopenharmony_ci * The code is deliberately inefficient with respect to the bit shifting
3638c2ecf20Sopenharmony_ci * and shall stay that way. This function is the root cause why the code
3648c2ecf20Sopenharmony_ci * shall be compiled without optimization. This function not only acts as
3658c2ecf20Sopenharmony_ci * folding operation, but this function's execution is used to measure
3668c2ecf20Sopenharmony_ci * the CPU execution time jitter. Any change to the loop in this function
3678c2ecf20Sopenharmony_ci * implies that careful retesting must be done.
3688c2ecf20Sopenharmony_ci *
3698c2ecf20Sopenharmony_ci * @ec [in] entropy collector struct
3708c2ecf20Sopenharmony_ci * @time [in] time stamp to be injected
3718c2ecf20Sopenharmony_ci * @loop_cnt [in] if a value not equal to 0 is set, use the given value as
3728c2ecf20Sopenharmony_ci *		  number of loops to perform the folding
3738c2ecf20Sopenharmony_ci * @stuck [in] Is the time stamp identified as stuck?
3748c2ecf20Sopenharmony_ci *
3758c2ecf20Sopenharmony_ci * Output:
3768c2ecf20Sopenharmony_ci * updated ec->data
3778c2ecf20Sopenharmony_ci *
3788c2ecf20Sopenharmony_ci * @return Number of loops the folding operation is performed
3798c2ecf20Sopenharmony_ci */
3808c2ecf20Sopenharmony_cistatic void jent_lfsr_time(struct rand_data *ec, __u64 time, __u64 loop_cnt,
3818c2ecf20Sopenharmony_ci			   int stuck)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	unsigned int i;
3848c2ecf20Sopenharmony_ci	__u64 j = 0;
3858c2ecf20Sopenharmony_ci	__u64 new = 0;
3868c2ecf20Sopenharmony_ci#define MAX_FOLD_LOOP_BIT 4
3878c2ecf20Sopenharmony_ci#define MIN_FOLD_LOOP_BIT 0
3888c2ecf20Sopenharmony_ci	__u64 fold_loop_cnt =
3898c2ecf20Sopenharmony_ci		jent_loop_shuffle(ec, MAX_FOLD_LOOP_BIT, MIN_FOLD_LOOP_BIT);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/*
3928c2ecf20Sopenharmony_ci	 * testing purposes -- allow test app to set the counter, not
3938c2ecf20Sopenharmony_ci	 * needed during runtime
3948c2ecf20Sopenharmony_ci	 */
3958c2ecf20Sopenharmony_ci	if (loop_cnt)
3968c2ecf20Sopenharmony_ci		fold_loop_cnt = loop_cnt;
3978c2ecf20Sopenharmony_ci	for (j = 0; j < fold_loop_cnt; j++) {
3988c2ecf20Sopenharmony_ci		new = ec->data;
3998c2ecf20Sopenharmony_ci		for (i = 1; (DATA_SIZE_BITS) >= i; i++) {
4008c2ecf20Sopenharmony_ci			__u64 tmp = time << (DATA_SIZE_BITS - i);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci			tmp = tmp >> (DATA_SIZE_BITS - 1);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci			/*
4058c2ecf20Sopenharmony_ci			* Fibonacci LSFR with polynomial of
4068c2ecf20Sopenharmony_ci			*  x^64 + x^61 + x^56 + x^31 + x^28 + x^23 + 1 which is
4078c2ecf20Sopenharmony_ci			*  primitive according to
4088c2ecf20Sopenharmony_ci			*   http://poincare.matf.bg.ac.rs/~ezivkovm/publications/primpol1.pdf
4098c2ecf20Sopenharmony_ci			* (the shift values are the polynomial values minus one
4108c2ecf20Sopenharmony_ci			* due to counting bits from 0 to 63). As the current
4118c2ecf20Sopenharmony_ci			* position is always the LSB, the polynomial only needs
4128c2ecf20Sopenharmony_ci			* to shift data in from the left without wrap.
4138c2ecf20Sopenharmony_ci			*/
4148c2ecf20Sopenharmony_ci			tmp ^= ((new >> 63) & 1);
4158c2ecf20Sopenharmony_ci			tmp ^= ((new >> 60) & 1);
4168c2ecf20Sopenharmony_ci			tmp ^= ((new >> 55) & 1);
4178c2ecf20Sopenharmony_ci			tmp ^= ((new >> 30) & 1);
4188c2ecf20Sopenharmony_ci			tmp ^= ((new >> 27) & 1);
4198c2ecf20Sopenharmony_ci			tmp ^= ((new >> 22) & 1);
4208c2ecf20Sopenharmony_ci			new <<= 1;
4218c2ecf20Sopenharmony_ci			new ^= tmp;
4228c2ecf20Sopenharmony_ci		}
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	/*
4268c2ecf20Sopenharmony_ci	 * If the time stamp is stuck, do not finally insert the value into
4278c2ecf20Sopenharmony_ci	 * the entropy pool. Although this operation should not do any harm
4288c2ecf20Sopenharmony_ci	 * even when the time stamp has no entropy, SP800-90B requires that
4298c2ecf20Sopenharmony_ci	 * any conditioning operation (SP800-90B considers the LFSR to be a
4308c2ecf20Sopenharmony_ci	 * conditioning operation) to have an identical amount of input
4318c2ecf20Sopenharmony_ci	 * data according to section 3.1.5.
4328c2ecf20Sopenharmony_ci	 */
4338c2ecf20Sopenharmony_ci	if (!stuck)
4348c2ecf20Sopenharmony_ci		ec->data = new;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci/**
4388c2ecf20Sopenharmony_ci * Memory Access noise source -- this is a noise source based on variations in
4398c2ecf20Sopenharmony_ci *				 memory access times
4408c2ecf20Sopenharmony_ci *
4418c2ecf20Sopenharmony_ci * This function performs memory accesses which will add to the timing
4428c2ecf20Sopenharmony_ci * variations due to an unknown amount of CPU wait states that need to be
4438c2ecf20Sopenharmony_ci * added when accessing memory. The memory size should be larger than the L1
4448c2ecf20Sopenharmony_ci * caches as outlined in the documentation and the associated testing.
4458c2ecf20Sopenharmony_ci *
4468c2ecf20Sopenharmony_ci * The L1 cache has a very high bandwidth, albeit its access rate is  usually
4478c2ecf20Sopenharmony_ci * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
4488c2ecf20Sopenharmony_ci * variations as the CPU has hardly to wait. Starting with L2, significant
4498c2ecf20Sopenharmony_ci * variations are added because L2 typically does not belong to the CPU any more
4508c2ecf20Sopenharmony_ci * and therefore a wider range of CPU wait states is necessary for accesses.
4518c2ecf20Sopenharmony_ci * L3 and real memory accesses have even a wider range of wait states. However,
4528c2ecf20Sopenharmony_ci * to reliably access either L3 or memory, the ec->mem memory must be quite
4538c2ecf20Sopenharmony_ci * large which is usually not desirable.
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * @ec [in] Reference to the entropy collector with the memory access data -- if
4568c2ecf20Sopenharmony_ci *	    the reference to the memory block to be accessed is NULL, this noise
4578c2ecf20Sopenharmony_ci *	    source is disabled
4588c2ecf20Sopenharmony_ci * @loop_cnt [in] if a value not equal to 0 is set, use the given value
4598c2ecf20Sopenharmony_ci *		  number of loops to perform the LFSR
4608c2ecf20Sopenharmony_ci */
4618c2ecf20Sopenharmony_cistatic void jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	unsigned int wrap = 0;
4648c2ecf20Sopenharmony_ci	__u64 i = 0;
4658c2ecf20Sopenharmony_ci#define MAX_ACC_LOOP_BIT 7
4668c2ecf20Sopenharmony_ci#define MIN_ACC_LOOP_BIT 0
4678c2ecf20Sopenharmony_ci	__u64 acc_loop_cnt =
4688c2ecf20Sopenharmony_ci		jent_loop_shuffle(ec, MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	if (NULL == ec || NULL == ec->mem)
4718c2ecf20Sopenharmony_ci		return;
4728c2ecf20Sopenharmony_ci	wrap = ec->memblocksize * ec->memblocks;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/*
4758c2ecf20Sopenharmony_ci	 * testing purposes -- allow test app to set the counter, not
4768c2ecf20Sopenharmony_ci	 * needed during runtime
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	if (loop_cnt)
4798c2ecf20Sopenharmony_ci		acc_loop_cnt = loop_cnt;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
4828c2ecf20Sopenharmony_ci		unsigned char *tmpval = ec->mem + ec->memlocation;
4838c2ecf20Sopenharmony_ci		/*
4848c2ecf20Sopenharmony_ci		 * memory access: just add 1 to one byte,
4858c2ecf20Sopenharmony_ci		 * wrap at 255 -- memory access implies read
4868c2ecf20Sopenharmony_ci		 * from and write to memory location
4878c2ecf20Sopenharmony_ci		 */
4888c2ecf20Sopenharmony_ci		*tmpval = (*tmpval + 1) & 0xff;
4898c2ecf20Sopenharmony_ci		/*
4908c2ecf20Sopenharmony_ci		 * Addition of memblocksize - 1 to pointer
4918c2ecf20Sopenharmony_ci		 * with wrap around logic to ensure that every
4928c2ecf20Sopenharmony_ci		 * memory location is hit evenly
4938c2ecf20Sopenharmony_ci		 */
4948c2ecf20Sopenharmony_ci		ec->memlocation = ec->memlocation + ec->memblocksize - 1;
4958c2ecf20Sopenharmony_ci		ec->memlocation = ec->memlocation % wrap;
4968c2ecf20Sopenharmony_ci	}
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci/***************************************************************************
5008c2ecf20Sopenharmony_ci * Start of entropy processing logic
5018c2ecf20Sopenharmony_ci ***************************************************************************/
5028c2ecf20Sopenharmony_ci/**
5038c2ecf20Sopenharmony_ci * This is the heart of the entropy generation: calculate time deltas and
5048c2ecf20Sopenharmony_ci * use the CPU jitter in the time deltas. The jitter is injected into the
5058c2ecf20Sopenharmony_ci * entropy pool.
5068c2ecf20Sopenharmony_ci *
5078c2ecf20Sopenharmony_ci * WARNING: ensure that ->prev_time is primed before using the output
5088c2ecf20Sopenharmony_ci *	    of this function! This can be done by calling this function
5098c2ecf20Sopenharmony_ci *	    and not using its result.
5108c2ecf20Sopenharmony_ci *
5118c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
5128c2ecf20Sopenharmony_ci *
5138c2ecf20Sopenharmony_ci * @return result of stuck test
5148c2ecf20Sopenharmony_ci */
5158c2ecf20Sopenharmony_cistatic int jent_measure_jitter(struct rand_data *ec)
5168c2ecf20Sopenharmony_ci{
5178c2ecf20Sopenharmony_ci	__u64 time = 0;
5188c2ecf20Sopenharmony_ci	__u64 current_delta = 0;
5198c2ecf20Sopenharmony_ci	int stuck;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* Invoke one noise source before time measurement to add variations */
5228c2ecf20Sopenharmony_ci	jent_memaccess(ec, 0);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/*
5258c2ecf20Sopenharmony_ci	 * Get time stamp and calculate time delta to previous
5268c2ecf20Sopenharmony_ci	 * invocation to measure the timing variations
5278c2ecf20Sopenharmony_ci	 */
5288c2ecf20Sopenharmony_ci	jent_get_nstime(&time);
5298c2ecf20Sopenharmony_ci	current_delta = jent_delta(ec->prev_time, time);
5308c2ecf20Sopenharmony_ci	ec->prev_time = time;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* Check whether we have a stuck measurement. */
5338c2ecf20Sopenharmony_ci	stuck = jent_stuck(ec, current_delta);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* Now call the next noise sources which also injects the data */
5368c2ecf20Sopenharmony_ci	jent_lfsr_time(ec, current_delta, 0, stuck);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	return stuck;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci/**
5428c2ecf20Sopenharmony_ci * Generator of one 64 bit random number
5438c2ecf20Sopenharmony_ci * Function fills rand_data->data
5448c2ecf20Sopenharmony_ci *
5458c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
5468c2ecf20Sopenharmony_ci */
5478c2ecf20Sopenharmony_cistatic void jent_gen_entropy(struct rand_data *ec)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	unsigned int k = 0;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/* priming of the ->prev_time value */
5528c2ecf20Sopenharmony_ci	jent_measure_jitter(ec);
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	while (1) {
5558c2ecf20Sopenharmony_ci		/* If a stuck measurement is received, repeat measurement */
5568c2ecf20Sopenharmony_ci		if (jent_measure_jitter(ec))
5578c2ecf20Sopenharmony_ci			continue;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci		/*
5608c2ecf20Sopenharmony_ci		 * We multiply the loop value with ->osr to obtain the
5618c2ecf20Sopenharmony_ci		 * oversampling rate requested by the caller
5628c2ecf20Sopenharmony_ci		 */
5638c2ecf20Sopenharmony_ci		if (++k >= (DATA_SIZE_BITS * ec->osr))
5648c2ecf20Sopenharmony_ci			break;
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci/**
5698c2ecf20Sopenharmony_ci * Entry function: Obtain entropy for the caller.
5708c2ecf20Sopenharmony_ci *
5718c2ecf20Sopenharmony_ci * This function invokes the entropy gathering logic as often to generate
5728c2ecf20Sopenharmony_ci * as many bytes as requested by the caller. The entropy gathering logic
5738c2ecf20Sopenharmony_ci * creates 64 bit per invocation.
5748c2ecf20Sopenharmony_ci *
5758c2ecf20Sopenharmony_ci * This function truncates the last 64 bit entropy value output to the exact
5768c2ecf20Sopenharmony_ci * size specified by the caller.
5778c2ecf20Sopenharmony_ci *
5788c2ecf20Sopenharmony_ci * @ec [in] Reference to entropy collector
5798c2ecf20Sopenharmony_ci * @data [in] pointer to buffer for storing random data -- buffer must already
5808c2ecf20Sopenharmony_ci *	      exist
5818c2ecf20Sopenharmony_ci * @len [in] size of the buffer, specifying also the requested number of random
5828c2ecf20Sopenharmony_ci *	     in bytes
5838c2ecf20Sopenharmony_ci *
5848c2ecf20Sopenharmony_ci * @return 0 when request is fulfilled or an error
5858c2ecf20Sopenharmony_ci *
5868c2ecf20Sopenharmony_ci * The following error codes can occur:
5878c2ecf20Sopenharmony_ci *	-1	entropy_collector is NULL
5888c2ecf20Sopenharmony_ci *	-2	RCT failed
5898c2ecf20Sopenharmony_ci *	-3	APT test failed
5908c2ecf20Sopenharmony_ci */
5918c2ecf20Sopenharmony_ciint jent_read_entropy(struct rand_data *ec, unsigned char *data,
5928c2ecf20Sopenharmony_ci		      unsigned int len)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	unsigned char *p = data;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (!ec)
5978c2ecf20Sopenharmony_ci		return -1;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	while (0 < len) {
6008c2ecf20Sopenharmony_ci		unsigned int tocopy;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		jent_gen_entropy(ec);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci		if (jent_health_failure(ec)) {
6058c2ecf20Sopenharmony_ci			int ret;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci			if (jent_rct_failure(ec))
6088c2ecf20Sopenharmony_ci				ret = -2;
6098c2ecf20Sopenharmony_ci			else
6108c2ecf20Sopenharmony_ci				ret = -3;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci			/*
6138c2ecf20Sopenharmony_ci			 * Re-initialize the noise source
6148c2ecf20Sopenharmony_ci			 *
6158c2ecf20Sopenharmony_ci			 * If the health test fails, the Jitter RNG remains
6168c2ecf20Sopenharmony_ci			 * in failure state and will return a health failure
6178c2ecf20Sopenharmony_ci			 * during next invocation.
6188c2ecf20Sopenharmony_ci			 */
6198c2ecf20Sopenharmony_ci			if (jent_entropy_init())
6208c2ecf20Sopenharmony_ci				return ret;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci			/* Set APT to initial state */
6238c2ecf20Sopenharmony_ci			jent_apt_reset(ec, 0);
6248c2ecf20Sopenharmony_ci			ec->apt_base_set = 0;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci			/* Set RCT to initial state */
6278c2ecf20Sopenharmony_ci			ec->rct_count = 0;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci			/* Re-enable Jitter RNG */
6308c2ecf20Sopenharmony_ci			ec->health_failure = 0;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci			/*
6338c2ecf20Sopenharmony_ci			 * Return the health test failure status to the
6348c2ecf20Sopenharmony_ci			 * caller as the generated value is not appropriate.
6358c2ecf20Sopenharmony_ci			 */
6368c2ecf20Sopenharmony_ci			return ret;
6378c2ecf20Sopenharmony_ci		}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci		if ((DATA_SIZE_BITS / 8) < len)
6408c2ecf20Sopenharmony_ci			tocopy = (DATA_SIZE_BITS / 8);
6418c2ecf20Sopenharmony_ci		else
6428c2ecf20Sopenharmony_ci			tocopy = len;
6438c2ecf20Sopenharmony_ci		jent_memcpy(p, &ec->data, tocopy);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci		len -= tocopy;
6468c2ecf20Sopenharmony_ci		p += tocopy;
6478c2ecf20Sopenharmony_ci	}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	return 0;
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/***************************************************************************
6538c2ecf20Sopenharmony_ci * Initialization logic
6548c2ecf20Sopenharmony_ci ***************************************************************************/
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistruct rand_data *jent_entropy_collector_alloc(unsigned int osr,
6578c2ecf20Sopenharmony_ci					       unsigned int flags)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	struct rand_data *entropy_collector;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	entropy_collector = jent_zalloc(sizeof(struct rand_data));
6628c2ecf20Sopenharmony_ci	if (!entropy_collector)
6638c2ecf20Sopenharmony_ci		return NULL;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
6668c2ecf20Sopenharmony_ci		/* Allocate memory for adding variations based on memory
6678c2ecf20Sopenharmony_ci		 * access
6688c2ecf20Sopenharmony_ci		 */
6698c2ecf20Sopenharmony_ci		entropy_collector->mem = jent_zalloc(JENT_MEMORY_SIZE);
6708c2ecf20Sopenharmony_ci		if (!entropy_collector->mem) {
6718c2ecf20Sopenharmony_ci			jent_zfree(entropy_collector);
6728c2ecf20Sopenharmony_ci			return NULL;
6738c2ecf20Sopenharmony_ci		}
6748c2ecf20Sopenharmony_ci		entropy_collector->memblocksize = JENT_MEMORY_BLOCKSIZE;
6758c2ecf20Sopenharmony_ci		entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
6768c2ecf20Sopenharmony_ci		entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
6778c2ecf20Sopenharmony_ci	}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	/* verify and set the oversampling rate */
6808c2ecf20Sopenharmony_ci	if (0 == osr)
6818c2ecf20Sopenharmony_ci		osr = 1; /* minimum sampling rate is 1 */
6828c2ecf20Sopenharmony_ci	entropy_collector->osr = osr;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	/* fill the data pad with non-zero values */
6858c2ecf20Sopenharmony_ci	jent_gen_entropy(entropy_collector);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	return entropy_collector;
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_civoid jent_entropy_collector_free(struct rand_data *entropy_collector)
6918c2ecf20Sopenharmony_ci{
6928c2ecf20Sopenharmony_ci	jent_zfree(entropy_collector->mem);
6938c2ecf20Sopenharmony_ci	entropy_collector->mem = NULL;
6948c2ecf20Sopenharmony_ci	jent_zfree(entropy_collector);
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ciint jent_entropy_init(void)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	int i;
7008c2ecf20Sopenharmony_ci	__u64 delta_sum = 0;
7018c2ecf20Sopenharmony_ci	__u64 old_delta = 0;
7028c2ecf20Sopenharmony_ci	unsigned int nonstuck = 0;
7038c2ecf20Sopenharmony_ci	int time_backwards = 0;
7048c2ecf20Sopenharmony_ci	int count_mod = 0;
7058c2ecf20Sopenharmony_ci	int count_stuck = 0;
7068c2ecf20Sopenharmony_ci	struct rand_data ec = { 0 };
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	/* Required for RCT */
7098c2ecf20Sopenharmony_ci	ec.osr = 1;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	/* We could perform statistical tests here, but the problem is
7128c2ecf20Sopenharmony_ci	 * that we only have a few loop counts to do testing. These
7138c2ecf20Sopenharmony_ci	 * loop counts may show some slight skew and we produce
7148c2ecf20Sopenharmony_ci	 * false positives.
7158c2ecf20Sopenharmony_ci	 *
7168c2ecf20Sopenharmony_ci	 * Moreover, only old systems show potentially problematic
7178c2ecf20Sopenharmony_ci	 * jitter entropy that could potentially be caught here. But
7188c2ecf20Sopenharmony_ci	 * the RNG is intended for hardware that is available or widely
7198c2ecf20Sopenharmony_ci	 * used, but not old systems that are long out of favor. Thus,
7208c2ecf20Sopenharmony_ci	 * no statistical tests.
7218c2ecf20Sopenharmony_ci	 */
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/*
7248c2ecf20Sopenharmony_ci	 * We could add a check for system capabilities such as clock_getres or
7258c2ecf20Sopenharmony_ci	 * check for CONFIG_X86_TSC, but it does not make much sense as the
7268c2ecf20Sopenharmony_ci	 * following sanity checks verify that we have a high-resolution
7278c2ecf20Sopenharmony_ci	 * timer.
7288c2ecf20Sopenharmony_ci	 */
7298c2ecf20Sopenharmony_ci	/*
7308c2ecf20Sopenharmony_ci	 * TESTLOOPCOUNT needs some loops to identify edge systems. 100 is
7318c2ecf20Sopenharmony_ci	 * definitely too little.
7328c2ecf20Sopenharmony_ci	 *
7338c2ecf20Sopenharmony_ci	 * SP800-90B requires at least 1024 initial test cycles.
7348c2ecf20Sopenharmony_ci	 */
7358c2ecf20Sopenharmony_ci#define TESTLOOPCOUNT 1024
7368c2ecf20Sopenharmony_ci#define CLEARCACHE 100
7378c2ecf20Sopenharmony_ci	for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
7388c2ecf20Sopenharmony_ci		__u64 time = 0;
7398c2ecf20Sopenharmony_ci		__u64 time2 = 0;
7408c2ecf20Sopenharmony_ci		__u64 delta = 0;
7418c2ecf20Sopenharmony_ci		unsigned int lowdelta = 0;
7428c2ecf20Sopenharmony_ci		int stuck;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		/* Invoke core entropy collection logic */
7458c2ecf20Sopenharmony_ci		jent_get_nstime(&time);
7468c2ecf20Sopenharmony_ci		ec.prev_time = time;
7478c2ecf20Sopenharmony_ci		jent_lfsr_time(&ec, time, 0, 0);
7488c2ecf20Sopenharmony_ci		jent_get_nstime(&time2);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci		/* test whether timer works */
7518c2ecf20Sopenharmony_ci		if (!time || !time2)
7528c2ecf20Sopenharmony_ci			return JENT_ENOTIME;
7538c2ecf20Sopenharmony_ci		delta = jent_delta(time, time2);
7548c2ecf20Sopenharmony_ci		/*
7558c2ecf20Sopenharmony_ci		 * test whether timer is fine grained enough to provide
7568c2ecf20Sopenharmony_ci		 * delta even when called shortly after each other -- this
7578c2ecf20Sopenharmony_ci		 * implies that we also have a high resolution timer
7588c2ecf20Sopenharmony_ci		 */
7598c2ecf20Sopenharmony_ci		if (!delta)
7608c2ecf20Sopenharmony_ci			return JENT_ECOARSETIME;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci		stuck = jent_stuck(&ec, delta);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		/*
7658c2ecf20Sopenharmony_ci		 * up to here we did not modify any variable that will be
7668c2ecf20Sopenharmony_ci		 * evaluated later, but we already performed some work. Thus we
7678c2ecf20Sopenharmony_ci		 * already have had an impact on the caches, branch prediction,
7688c2ecf20Sopenharmony_ci		 * etc. with the goal to clear it to get the worst case
7698c2ecf20Sopenharmony_ci		 * measurements.
7708c2ecf20Sopenharmony_ci		 */
7718c2ecf20Sopenharmony_ci		if (CLEARCACHE > i)
7728c2ecf20Sopenharmony_ci			continue;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci		if (stuck)
7758c2ecf20Sopenharmony_ci			count_stuck++;
7768c2ecf20Sopenharmony_ci		else {
7778c2ecf20Sopenharmony_ci			nonstuck++;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci			/*
7808c2ecf20Sopenharmony_ci			 * Ensure that the APT succeeded.
7818c2ecf20Sopenharmony_ci			 *
7828c2ecf20Sopenharmony_ci			 * With the check below that count_stuck must be less
7838c2ecf20Sopenharmony_ci			 * than 10% of the overall generated raw entropy values
7848c2ecf20Sopenharmony_ci			 * it is guaranteed that the APT is invoked at
7858c2ecf20Sopenharmony_ci			 * floor((TESTLOOPCOUNT * 0.9) / 64) == 14 times.
7868c2ecf20Sopenharmony_ci			 */
7878c2ecf20Sopenharmony_ci			if ((nonstuck % JENT_APT_WINDOW_SIZE) == 0) {
7888c2ecf20Sopenharmony_ci				jent_apt_reset(&ec,
7898c2ecf20Sopenharmony_ci					       delta & JENT_APT_WORD_MASK);
7908c2ecf20Sopenharmony_ci				if (jent_health_failure(&ec))
7918c2ecf20Sopenharmony_ci					return JENT_EHEALTH;
7928c2ecf20Sopenharmony_ci			}
7938c2ecf20Sopenharmony_ci		}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci		/* Validate RCT */
7968c2ecf20Sopenharmony_ci		if (jent_rct_failure(&ec))
7978c2ecf20Sopenharmony_ci			return JENT_ERCT;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci		/* test whether we have an increasing timer */
8008c2ecf20Sopenharmony_ci		if (!(time2 > time))
8018c2ecf20Sopenharmony_ci			time_backwards++;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci		/* use 32 bit value to ensure compilation on 32 bit arches */
8048c2ecf20Sopenharmony_ci		lowdelta = time2 - time;
8058c2ecf20Sopenharmony_ci		if (!(lowdelta % 100))
8068c2ecf20Sopenharmony_ci			count_mod++;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci		/*
8098c2ecf20Sopenharmony_ci		 * ensure that we have a varying delta timer which is necessary
8108c2ecf20Sopenharmony_ci		 * for the calculation of entropy -- perform this check
8118c2ecf20Sopenharmony_ci		 * only after the first loop is executed as we need to prime
8128c2ecf20Sopenharmony_ci		 * the old_data value
8138c2ecf20Sopenharmony_ci		 */
8148c2ecf20Sopenharmony_ci		if (delta > old_delta)
8158c2ecf20Sopenharmony_ci			delta_sum += (delta - old_delta);
8168c2ecf20Sopenharmony_ci		else
8178c2ecf20Sopenharmony_ci			delta_sum += (old_delta - delta);
8188c2ecf20Sopenharmony_ci		old_delta = delta;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	/*
8228c2ecf20Sopenharmony_ci	 * we allow up to three times the time running backwards.
8238c2ecf20Sopenharmony_ci	 * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
8248c2ecf20Sopenharmony_ci	 * if such an operation just happens to interfere with our test, it
8258c2ecf20Sopenharmony_ci	 * should not fail. The value of 3 should cover the NTP case being
8268c2ecf20Sopenharmony_ci	 * performed during our test run.
8278c2ecf20Sopenharmony_ci	 */
8288c2ecf20Sopenharmony_ci	if (3 < time_backwards)
8298c2ecf20Sopenharmony_ci		return JENT_ENOMONOTONIC;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/*
8328c2ecf20Sopenharmony_ci	 * Variations of deltas of time must on average be larger
8338c2ecf20Sopenharmony_ci	 * than 1 to ensure the entropy estimation
8348c2ecf20Sopenharmony_ci	 * implied with 1 is preserved
8358c2ecf20Sopenharmony_ci	 */
8368c2ecf20Sopenharmony_ci	if ((delta_sum) <= 1)
8378c2ecf20Sopenharmony_ci		return JENT_EVARVAR;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	/*
8408c2ecf20Sopenharmony_ci	 * Ensure that we have variations in the time stamp below 10 for at
8418c2ecf20Sopenharmony_ci	 * least 10% of all checks -- on some platforms, the counter increments
8428c2ecf20Sopenharmony_ci	 * in multiples of 100, but not always
8438c2ecf20Sopenharmony_ci	 */
8448c2ecf20Sopenharmony_ci	if ((TESTLOOPCOUNT/10 * 9) < count_mod)
8458c2ecf20Sopenharmony_ci		return JENT_ECOARSETIME;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	/*
8488c2ecf20Sopenharmony_ci	 * If we have more than 90% stuck results, then this Jitter RNG is
8498c2ecf20Sopenharmony_ci	 * likely to not work well.
8508c2ecf20Sopenharmony_ci	 */
8518c2ecf20Sopenharmony_ci	if ((TESTLOOPCOUNT/10 * 9) < count_stuck)
8528c2ecf20Sopenharmony_ci		return JENT_ESTUCK;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	return 0;
8558c2ecf20Sopenharmony_ci}
856