Debian Codegraph @

lib/jitterentropy-internal.h

raw symbols SCIP
1 /*
2 * Non-physical true random number generator based on timing jitter.
3 *
4 * Copyright Stephan Mueller <[email protected]>, 2014 - 2026
5 *
6 * License
7 * =======
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, and the entire permission notice in its entirety,
14 * including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
20 * written permission.
21 *
22 * ALTERNATIVELY, this product may be distributed under the terms of
23 * the GNU General Public License, in which case the provisions of the GPL are
24 * required INSTEAD OF the above restrictions. (This clause is
25 * necessary due to a potential bad interaction between the GPL and
26 * the restrictions contained in a BSD-style copyright.)
27 *
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
31 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
39 * DAMAGE.
40 */
41
42 #ifndef _JITTERENTROPY_INTERNAL_H
43 #define _JITTERENTROPY_INTERNAL_H
44
45 #include "jitterentropy.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
52
53 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
54
55 #ifndef JENT_STUCK_INIT_THRES
56 /*
57 * Per default, not more than 90% of all measurements during initialization
58 * are allowed to be stuck.
59 *
60 * It is allowed to change this value as required for the intended environment.
61 */
62 #define JENT_STUCK_INIT_THRES(x) (((x) * 9) / 10)
63 #endif
64
65 /***************************************************************************
66 * Jitter RNG Configuration Section
67 *
68 * You may alter the following options
69 ***************************************************************************/
70
71 /*
72 * Enable timer-less timer support with JENT_CONF_ENABLE_INTERNAL_TIMER
73 *
74 * In case the hardware is identified to not provide a high-resolution time
75 * stamp, this option enables a built-in high-resolution time stamp mechanism.
76 *
77 * The timer-less noise source is based on threads. This noise source requires
78 * the linking with the POSIX threads library. I.e. the executing environment
79 * must offer POSIX threads. If this option is disabled, no linking
80 * with the POSIX threads library is needed.
81 */
82
83 /*
84 * Shall the LAG predictor health test be enabled?
85 */
86 #define JENT_HEALTH_LAG_PREDICTOR
87
88 /*
89 * Shall the jent_memaccess use a (statistically) random selection for the
90 * memory to update?
91 */
92 #ifndef JENT_TEST_MEASURE_RAW_MEMORY_ACCESS
93 #define JENT_RANDOM_MEMACCESS
94 #else
95 #undef JENT_RANDOM_MEMACCESS
96 #endif
97
98 /*
99 * Mask specifying the number of bits of the raw entropy data of the time delta
100 * value used for the APT.
101 *
102 * This value implies that for the APT, only the bits specified by
103 * JENT_APT_MASK are taken. This was suggested in a draft IG D.K resolution 22
104 * provided by NIST, but further analysis
105 * (https://www.untruth.org/~josh/sp80090b/CMUF%20EWG%20Draft%20IG%20D.K%20Comments%20D10.pdf)
106 * suggests that this truncation / translation generally results in a health
107 * test with both a higher false positive rate (because multiple raw symbols
108 * map to the same symbol within the health test) and a lower statistical power
109 * when the APT cutoff is selected based on the apparent truncated entropy
110 * (i.e., truncation generally makes the test worse). NIST has since withdrawn
111 * this draft and stated that they will not propose truncation prior to
112 * health testing.
113 * Because the general tendency of such truncation to make the health test
114 * worse the default value is set such that no data is masked out and this
115 * should only be changed if a hardware-specific analysis suggests that some
116 * other mask setting is beneficial.
117 * The mask is applied to a time stamp where the GCD is already divided out, and thus no
118 * "non-moving" low-order bits are present.
119 */
120 #define JENT_APT_MASK (UINT64_C(0xffffffffffffffff))
121
122 /*
123 * This parameter defines the default memory buffer size for the memory access
124 * loop. This value implies a memory size of 1 << JENT_DEFAULT_MEMORY_BITS.
125 *
126 * It is permissible to configure this value differently at compile time if the
127 * observed entropy rate is too small.
128 *
129 * This parameter is applied if the Jitter RNG:
130 * - is not instantiated with JENT_MAX_MEMSIZE_* flag
131 * - cannot determine the L1 cache size during starup.
132 */
133 #ifndef JENT_DEFAULT_MEMORY_BITS
134 # define JENT_DEFAULT_MEMORY_BITS 18
135 #endif
136
137 /* This parameter establishes the multiplicative factor that the desired
138 * memory region size should be larger than the observed cache size; the
139 * multiplicative factor is 2^JENT_CACHE_SHIFT_BITS.
140 * Set this to 0 if the desired memory region should be at least as large as
141 * the cache. If one wants most of the memory updates to result in a memory
142 * update, then this value should be at least 1.
143 * If the memory updates should dominantly result in a memory update, then
144 * the value should be set to at least 3.
145 * The actual size of the memory region is never larger than requested by
146 * the passed in JENT_MAX_MEMSIZE_* flag (if provided) or JENT_MEMORY_SIZE
147 * (if no JENT_MAX_MEMSIZE_* flag is provided).
148 */
149 #ifndef JENT_CACHE_SHIFT_BITS
150 #define JENT_CACHE_SHIFT_BITS 0
151 #endif
152
153 /*
154 * Memory access loop count: This value defines the default memory access loop
155 * count. The memory access loop is one of the hearts of the Jitter RNG. The
156 * number of loop counts has a direct impact on the entropy rate.
157 *
158 * It is permissible to configure this value differently at compile time if the
159 * observed entropy rate is too small.
160 *
161 * NOTE: When you modify this value, you are directly altering the behavior of
162 * the noise source. Make sure you fully understand what you do. If you want to
163 * individually measure the memory access loop entropy rate, use the
164 * jitterentropy-hashtime tool with the command line option of --memaccess.
165 */
166 #ifndef JENT_MEM_ACC_LOOP_DEFAULT
167 #define JENT_MEM_ACC_LOOP_DEFAULT 128
168 #endif
169
170 /*
171 * Memory access loop initialization count: This value defines the multiplier of
172 * the memory access loop count during initialization phase, when the memory
173 * access based loop is the sole entropy provider. Typically a higher iteration
174 * count is necessary to take enough time.
175 *
176 * It is permissible to configure this value differently at compile time if the
177 * observed entropy rate is too small.
178 *
179 * NOTE: When you modify this value, you are directly altering the behavior of
180 * the noise source during NTG.1 initialization.
181 * Make sure you fully understand what you do. If you want to
182 * individually measure the hash loop entropy rate, use the
183 * jitterentropy-hashtime tool with the command line option of --hashloop.
184 */
185 #ifndef JENT_MEM_ACC_LOOP_INIT
186 #define JENT_MEM_ACC_LOOP_INIT 3
187 #endif
188
189 /*
190 * Hash loop count: This value defines the default hash loop count. The hash
191 * loop is one of the hearts of the Jitter RNG. The number of loop counts has a
192 * direct impact on the entropy rate.
193 *
194 * It is permissible to configure this value differently at compile time if the
195 * observed entropy rate is too small.
196 *
197 * This value is applied if the Jitter RNG:
198 * - is not instantiated with JENT_HASHLOOP_* flag
199 *
200 * NOTE: When you modify this value, you are directly altering the behavior of
201 * the noise source. Make sure you fully understand what you do. If you want to
202 * individually measure the hash loop entropy rate, use the
203 * jitterentropy-hashtime tool with the command line option of --hashloop.
204 */
205 #ifndef JENT_HASH_LOOP_DEFAULT
206 #define JENT_HASH_LOOP_DEFAULT 1
207 #endif
208
209 /*
210 * Hash loop initialization count: This value defines the multiplier of the
211 * hash loop count during initialization phase, when the SHA-3-based loop is the
212 * sole entropy provider. Typically a higher iteration count is necessary to
213 * take enough time.
214 *
215 * It is permissible to configure this value differently at compile time if the
216 * observed entropy rate is too small.
217 *
218 * NOTE: When you modify this value, you are directly altering the behavior of
219 * the noise source during NTG.1 initialization.
220 * Make sure you fully understand what you do. If you want to
221 * individually measure the hash loop entropy rate, use the
222 * jitterentropy-hashtime tool with the command line option of --hashloop.
223 */
224 #ifndef JENT_HASH_LOOP_INIT
225 #define JENT_HASH_LOOP_INIT 3
226 #endif
227
228 /*
229 * Oversampling rate: This value defines the default oversampling rate. The
230 * OSR defines the global heuristic entropy rate of 1/OSR.
231 *
232 * It is permissible to configure this value differently at compile time.
233 *
234 * This value is applied if the Jitter RNG:
235 * - is instantiated with an OSR of 0 provided to the initialization API
236 *
237 * During initial health tests or jent_read_entropy_safe, the RNG instance
238 * may re-initialize with an incremented OSR, which stops at JENT_OSR_MAX
239 * and returns a failure condition. Otherwise this would run "forever".
240 * Set another value instead of the default 20, if necessary.
241 */
242 #ifndef JENT_MIN_OSR
243 #define JENT_MIN_OSR 3
244 #endif
245
246 #ifndef JENT_MAX_OSR
247 #define JENT_MAX_OSR 20
248 #endif
249
250 /***************************************************************************
251 * Jitter RNG State Definition Section
252 ***************************************************************************/
253
254 #define JENT_SHA3_256_SIZE_DIGEST_BITS 256
255 #define JENT_SHA3_256_SIZE_DIGEST (JENT_SHA3_256_SIZE_DIGEST_BITS >> 3)
256
257 /*
258 * The output 256 bits can receive more than 256 bits of min entropy,
259 * of course, but the 256-bit output of XDRBG-256(M) can only
260 * asymptotically approach 256 bits of min entropy, not attain that bound.
261 * Random maps will tend to have output collisions, which reduces the creditable
262 * output entropy (that is what SP 800-90B Section 3.1.5.1.2 attempts to bound).
263 *
264 * The value "64" is justified in Appendix A.4 of the current 90C draft,
265 * and aligns with NIST's in "epsilon" definition in this document, which is
266 * that a string can be considered "full entropy" if you can bound the min
267 * entropy in each bit of output to at least 1-epsilon, where epsilon is
268 * required to be <= 2^(-32).
269 *
270 * The additional bit for the safety factor comes from the fact that the
271 * we want the internal state variable to have 256 + 64 bit of entropy. As this
272 * state variable is generated by one SHAKE256 operation of the input data, we
273 * loose one bit of entropy due to the SHAKE. For more details, see the NTG.1
274 * analysis provided with the big documentation.
275 */
276 #define ENTROPY_SAFETY_FACTOR (64 + 1)
277
278 enum jent_startup_state {
279 jent_startup_completed,
280 jent_startup_sha3,
281 jent_startup_memory
282 };
283
284 /* The entropy pool */
285 struct rand_data
286 {
287 /* all data values that are vital to maintain the security
288 * of the RNG are marked as SENSITIVE. A user must not
289 * access that information while the RNG executes its loops to
290 * calculate the next random value. */
291 void *hash_state; /* SENSITIVE hash state entropy pool */
292 uint64_t prev_time; /* SENSITIVE Previous time stamp */
293 #define DATA_SIZE_BITS (JENT_SHA3_256_SIZE_DIGEST_BITS)
294
295 #ifndef JENT_HEALTH_LAG_PREDICTOR
296 uint64_t last_delta; /* SENSITIVE stuck test */
297 uint64_t last_delta2; /* SENSITIVE stuck test */
298 #endif /* JENT_HEALTH_LAG_PREDICTOR */
299
300 unsigned int flags; /* Flags used to initialize */
301 unsigned int osr; /* Oversampling rate */
302
303 /* Initialization state supporting AIS 20/31 NTG.1 */
304 enum jent_startup_state startup_state;
305
306 /* The step size should be larger than the cacheline size. */
307 #ifndef JENT_MEMORY_BLOCKSIZE
308 # define JENT_MEMORY_BLOCKSIZE 128
309 #endif
310 #define JENT_MEMORY_BLOCKS(ec) ((ec->memmask + 1) / JENT_MEMORY_BLOCKSIZE)
311
312 unsigned char *mem; /* Memory access location with size of
313 * memmask + 1 */
314
315 uint32_t memmask; /* Memory mask (size of memory - 1) */
316 unsigned int memlocation; /* Pointer to byte in *mem */
317 unsigned int memaccessloops; /* Number of memory accesses per random
318 * bit generation */
319
320 unsigned int hashloopcnt; /* Hash loop count */
321
322 /* Repetition Count Test */
323 unsigned int rct_count; /* Number of stuck values */
324 unsigned short rct_cutoff; /* RCT intermittent cutoff */
325 unsigned short rct_cutoff_permanent; /* RCT permanent cutoff */
326
327 /* Adaptive Proportion Test for a significance level of 2^-30 */
328 unsigned int apt_cutoff; /* Intermittent health test failure */
329 unsigned int apt_cutoff_permanent; /* Permanent health test failure */
330 #define JENT_APT_WINDOW_SIZE 512 /* Data window size */
331 unsigned int apt_observations; /* Number of collected observations in
332 * current window. */
333 unsigned int apt_count; /* The number of times the reference
334 * symbol been encountered in the
335 * window. */
336 uint64_t apt_base; /* APT base reference */
337 unsigned int health_failure; /* Permanent health failure */
338
339 /* RCT with memory */
340 unsigned short rct_mem_ctr; /* Loop iteration for generating random bytes */
341 unsigned short rct_mem_nosr; /* Minimum iteration count of measure jitter loop */
342 unsigned short rct_mem_count; /* Number of stuck values */
343 unsigned short rct_mem_cutoff; /* RCT intermittent cutoff */
344 unsigned short rct_mem_cutoff_permanent; /* RCT permanent cutoff */
345
346 unsigned int apt_base_set:1; /* APT base reference set? */
347 unsigned int fips_enabled:1;
348 unsigned int enable_notime:1; /* Use internal high-res timer */
349 unsigned int max_mem_set:1; /* Maximum memory configured by user */
350 unsigned int in_recovery:1; /* Flag to indicate a recovery op. */
351
352 #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
353 volatile uint8_t notime_interrupt; /* indicator to interrupt ctr */
354 volatile uint64_t notime_timer; /* high-res timer mock-up */
355 uint64_t notime_prev_timer; /* previous timer value */
356 void *notime_thread_ctx; /* register thread data */
357 #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */
358
359 uint64_t jent_common_timer_gcd; /* Common divisor for all time deltas */
360
361 #ifdef JENT_HEALTH_LAG_PREDICTOR
362 /* Lag predictor test to look for re-occurring patterns. */
363
364 /* The lag global cutoff selected based on the selection of osr. */
365 unsigned int lag_global_cutoff;
366
367 /* The lag local cutoff selected based on the selection of osr. */
368 unsigned int lag_local_cutoff;
369
370 /*
371 * The number of times the lag predictor was correct. Compared to the
372 * global cutoff.
373 */
374 unsigned int lag_prediction_success_count;
375
376 /*
377 * The size of the current run of successes. Compared to the local
378 * cutoff.
379 */
380 unsigned int lag_prediction_success_run;
381
382 /*
383 * The total number of collected observations since the health test was
384 * last reset.
385 */
386 unsigned int lag_best_predictor;
387
388 /*
389 * The total number of collected observations since the health test was
390 * last reset.
391 */
392 unsigned int lag_observations;
393
394 /*
395 * This is the size of the window used by the predictor. The predictor
396 * is reset between windows.
397 */
398 #define JENT_LAG_WINDOW_SIZE (1U<<17)
399
400 /*
401 * The amount of history to base predictions on. This must be a power
402 * of 2. Must be 4 or greater.
403 */
404 #define JENT_LAG_HISTORY_SIZE 8
405 #define JENT_LAG_MASK (JENT_LAG_HISTORY_SIZE - 1)
406
407 /* The delta history for the lag predictor. */
408 uint64_t lag_delta_history[JENT_LAG_HISTORY_SIZE];
409
410 /* The scoreboard that tracks how successful each predictor lag is. */
411 unsigned int lag_scoreboard[JENT_LAG_HISTORY_SIZE];
412 #endif /* JENT_HEALTH_LAG_PREDICTOR */
413 };
414
415 #ifdef __cplusplus
416 }
417 #endif
418
419 #endif /* _JITTERENTROPY_INTERNAL_H */