Debian Codegraph @

lib/jitterentropy-noise.c

raw symbols SCIP
1 /* Jitter RNG: Noise Sources
2 *
3 * Copyright (C) 2021 - 2026, Stephan Mueller <[email protected]>
4 *
5 * License: see LICENSE file in root directory
6 *
7 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
8 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
10 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
11 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
12 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
13 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
14 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
15 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
16 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
17 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
18 * DAMAGE.
19 */
20
21 #include "jitterentropy-noise.h"cxx . . $ `<file>/lib/jitterentropy-noise.h`/
22 #include "jitterentropy-health.h"cxx . . $ `<file>/lib/jitterentropy-health.h`/
23 #include "jitterentropy-timer.h"cxx . . $ `<file>/lib/jitterentropy-timer.h`/
24 #include "jitterentropy-sha3.h"cxx . . $ `<file>/lib/jitterentropy-sha3.h`/
25
26 /***************************************************************************
27 * Noise sources
28 ***************************************************************************/
29
30 /*
31 * Structure of the intermediary buffer:
32 *
33 * | time delta | domain separator | hash loop hash | 0 ... |
34 *
35 * Note, this buffer is truncted to the current rate size which implies that
36 * data with entropy must be placed at a location to guarantee they are not
37 * truncated off.
38 */
39 #define JENT_SIZEOF_TIMEDELTAcxx . . $ `lib/jitterentropy-noise.c:39:9`! (sizeof(uint64_t))
40 #define JENT_SIZEOF_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:40:9`! (sizeof(uint8_t))
41 #define JENT_SIZEOF_HASH_BLOCKcxx . . $ `lib/jitterentropy-noise.c:41:9`! (JENT_SHA3_256_SIZE_DIGESTcxx . . $ `lib/jitterentropy-internal.h:255:9`!)
42 #define JENT_SIZEOF_INTERMEDIARY_DATAcxx . . $ `lib/jitterentropy-noise.c:42:9`! (JENT_SIZEOF_TIMEDELTAcxx . . $ `lib/jitterentropy-noise.c:39:9`! + \
43 JENT_SIZEOF_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:40:9`! + \
44 JENT_SIZEOF_HASH_BLOCKcxx . . $ `lib/jitterentropy-noise.c:41:9`!)
45
46 /* Intemediary is as big as the maximum rate it will be read with */
47 #define JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`! (JENT_SHA3_256_SIZE_BLOCKcxx . . $ `lib/jitterentropy-sha3.h:32:9`!)
48
49 #define JENT_OFFSET_TIMEDELTAcxx . . $ `lib/jitterentropy-noise.c:49:9`! (0)
50 #define JENT_OFFSET_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:50:9`! \
51 (JENT_OFFSET_TIMEDELTAcxx . . $ `lib/jitterentropy-noise.c:49:9`! + JENT_SIZEOF_TIMEDELTAcxx . . $ `lib/jitterentropy-noise.c:39:9`!)
52 #define JENT_OFFSET_HASH_BLOCKcxx . . $ `lib/jitterentropy-noise.c:52:9`! \
53 (JENT_OFFSET_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:50:9`! + JENT_SIZEOF_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:40:9`!)
54
55 /**
56 * Insert a data block into the entropy pool
57 *
58 * The function inserts the intermediary buffer and the time delta together
59 * into the entropy pool. The intermediary buffer is of exact the SHA3-256 rate
60 * size to ensure that always one Keccak operation is triggered.
61 *
62 * Note, this function also clears the intermediary buffer immediately after it
63 * was injected into the entropy pool.
64 *
65 * @param[in] ec Reference to entropy collector
66 * @param[in] time_delta the time delta raw entropy value
67 * @param[in] intermediary buffer that may hold other data
68 */
69 static void jent_hash_insertcxx . . $ jent_hash_insert(946ccdf2e067f5ec).(struct rand_datacxx . . $ rand_data# *ec, uint64_tcxx . . $ uint64_t# time_delta,
70 uint8_tcxx . . $ uint8_t# intermediary[JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`!])
71 {
72 /*
73 * Insert the time stamp into the intermediary buffer after the message
74 * digest of the intermediate data.
75 */
76 memcpycxx . . $ memcpy(cc817cb175f282d).(intermediary + JENT_OFFSET_TIMEDELTAcxx . . $ `lib/jitterentropy-noise.c:49:9`!,
77 (uint8_tcxx . . $ uint8_t# *)&time_delta, sizeof(uint64_tcxx . . $ uint64_t#));
78
79 BUILD_BUG_ONcxx . . $ `lib/jitterentropy-internal.h:51:9`!(JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`! < JENT_SIZEOF_INTERMEDIARY_DATAcxx . . $ `lib/jitterentropy-noise.c:42:9`!);
80
81 /*
82 * Inject the data from the intermediary buffer, including the hash we
83 * are using for timing, and the time stamp. Only the time is considered
84 * to contain any entropy. The intermediary buffer is exactly rate-size
85 * to always cause a Keccak operation.
86 *
87 * This operation seeds the XDRBG conditioning component as follows:
88 *
89 * XDRBG reseed:
90 * V ← XOF( encode(( V' || seed ), α, 1), |V| )
91 *
92 * where
93 *
94 * seed ← (intermediary_0 || intermediary_1 || ... ||
95 * intermediary_[(osr + safety_factor)*256])
96 */
97 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(ec->hash_statecxx . . $ rand_data#hash_state., intermediary,
98 jent_sha3_ratecxx . . $ jent_sha3_rate(6769d0e399bf81e3).(ec->hash_statecxx . . $ rand_data#hash_state.));
99 jent_memset_securecxx . . $ jent_memset_secure(5500c4293b1bbef0).(intermediary, JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`!);
100 }
101
102 /**
103 * Hash loop noise source -- this is the noise source based on the CPU
104 * execution time jitter
105 *
106 * @param[in] ec entropy collector struct
107 * @param[in] loop_cnt if a value not equal to 0 is set, use the given value as
108 * number of loops to perform the hash operation
109 * @param[in] stuck Is the time delta identified as stuck?
110 */
111 static void jent_hash_loopcxx . . $ jent_hash_loop(f4413f335a0bb650).(struct rand_datacxx . . $ rand_data# *ec,
112 uint8_tcxx . . $ uint8_t# intermediary[JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`!],
113 uint64_tcxx . . $ uint64_t# loop_cnt)
114 {
115 HASH_CTX_ON_STACKcxx . . $ `lib/jitterentropy-sha3.h:53:9`!(ctx);
116 uint8_tcxx . . $ uint8_t# *digest = intermediary + JENT_OFFSET_HASH_BLOCKcxx . . $ `lib/jitterentropy-noise.c:52:9`!;
117 uint64_tcxx . . $ uint64_t# j = 0;
118
119 /*
120 * allow caller to set the counter
121 */
122 uint64_tcxx . . $ uint64_t# hash_loop_cnt = loop_cnt ? loop_cnt : ec->hashloopcntcxx . . $ rand_data#hashloopcnt.;
123
124 BUILD_BUG_ONcxx . . $ `lib/jitterentropy-internal.h:51:9`!(JENT_HASH_LOOP_DEFAULTcxx . . $ `lib/jitterentropy-internal.h:206:9`! < 1);
125 BUILD_BUG_ONcxx . . $ `lib/jitterentropy-internal.h:51:9`!(JENT_HASH_LOOP_INITcxx . . $ `lib/jitterentropy-internal.h:225:9`! < 1);
126
127 jent_sha3_256_initcxx . . $ jent_sha3_256_init(86d95783d3e80d87).(&ctx);
128
129 /*
130 * This loop fills a buffer which is injected into the entropy pool.
131 * The main reason for this loop is to execute something over which we
132 * can perform a timing measurement. The injection of the resulting
133 * data into the pool is performed to ensure the result is used and
134 * the compiler cannot optimize the loop away in case the result is not
135 * used at all. Yet that data is considered "additional information"
136 * considering the terminology from SP800-90A without any entropy.
137 *
138 * Note, it does not matter which or how much data you inject, we are
139 * interested in one Keccack1600 compression operation performed with
140 * the sha3_final.
141 */
142 for (j = 0; j < hash_loop_cnt; j++) {
143 /* Limit data size to prevent Keccak operation during update */
144 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx, digest, JENT_SHA3_256_SIZE_DIGESTcxx . . $ `lib/jitterentropy-internal.h:255:9`!);
145 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx, (uint8_tcxx . . $ uint8_t# *)&ec->rct_countcxx . . $ rand_data#rct_count.,
146 sizeof(ec->rct_countcxx . . $ rand_data#rct_count.));
147 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx, (uint8_tcxx . . $ uint8_t# *)&ec->apt_cutoffcxx . . $ rand_data#apt_cutoff.,
148 sizeof(ec->apt_cutoffcxx . . $ rand_data#apt_cutoff.));
149 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx, (uint8_tcxx . . $ uint8_t# *)&ec->apt_observationscxx . . $ rand_data#apt_observations.,
150 sizeof(ec->apt_observationscxx . . $ rand_data#apt_observations.));
151 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx, (uint8_tcxx . . $ uint8_t# *)&ec->apt_countcxx . . $ rand_data#apt_count.,
152 sizeof(ec->apt_countcxx . . $ rand_data#apt_count.));
153 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx,(uint8_tcxx . . $ uint8_t# *) &ec->apt_basecxx . . $ rand_data#apt_base.,
154 sizeof(ec->apt_basecxx . . $ rand_data#apt_base.));
155 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx,(uint8_tcxx . . $ uint8_t# *) &ec->rct_mem_countcxx . . $ rand_data#rct_mem_count.,
156 sizeof(ec->rct_mem_countcxx . . $ rand_data#rct_mem_count.));
157 jent_sha3_updatecxx . . $ jent_sha3_update(4f2271f98ec6a915).(&ctx, (uint8_tcxx . . $ uint8_t# *)&j, sizeof(uint64_tcxx . . $ uint64_t#));
158 jent_sha3_finalcxx . . $ jent_sha3_final(e07c871ace1d2160).(&ctx, digest);
159 }
160
161 jent_memset_securecxx . . $ jent_memset_secure(5500c4293b1bbef0).(&ctx, JENT_SHA_MAX_CTX_SIZEcxx . . $ `lib/jitterentropy-sha3.h:52:9`!);
162 }
163
164 static inline uint32_tcxx . . $ uint32_t# uint32rotlcxx . . $ uint32rotl(5bb05f763058421f).(const uint32_tcxx . . $ uint32_t# x, int k)
165 {
166 return (x << k) | (x >> (32 - k));
167 }
168
169 static inline uint32_tcxx . . $ uint32_t# xoshiro128starstarcxx . . $ xoshiro128starstar(c22c494133c14864).(uint32_tcxx . . $ uint32_t# *s)
170 {
171 const uint32_tcxx . . $ uint32_t# result = uint32rotlcxx . . $ uint32rotl(5bb05f763058421f).(s[1] * 5, 7) * 9;
172 const uint32_tcxx . . $ uint32_t# t = s[1] << 9;
173
174 s[2] ^= s[0];
175 s[3] ^= s[1];
176 s[1] ^= s[2];
177 s[0] ^= s[3];
178
179 s[2] ^= t;
180
181 s[3] = uint32rotlcxx . . $ uint32rotl(5bb05f763058421f).(s[3], 11);
182
183 return result;
184 }
185
186 /**
187 * Memory access noise source -- this is the noise source based on the memory
188 * access time jitter
189 *
190 * @param[in] ec entropy collector struct
191 * @param[in] loop_cnt if a value not equal to 0 is set, use the given value as
192 * number of loops to perform the hash operation
193 */
194 static void jent_memaccess_pseudorandomcxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).(struct rand_datacxx . . $ rand_data# *ec, uint64_tcxx . . $ uint64_t# loop_cnt,
195 uint64_tcxx . . $ uint64_t# *current_delta)
196 {
197 uint64_tcxx . . $ uint64_t# i = 0, time_now_start = 0, time_now_end = 0, tmp_delta = 0;
198 unioncxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).$anonymous_type_0# {
199 uint32_tcxx . . $ uint32_t# ucxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).$anonymous_type_0#u.[4];
200 uint8_tcxx . . $ uint8_t# bcxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).$anonymous_type_0#b.[sizeof(uint32_tcxx . . $ uint32_t#) * 4];
201 } prngState = { .u = {0x8e93eec0, 0xce65608a, 0xa8d46b46, 0xe83cef69} };
202 uint32_tcxx . . $ uint32_t# addressMask;
203
204 /*
205 * allow caller to set the counter
206 */
207 uint64_tcxx . . $ uint64_t# mem_loop_cnt = loop_cnt ? loop_cnt : ec->memaccessloopscxx . . $ rand_data#memaccessloops.;
208
209 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == ec || NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == ec->memcxx . . $ rand_data#mem.)
210 return;
211 addressMask = ec->memmaskcxx . . $ rand_data#memmask.;
212
213 /*
214 * Mix the current data into prngState
215 *
216 * Any time you see a PRNG in a noise source, you should be concerned.
217 *
218 * The PRNG doesn’t directly produce the raw noise, it just adjusts the
219 * location being updated. The timing of the update is part of the raw
220 * sample. The main thing this process gets you isn’t better
221 * “per-update” timing, it gets you mostly independent “per-update”
222 * timing, so we can now benefit from the Central Limit Theorem!
223 */
224 for (i = 0; i < sizeof(prngState); i++) {
225 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now_start);
226 prngState.bcxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).$anonymous_type_0#b.[i] ^= (uint8_tcxx . . $ uint8_t#)(time_now_start & 0xff);
227 }
228
229 /*
230 * Obtain the start time of the timing measurement when requested by
231 * the caller.
232 */
233 if (current_delta)
234 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now_start);
235
236 for (i = 0; i < mem_loop_cnt; i++) {
237 /* Take PRNG output to find the memory location to update. */
238 unsigned char *tmpval = ec->memcxx . . $ rand_data#mem. +
239 (xoshiro128starstarcxx . . $ xoshiro128starstar(c22c494133c14864).(prngState.ucxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).$anonymous_type_0#u.) &
240 addressMask);
241
242 /*
243 * memory access: just add 1 to one byte,
244 * wrap at 255 -- memory access implies read
245 * from and write to memory location
246 */
247 *tmpval = (unsigned char)((*tmpval + 1) & 0xff);
248 }
249
250 /*
251 * Calculate the execution time by measuring the end time and obtain
252 * the time delta.
253 */
254 if (current_delta) {
255 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now_end);
256 tmp_delta += jent_deltacxx . . $ jent_delta(80bad76eed2d2579).(time_now_start, time_now_end) /
257 ec->jent_common_timer_gcdcxx . . $ rand_data#jent_common_timer_gcd.;
258 *current_delta = tmp_delta;
259 }
260 }
261
262 /**
263 * Memory Access noise source -- this is a noise source based on variations in
264 * memory access times
265 *
266 * This function performs memory accesses which will add to the timing
267 * variations due to an unknown amount of CPU wait states that need to be
268 * added when accessing memory. The memory size should be larger than the L1
269 * caches as outlined in the documentation and the associated testing.
270 *
271 * The L1 cache has a very high bandwidth, albeit its access rate is usually
272 * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
273 * variations as the CPU has hardly to wait. Starting with L2, significant
274 * variations are added because L2 typically does not belong to the CPU any more
275 * and therefore a wider range of CPU wait states is necessary for accesses.
276 * L3 and real memory accesses have even a wider range of wait states. However,
277 * to reliably access either L3 or memory, the ec->mem memory must be quite
278 * large which is usually not desirable.
279 *
280 * @param[in] ec Reference to the entropy collector with the memory access data -- if
281 * the reference to the memory block to be accessed is NULL, this noise
282 * source is disabled
283 * @param[in] loop_cnt if a value not equal to 0 is set, use the given value as
284 * number of loops to perform the hash operation
285 */
286 static void jent_memaccess_deterministiccxx . . $ jent_memaccess_deterministic(46fd2d4b8959ab6c).(struct rand_datacxx . . $ rand_data# *ec,
287 uint64_tcxx . . $ uint64_t# loop_cnt,
288 uint64_tcxx . . $ uint64_t# *current_delta)
289 {
290 uint64_tcxx . . $ uint64_t# time_now_start = 0, time_now_end = 0, tmp_delta = 0;
291 unsigned int wrap = 0;
292 uint64_tcxx . . $ uint64_t# i = 0;
293
294 /*
295 * allow caller to set the counter
296 */
297 uint64_tcxx . . $ uint64_t# mem_loop_cnt = loop_cnt ? loop_cnt : ec->memaccessloopscxx . . $ rand_data#memaccessloops.;
298
299 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == ec || NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == ec->memcxx . . $ rand_data#mem.)
300 return;
301 wrap = ec->memmaskcxx . . $ rand_data#memmask. + 1;
302
303 if (current_delta)
304 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now_start);
305
306 for (i = 0; i < mem_loop_cnt; i++) {
307 unsigned char *tmpval = ec->memcxx . . $ rand_data#mem. + ec->memlocationcxx . . $ rand_data#memlocation.;
308
309 /*
310 * memory access: just add 1 to one byte,
311 * wrap at 255 -- memory access implies read
312 * from and write to memory location
313 */
314 *tmpval = (unsigned char)((*tmpval + 1) & 0xff);
315
316 /*
317 * Addition of memblocksize - 1 to pointer
318 * with wrap around logic to ensure that every
319 * memory location is hit evenly
320 */
321 ec->memlocationcxx . . $ rand_data#memlocation. = ec->memlocationcxx . . $ rand_data#memlocation. + JENT_MEMORY_BLOCKSIZEcxx . . $ `lib/jitterentropy-internal.h:308:10`! - 1;
322 ec->memlocationcxx . . $ rand_data#memlocation. = ec->memlocationcxx . . $ rand_data#memlocation. % wrap;
323 }
324
325 if (current_delta) {
326 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now_end);
327 tmp_delta += jent_deltacxx . . $ jent_delta(80bad76eed2d2579).(time_now_start, time_now_end) /
328 ec->jent_common_timer_gcdcxx . . $ rand_data#jent_common_timer_gcd.;
329 *current_delta = tmp_delta;
330 }
331 }
332
333 /***************************************************************************
334 * Start of entropy processing logic
335 ***************************************************************************/
336 /**
337 * This is the heart of the entropy generation for NTG.1 startup, invoking only
338 * the memory access noise source: calculate time deltas and use the CPU jitter
339 * in the time deltas. The jitter is injected into the entropy pool.
340 *
341 * @param[in] ec Reference to entropy collector
342 * @param[in] loop_cnt see jent_hash_time
343 * @param[out] ret_current_delta Test interface: return time delta - may be NULL
344 *
345 * @return: result of stuck test
346 */
347 unsigned int jent_measure_jitter_ntg1_memaccesscxx . . $ jent_measure_jitter_ntg1_memaccess(135d38c3d516dec8).(struct rand_datacxx . . $ rand_data# *ec,
348 uint64_tcxx . . $ uint64_t# loop_cnt,
349 uint64_tcxx . . $ uint64_t# *ret_current_delta)
350 {
351 uint8_tcxx . . $ uint8_t# intermediary[JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`!] = { 0 };
352 uint64_tcxx . . $ uint64_t# current_delta = 0;
353 unsigned int stuck;
354
355 /*
356 * Now call the memory noise source with tripple the default iteration
357 * count considering this is the only noise source.
358 *
359 * The call returns the execution time delta.
360 *
361 * For the NTG.1 discussion, the following is of interest. NTG.1
362 * mandates that 2 separate noise sources are used. The hash loop
363 * operation uses the variations from the CPU instructions and the L1
364 * cache data. The memory access loop here shall deliver variations from
365 * the L2, L3 caches or RAM. To ensure that as little as possible L1
366 * operations are present, the xoshiro128starstar operation is not used.
367 * The deterministic operation has less instructions and less L1
368 * accesses. Therefore, the deterministic operation only is used here.
369 *
370 * Furthermore, the increase of the memory access loop by 3 (the value
371 * below is added to the original memory access loop) to ensure that
372 * sufficient variations from L2 are collected to meet the NTG.1
373 * requirement of at least 240 bits of entropy from the L2/L3/RAM
374 * accesses.
375 */
376 jent_memaccess_deterministiccxx . . $ jent_memaccess_deterministic(46fd2d4b8959ab6c).(
377 ec, loop_cnt ? loop_cnt :
378 ec->memaccessloopscxx . . $ rand_data#memaccessloops. * JENT_MEM_ACC_LOOP_INITcxx . . $ `lib/jitterentropy-internal.h:186:9`!,
379 &current_delta);
380
381 /*
382 * Check whether we have a stuck measurement - and apply the health
383 * tests.
384 */
385 stuck = jent_stuckcxx . . $ jent_stuck(deafe999b085c65a).(ec, current_delta);
386
387 /* Domain separation */
388 intermediary[JENT_OFFSET_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:50:9`!] = 0x01;
389
390 /* Insert the data into the entropy pool */
391 jent_hash_insertcxx . . $ jent_hash_insert(946ccdf2e067f5ec).(ec, current_delta, intermediary);
392
393 /* return the raw entropy value */
394 if (ret_current_delta)
395 *ret_current_delta = current_delta;
396
397 return stuck;
398 }
399
400 /**
401 * This is the heart of the entropy generation for NTG.1 startup, invoking only
402 * the hash loop noise source: calculate time deltas and use the CPU jitter in
403 * the time deltas. The jitter is injected into the entropy pool.
404 *
405 * @param[in] ec Reference to entropy collector
406 * @param[in] loop_cnt see jent_hash_loop
407 * @param[out] ret_current_delta Test interface: return time delta - may be NULL
408 *
409 * @return: result of stuck test
410 */
411 unsigned int jent_measure_jitter_ntg1_sha3cxx . . $ jent_measure_jitter_ntg1_sha3(135d38c3d516dec8).(struct rand_datacxx . . $ rand_data# *ec,
412 uint64_tcxx . . $ uint64_t# loop_cnt,
413 uint64_tcxx . . $ uint64_t# *ret_current_delta)
414 {
415 uint8_tcxx . . $ uint8_t# intermediary[JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`!] = { 0 };
416 uint64_tcxx . . $ uint64_t# time_now = 0;
417 uint64_tcxx . . $ uint64_t# current_delta = 0;
418 unsigned int stuck;
419
420 /*
421 * Get time stamp to only measure the execution time of the hash loop
422 * to make this part an independent entropy source (even excluding the
423 * SHA3 update to insert the data into the entropy pool).
424 */
425 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &ec->prev_timecxx . . $ rand_data#prev_time.);
426
427 /*
428 * Now call the hash noise source with tripple the default iteration
429 * count considering this is the only noise source.
430 *
431 * Place the digest at an offset allowing the time stamp and the
432 * domain separator to be added before.
433 */
434 jent_hash_loopcxx . . $ jent_hash_loop(f4413f335a0bb650).(ec, intermediary,
435 loop_cnt ? loop_cnt :
436 ec->hashloopcntcxx . . $ rand_data#hashloopcnt. * JENT_HASH_LOOP_INITcxx . . $ `lib/jitterentropy-internal.h:225:9`!);
437
438 /*
439 * Get time stamp and calculate time delta to previous
440 * invocation to measure the timing variations
441 */
442 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now);
443 current_delta = jent_deltacxx . . $ jent_delta(80bad76eed2d2579).(ec->prev_timecxx . . $ rand_data#prev_time., time_now) /
444 ec->jent_common_timer_gcdcxx . . $ rand_data#jent_common_timer_gcd.;
445
446 /*
447 * Check whether we have a stuck measurement - and apply the health
448 * tests.
449 */
450 stuck = jent_stuckcxx . . $ jent_stuck(deafe999b085c65a).(ec, current_delta);
451
452 /* Domain separation */
453 intermediary[JENT_OFFSET_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:50:9`!] = 0x02;
454
455 /* Insert the data into the entropy pool */
456 jent_hash_insertcxx . . $ jent_hash_insert(946ccdf2e067f5ec).(ec, current_delta, intermediary);
457
458 /* return the raw entropy value */
459 if (ret_current_delta)
460 *ret_current_delta = current_delta;
461
462 return stuck;
463 }
464
465 /**
466 * This is the heart of the entropy generation: calculate time deltas and
467 * use the CPU jitter in the time deltas. The jitter is injected into the
468 * entropy pool.
469 *
470 * WARNING: ensure that ->prev_time is primed before using the output
471 * of this function! This can be done by calling this function
472 * and not using its result.
473 *
474 * @param[in] ec Reference to entropy collector
475 * @param[in] loop_cnt see jent_hash_loop
476 * @param[out] ret_current_delta Test interface: return time delta - may be NULL
477 *
478 * @return: result of stuck test
479 */
480 unsigned int jent_measure_jittercxx . . $ jent_measure_jitter(135d38c3d516dec8).(struct rand_datacxx . . $ rand_data# *ec,
481 uint64_tcxx . . $ uint64_t# loop_cnt,
482 uint64_tcxx . . $ uint64_t# *ret_current_delta)
483 {
484 /* Size of intermediary ensures a Keccak operation during hash_update */
485 uint8_tcxx . . $ uint8_t# intermediary[JENT_SIZEOF_INTERMEDIARYcxx . . $ `lib/jitterentropy-noise.c:47:9`!] = { 0 };
486
487 uint64_tcxx . . $ uint64_t# time_now = 0;
488 uint64_tcxx . . $ uint64_t# current_delta = 0;
489 unsigned int stuck;
490
491 /* Invoke memory access loop noise source */
492 #ifdef JENT_RANDOM_MEMACCESScxx . . $ `lib/jitterentropy-internal.h:93:9`!
493 jent_memaccess_pseudorandomcxx . . $ jent_memaccess_pseudorandom(46fd2d4b8959ab6c).(ec, loop_cnt, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
494 #else
495 jent_memaccess_deterministic(ec, loop_cnt, NULL);
496 #endif
497
498 /*
499 * Get time stamp and calculate time delta to previous
500 * invocation to measure the timing variations
501 */
502 jent_get_nstime_internalcxx . . $ jent_get_nstime_internal(3d346caf36e56379).(ec, &time_now);
503 current_delta = jent_deltacxx . . $ jent_delta(80bad76eed2d2579).(ec->prev_timecxx . . $ rand_data#prev_time., time_now) /
504 ec->jent_common_timer_gcdcxx . . $ rand_data#jent_common_timer_gcd.;
505 ec->prev_timecxx . . $ rand_data#prev_time. = time_now;
506
507 /* Check whether we have a stuck measurement. */
508 stuck = jent_stuckcxx . . $ jent_stuck(deafe999b085c65a).(ec, current_delta);
509
510 /* Invoke hash loop noise source */
511 jent_hash_loopcxx . . $ jent_hash_loop(f4413f335a0bb650).(ec, intermediary, loop_cnt);
512
513 /* Domain separation */
514 intermediary[JENT_OFFSET_DOMAINSEPARATORcxx . . $ `lib/jitterentropy-noise.c:50:9`!] = 0x03;
515
516 /* Insert the data into the entropy pool */
517 jent_hash_insertcxx . . $ jent_hash_insert(946ccdf2e067f5ec).(ec, current_delta, intermediary);
518
519 /* return the raw entropy value */
520 if (ret_current_delta)
521 *ret_current_delta = current_delta;
522
523 return stuck;
524 }
525
526 /*
527 * We multiply the loop value with ->osr to obtain the oversampling rate
528 * requested by the caller
529 */
530 #define JENT_MEASURE_JITTER_LOOP_CTRcxx . . $ `lib/jitterentropy-noise.c:530:9`!(_osr, _safety_factor) \
531 ((DATA_SIZE_BITScxx . . $ `lib/jitterentropy-internal.h:293:9`! + (_safety_factor)) * (_osr))
532
533 /*
534 * The health test RCT with memory operates on multiples of three time deltas.
535 * Therefore, round up the jitter loop counter to the nearest multiple of three.
536 */
537 #define JENT_ROUNDUP_TO_THREEcxx . . $ `lib/jitterentropy-noise.c:537:9`!(x) \
538 ( (((x) + 2) / 3) * 3 )
539 #define JENT_ADJUSTED_MEASURE_JITTER_LOOP_CTRcxx . . $ `lib/jitterentropy-noise.c:539:9`!(_osr, _safety_factor) \
540 JENT_ROUNDUP_TO_THREEcxx . . $ `lib/jitterentropy-noise.c:537:9`!( \
541 JENT_MEASURE_JITTER_LOOP_CTRcxx . . $ `lib/jitterentropy-noise.c:530:9`!(_osr, _safety_factor))
542
543 static void jent_random_data_onecxx . . $ jent_random_data_one(a1d3d1499f4342).(
544 struct rand_datacxx . . $ rand_data# *ec,
545 unsigned int (*measure_jitter)(struct rand_datacxx . . $ rand_data# *ec,
546 uint64_tcxx . . $ uint64_t# loop_cnt,
547 uint64_tcxx . . $ uint64_t# *ret_current_delta))
548 {
549 unsigned int safety_factor = 0, ctr = 0;
550
551 if (ec->fips_enabledcxx . . $ rand_data#fips_enabled.)
552 safety_factor = ENTROPY_SAFETY_FACTORcxx . . $ `lib/jitterentropy-internal.h:276:9`!;
553
554 /* RCT with memory: start a new iteration loop */
555 ec->rct_mem_ctrcxx . . $ rand_data#rct_mem_ctr. = 0;
556
557 /* Obtain number of loop iterations */
558 ec->rct_mem_nosrcxx . . $ rand_data#rct_mem_nosr. = (unsigned short)
559 JENT_ADJUSTED_MEASURE_JITTER_LOOP_CTRcxx . . $ `lib/jitterentropy-noise.c:539:9`!(ec->osr, safety_factor);
560
561 /* Safety measure against wrapping */
562 if (ec->rct_mem_nosrcxx . . $ rand_data#rct_mem_nosr. < DATA_SIZE_BITScxx . . $ `lib/jitterentropy-internal.h:293:9`!) {
563 ec->health_failurecxx . . $ rand_data#health_failure. |= JENT_RCT_MEM_FAILURE_PERMANENTcxx . . $ `jitterentropy.h:296:9`!;
564 return;
565 }
566
567 /* Entropy collection loop */
568 while (!jent_health_failurecxx . . $ jent_health_failure(18e832fdb9a2d285).(ec)) {
569 /* If a stuck measurement is received, repeat measurement */
570 if (measure_jitter(ec, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!))
571 continue;
572
573 if (++ctr >= ec->rct_mem_nosrcxx . . $ rand_data#rct_mem_nosr.)
574 break;
575 }
576 }
577
578 /**
579 * Generator of one 256 bit random number
580 * Function fills rand_data->hash_state
581 *
582 * @param[in] ec Reference to entropy collector
583 */
584 void jent_random_datacxx . . $ jent_random_data(dd399101f0e3ec34).(struct rand_datacxx . . $ rand_data# *ec)
585 {
586 /*
587 * Select which noise source to use for the entropy collection
588 */
589 switch (ec->startup_statecxx . . $ rand_data#startup_state.) {
590 case jent_startup_memorycxx . . $ jent_startup_memory.:
591 jent_random_data_onecxx . . $ jent_random_data_one(a1d3d1499f4342).(ec, jent_measure_jitter_ntg1_memaccesscxx . . $ jent_measure_jitter_ntg1_memaccess(135d38c3d516dec8).);
592 ec->startup_statecxx . . $ rand_data#startup_state.--;
593
594 /*
595 * Initialize the health tests as we fall through to
596 * independently invoke the next noise source.
597 */
598 jent_health_initcxx . . $ jent_health_init(ad5cb48bdf29082d).(ec, ec->flagscxx . . $ rand_data#flags. & JENT_NTG1cxx . . $ `jitterentropy.h:93:9`! ?
599 jent_health_init_type_ntg1cxx . . $ jent_health_init_type_ntg1. :
600 jent_health_init_type_commoncxx . . $ jent_health_init_type_common.);
601
602 /* FALLTHROUGH */
603 case jent_startup_sha3cxx . . $ jent_startup_sha3.:
604 jent_random_data_onecxx . . $ jent_random_data_one(a1d3d1499f4342).(ec, jent_measure_jitter_ntg1_sha3cxx . . $ jent_measure_jitter_ntg1_sha3(135d38c3d516dec8).);
605 ec->startup_statecxx . . $ rand_data#startup_state.--;
606
607 /*
608 * Initialize the health tests as we fall through to
609 * independently invoke the next noise source.
610 */
611 jent_health_initcxx . . $ jent_health_init(ad5cb48bdf29082d).(ec, ec->flagscxx . . $ rand_data#flags. & JENT_NTG1cxx . . $ `jitterentropy.h:93:9`! ?
612 jent_health_init_type_ntg1cxx . . $ jent_health_init_type_ntg1. :
613 jent_health_init_type_commoncxx . . $ jent_health_init_type_common.);
614
615 break;
616 case jent_startup_completedcxx . . $ jent_startup_completed.:
617 default:
618 /* priming of the ->prev_time value */
619 jent_measure_jittercxx . . $ jent_measure_jitter(135d38c3d516dec8).(ec, 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
620 jent_random_data_onecxx . . $ jent_random_data_one(a1d3d1499f4342).(ec, jent_measure_jittercxx . . $ jent_measure_jitter(135d38c3d516dec8).);
621 }
622 }
623
624 void jent_read_random_blockcxx . . $ jent_read_random_block(52e5be176d950c0d).(struct rand_datacxx . . $ rand_data# *ec, char *dst, size_t dst_len)
625 {
626 jent_drbg_generate_blockcxx . . $ jent_drbg_generate_block(5c0374f9bc02e3a7).(ec->hash_statecxx . . $ rand_data#hash_state., (uint8_tcxx . . $ uint8_t#*)dst, dst_len);
627 }