| 1 | /* |
| 2 | * Non-physical true random number generator based on timing jitter. |
| 3 | * |
| 4 | * Copyright Stephan Mueller <[email protected]>, 2014 - 2026 |
| 5 | * |
| 6 | * Design |
| 7 | * ====== |
| 8 | * |
| 9 | * See documentation in doc/ folder. |
| 10 | * |
| 11 | * Interface |
| 12 | * ========= |
| 13 | * |
| 14 | * See documentation in jitterentropy(3) man page. |
| 15 | * |
| 16 | * License: see LICENSE file in root directory |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 21 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 28 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 29 | * DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "jitterentropy-base.h" |
| 33 | #include "jitterentropy-gcd.h" |
| 34 | #include "jitterentropy-health.h" |
| 35 | #include "jitterentropy-internal.h" |
| 36 | #include "jitterentropy-noise.h" |
| 37 | #include "jitterentropy-timer.h" |
| 38 | #include "jitterentropy-sha3.h" |
| 39 | |
| 40 | /*************************************************************************** |
| 41 | * Jitter RNG Static Definitions |
| 42 | * |
| 43 | * None of the following should be altered |
| 44 | ***************************************************************************/ |
| 45 | |
| 46 | #ifdef __OPTIMIZE__ |
| 47 | #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c." |
| 48 | #endif |
| 49 | |
| 50 | /* |
| 51 | * JENT_POWERUP_TESTLOOPCOUNT needs some loops to identify edge |
| 52 | * systems. 100 is definitely too little. |
| 53 | * |
| 54 | * SP800-90B requires at least 1024 initial test cycles. |
| 55 | */ |
| 56 | #define JENT_POWERUP_TESTLOOPCOUNT 1024 |
| 57 | |
| 58 | /* |
| 59 | * ensure_osr_is_at_least_minimal ensures that the over sampling rate is, at |
| 60 | * minimum, JENT_MIN_OSR. |
| 61 | * |
| 62 | * @return returns the argument current_osr if equal to or larger than |
| 63 | * JENT_MIN_OSR otherwise, returns JENT_MIN_OSR. |
| 64 | */ |
| 65 | static unsigned int ensure_osr_is_at_least_minimal(unsigned int current_osr) |
| 66 | { |
| 67 | if (current_osr < JENT_MIN_OSR) |
| 68 | return (unsigned int) JENT_MIN_OSR; |
| 69 | else |
| 70 | return current_osr; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * jent_version() - Return machine-usable version number of jent library |
| 75 | * |
| 76 | * The function returns a version number that is monotonic increasing |
| 77 | * for newer versions. The version numbers are multiples of 100. For example, |
| 78 | * version 1.2.3 is converted to 1020300 -- the last two digits are reserved |
| 79 | * for future use. |
| 80 | * |
| 81 | * The result of this function can be used in comparing the version number |
| 82 | * in a calling program if version-specific calls need to be make. |
| 83 | * |
| 84 | * @return Version number of jitterentropy library |
| 85 | */ |
| 86 | JENT_PRIVATE_STATIC |
| 87 | unsigned int jent_version(void) |
| 88 | { |
| 89 | return JENT_VERSION; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * jent_secure_memory_supported() - Return if secure memory is used |
| 94 | * |
| 95 | * Secure memory uses guard pages, swap protection and zeroize on |
| 96 | * free. |
| 97 | */ |
| 98 | JENT_PRIVATE_STATIC |
| 99 | int jent_secure_memory_supported(void) |
| 100 | { |
| 101 | #ifdef CONFIG_CRYPTO_CPU_JITTERENTROPY_SECURE_MEMORY |
| 102 | return 1; |
| 103 | #else |
| 104 | return 0; |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | /*************************************************************************** |
| 109 | * Helper |
| 110 | ***************************************************************************/ |
| 111 | |
| 112 | /* Calculate log2 of given value assuming that the value is a power of 2 */ |
| 113 | static inline unsigned int jent_log2_simple(unsigned int val) |
| 114 | { |
| 115 | unsigned int idx = 0; |
| 116 | |
| 117 | while (val >>= 1) |
| 118 | idx++; |
| 119 | return idx; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Obtain memory size to allocate for memory access variations. |
| 124 | * |
| 125 | * The maximum variations we can get from the memory access is when we allocate |
| 126 | * a bit more memory than we have as data cache. But allocating as much |
| 127 | * memory as we have as data cache might strain the resources on the system |
| 128 | * more than necessary. |
| 129 | * |
| 130 | * On a lot of systems it is not necessary to need so much memory as the |
| 131 | * variations coming from the general Jitter RNG execution commonly provide |
| 132 | * large amount of variations. |
| 133 | * |
| 134 | * Thus, the default is: |
| 135 | * * size provided by the caller, or |
| 136 | * * cache information * (1 << JENT_CACHE_SHIFT_BITS) where by default |
| 137 | * only L1 cache size is uzed or with JENT_CACHE_ALL all caches are used |
| 138 | * to determine the memory size, or |
| 139 | * * 1 << JENT_DEFAULT_MEMORY_BITS |
| 140 | * |
| 141 | * All is capped by JENT_MAX_MEMSIZE_MAX |
| 142 | */ |
| 143 | static inline unsigned int jent_update_memsize(unsigned int flags, |
| 144 | unsigned int inc) |
| 145 | { |
| 146 | unsigned int global_max = JENT_FLAGS_TO_MAX_MEMSIZE( |
| 147 | JENT_MAX_MEMSIZE_MAX); |
| 148 | unsigned int max; |
| 149 | |
| 150 | max = JENT_FLAGS_TO_MAX_MEMSIZE(flags); |
| 151 | |
| 152 | if (!max) { |
| 153 | /* |
| 154 | * The safe starting value is the cache size increased by the |
| 155 | * multiplicator. |
| 156 | */ |
| 157 | max = jent_log2_simple(jent_cache_size_roundup( |
| 158 | !!(flags & JENT_CACHE_ALL))); |
| 159 | if (!max) { |
| 160 | max = JENT_DEFAULT_MEMORY_BITS; |
| 161 | } else { |
| 162 | max += JENT_CACHE_SHIFT_BITS; |
| 163 | |
| 164 | if (!(flags & JENT_CACHE_ALL)) { |
| 165 | /* |
| 166 | * We increase the memory size 4-fold. This is |
| 167 | * due to ensure that the memory access |
| 168 | * operation mostly is caused by L1 misses and |
| 169 | * L2 hits. |
| 170 | */ |
| 171 | max += 2; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* Adjust offset */ |
| 176 | max = (max > JENT_MAX_MEMSIZE_OFFSET) ? |
| 177 | max - JENT_MAX_MEMSIZE_OFFSET : 0; |
| 178 | } else { |
| 179 | max += inc; |
| 180 | } |
| 181 | |
| 182 | max = (max > global_max) ? global_max : max; |
| 183 | |
| 184 | /* Clear out the max size */ |
| 185 | flags &= ~JENT_MAX_MEMSIZE_MASK; |
| 186 | /* Set the freshly calculated max size */ |
| 187 | flags |= JENT_MAX_MEMSIZE_TO_FLAGS(max); |
| 188 | |
| 189 | return flags; |
| 190 | } |
| 191 | |
| 192 | static inline unsigned int jent_update_hashloop(unsigned int flags, |
| 193 | unsigned int inc) |
| 194 | { |
| 195 | unsigned int global_max = JENT_FLAGS_TO_HASHLOOP(JENT_MAX_HASHLOOP); |
| 196 | unsigned int max; |
| 197 | |
| 198 | max = JENT_FLAGS_TO_HASHLOOP(flags); |
| 199 | max += inc; |
| 200 | max = (max > global_max) ? global_max : max; |
| 201 | |
| 202 | /* Clear out the max size */ |
| 203 | flags &= ~(unsigned int)JENT_MAX_HASHLOOP_MASK; |
| 204 | /* Set the freshly calculated max size */ |
| 205 | flags |= JENT_HASHLOOP_TO_FLAGS(max); |
| 206 | |
| 207 | return flags; |
| 208 | } |
| 209 | |
| 210 | /*************************************************************************** |
| 211 | * Random Number Generation |
| 212 | ***************************************************************************/ |
| 213 | |
| 214 | /** |
| 215 | * Entry function: Obtain entropy for the caller. |
| 216 | * |
| 217 | * This function invokes the entropy gathering logic as often to generate |
| 218 | * as many bytes as requested by the caller. The entropy gathering logic |
| 219 | * creates 64 bit per invocation. |
| 220 | * |
| 221 | * This function truncates the last 64 bit entropy value output to the exact |
| 222 | * size specified by the caller. |
| 223 | * |
| 224 | * @param[in] ec Reference to entropy collector |
| 225 | * @param[out] data pointer to buffer for storing random data -- buffer must |
| 226 | * already exist |
| 227 | * @param[in] len size of the buffer, specifying also the requested number of random |
| 228 | * in bytes |
| 229 | * |
| 230 | * @return number of bytes returned when request is fulfilled or an error |
| 231 | * |
| 232 | * The following error codes can occur: |
| 233 | * -1 entropy_collector is NULL |
| 234 | * -2 RCT failed |
| 235 | * -3 APT failed |
| 236 | * -4 The timer cannot be initialized |
| 237 | * -5 LAG failure |
| 238 | * -6 RCT permanent failure |
| 239 | * -7 APT permanent failure |
| 240 | * -8 LAG permanent failure |
| 241 | * -9 RCT with memory failed |
| 242 | * -10 RCT with memory permanent failure |
| 243 | */ |
| 244 | JENT_PRIVATE_STATIC |
| 245 | ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len) |
| 246 | { |
| 247 | /* |
| 248 | * Maximum value representable by ssize_t. Use a portable |
| 249 | * definition in case SSIZE_MAX is not available under strict |
| 250 | * C standard modes. It is nevertheless available on POSIX systems. |
| 251 | */ |
| 252 | static const size_t ssize_max = (size_t)(((size_t)1 << (sizeof(ssize_t) * 8 - 1)) - 1); |
| 253 | char *p = data; |
| 254 | size_t orig_len; |
| 255 | int ret = 0; |
| 256 | |
| 257 | /* check obvious misuse of API */ |
| 258 | if (!ec || (data == NULL && len > 0)) |
| 259 | return -1; |
| 260 | |
| 261 | /* |
| 262 | * (hypothetical) edge case: clamp to ssize_t range to prevent |
| 263 | * negative return on cast |
| 264 | */ |
| 265 | if (len > ssize_max) |
| 266 | len = ssize_max; |
| 267 | orig_len = len; |
| 268 | |
| 269 | if (jent_notime_settick(ec)) |
| 270 | return -4; |
| 271 | |
| 272 | while (len > 0) { |
| 273 | size_t tocopy; |
| 274 | unsigned int health_test_result; |
| 275 | |
| 276 | jent_random_data(ec); |
| 277 | |
| 278 | if ((health_test_result = jent_health_failure(ec))) { |
| 279 | if (health_test_result & JENT_RCT_FAILURE_PERMANENT) |
| 280 | ret = -6; |
| 281 | else if (health_test_result & |
| 282 | JENT_APT_FAILURE_PERMANENT) |
| 283 | ret = -7; |
| 284 | else if (health_test_result & |
| 285 | JENT_LAG_FAILURE_PERMANENT) |
| 286 | ret = -8; |
| 287 | else if (health_test_result & |
| 288 | JENT_RCT_MEM_FAILURE_PERMANENT) |
| 289 | ret = -10; |
| 290 | else if (health_test_result & JENT_RCT_FAILURE) |
| 291 | ret = -2; |
| 292 | else if (health_test_result & JENT_APT_FAILURE) |
| 293 | ret = -3; |
| 294 | else if (health_test_result & JENT_RCT_MEM_FAILURE) |
| 295 | ret = -9; |
| 296 | else |
| 297 | ret = -5; |
| 298 | |
| 299 | goto err; |
| 300 | } |
| 301 | |
| 302 | if ((DATA_SIZE_BITS / 8) < len) |
| 303 | tocopy = (DATA_SIZE_BITS / 8); |
| 304 | else |
| 305 | tocopy = len; |
| 306 | |
| 307 | jent_read_random_block(ec, p, tocopy); |
| 308 | |
| 309 | len -= tocopy; |
| 310 | p += tocopy; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Enhanced backtracking support: At this point, the hash state |
| 315 | * contains the digest of the previous Jitter RNG collection round |
| 316 | * which is inserted there by jent_read_random_block with the SHA |
| 317 | * update operation. At the current code location we completed |
| 318 | * one request for a caller and we do not know how long it will |
| 319 | * take until a new request is sent to us. To guarantee enhanced |
| 320 | * backtracking resistance at this point (i.e. ensure that an attacker |
| 321 | * cannot obtain information about prior random numbers we generated), |
| 322 | * but still stirring the hash state with old data the Jitter RNG |
| 323 | * obtains a new message digest from its state and re-inserts it. |
| 324 | * After this operation, the Jitter RNG state is still stirred with |
| 325 | * the old data, but an attacker who gets access to the memory after |
| 326 | * this point cannot deduce the random numbers produced by the |
| 327 | * Jitter RNG prior to this point. |
| 328 | */ |
| 329 | /* |
| 330 | * If we use secured memory, where backtracking support may not be |
| 331 | * needed because the state is protected in a different method, |
| 332 | * it is permissible to drop this support. But strongly weigh the |
| 333 | * pros and cons considering that the SHA3 operation is not that |
| 334 | * expensive. |
| 335 | */ |
| 336 | #ifndef CONFIG_CRYPTO_CPU_JITTERENTROPY_SECURE_MEMORY |
| 337 | jent_read_random_block(ec, NULL, 0); |
| 338 | #endif |
| 339 | |
| 340 | err: |
| 341 | jent_notime_unsettick(ec); |
| 342 | return ret ? ret : (ssize_t)orig_len; |
| 343 | } |
| 344 | |
| 345 | static int jent_health_failure_reset( |
| 346 | struct rand_data **ec, struct rand_data *(*alloc)(unsigned int osr, |
| 347 | unsigned int flags)) |
| 348 | { |
| 349 | struct rand_data *new_ec; |
| 350 | unsigned int osr, flags; |
| 351 | |
| 352 | /* Increment OSR */ |
| 353 | osr = (*ec)->osr + 1; |
| 354 | |
| 355 | /* Remember flags value */ |
| 356 | flags = (*ec)->flags; |
| 357 | |
| 358 | /* generic arbitrary cutoff to prevent running "forever" */ |
| 359 | if (osr > JENT_MAX_OSR) |
| 360 | return -1; |
| 361 | |
| 362 | /* |
| 363 | * If the caller did not set any specific maximum value let the Jitter |
| 364 | * RNG increase the maximum memory by one step. |
| 365 | */ |
| 366 | if (!(*ec)->max_mem_set) |
| 367 | flags = jent_update_memsize(flags, 1); |
| 368 | |
| 369 | /* Increment hash loop count by one */ |
| 370 | flags = jent_update_hashloop(flags, 1); |
| 371 | |
| 372 | /* Perform new health test with updated OSR */ |
| 373 | while (jent_entropy_init_ex(osr, flags)) { |
| 374 | osr++; |
| 375 | if (osr > JENT_MAX_OSR) |
| 376 | return -1; |
| 377 | } |
| 378 | |
| 379 | new_ec = alloc(osr, flags); |
| 380 | |
| 381 | /* |
| 382 | * In case of an error, leave the existing ec state untouched as a |
| 383 | * safety measure. But it is in error state and is of not much use. |
| 384 | */ |
| 385 | if (!new_ec) |
| 386 | return -1; |
| 387 | |
| 388 | /* Remember whether caller configured memory size */ |
| 389 | new_ec->max_mem_set = !!(*ec)->max_mem_set; |
| 390 | |
| 391 | /* |
| 392 | * Duplicate the state of the health tests to ensure the newly allocated |
| 393 | * state will continue from the current health state. |
| 394 | */ |
| 395 | jent_apt_duplicate(new_ec, *ec); |
| 396 | jent_rct_duplicate(new_ec); |
| 397 | jent_lag_duplicate(new_ec, *ec); |
| 398 | jent_rct_mem_duplicate(new_ec, *ec); |
| 399 | |
| 400 | jent_entropy_collector_free(*ec); |
| 401 | *ec = new_ec; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static struct rand_data *_jent_entropy_collector_alloc(unsigned int osr, |
| 407 | unsigned int flags); |
| 408 | |
| 409 | /** |
| 410 | * Entry function: Obtain entropy for the caller. |
| 411 | * |
| 412 | * This is a service function to jent_read_entropy() with the difference |
| 413 | * that it automatically re-allocates the entropy collector if a health |
| 414 | * test failure is observed. Before reallocation, a new power-on health test |
| 415 | * is performed. The allocation of the new entropy collector automatically |
| 416 | * increases the OSR by one. This is done based on the idea that a health |
| 417 | * test failure indicates that the assumed entropy rate is too high. |
| 418 | * |
| 419 | * Note the function returns with an health test error if the OSR is |
| 420 | * getting too large. If an error is returned by this function, the Jitter RNG |
| 421 | * is not safe to be used on the current system. |
| 422 | * |
| 423 | * @param[in] ec Reference to entropy collector - this is a double pointer as |
| 424 | * The entropy collector may be freed and reallocated. |
| 425 | * @param[out] data pointer to buffer for storing random data -- buffer must |
| 426 | * already exist |
| 427 | * @param[in] len size of the buffer, specifying also the requested number of |
| 428 | * random in bytes |
| 429 | * |
| 430 | * @return see jent_read_entropy() |
| 431 | */ |
| 432 | JENT_PRIVATE_STATIC |
| 433 | ssize_t jent_read_entropy_safe(struct rand_data **ec, char *data, size_t len) |
| 434 | { |
| 435 | /* |
| 436 | * Maximum value representable by ssize_t. Use a portable |
| 437 | * definition in case SSIZE_MAX is not available under strict |
| 438 | * C standard modes. It is nevertheless available on POSIX systems. |
| 439 | */ |
| 440 | static const size_t ssize_max = (size_t)(((size_t)1 << (sizeof(ssize_t) * 8 - 1)) - 1); |
| 441 | char *p = data; |
| 442 | size_t orig_len; |
| 443 | ssize_t ret = 0; |
| 444 | |
| 445 | /* check obvious misuse of API */ |
| 446 | if (!ec || (data == NULL && len > 0)) |
| 447 | return -1; |
| 448 | |
| 449 | /* |
| 450 | * (hypothetical) edge case: clamp to ssize_t range to prevent |
| 451 | * negative return on cast |
| 452 | */ |
| 453 | if (len > ssize_max) |
| 454 | len = ssize_max; |
| 455 | orig_len = len; |
| 456 | |
| 457 | while (len > 0) { |
| 458 | ret = jent_read_entropy(*ec, p, len); |
| 459 | |
| 460 | switch (ret) { |
| 461 | /* Generic errors are returned immediately */ |
| 462 | case -1: |
| 463 | case -4: |
| 464 | |
| 465 | /* Permanent health errors are returned immediately */ |
| 466 | case -6: |
| 467 | case -7: |
| 468 | case -8: |
| 469 | case -10: |
| 470 | return ret; |
| 471 | |
| 472 | /* Intermittent health errors */ |
| 473 | case -2: |
| 474 | case -3: |
| 475 | case -5: |
| 476 | case -9: |
| 477 | /* |
| 478 | * Re-allocate the entropy collector with updated |
| 479 | * OSR, hash loop count and memory size and run |
| 480 | * the startup sequence for NTG.1 again. |
| 481 | * |
| 482 | * If we fail here, the Jitter RNG returns the error. |
| 483 | */ |
| 484 | if (jent_health_failure_reset( |
| 485 | ec, _jent_entropy_collector_alloc)) |
| 486 | return ret; |
| 487 | |
| 488 | /* |
| 489 | * We are not returning the intermittent errors here. |
| 490 | * If a caller wants them, he should register a callback |
| 491 | * with jent_set_fips_failure_callback. |
| 492 | */ |
| 493 | |
| 494 | break; |
| 495 | |
| 496 | default: |
| 497 | /* defensive check for uncaught errors */ |
| 498 | if (ret >= 0) { |
| 499 | len -= (size_t)ret; |
| 500 | p += (size_t)ret; |
| 501 | } else { |
| 502 | return -1; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return (ssize_t)orig_len; |
| 508 | } |
| 509 | |
| 510 | /*************************************************************************** |
| 511 | * Initialization logic |
| 512 | ***************************************************************************/ |
| 513 | |
| 514 | uint32_t jent_memsize(unsigned int flags) |
| 515 | { |
| 516 | uint32_t memsize = JENT_FLAGS_TO_MAX_MEMSIZE(flags); |
| 517 | |
| 518 | if (memsize == 0) { |
| 519 | memsize = JENT_DEFAULT_MEMORY_BITS; |
| 520 | } else { |
| 521 | memsize = memsize + JENT_MAX_MEMSIZE_OFFSET; |
| 522 | } |
| 523 | |
| 524 | memsize = UINT32_C(1) << memsize; |
| 525 | |
| 526 | return memsize; |
| 527 | } |
| 528 | |
| 529 | unsigned int jent_hashloop_cnt(unsigned int flags) |
| 530 | { |
| 531 | unsigned int cnt = JENT_FLAGS_TO_HASHLOOP(flags); |
| 532 | |
| 533 | if (cnt == 0) |
| 534 | cnt = JENT_HASH_LOOP_DEFAULT; |
| 535 | else |
| 536 | cnt = UINT32_C(1) << cnt; |
| 537 | |
| 538 | return cnt; |
| 539 | } |
| 540 | |
| 541 | static int jent_selftest_run = 0; |
| 542 | |
| 543 | static struct rand_data |
| 544 | *jent_entropy_collector_alloc_internal(unsigned int osr, unsigned int flags) |
| 545 | { |
| 546 | struct rand_data *entropy_collector; |
| 547 | uint32_t memsize = 0; |
| 548 | |
| 549 | /* |
| 550 | * Requesting disabling and forcing of internal timer |
| 551 | * makes no sense. |
| 552 | */ |
| 553 | if ((flags & JENT_DISABLE_INTERNAL_TIMER) && |
| 554 | (flags & JENT_FORCE_INTERNAL_TIMER)) |
| 555 | return NULL; |
| 556 | |
| 557 | /* |
| 558 | * Ensure over sampling rate is not too low. |
| 559 | */ |
| 560 | osr = ensure_osr_is_at_least_minimal(osr); |
| 561 | |
| 562 | /* |
| 563 | * Reject too high OSR |
| 564 | */ |
| 565 | if (osr > JENT_MAX_OSR) |
| 566 | return NULL; |
| 567 | |
| 568 | /* Force the self test to be run */ |
| 569 | if (!jent_selftest_run && jent_entropy_init_ex(osr, flags)) |
| 570 | return NULL; |
| 571 | |
| 572 | /* |
| 573 | * NTG.1 requires to disable the internal timer. |
| 574 | */ |
| 575 | if (flags & JENT_NTG1) |
| 576 | flags |= JENT_DISABLE_INTERNAL_TIMER; |
| 577 | |
| 578 | /* |
| 579 | * If the initial test code concludes to force the internal timer |
| 580 | * and the user requests it not to be used, do not allocate |
| 581 | * the Jitter RNG instance. |
| 582 | */ |
| 583 | if (jent_notime_forced() && (flags & JENT_DISABLE_INTERNAL_TIMER)) |
| 584 | return NULL; |
| 585 | |
| 586 | entropy_collector = jent_zalloc(sizeof(struct rand_data)); |
| 587 | if (NULL == entropy_collector) |
| 588 | return NULL; |
| 589 | |
| 590 | if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) { |
| 591 | flags = jent_update_memsize(flags, 0); |
| 592 | memsize = jent_memsize(flags); |
| 593 | entropy_collector->mem = (unsigned char *)jent_zalloc(memsize); |
| 594 | if (entropy_collector->mem == NULL) |
| 595 | goto err; |
| 596 | |
| 597 | /* |
| 598 | * Transform the size into a mask - it is assumed that size is |
| 599 | * a power of 2. |
| 600 | */ |
| 601 | entropy_collector->memmask = memsize - 1; |
| 602 | entropy_collector->memaccessloops = JENT_MEM_ACC_LOOP_DEFAULT; |
| 603 | } |
| 604 | |
| 605 | /* Set the hash loop count */ |
| 606 | flags = jent_update_hashloop(flags, 0); |
| 607 | entropy_collector->hashloopcnt = jent_hashloop_cnt(flags); |
| 608 | |
| 609 | if (jent_sha3_alloc(&entropy_collector->hash_state)) |
| 610 | goto err; |
| 611 | |
| 612 | /* |
| 613 | * Initialize the hash state for the XDRBG |
| 614 | */ |
| 615 | jent_shake256_init(entropy_collector->hash_state); |
| 616 | |
| 617 | if ((flags & JENT_FORCE_FIPS) || jent_fips_enabled()) { |
| 618 | /* |
| 619 | * NIST explicitly suggested to use an identical approach |
| 620 | * to the initialization of the conditioner as specified |
| 621 | * for the NTG.1 compliance. |
| 622 | */ |
| 623 | entropy_collector->startup_state = jent_startup_memory; |
| 624 | entropy_collector->fips_enabled = 1; |
| 625 | } |
| 626 | |
| 627 | /* Set the oversampling rate */ |
| 628 | entropy_collector->osr = osr; |
| 629 | entropy_collector->flags = flags; |
| 630 | |
| 631 | /* |
| 632 | * BSI AIS 20/31 NTG.1 requires that during startup 2 noise sources |
| 633 | * are sampled where each independently delivers 240 bits of entropy. |
| 634 | * This is ensured by setting the startup state such that the memory |
| 635 | * access is treated independently from the SHA3 operation and both |
| 636 | * must separately deliver the requested amount of entropy. |
| 637 | * |
| 638 | * NTG.1 implies the enabling of the FIPS mode to apply noise source |
| 639 | * oversampling and the enabling of the health tests. |
| 640 | */ |
| 641 | if (flags & JENT_NTG1) { |
| 642 | entropy_collector->startup_state = jent_startup_memory; |
| 643 | entropy_collector->fips_enabled = 1; |
| 644 | } |
| 645 | |
| 646 | /* Initialize the health tests */ |
| 647 | jent_health_init(entropy_collector, flags & JENT_NTG1 ? |
| 648 | jent_health_init_type_ntg1 : |
| 649 | jent_health_init_type_common); |
| 650 | |
| 651 | /* Was jent_entropy_init run (establishing the common GCD)? */ |
| 652 | if (jent_gcd_get(&entropy_collector->jent_common_timer_gcd)) { |
| 653 | /* |
| 654 | * It was not. This should probably be an error, but this |
| 655 | * behavior breaks the test code. Set the gcd to a value that |
| 656 | * won't hurt anything. |
| 657 | */ |
| 658 | entropy_collector->jent_common_timer_gcd = 1; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Use timer-less noise source - note, OSR must be set in |
| 663 | * entropy_collector! |
| 664 | */ |
| 665 | if (!(flags & JENT_DISABLE_INTERNAL_TIMER)) { |
| 666 | if (jent_notime_enable(entropy_collector, flags)) |
| 667 | goto err; |
| 668 | } |
| 669 | |
| 670 | return entropy_collector; |
| 671 | |
| 672 | err: |
| 673 | jent_entropy_collector_free(entropy_collector); |
| 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | static struct rand_data *_jent_entropy_collector_alloc(unsigned int osr, |
| 678 | unsigned int flags) |
| 679 | { |
| 680 | struct rand_data *ec = jent_entropy_collector_alloc_internal(osr, |
| 681 | flags); |
| 682 | |
| 683 | if (!ec) |
| 684 | return ec; |
| 685 | |
| 686 | /* fill the data pad with non-zero values */ |
| 687 | if (jent_notime_settick(ec)) { |
| 688 | jent_entropy_collector_free(ec); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * Assure, that we always have 512 bits (NTG.1 / FIPS compliance due to |
| 694 | * startup_state is set to 2) or 256 bits (other cases) entropy in |
| 695 | * our hash state before outputting a block by adding at least 256 bits |
| 696 | * before first usage. 512 bits are always transferred to the next state |
| 697 | * before the actual generation of random numbers to be returned to the |
| 698 | * caller. The size is due to the XDRBG state variable. |
| 699 | * |
| 700 | * For NTG.1: already perform the startup stages guaranteeing the |
| 701 | * invocation of 2 noise sources each delivering 240 bits of entropy |
| 702 | * at least at this point. |
| 703 | */ |
| 704 | do { |
| 705 | jent_random_data(ec); |
| 706 | |
| 707 | /* |
| 708 | * Check for any kind of health error at this point including |
| 709 | * intermittent or permanent errors. If we observed one, |
| 710 | * re-initialize the entropy collector. |
| 711 | */ |
| 712 | if (jent_health_failure(ec)) { |
| 713 | |
| 714 | /* |
| 715 | * Re-allocate the entropy collector with updated |
| 716 | * OSR, hash loop count and memory size. |
| 717 | */ |
| 718 | if (jent_health_failure_reset( |
| 719 | &ec, jent_entropy_collector_alloc_internal)) { |
| 720 | jent_entropy_collector_free(ec); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * The reset replaced ec with a freshly allocated |
| 726 | * collector. That new collector has enable_notime |
| 727 | * set but no running timer thread (the old thread |
| 728 | * was stopped when the old collector was freed). |
| 729 | * Restart the timer thread before re-entering the |
| 730 | * loop, otherwise jent_get_nstime_internal will |
| 731 | * spin forever waiting for a counter that nobody |
| 732 | * increments. |
| 733 | */ |
| 734 | if (jent_notime_settick(ec)) { |
| 735 | jent_entropy_collector_free(ec); |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | /* Rerun startup sequence */ |
| 740 | continue; |
| 741 | } |
| 742 | } while (ec->startup_state != jent_startup_completed); |
| 743 | |
| 744 | jent_notime_unsettick(ec); |
| 745 | |
| 746 | return ec; |
| 747 | } |
| 748 | |
| 749 | JENT_PRIVATE_STATIC |
| 750 | struct rand_data *jent_entropy_collector_alloc(unsigned int osr, |
| 751 | unsigned int flags) |
| 752 | { |
| 753 | struct rand_data *ec = _jent_entropy_collector_alloc(osr, flags); |
| 754 | |
| 755 | if (!ec) |
| 756 | return ec; |
| 757 | |
| 758 | /* |
| 759 | * Define max_mem_set only if the external caller defined such memory. |
| 760 | */ |
| 761 | ec->max_mem_set = !!JENT_FLAGS_TO_MAX_MEMSIZE(flags); |
| 762 | |
| 763 | return ec; |
| 764 | } |
| 765 | |
| 766 | JENT_PRIVATE_STATIC |
| 767 | void jent_entropy_collector_free(struct rand_data *entropy_collector) |
| 768 | { |
| 769 | if (entropy_collector != NULL) { |
| 770 | /* Safety measure */ |
| 771 | jent_notime_unsettick(entropy_collector); |
| 772 | |
| 773 | jent_notime_disable(entropy_collector); |
| 774 | |
| 775 | if (entropy_collector->hash_state != NULL) { |
| 776 | jent_sha3_dealloc(entropy_collector->hash_state); |
| 777 | entropy_collector->hash_state = NULL; |
| 778 | } |
| 779 | |
| 780 | if (entropy_collector->mem != NULL) { |
| 781 | jent_zfree(entropy_collector->mem, |
| 782 | jent_memsize(entropy_collector->flags)); |
| 783 | entropy_collector->mem = NULL; |
| 784 | } |
| 785 | jent_zfree(entropy_collector, sizeof(struct rand_data)); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | int jent_time_entropy_init(unsigned int osr, unsigned int flags) |
| 790 | { |
| 791 | struct rand_data *ec; |
| 792 | uint64_t *delta_history; |
| 793 | int i, time_backwards = 0, count_stuck = 0, ret = 0; |
| 794 | unsigned int health_test_result; |
| 795 | |
| 796 | delta_history = jent_gcd_init(JENT_POWERUP_TESTLOOPCOUNT); |
| 797 | if (!delta_history) |
| 798 | return EMEM; |
| 799 | |
| 800 | if (flags & JENT_FORCE_INTERNAL_TIMER) |
| 801 | jent_notime_force(); |
| 802 | else |
| 803 | flags |= JENT_DISABLE_INTERNAL_TIMER; |
| 804 | |
| 805 | /* |
| 806 | * If the start-up health tests (including the APT and RCT) are not |
| 807 | * run, then the entropy source is not 90B compliant. We could test if |
| 808 | * fips_enabled should be set using the jent_fips_enabled() function, |
| 809 | * but this can be overridden using the JENT_FORCE_FIPS flag, which |
| 810 | * isn't passed in yet. It is better to run the tests on the small |
| 811 | * amount of data that we have, which should not fail unless things |
| 812 | * are really bad. |
| 813 | */ |
| 814 | flags |= JENT_FORCE_FIPS; |
| 815 | ec = jent_entropy_collector_alloc_internal(osr, flags); |
| 816 | if (!ec) { |
| 817 | ret = EMEM; |
| 818 | goto out; |
| 819 | } |
| 820 | |
| 821 | if (jent_notime_settick(ec)) { |
| 822 | ret = EMEM; |
| 823 | goto out; |
| 824 | } |
| 825 | |
| 826 | /* To initialize the prior time. */ |
| 827 | jent_measure_jitter(ec, 0, NULL); |
| 828 | |
| 829 | /* We could perform statistical tests here, but the problem is |
| 830 | * that we only have a few loop counts to do testing. These |
| 831 | * loop counts may show some slight skew leading to false positives. |
| 832 | */ |
| 833 | |
| 834 | /* |
| 835 | * We could add a check for system capabilities such as clock_getres or |
| 836 | * check for CONFIG_X86_TSC, but it does not make much sense as the |
| 837 | * following sanity checks verify that we have a high-resolution |
| 838 | * timer. |
| 839 | */ |
| 840 | #define CLEARCACHE 100 |
| 841 | for (i = -CLEARCACHE; i < JENT_POWERUP_TESTLOOPCOUNT; i++) { |
| 842 | uint64_t start_time = 0, end_time = 0, delta = 0; |
| 843 | unsigned int stuck; |
| 844 | |
| 845 | /* Invoke core entropy collection logic */ |
| 846 | stuck = jent_measure_jitter(ec, 0, &delta); |
| 847 | end_time = ec->prev_time; |
| 848 | start_time = ec->prev_time - delta; |
| 849 | |
| 850 | /* test whether timer works */ |
| 851 | if (!start_time || !end_time) { |
| 852 | ret = ENOTIME; |
| 853 | goto out; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * test whether timer is fine grained enough to provide |
| 858 | * delta even when called shortly after each other -- this |
| 859 | * implies that we also have a high resolution timer |
| 860 | */ |
| 861 | if (!delta) { |
| 862 | ret = ECOARSETIME; |
| 863 | goto out; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * up to here we did not modify any variable that will be |
| 868 | * evaluated later, but we already performed some work. Thus we |
| 869 | * already have had an impact on the caches, branch prediction, |
| 870 | * etc. with the goal to clear it to get the worst case |
| 871 | * measurements. |
| 872 | */ |
| 873 | if (i < 0) |
| 874 | continue; |
| 875 | |
| 876 | if (stuck) |
| 877 | count_stuck++; |
| 878 | |
| 879 | /* test whether we have an increasing timer */ |
| 880 | if (!(end_time > start_time)) |
| 881 | time_backwards++; |
| 882 | |
| 883 | /* Watch for common adjacent GCD values */ |
| 884 | jent_gcd_add_value(delta_history, delta, i); |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * we allow up to three times the time running backwards. |
| 889 | * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus, |
| 890 | * if such an operation just happens to interfere with our test, it |
| 891 | * should not fail. The value of 3 should cover the NTP case being |
| 892 | * performed during our test run. |
| 893 | */ |
| 894 | if (time_backwards > 3) { |
| 895 | ret = ENOMONOTONIC; |
| 896 | goto out; |
| 897 | } |
| 898 | |
| 899 | /* First, did we encounter a health test failure? */ |
| 900 | if ((health_test_result = jent_health_failure(ec))) { |
| 901 | ret = (health_test_result & JENT_RCT_FAILURE) ? ERCT : EHEALTH; |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | ret = jent_gcd_analyze(delta_history, JENT_POWERUP_TESTLOOPCOUNT, |
| 906 | ec->osr); |
| 907 | if (ret) |
| 908 | goto out; |
| 909 | |
| 910 | /* |
| 911 | * If we have more than 90% stuck results, then this Jitter RNG is |
| 912 | * likely to not work well. |
| 913 | */ |
| 914 | if (JENT_STUCK_INIT_THRES(JENT_POWERUP_TESTLOOPCOUNT) < count_stuck) |
| 915 | ret = ESTUCK; |
| 916 | |
| 917 | out: |
| 918 | jent_gcd_fini(delta_history, JENT_POWERUP_TESTLOOPCOUNT); |
| 919 | |
| 920 | /* NOOP if notime disabled. Can be done unconditionally */ |
| 921 | if (ec) |
| 922 | jent_notime_unsettick(ec); |
| 923 | |
| 924 | jent_entropy_collector_free(ec); |
| 925 | |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | static inline int jent_entropy_init_common_pre(void) |
| 930 | { |
| 931 | int ret; |
| 932 | |
| 933 | jent_notime_block_switch(); |
| 934 | jent_health_cb_block_switch(); |
| 935 | |
| 936 | if (jent_sha3_tester()) |
| 937 | return EHASH; |
| 938 | |
| 939 | ret = jent_gcd_selftest(); |
| 940 | |
| 941 | jent_selftest_run = 1; |
| 942 | |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | static inline int jent_entropy_init_common_post(int ret) |
| 947 | { |
| 948 | /* Unmark the execution of the self tests if they failed. */ |
| 949 | if (ret) |
| 950 | jent_selftest_run = 0; |
| 951 | |
| 952 | return ret; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Note: This function and jent_entropy_init_ex() are NOT thread-safe. |
| 957 | * They modify global state (GCD, self-test flag, notime configuration). |
| 958 | * The caller must ensure that initialization is performed exactly once |
| 959 | * before any concurrent use of the library (e.g. via pthread_once or |
| 960 | * equivalent). |
| 961 | */ |
| 962 | JENT_PRIVATE_STATIC |
| 963 | int jent_entropy_init(void) |
| 964 | { |
| 965 | int ret = jent_entropy_init_common_pre(); |
| 966 | |
| 967 | if (ret) |
| 968 | return ret; |
| 969 | |
| 970 | ret = jent_time_entropy_init(0, JENT_DISABLE_INTERNAL_TIMER); |
| 971 | |
| 972 | #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER |
| 973 | if (ret) |
| 974 | ret = jent_time_entropy_init(0, JENT_FORCE_INTERNAL_TIMER); |
| 975 | #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */ |
| 976 | |
| 977 | return jent_entropy_init_common_post(ret); |
| 978 | } |
| 979 | |
| 980 | JENT_PRIVATE_STATIC |
| 981 | int jent_entropy_init_ex(unsigned int osr, unsigned int flags) |
| 982 | { |
| 983 | int ret = jent_entropy_init_common_pre(); |
| 984 | |
| 985 | if (ret) |
| 986 | return ret; |
| 987 | |
| 988 | ret = ENOTIME; |
| 989 | |
| 990 | /* Test without internal timer unless caller does not want it */ |
| 991 | if (!(flags & JENT_FORCE_INTERNAL_TIMER)) |
| 992 | ret = jent_time_entropy_init(osr, |
| 993 | flags | JENT_DISABLE_INTERNAL_TIMER); |
| 994 | |
| 995 | #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER |
| 996 | /* Test with internal timer unless caller does not want it */ |
| 997 | if (ret && !(flags & JENT_DISABLE_INTERNAL_TIMER)) |
| 998 | ret = jent_time_entropy_init(osr, |
| 999 | flags | JENT_FORCE_INTERNAL_TIMER); |
| 1000 | #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */ |
| 1001 | |
| 1002 | return jent_entropy_init_common_post(ret); |
| 1003 | } |
| 1004 | |
| 1005 | JENT_PRIVATE_STATIC |
| 1006 | int jent_entropy_switch_notime_impl(struct jent_notime_thread *new_thread) |
| 1007 | { |
| 1008 | return jent_notime_switch(new_thread); |
| 1009 | } |
| 1010 | |
| 1011 | JENT_PRIVATE_STATIC |
| 1012 | int jent_set_fips_failure_callback(jent_fips_failure_cb cb) |
| 1013 | { |
| 1014 | return jent_set_fips_failure_callback_internal(cb); |
| 1015 | } |