Linux Audio

Check our new training course

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