Blame view

crypto/jitterentropy.c 25.2 KB
bb5530e40   Stephan Mueller   crypto: jitterent...
1
  /*
dfc9fa919   Stephan Mueller   crypto: jitterent...
2
3
   * Non-physical true random number generator based on timing jitter --
   * Jitter RNG standalone code.
bb5530e40   Stephan Mueller   crypto: jitterent...
4
   *
764428fe9   Stephan Müller   crypto: jitter - ...
5
   * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2020
bb5530e40   Stephan Mueller   crypto: jitterent...
6
7
8
9
   *
   * Design
   * ======
   *
9332a9e73   Alexander A. Klimov   crypto: Replace H...
10
   * See https://www.chronox.de/jent.html
bb5530e40   Stephan Mueller   crypto: jitterent...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
   *
   * License
   * =======
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   * 1. Redistributions of source code must retain the above copyright
   *    notice, and the entire permission notice in its entirety,
   *    including the disclaimer of warranties.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. The name of the author may not be used to endorse or promote
   *    products derived from this software without specific prior
   *    written permission.
   *
   * ALTERNATIVELY, this product may be distributed under the terms of
   * the GNU General Public License, in which case the provisions of the GPL2 are
   * required INSTEAD OF the above restrictions.  (This clause is
   * necessary due to a potential bad interaction between the GPL and
   * the restrictions contained in a BSD-style copyright.)
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
   * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
   * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
   * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
   * DAMAGE.
   */
  
  /*
   * This Jitterentropy RNG is based on the jitterentropy library
9332a9e73   Alexander A. Klimov   crypto: Replace H...
50
   * version 2.2.0 provided at https://www.chronox.de/jent.html
bb5530e40   Stephan Mueller   crypto: jitterent...
51
   */
dfc9fa919   Stephan Mueller   crypto: jitterent...
52
53
54
55
56
57
58
59
  #ifdef __OPTIMIZE__
   #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
  #endif
  
  typedef	unsigned long long	__u64;
  typedef	long long		__s64;
  typedef	unsigned int		__u32;
  #define NULL    ((void *) 0)
bb5530e40   Stephan Mueller   crypto: jitterent...
60

bb5530e40   Stephan Mueller   crypto: jitterent...
61
62
63
64
65
66
67
68
69
70
71
72
  /* The entropy pool */
  struct rand_data {
  	/* all data values that are vital to maintain the security
  	 * of the RNG are marked as SENSITIVE. A user must not
  	 * access that information while the RNG executes its loops to
  	 * calculate the next random value. */
  	__u64 data;		/* SENSITIVE Actual random number */
  	__u64 old_data;		/* SENSITIVE Previous random number */
  	__u64 prev_time;	/* SENSITIVE Previous time stamp */
  #define DATA_SIZE_BITS ((sizeof(__u64)) * 8)
  	__u64 last_delta;	/* SENSITIVE stuck test */
  	__s64 last_delta2;	/* SENSITIVE stuck test */
bb5530e40   Stephan Mueller   crypto: jitterent...
73
  	unsigned int osr;	/* Oversample rate */
bb5530e40   Stephan Mueller   crypto: jitterent...
74
75
76
77
78
79
80
81
82
83
84
  #define JENT_MEMORY_BLOCKS 64
  #define JENT_MEMORY_BLOCKSIZE 32
  #define JENT_MEMORY_ACCESSLOOPS 128
  #define JENT_MEMORY_SIZE (JENT_MEMORY_BLOCKS*JENT_MEMORY_BLOCKSIZE)
  	unsigned char *mem;	/* Memory access location with size of
  				 * memblocks * memblocksize */
  	unsigned int memlocation; /* Pointer to byte in *mem */
  	unsigned int memblocks;	/* Number of memory blocks in *mem */
  	unsigned int memblocksize; /* Size of one memory block in bytes */
  	unsigned int memaccessloops; /* Number of memory accesses per random
  				      * bit generation */
764428fe9   Stephan Müller   crypto: jitter - ...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  
  	/* Repetition Count Test */
  	int rct_count;			/* Number of stuck values */
  
  	/* Adaptive Proportion Test for a significance level of 2^-30 */
  #define JENT_APT_CUTOFF		325	/* Taken from SP800-90B sec 4.4.2 */
  #define JENT_APT_WINDOW_SIZE	512	/* Data window size */
  	/* LSB of time stamp to process */
  #define JENT_APT_LSB		16
  #define JENT_APT_WORD_MASK	(JENT_APT_LSB - 1)
  	unsigned int apt_observations;	/* Number of collected observations */
  	unsigned int apt_count;		/* APT counter */
  	unsigned int apt_base;		/* APT base reference */
  	unsigned int apt_base_set:1;	/* APT base reference set? */
  
  	unsigned int health_failure:1;	/* Permanent health failure */
bb5530e40   Stephan Mueller   crypto: jitterent...
101
102
103
  };
  
  /* Flags that can be used to initialize the RNG */
bb5530e40   Stephan Mueller   crypto: jitterent...
104
105
106
  #define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more
  					   * entropy, saves MEMORY_SIZE RAM for
  					   * entropy collector */
bb5530e40   Stephan Mueller   crypto: jitterent...
107
108
109
110
  /* -- error codes for init function -- */
  #define JENT_ENOTIME		1 /* Timer service not available */
  #define JENT_ECOARSETIME	2 /* Timer too coarse for RNG */
  #define JENT_ENOMONOTONIC	3 /* Timer is not monotonic increasing */
bb5530e40   Stephan Mueller   crypto: jitterent...
111
112
113
  #define JENT_EVARVAR		5 /* Timer does not produce variations of
  				   * variations (2nd derivation of time is
  				   * zero). */
d9d67c87a   Stephan Müller   crypto: jitter - ...
114
  #define JENT_ESTUCK		8 /* Too many stuck results during init. */
764428fe9   Stephan Müller   crypto: jitter - ...
115
116
117
118
  #define JENT_EHEALTH		9 /* Health test failed during initialization */
  #define JENT_ERCT		10 /* RCT failed during initialization */
  
  #include "jitterentropy.h"
bb5530e40   Stephan Mueller   crypto: jitterent...
119
120
  
  /***************************************************************************
764428fe9   Stephan Müller   crypto: jitter - ...
121
122
123
   * Adaptive Proportion Test
   *
   * This test complies with SP800-90B section 4.4.2.
bb5530e40   Stephan Mueller   crypto: jitterent...
124
   ***************************************************************************/
764428fe9   Stephan Müller   crypto: jitter - ...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  /**
   * Reset the APT counter
   *
   * @ec [in] Reference to entropy collector
   */
  static void jent_apt_reset(struct rand_data *ec, unsigned int delta_masked)
  {
  	/* Reset APT counter */
  	ec->apt_count = 0;
  	ec->apt_base = delta_masked;
  	ec->apt_observations = 0;
  }
  
  /**
   * Insert a new entropy event into APT
   *
   * @ec [in] Reference to entropy collector
   * @delta_masked [in] Masked time delta to process
   */
  static void jent_apt_insert(struct rand_data *ec, unsigned int delta_masked)
  {
  	/* Initialize the base reference */
  	if (!ec->apt_base_set) {
  		ec->apt_base = delta_masked;
  		ec->apt_base_set = 1;
  		return;
  	}
  
  	if (delta_masked == ec->apt_base) {
  		ec->apt_count++;
  
  		if (ec->apt_count >= JENT_APT_CUTOFF)
  			ec->health_failure = 1;
  	}
  
  	ec->apt_observations++;
  
  	if (ec->apt_observations >= JENT_APT_WINDOW_SIZE)
  		jent_apt_reset(ec, delta_masked);
  }
  
  /***************************************************************************
   * Stuck Test and its use as Repetition Count Test
   *
   * The Jitter RNG uses an enhanced version of the Repetition Count Test
   * (RCT) specified in SP800-90B section 4.4.1. Instead of counting identical
   * back-to-back values, the input to the RCT is the counting of the stuck
   * values during the generation of one Jitter RNG output block.
   *
   * The RCT is applied with an alpha of 2^{-30} compliant to FIPS 140-2 IG 9.8.
   *
   * During the counting operation, the Jitter RNG always calculates the RCT
   * cut-off value of C. If that value exceeds the allowed cut-off value,
   * the Jitter RNG output block will be calculated completely but discarded at
   * the end. The caller of the Jitter RNG is informed with an error code.
   ***************************************************************************/
  
  /**
   * Repetition Count Test as defined in SP800-90B section 4.4.1
   *
   * @ec [in] Reference to entropy collector
   * @stuck [in] Indicator whether the value is stuck
   */
  static void jent_rct_insert(struct rand_data *ec, int stuck)
  {
  	/*
  	 * If we have a count less than zero, a previous RCT round identified
  	 * a failure. We will not overwrite it.
  	 */
  	if (ec->rct_count < 0)
  		return;
  
  	if (stuck) {
  		ec->rct_count++;
  
  		/*
  		 * The cutoff value is based on the following consideration:
  		 * alpha = 2^-30 as recommended in FIPS 140-2 IG 9.8.
  		 * In addition, we require an entropy value H of 1/OSR as this
  		 * is the minimum entropy required to provide full entropy.
  		 * Note, we collect 64 * OSR deltas for inserting them into
  		 * the entropy pool which should then have (close to) 64 bits
  		 * of entropy.
  		 *
  		 * Note, ec->rct_count (which equals to value B in the pseudo
  		 * code of SP800-90B section 4.4.1) starts with zero. Hence
  		 * we need to subtract one from the cutoff value as calculated
  		 * following SP800-90B.
  		 */
  		if ((unsigned int)ec->rct_count >= (31 * ec->osr)) {
  			ec->rct_count = -1;
  			ec->health_failure = 1;
  		}
  	} else {
  		ec->rct_count = 0;
  	}
  }
  
  /**
   * Is there an RCT health test failure?
   *
   * @ec [in] Reference to entropy collector
   *
   * @return
   * 	0 No health test failure
   * 	1 Permanent health test failure
   */
  static int jent_rct_failure(struct rand_data *ec)
  {
  	if (ec->rct_count < 0)
  		return 1;
  	return 0;
  }
  
  static inline __u64 jent_delta(__u64 prev, __u64 next)
  {
  #define JENT_UINT64_MAX		(__u64)(~((__u64) 0))
  	return (prev < next) ? (next - prev) :
  			       (JENT_UINT64_MAX - prev + 1 + next);
  }
  
  /**
   * Stuck test by checking the:
   * 	1st derivative of the jitter measurement (time delta)
   * 	2nd derivative of the jitter measurement (delta of time deltas)
   * 	3rd derivative of the jitter measurement (delta of delta of time deltas)
   *
   * All values must always be non-zero.
   *
   * @ec [in] Reference to entropy collector
   * @current_delta [in] Jitter time delta
   *
   * @return
   * 	0 jitter measurement not stuck (good bit)
   * 	1 jitter measurement stuck (reject bit)
   */
  static int jent_stuck(struct rand_data *ec, __u64 current_delta)
  {
  	__u64 delta2 = jent_delta(ec->last_delta, current_delta);
  	__u64 delta3 = jent_delta(ec->last_delta2, delta2);
  	unsigned int delta_masked = current_delta & JENT_APT_WORD_MASK;
  
  	ec->last_delta = current_delta;
  	ec->last_delta2 = delta2;
  
  	/*
  	 * Insert the result of the comparison of two back-to-back time
  	 * deltas.
  	 */
  	jent_apt_insert(ec, delta_masked);
  
  	if (!current_delta || !delta2 || !delta3) {
  		/* RCT with a stuck bit */
  		jent_rct_insert(ec, 1);
  		return 1;
  	}
  
  	/* RCT with a non-stuck bit */
  	jent_rct_insert(ec, 0);
  
  	return 0;
  }
  
  /**
   * Report any health test failures
   *
   * @ec [in] Reference to entropy collector
   *
   * @return
   * 	0 No health test failure
   * 	1 Permanent health test failure
   */
  static int jent_health_failure(struct rand_data *ec)
  {
  	/* Test is only enabled in FIPS mode */
  	if (!jent_fips_enabled())
  		return 0;
  
  	return ec->health_failure;
  }
  
  /***************************************************************************
   * Noise sources
   ***************************************************************************/
bb5530e40   Stephan Mueller   crypto: jitterent...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  
  /**
   * Update of the loop count used for the next round of
   * an entropy collection.
   *
   * Input:
   * @ec entropy collector struct -- may be NULL
   * @bits is the number of low bits of the timer to consider
   * @min is the number of bits we shift the timer value to the right at
   *	the end to make sure we have a guaranteed minimum value
   *
   * @return Newly calculated loop counter
   */
  static __u64 jent_loop_shuffle(struct rand_data *ec,
  			       unsigned int bits, unsigned int min)
  {
  	__u64 time = 0;
  	__u64 shuffle = 0;
  	unsigned int i = 0;
  	unsigned int mask = (1<<bits) - 1;
  
  	jent_get_nstime(&time);
  	/*
d9d67c87a   Stephan Müller   crypto: jitter - ...
332
333
  	 * Mix the current state of the random number into the shuffle
  	 * calculation to balance that shuffle a bit more.
bb5530e40   Stephan Mueller   crypto: jitterent...
334
335
336
337
  	 */
  	if (ec)
  		time ^= ec->data;
  	/*
d9d67c87a   Stephan Müller   crypto: jitter - ...
338
339
  	 * We fold the time value as much as possible to ensure that as many
  	 * bits of the time stamp are included as possible.
bb5530e40   Stephan Mueller   crypto: jitterent...
340
  	 */
d9d67c87a   Stephan Müller   crypto: jitter - ...
341
  	for (i = 0; ((DATA_SIZE_BITS + bits - 1) / bits) > i; i++) {
bb5530e40   Stephan Mueller   crypto: jitterent...
342
343
344
345
346
347
348
349
350
351
  		shuffle ^= time & mask;
  		time = time >> bits;
  	}
  
  	/*
  	 * We add a lower boundary value to ensure we have a minimum
  	 * RNG loop count.
  	 */
  	return (shuffle + (1<<min));
  }
bb5530e40   Stephan Mueller   crypto: jitterent...
352
353
354
355
  /**
   * CPU Jitter noise source -- this is the noise source based on the CPU
   *			      execution time jitter
   *
d9d67c87a   Stephan Müller   crypto: jitter - ...
356
357
   * This function injects the individual bits of the time value into the
   * entropy pool using an LFSR.
bb5530e40   Stephan Mueller   crypto: jitterent...
358
   *
d9d67c87a   Stephan Müller   crypto: jitter - ...
359
360
361
362
363
364
   * The code is deliberately inefficient with respect to the bit shifting
   * and shall stay that way. This function is the root cause why the code
   * shall be compiled without optimization. This function not only acts as
   * folding operation, but this function's execution is used to measure
   * the CPU execution time jitter. Any change to the loop in this function
   * implies that careful retesting must be done.
bb5530e40   Stephan Mueller   crypto: jitterent...
365
   *
764428fe9   Stephan Müller   crypto: jitter - ...
366
367
368
369
370
   * @ec [in] entropy collector struct
   * @time [in] time stamp to be injected
   * @loop_cnt [in] if a value not equal to 0 is set, use the given value as
   *		  number of loops to perform the folding
   * @stuck [in] Is the time stamp identified as stuck?
bb5530e40   Stephan Mueller   crypto: jitterent...
371
372
   *
   * Output:
d9d67c87a   Stephan Müller   crypto: jitter - ...
373
   * updated ec->data
bb5530e40   Stephan Mueller   crypto: jitterent...
374
375
376
   *
   * @return Number of loops the folding operation is performed
   */
764428fe9   Stephan Müller   crypto: jitter - ...
377
378
  static void jent_lfsr_time(struct rand_data *ec, __u64 time, __u64 loop_cnt,
  			   int stuck)
bb5530e40   Stephan Mueller   crypto: jitterent...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  {
  	unsigned int i;
  	__u64 j = 0;
  	__u64 new = 0;
  #define MAX_FOLD_LOOP_BIT 4
  #define MIN_FOLD_LOOP_BIT 0
  	__u64 fold_loop_cnt =
  		jent_loop_shuffle(ec, MAX_FOLD_LOOP_BIT, MIN_FOLD_LOOP_BIT);
  
  	/*
  	 * testing purposes -- allow test app to set the counter, not
  	 * needed during runtime
  	 */
  	if (loop_cnt)
  		fold_loop_cnt = loop_cnt;
  	for (j = 0; j < fold_loop_cnt; j++) {
d9d67c87a   Stephan Müller   crypto: jitter - ...
395
  		new = ec->data;
bb5530e40   Stephan Mueller   crypto: jitterent...
396
397
398
399
  		for (i = 1; (DATA_SIZE_BITS) >= i; i++) {
  			__u64 tmp = time << (DATA_SIZE_BITS - i);
  
  			tmp = tmp >> (DATA_SIZE_BITS - 1);
d9d67c87a   Stephan Müller   crypto: jitter - ...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
  
  			/*
  			* Fibonacci LSFR with polynomial of
  			*  x^64 + x^61 + x^56 + x^31 + x^28 + x^23 + 1 which is
  			*  primitive according to
  			*   http://poincare.matf.bg.ac.rs/~ezivkovm/publications/primpol1.pdf
  			* (the shift values are the polynomial values minus one
  			* due to counting bits from 0 to 63). As the current
  			* position is always the LSB, the polynomial only needs
  			* to shift data in from the left without wrap.
  			*/
  			tmp ^= ((new >> 63) & 1);
  			tmp ^= ((new >> 60) & 1);
  			tmp ^= ((new >> 55) & 1);
  			tmp ^= ((new >> 30) & 1);
  			tmp ^= ((new >> 27) & 1);
  			tmp ^= ((new >> 22) & 1);
  			new <<= 1;
bb5530e40   Stephan Mueller   crypto: jitterent...
418
419
420
  			new ^= tmp;
  		}
  	}
d9d67c87a   Stephan Müller   crypto: jitter - ...
421

764428fe9   Stephan Müller   crypto: jitter - ...
422
423
424
425
426
427
428
429
430
431
  	/*
  	 * If the time stamp is stuck, do not finally insert the value into
  	 * the entropy pool. Although this operation should not do any harm
  	 * even when the time stamp has no entropy, SP800-90B requires that
  	 * any conditioning operation (SP800-90B considers the LFSR to be a
  	 * conditioning operation) to have an identical amount of input
  	 * data according to section 3.1.5.
  	 */
  	if (!stuck)
  		ec->data = new;
bb5530e40   Stephan Mueller   crypto: jitterent...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  }
  
  /**
   * Memory Access noise source -- this is a noise source based on variations in
   *				 memory access times
   *
   * This function performs memory accesses which will add to the timing
   * variations due to an unknown amount of CPU wait states that need to be
   * added when accessing memory. The memory size should be larger than the L1
   * caches as outlined in the documentation and the associated testing.
   *
   * The L1 cache has a very high bandwidth, albeit its access rate is  usually
   * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
   * variations as the CPU has hardly to wait. Starting with L2, significant
   * variations are added because L2 typically does not belong to the CPU any more
   * and therefore a wider range of CPU wait states is necessary for accesses.
   * L3 and real memory accesses have even a wider range of wait states. However,
   * to reliably access either L3 or memory, the ec->mem memory must be quite
   * large which is usually not desirable.
   *
764428fe9   Stephan Müller   crypto: jitter - ...
452
453
454
455
456
   * @ec [in] Reference to the entropy collector with the memory access data -- if
   *	    the reference to the memory block to be accessed is NULL, this noise
   *	    source is disabled
   * @loop_cnt [in] if a value not equal to 0 is set, use the given value
   *		  number of loops to perform the LFSR
bb5530e40   Stephan Mueller   crypto: jitterent...
457
   */
764428fe9   Stephan Müller   crypto: jitter - ...
458
  static void jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
bb5530e40   Stephan Mueller   crypto: jitterent...
459
  {
bb5530e40   Stephan Mueller   crypto: jitterent...
460
461
462
463
464
465
466
467
  	unsigned int wrap = 0;
  	__u64 i = 0;
  #define MAX_ACC_LOOP_BIT 7
  #define MIN_ACC_LOOP_BIT 0
  	__u64 acc_loop_cnt =
  		jent_loop_shuffle(ec, MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
  
  	if (NULL == ec || NULL == ec->mem)
764428fe9   Stephan Müller   crypto: jitter - ...
468
  		return;
bb5530e40   Stephan Mueller   crypto: jitterent...
469
470
471
472
473
474
475
476
477
478
  	wrap = ec->memblocksize * ec->memblocks;
  
  	/*
  	 * testing purposes -- allow test app to set the counter, not
  	 * needed during runtime
  	 */
  	if (loop_cnt)
  		acc_loop_cnt = loop_cnt;
  
  	for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
d9d67c87a   Stephan Müller   crypto: jitter - ...
479
  		unsigned char *tmpval = ec->mem + ec->memlocation;
bb5530e40   Stephan Mueller   crypto: jitterent...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
  		/*
  		 * memory access: just add 1 to one byte,
  		 * wrap at 255 -- memory access implies read
  		 * from and write to memory location
  		 */
  		*tmpval = (*tmpval + 1) & 0xff;
  		/*
  		 * Addition of memblocksize - 1 to pointer
  		 * with wrap around logic to ensure that every
  		 * memory location is hit evenly
  		 */
  		ec->memlocation = ec->memlocation + ec->memblocksize - 1;
  		ec->memlocation = ec->memlocation % wrap;
  	}
bb5530e40   Stephan Mueller   crypto: jitterent...
494
495
496
497
498
  }
  
  /***************************************************************************
   * Start of entropy processing logic
   ***************************************************************************/
bb5530e40   Stephan Mueller   crypto: jitterent...
499
500
  /**
   * This is the heart of the entropy generation: calculate time deltas and
d9d67c87a   Stephan Müller   crypto: jitter - ...
501
502
   * use the CPU jitter in the time deltas. The jitter is injected into the
   * entropy pool.
bb5530e40   Stephan Mueller   crypto: jitterent...
503
504
505
506
507
   *
   * WARNING: ensure that ->prev_time is primed before using the output
   *	    of this function! This can be done by calling this function
   *	    and not using its result.
   *
764428fe9   Stephan Müller   crypto: jitter - ...
508
   * @ec [in] Reference to entropy collector
bb5530e40   Stephan Mueller   crypto: jitterent...
509
   *
d9d67c87a   Stephan Müller   crypto: jitter - ...
510
   * @return result of stuck test
bb5530e40   Stephan Mueller   crypto: jitterent...
511
   */
d9d67c87a   Stephan Müller   crypto: jitter - ...
512
  static int jent_measure_jitter(struct rand_data *ec)
bb5530e40   Stephan Mueller   crypto: jitterent...
513
514
  {
  	__u64 time = 0;
bb5530e40   Stephan Mueller   crypto: jitterent...
515
  	__u64 current_delta = 0;
764428fe9   Stephan Müller   crypto: jitter - ...
516
  	int stuck;
bb5530e40   Stephan Mueller   crypto: jitterent...
517
518
519
520
521
522
523
524
525
  
  	/* Invoke one noise source before time measurement to add variations */
  	jent_memaccess(ec, 0);
  
  	/*
  	 * Get time stamp and calculate time delta to previous
  	 * invocation to measure the timing variations
  	 */
  	jent_get_nstime(&time);
764428fe9   Stephan Müller   crypto: jitter - ...
526
  	current_delta = jent_delta(ec->prev_time, time);
bb5530e40   Stephan Mueller   crypto: jitterent...
527
  	ec->prev_time = time;
764428fe9   Stephan Müller   crypto: jitter - ...
528
529
  	/* Check whether we have a stuck measurement. */
  	stuck = jent_stuck(ec, current_delta);
d9d67c87a   Stephan Müller   crypto: jitter - ...
530
  	/* Now call the next noise sources which also injects the data */
764428fe9   Stephan Müller   crypto: jitter - ...
531
  	jent_lfsr_time(ec, current_delta, 0, stuck);
bb5530e40   Stephan Mueller   crypto: jitterent...
532

764428fe9   Stephan Müller   crypto: jitter - ...
533
  	return stuck;
bb5530e40   Stephan Mueller   crypto: jitterent...
534
535
536
537
538
539
  }
  
  /**
   * Generator of one 64 bit random number
   * Function fills rand_data->data
   *
764428fe9   Stephan Müller   crypto: jitter - ...
540
   * @ec [in] Reference to entropy collector
bb5530e40   Stephan Mueller   crypto: jitterent...
541
542
543
544
545
546
547
548
549
   */
  static void jent_gen_entropy(struct rand_data *ec)
  {
  	unsigned int k = 0;
  
  	/* priming of the ->prev_time value */
  	jent_measure_jitter(ec);
  
  	while (1) {
d9d67c87a   Stephan Müller   crypto: jitter - ...
550
551
  		/* If a stuck measurement is received, repeat measurement */
  		if (jent_measure_jitter(ec))
bb5530e40   Stephan Mueller   crypto: jitterent...
552
  			continue;
bb5530e40   Stephan Mueller   crypto: jitterent...
553
554
555
556
557
558
559
560
  
  		/*
  		 * We multiply the loop value with ->osr to obtain the
  		 * oversampling rate requested by the caller
  		 */
  		if (++k >= (DATA_SIZE_BITS * ec->osr))
  			break;
  	}
bb5530e40   Stephan Mueller   crypto: jitterent...
561
562
563
  }
  
  /**
bb5530e40   Stephan Mueller   crypto: jitterent...
564
565
566
567
568
569
570
571
572
   * Entry function: Obtain entropy for the caller.
   *
   * This function invokes the entropy gathering logic as often to generate
   * as many bytes as requested by the caller. The entropy gathering logic
   * creates 64 bit per invocation.
   *
   * This function truncates the last 64 bit entropy value output to the exact
   * size specified by the caller.
   *
764428fe9   Stephan Müller   crypto: jitter - ...
573
574
575
576
577
   * @ec [in] Reference to entropy collector
   * @data [in] pointer to buffer for storing random data -- buffer must already
   *	      exist
   * @len [in] size of the buffer, specifying also the requested number of random
   *	     in bytes
bb5530e40   Stephan Mueller   crypto: jitterent...
578
579
580
581
582
   *
   * @return 0 when request is fulfilled or an error
   *
   * The following error codes can occur:
   *	-1	entropy_collector is NULL
764428fe9   Stephan Müller   crypto: jitter - ...
583
584
   *	-2	RCT failed
   *	-3	APT test failed
bb5530e40   Stephan Mueller   crypto: jitterent...
585
   */
dfc9fa919   Stephan Mueller   crypto: jitterent...
586
587
  int jent_read_entropy(struct rand_data *ec, unsigned char *data,
  		      unsigned int len)
bb5530e40   Stephan Mueller   crypto: jitterent...
588
  {
dfc9fa919   Stephan Mueller   crypto: jitterent...
589
  	unsigned char *p = data;
bb5530e40   Stephan Mueller   crypto: jitterent...
590
591
  
  	if (!ec)
dfc9fa919   Stephan Mueller   crypto: jitterent...
592
  		return -1;
bb5530e40   Stephan Mueller   crypto: jitterent...
593
594
  
  	while (0 < len) {
dfc9fa919   Stephan Mueller   crypto: jitterent...
595
  		unsigned int tocopy;
bb5530e40   Stephan Mueller   crypto: jitterent...
596
597
  
  		jent_gen_entropy(ec);
764428fe9   Stephan Müller   crypto: jitter - ...
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  
  		if (jent_health_failure(ec)) {
  			int ret;
  
  			if (jent_rct_failure(ec))
  				ret = -2;
  			else
  				ret = -3;
  
  			/*
  			 * Re-initialize the noise source
  			 *
  			 * If the health test fails, the Jitter RNG remains
  			 * in failure state and will return a health failure
  			 * during next invocation.
  			 */
  			if (jent_entropy_init())
  				return ret;
  
  			/* Set APT to initial state */
  			jent_apt_reset(ec, 0);
  			ec->apt_base_set = 0;
  
  			/* Set RCT to initial state */
  			ec->rct_count = 0;
  
  			/* Re-enable Jitter RNG */
  			ec->health_failure = 0;
  
  			/*
  			 * Return the health test failure status to the
  			 * caller as the generated value is not appropriate.
  			 */
  			return ret;
  		}
bb5530e40   Stephan Mueller   crypto: jitterent...
633
634
635
636
  		if ((DATA_SIZE_BITS / 8) < len)
  			tocopy = (DATA_SIZE_BITS / 8);
  		else
  			tocopy = len;
dfc9fa919   Stephan Mueller   crypto: jitterent...
637
  		jent_memcpy(p, &ec->data, tocopy);
bb5530e40   Stephan Mueller   crypto: jitterent...
638
639
640
641
642
643
644
645
646
647
648
  
  		len -= tocopy;
  		p += tocopy;
  	}
  
  	return 0;
  }
  
  /***************************************************************************
   * Initialization logic
   ***************************************************************************/
dfc9fa919   Stephan Mueller   crypto: jitterent...
649
650
  struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
  					       unsigned int flags)
bb5530e40   Stephan Mueller   crypto: jitterent...
651
652
  {
  	struct rand_data *entropy_collector;
dfc9fa919   Stephan Mueller   crypto: jitterent...
653
  	entropy_collector = jent_zalloc(sizeof(struct rand_data));
bb5530e40   Stephan Mueller   crypto: jitterent...
654
655
656
657
658
659
660
  	if (!entropy_collector)
  		return NULL;
  
  	if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
  		/* Allocate memory for adding variations based on memory
  		 * access
  		 */
dfc9fa919   Stephan Mueller   crypto: jitterent...
661
  		entropy_collector->mem = jent_zalloc(JENT_MEMORY_SIZE);
bb5530e40   Stephan Mueller   crypto: jitterent...
662
  		if (!entropy_collector->mem) {
dfc9fa919   Stephan Mueller   crypto: jitterent...
663
  			jent_zfree(entropy_collector);
bb5530e40   Stephan Mueller   crypto: jitterent...
664
665
666
667
668
669
670
671
672
673
674
  			return NULL;
  		}
  		entropy_collector->memblocksize = JENT_MEMORY_BLOCKSIZE;
  		entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
  		entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
  	}
  
  	/* verify and set the oversampling rate */
  	if (0 == osr)
  		osr = 1; /* minimum sampling rate is 1 */
  	entropy_collector->osr = osr;
bb5530e40   Stephan Mueller   crypto: jitterent...
675
676
677
678
679
  	/* fill the data pad with non-zero values */
  	jent_gen_entropy(entropy_collector);
  
  	return entropy_collector;
  }
dfc9fa919   Stephan Mueller   crypto: jitterent...
680
  void jent_entropy_collector_free(struct rand_data *entropy_collector)
bb5530e40   Stephan Mueller   crypto: jitterent...
681
  {
cea0a3c30   Markus Elfring   crypto: jitterent...
682
  	jent_zfree(entropy_collector->mem);
bb5530e40   Stephan Mueller   crypto: jitterent...
683
  	entropy_collector->mem = NULL;
cea0a3c30   Markus Elfring   crypto: jitterent...
684
  	jent_zfree(entropy_collector);
bb5530e40   Stephan Mueller   crypto: jitterent...
685
  }
dfc9fa919   Stephan Mueller   crypto: jitterent...
686
  int jent_entropy_init(void)
bb5530e40   Stephan Mueller   crypto: jitterent...
687
688
689
690
  {
  	int i;
  	__u64 delta_sum = 0;
  	__u64 old_delta = 0;
764428fe9   Stephan Müller   crypto: jitter - ...
691
  	unsigned int nonstuck = 0;
bb5530e40   Stephan Mueller   crypto: jitterent...
692
  	int time_backwards = 0;
bb5530e40   Stephan Mueller   crypto: jitterent...
693
  	int count_mod = 0;
d9d67c87a   Stephan Müller   crypto: jitter - ...
694
695
  	int count_stuck = 0;
  	struct rand_data ec = { 0 };
bb5530e40   Stephan Mueller   crypto: jitterent...
696

764428fe9   Stephan Müller   crypto: jitter - ...
697
698
  	/* Required for RCT */
  	ec.osr = 1;
bb5530e40   Stephan Mueller   crypto: jitterent...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
  	/* We could perform statistical tests here, but the problem is
  	 * that we only have a few loop counts to do testing. These
  	 * loop counts may show some slight skew and we produce
  	 * false positives.
  	 *
  	 * Moreover, only old systems show potentially problematic
  	 * jitter entropy that could potentially be caught here. But
  	 * the RNG is intended for hardware that is available or widely
  	 * used, but not old systems that are long out of favor. Thus,
  	 * no statistical tests.
  	 */
  
  	/*
  	 * We could add a check for system capabilities such as clock_getres or
  	 * check for CONFIG_X86_TSC, but it does not make much sense as the
  	 * following sanity checks verify that we have a high-resolution
  	 * timer.
  	 */
  	/*
  	 * TESTLOOPCOUNT needs some loops to identify edge systems. 100 is
  	 * definitely too little.
764428fe9   Stephan Müller   crypto: jitter - ...
720
721
  	 *
  	 * SP800-90B requires at least 1024 initial test cycles.
bb5530e40   Stephan Mueller   crypto: jitterent...
722
  	 */
764428fe9   Stephan Müller   crypto: jitter - ...
723
  #define TESTLOOPCOUNT 1024
bb5530e40   Stephan Mueller   crypto: jitterent...
724
725
726
727
  #define CLEARCACHE 100
  	for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
  		__u64 time = 0;
  		__u64 time2 = 0;
bb5530e40   Stephan Mueller   crypto: jitterent...
728
729
  		__u64 delta = 0;
  		unsigned int lowdelta = 0;
d9d67c87a   Stephan Müller   crypto: jitter - ...
730
  		int stuck;
bb5530e40   Stephan Mueller   crypto: jitterent...
731

d9d67c87a   Stephan Müller   crypto: jitter - ...
732
  		/* Invoke core entropy collection logic */
bb5530e40   Stephan Mueller   crypto: jitterent...
733
  		jent_get_nstime(&time);
d9d67c87a   Stephan Müller   crypto: jitter - ...
734
  		ec.prev_time = time;
764428fe9   Stephan Müller   crypto: jitter - ...
735
  		jent_lfsr_time(&ec, time, 0, 0);
bb5530e40   Stephan Mueller   crypto: jitterent...
736
737
738
739
740
  		jent_get_nstime(&time2);
  
  		/* test whether timer works */
  		if (!time || !time2)
  			return JENT_ENOTIME;
764428fe9   Stephan Müller   crypto: jitter - ...
741
  		delta = jent_delta(time, time2);
bb5530e40   Stephan Mueller   crypto: jitterent...
742
743
744
745
746
747
748
  		/*
  		 * test whether timer is fine grained enough to provide
  		 * delta even when called shortly after each other -- this
  		 * implies that we also have a high resolution timer
  		 */
  		if (!delta)
  			return JENT_ECOARSETIME;
d9d67c87a   Stephan Müller   crypto: jitter - ...
749
  		stuck = jent_stuck(&ec, delta);
bb5530e40   Stephan Mueller   crypto: jitterent...
750
751
752
753
754
755
756
757
758
  		/*
  		 * up to here we did not modify any variable that will be
  		 * evaluated later, but we already performed some work. Thus we
  		 * already have had an impact on the caches, branch prediction,
  		 * etc. with the goal to clear it to get the worst case
  		 * measurements.
  		 */
  		if (CLEARCACHE > i)
  			continue;
d9d67c87a   Stephan Müller   crypto: jitter - ...
759
760
  		if (stuck)
  			count_stuck++;
764428fe9   Stephan Müller   crypto: jitter - ...
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
  		else {
  			nonstuck++;
  
  			/*
  			 * Ensure that the APT succeeded.
  			 *
  			 * With the check below that count_stuck must be less
  			 * than 10% of the overall generated raw entropy values
  			 * it is guaranteed that the APT is invoked at
  			 * floor((TESTLOOPCOUNT * 0.9) / 64) == 14 times.
  			 */
  			if ((nonstuck % JENT_APT_WINDOW_SIZE) == 0) {
  				jent_apt_reset(&ec,
  					       delta & JENT_APT_WORD_MASK);
  				if (jent_health_failure(&ec))
  					return JENT_EHEALTH;
  			}
  		}
  
  		/* Validate RCT */
  		if (jent_rct_failure(&ec))
  			return JENT_ERCT;
d9d67c87a   Stephan Müller   crypto: jitter - ...
783

bb5530e40   Stephan Mueller   crypto: jitterent...
784
785
786
  		/* test whether we have an increasing timer */
  		if (!(time2 > time))
  			time_backwards++;
d9d67c87a   Stephan Müller   crypto: jitter - ...
787
  		/* use 32 bit value to ensure compilation on 32 bit arches */
bb5530e40   Stephan Mueller   crypto: jitterent...
788
789
790
791
792
793
794
795
796
797
  		lowdelta = time2 - time;
  		if (!(lowdelta % 100))
  			count_mod++;
  
  		/*
  		 * ensure that we have a varying delta timer which is necessary
  		 * for the calculation of entropy -- perform this check
  		 * only after the first loop is executed as we need to prime
  		 * the old_data value
  		 */
d9d67c87a   Stephan Müller   crypto: jitter - ...
798
799
800
801
  		if (delta > old_delta)
  			delta_sum += (delta - old_delta);
  		else
  			delta_sum += (old_delta - delta);
bb5530e40   Stephan Mueller   crypto: jitterent...
802
803
804
805
806
807
808
809
810
811
812
813
  		old_delta = delta;
  	}
  
  	/*
  	 * we allow up to three times the time running backwards.
  	 * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
  	 * if such an operation just happens to interfere with our test, it
  	 * should not fail. The value of 3 should cover the NTP case being
  	 * performed during our test run.
  	 */
  	if (3 < time_backwards)
  		return JENT_ENOMONOTONIC;
bb5530e40   Stephan Mueller   crypto: jitterent...
814
815
816
817
818
819
  
  	/*
  	 * Variations of deltas of time must on average be larger
  	 * than 1 to ensure the entropy estimation
  	 * implied with 1 is preserved
  	 */
d9d67c87a   Stephan Müller   crypto: jitter - ...
820
821
  	if ((delta_sum) <= 1)
  		return JENT_EVARVAR;
bb5530e40   Stephan Mueller   crypto: jitterent...
822
823
824
  
  	/*
  	 * Ensure that we have variations in the time stamp below 10 for at
d9d67c87a   Stephan Müller   crypto: jitter - ...
825
826
  	 * least 10% of all checks -- on some platforms, the counter increments
  	 * in multiples of 100, but not always
bb5530e40   Stephan Mueller   crypto: jitterent...
827
828
829
  	 */
  	if ((TESTLOOPCOUNT/10 * 9) < count_mod)
  		return JENT_ECOARSETIME;
d9d67c87a   Stephan Müller   crypto: jitter - ...
830
831
832
833
834
835
  	/*
  	 * If we have more than 90% stuck results, then this Jitter RNG is
  	 * likely to not work well.
  	 */
  	if ((TESTLOOPCOUNT/10 * 9) < count_stuck)
  		return JENT_ESTUCK;
bb5530e40   Stephan Mueller   crypto: jitterent...
836
837
  	return 0;
  }