| 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" |
| 22 | #include "jitterentropy-health.h" |
| 23 | #include "jitterentropy-timer.h" |
| 24 | #include "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_TIMEDELTA (sizeof(uint64_t)) |
| 40 | #define JENT_SIZEOF_DOMAINSEPARATOR (sizeof(uint8_t)) |
| 41 | #define JENT_SIZEOF_HASH_BLOCK (JENT_SHA3_256_SIZE_DIGEST) |
| 42 | #define JENT_SIZEOF_INTERMEDIARY_DATA (JENT_SIZEOF_TIMEDELTA + \ |
| 43 | JENT_SIZEOF_DOMAINSEPARATOR + \ |
| 44 | JENT_SIZEOF_HASH_BLOCK) |
| 45 | |
| 46 | /* Intemediary is as big as the maximum rate it will be read with */ |
| 47 | #define JENT_SIZEOF_INTERMEDIARY (JENT_SHA3_256_SIZE_BLOCK) |
| 48 | |
| 49 | #define JENT_OFFSET_TIMEDELTA (0) |
| 50 | #define JENT_OFFSET_DOMAINSEPARATOR \ |
| 51 | (JENT_OFFSET_TIMEDELTA + JENT_SIZEOF_TIMEDELTA) |
| 52 | #define JENT_OFFSET_HASH_BLOCK \ |
| 53 | (JENT_OFFSET_DOMAINSEPARATOR + JENT_SIZEOF_DOMAINSEPARATOR) |
| 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_insert(struct rand_data *ec, uint64_t time_delta, |
| 70 | uint8_t intermediary[JENT_SIZEOF_INTERMEDIARY]) |
| 71 | { |
| 72 | /* |
| 73 | * Insert the time stamp into the intermediary buffer after the message |
| 74 | * digest of the intermediate data. |
| 75 | */ |
| 76 | memcpy(intermediary + JENT_OFFSET_TIMEDELTA, |
| 77 | (uint8_t *)&time_delta, sizeof(uint64_t)); |
| 78 | |
| 79 | BUILD_BUG_ON(JENT_SIZEOF_INTERMEDIARY < JENT_SIZEOF_INTERMEDIARY_DATA); |
| 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_update(ec->hash_state, intermediary, |
| 98 | jent_sha3_rate(ec->hash_state)); |
| 99 | jent_memset_secure(intermediary, JENT_SIZEOF_INTERMEDIARY); |
| 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_loop(struct rand_data *ec, |
| 112 | uint8_t intermediary[JENT_SIZEOF_INTERMEDIARY], |
| 113 | uint64_t loop_cnt) |
| 114 | { |
| 115 | HASH_CTX_ON_STACK(ctx); |
| 116 | uint8_t *digest = intermediary + JENT_OFFSET_HASH_BLOCK; |
| 117 | uint64_t j = 0; |
| 118 | |
| 119 | /* |
| 120 | * allow caller to set the counter |
| 121 | */ |
| 122 | uint64_t hash_loop_cnt = loop_cnt ? loop_cnt : ec->hashloopcnt; |
| 123 | |
| 124 | BUILD_BUG_ON(JENT_HASH_LOOP_DEFAULT < 1); |
| 125 | BUILD_BUG_ON(JENT_HASH_LOOP_INIT < 1); |
| 126 | |
| 127 | jent_sha3_256_init(&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_update(&ctx, digest, JENT_SHA3_256_SIZE_DIGEST); |
| 145 | jent_sha3_update(&ctx, (uint8_t *)&ec->rct_count, |
| 146 | sizeof(ec->rct_count)); |
| 147 | jent_sha3_update(&ctx, (uint8_t *)&ec->apt_cutoff, |
| 148 | sizeof(ec->apt_cutoff)); |
| 149 | jent_sha3_update(&ctx, (uint8_t *)&ec->apt_observations, |
| 150 | sizeof(ec->apt_observations)); |
| 151 | jent_sha3_update(&ctx, (uint8_t *)&ec->apt_count, |
| 152 | sizeof(ec->apt_count)); |
| 153 | jent_sha3_update(&ctx,(uint8_t *) &ec->apt_base, |
| 154 | sizeof(ec->apt_base)); |
| 155 | jent_sha3_update(&ctx,(uint8_t *) &ec->rct_mem_count, |
| 156 | sizeof(ec->rct_mem_count)); |
| 157 | jent_sha3_update(&ctx, (uint8_t *)&j, sizeof(uint64_t)); |
| 158 | jent_sha3_final(&ctx, digest); |
| 159 | } |
| 160 | |
| 161 | jent_memset_secure(&ctx, JENT_SHA_MAX_CTX_SIZE); |
| 162 | } |
| 163 | |
| 164 | static inline uint32_t uint32rotl(const uint32_t x, int k) |
| 165 | { |
| 166 | return (x << k) | (x >> (32 - k)); |
| 167 | } |
| 168 | |
| 169 | static inline uint32_t xoshiro128starstar(uint32_t *s) |
| 170 | { |
| 171 | const uint32_t result = uint32rotl(s[1] * 5, 7) * 9; |
| 172 | const 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] = uint32rotl(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_pseudorandom(struct rand_data *ec, uint64_t loop_cnt, |
| 195 | uint64_t *current_delta) |
| 196 | { |
| 197 | uint64_t i = 0, time_now_start = 0, time_now_end = 0, tmp_delta = 0; |
| 198 | union { |
| 199 | uint32_t u[4]; |
| 200 | uint8_t b[sizeof(uint32_t) * 4]; |
| 201 | } prngState = { .u = {0x8e93eec0, 0xce65608a, 0xa8d46b46, 0xe83cef69} }; |
| 202 | uint32_t addressMask; |
| 203 | |
| 204 | /* |
| 205 | * allow caller to set the counter |
| 206 | */ |
| 207 | uint64_t mem_loop_cnt = loop_cnt ? loop_cnt : ec->memaccessloops; |
| 208 | |
| 209 | if (NULL == ec || NULL == ec->mem) |
| 210 | return; |
| 211 | addressMask = ec->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_internal(ec, &time_now_start); |
| 226 | prngState.b[i] ^= (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_internal(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->mem + |
| 239 | (xoshiro128starstar(prngState.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_internal(ec, &time_now_end); |
| 256 | tmp_delta += jent_delta(time_now_start, time_now_end) / |
| 257 | ec->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_deterministic(struct rand_data *ec, |
| 287 | uint64_t loop_cnt, |
| 288 | uint64_t *current_delta) |
| 289 | { |
| 290 | uint64_t time_now_start = 0, time_now_end = 0, tmp_delta = 0; |
| 291 | unsigned int wrap = 0; |
| 292 | uint64_t i = 0; |
| 293 | |
| 294 | /* |
| 295 | * allow caller to set the counter |
| 296 | */ |
| 297 | uint64_t mem_loop_cnt = loop_cnt ? loop_cnt : ec->memaccessloops; |
| 298 | |
| 299 | if (NULL == ec || NULL == ec->mem) |
| 300 | return; |
| 301 | wrap = ec->memmask + 1; |
| 302 | |
| 303 | if (current_delta) |
| 304 | jent_get_nstime_internal(ec, &time_now_start); |
| 305 | |
| 306 | for (i = 0; i < mem_loop_cnt; i++) { |
| 307 | unsigned char *tmpval = ec->mem + ec->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->memlocation = ec->memlocation + JENT_MEMORY_BLOCKSIZE - 1; |
| 322 | ec->memlocation = ec->memlocation % wrap; |
| 323 | } |
| 324 | |
| 325 | if (current_delta) { |
| 326 | jent_get_nstime_internal(ec, &time_now_end); |
| 327 | tmp_delta += jent_delta(time_now_start, time_now_end) / |
| 328 | ec->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_memaccess(struct rand_data *ec, |
| 348 | uint64_t loop_cnt, |
| 349 | uint64_t *ret_current_delta) |
| 350 | { |
| 351 | uint8_t intermediary[JENT_SIZEOF_INTERMEDIARY] = { 0 }; |
| 352 | 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_deterministic( |
| 377 | ec, loop_cnt ? loop_cnt : |
| 378 | ec->memaccessloops * JENT_MEM_ACC_LOOP_INIT, |
| 379 | ¤t_delta); |
| 380 | |
| 381 | /* |
| 382 | * Check whether we have a stuck measurement - and apply the health |
| 383 | * tests. |
| 384 | */ |
| 385 | stuck = jent_stuck(ec, current_delta); |
| 386 | |
| 387 | /* Domain separation */ |
| 388 | intermediary[JENT_OFFSET_DOMAINSEPARATOR] = 0x01; |
| 389 | |
| 390 | /* Insert the data into the entropy pool */ |
| 391 | jent_hash_insert(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_sha3(struct rand_data *ec, |
| 412 | uint64_t loop_cnt, |
| 413 | uint64_t *ret_current_delta) |
| 414 | { |
| 415 | uint8_t intermediary[JENT_SIZEOF_INTERMEDIARY] = { 0 }; |
| 416 | uint64_t time_now = 0; |
| 417 | 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_internal(ec, &ec->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_loop(ec, intermediary, |
| 435 | loop_cnt ? loop_cnt : |
| 436 | ec->hashloopcnt * JENT_HASH_LOOP_INIT); |
| 437 | |
| 438 | /* |
| 439 | * Get time stamp and calculate time delta to previous |
| 440 | * invocation to measure the timing variations |
| 441 | */ |
| 442 | jent_get_nstime_internal(ec, &time_now); |
| 443 | current_delta = jent_delta(ec->prev_time, time_now) / |
| 444 | ec->jent_common_timer_gcd; |
| 445 | |
| 446 | /* |
| 447 | * Check whether we have a stuck measurement - and apply the health |
| 448 | * tests. |
| 449 | */ |
| 450 | stuck = jent_stuck(ec, current_delta); |
| 451 | |
| 452 | /* Domain separation */ |
| 453 | intermediary[JENT_OFFSET_DOMAINSEPARATOR] = 0x02; |
| 454 | |
| 455 | /* Insert the data into the entropy pool */ |
| 456 | jent_hash_insert(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_jitter(struct rand_data *ec, |
| 481 | uint64_t loop_cnt, |
| 482 | uint64_t *ret_current_delta) |
| 483 | { |
| 484 | /* Size of intermediary ensures a Keccak operation during hash_update */ |
| 485 | uint8_t intermediary[JENT_SIZEOF_INTERMEDIARY] = { 0 }; |
| 486 | |
| 487 | uint64_t time_now = 0; |
| 488 | uint64_t current_delta = 0; |
| 489 | unsigned int stuck; |
| 490 | |
| 491 | /* Invoke memory access loop noise source */ |
| 492 | #ifdef JENT_RANDOM_MEMACCESS |
| 493 | jent_memaccess_pseudorandom(ec, loop_cnt, NULL); |
| 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_internal(ec, &time_now); |
| 503 | current_delta = jent_delta(ec->prev_time, time_now) / |
| 504 | ec->jent_common_timer_gcd; |
| 505 | ec->prev_time = time_now; |
| 506 | |
| 507 | /* Check whether we have a stuck measurement. */ |
| 508 | stuck = jent_stuck(ec, current_delta); |
| 509 | |
| 510 | /* Invoke hash loop noise source */ |
| 511 | jent_hash_loop(ec, intermediary, loop_cnt); |
| 512 | |
| 513 | /* Domain separation */ |
| 514 | intermediary[JENT_OFFSET_DOMAINSEPARATOR] = 0x03; |
| 515 | |
| 516 | /* Insert the data into the entropy pool */ |
| 517 | jent_hash_insert(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_CTR(_osr, _safety_factor) \ |
| 531 | ((DATA_SIZE_BITS + (_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_THREE(x) \ |
| 538 | ( (((x) + 2) / 3) * 3 ) |
| 539 | #define JENT_ADJUSTED_MEASURE_JITTER_LOOP_CTR(_osr, _safety_factor) \ |
| 540 | JENT_ROUNDUP_TO_THREE( \ |
| 541 | JENT_MEASURE_JITTER_LOOP_CTR(_osr, _safety_factor)) |
| 542 | |
| 543 | static void jent_random_data_one( |
| 544 | struct rand_data *ec, |
| 545 | unsigned int (*measure_jitter)(struct rand_data *ec, |
| 546 | uint64_t loop_cnt, |
| 547 | uint64_t *ret_current_delta)) |
| 548 | { |
| 549 | unsigned int safety_factor = 0, ctr = 0; |
| 550 | |
| 551 | if (ec->fips_enabled) |
| 552 | safety_factor = ENTROPY_SAFETY_FACTOR; |
| 553 | |
| 554 | /* RCT with memory: start a new iteration loop */ |
| 555 | ec->rct_mem_ctr = 0; |
| 556 | |
| 557 | /* Obtain number of loop iterations */ |
| 558 | ec->rct_mem_nosr = (unsigned short) |
| 559 | JENT_ADJUSTED_MEASURE_JITTER_LOOP_CTR(ec->osr, safety_factor); |
| 560 | |
| 561 | /* Safety measure against wrapping */ |
| 562 | if (ec->rct_mem_nosr < DATA_SIZE_BITS) { |
| 563 | ec->health_failure |= JENT_RCT_MEM_FAILURE_PERMANENT; |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | /* Entropy collection loop */ |
| 568 | while (!jent_health_failure(ec)) { |
| 569 | /* If a stuck measurement is received, repeat measurement */ |
| 570 | if (measure_jitter(ec, 0, NULL)) |
| 571 | continue; |
| 572 | |
| 573 | if (++ctr >= ec->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_data(struct rand_data *ec) |
| 585 | { |
| 586 | /* |
| 587 | * Select which noise source to use for the entropy collection |
| 588 | */ |
| 589 | switch (ec->startup_state) { |
| 590 | case jent_startup_memory: |
| 591 | jent_random_data_one(ec, jent_measure_jitter_ntg1_memaccess); |
| 592 | ec->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_init(ec, ec->flags & JENT_NTG1 ? |
| 599 | jent_health_init_type_ntg1 : |
| 600 | jent_health_init_type_common); |
| 601 | |
| 602 | /* FALLTHROUGH */ |
| 603 | case jent_startup_sha3: |
| 604 | jent_random_data_one(ec, jent_measure_jitter_ntg1_sha3); |
| 605 | ec->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_init(ec, ec->flags & JENT_NTG1 ? |
| 612 | jent_health_init_type_ntg1 : |
| 613 | jent_health_init_type_common); |
| 614 | |
| 615 | break; |
| 616 | case jent_startup_completed: |
| 617 | default: |
| 618 | /* priming of the ->prev_time value */ |
| 619 | jent_measure_jitter(ec, 0, NULL); |
| 620 | jent_random_data_one(ec, jent_measure_jitter); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void jent_read_random_block(struct rand_data *ec, char *dst, size_t dst_len) |
| 625 | { |
| 626 | jent_drbg_generate_block(ec->hash_state, (uint8_t*)dst, dst_len); |
| 627 | } |