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