You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

758 lines
21 KiB

  1. /*
  2. * Non-physical true random number generator based on timing jitter.
  3. *
  4. * Copyright Stephan Mueller <smueller@chronox.de>, 2014 - 2022
  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. #include "jitterentropy.h"
  32. #include "jitterentropy-base.h"
  33. #include "jitterentropy-gcd.h"
  34. #include "jitterentropy-health.h"
  35. #include "jitterentropy-noise.h"
  36. #include "jitterentropy-timer.h"
  37. #include "jitterentropy-sha3.h"
  38. #define MAJVERSION 3 /* API / ABI incompatible changes, functional changes that
  39. * require consumer to be updated (as long as this number
  40. * is zero, the API is not considered stable and can
  41. * change without a bump of the major version) */
  42. #define MINVERSION 4 /* API compatible, ABI may change, functional
  43. * enhancements only, consumer can be left unchanged if
  44. * enhancements are not considered */
  45. #define PATCHLEVEL 1 /* API / ABI compatible, no functional changes, no
  46. * enhancements, bug fixes only */
  47. /***************************************************************************
  48. * Jitter RNG Static Definitions
  49. *
  50. * None of the following should be altered
  51. ***************************************************************************/
  52. #ifdef __OPTIMIZE__
  53. #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
  54. #endif
  55. /*
  56. * JENT_POWERUP_TESTLOOPCOUNT needs some loops to identify edge
  57. * systems. 100 is definitely too little.
  58. *
  59. * SP800-90B requires at least 1024 initial test cycles.
  60. */
  61. #define JENT_POWERUP_TESTLOOPCOUNT 1024
  62. /**
  63. * jent_version() - Return machine-usable version number of jent library
  64. *
  65. * The function returns a version number that is monotonic increasing
  66. * for newer versions. The version numbers are multiples of 100. For example,
  67. * version 1.2.3 is converted to 1020300 -- the last two digits are reserved
  68. * for future use.
  69. *
  70. * The result of this function can be used in comparing the version number
  71. * in a calling program if version-specific calls need to be make.
  72. *
  73. * @return Version number of jitterentropy library
  74. */
  75. JENT_PRIVATE_STATIC
  76. unsigned int jent_version(void)
  77. {
  78. unsigned int version = 0;
  79. version = MAJVERSION * 1000000;
  80. version += MINVERSION * 10000;
  81. version += PATCHLEVEL * 100;
  82. return version;
  83. }
  84. /***************************************************************************
  85. * Helper
  86. ***************************************************************************/
  87. /* Calculate log2 of given value assuming that the value is a power of 2 */
  88. static inline unsigned int jent_log2_simple(unsigned int val)
  89. {
  90. unsigned int idx = 0;
  91. while (val >>= 1)
  92. idx++;
  93. return idx;
  94. }
  95. /* Increase the memory size by one step */
  96. static inline unsigned int jent_update_memsize(unsigned int flags)
  97. {
  98. unsigned int global_max = JENT_FLAGS_TO_MAX_MEMSIZE(
  99. JENT_MAX_MEMSIZE_MAX);
  100. unsigned int max;
  101. max = JENT_FLAGS_TO_MAX_MEMSIZE(flags);
  102. if (!max) {
  103. /*
  104. * The safe starting value is the amount of memory we allocated
  105. * last round.
  106. */
  107. max = jent_log2_simple(JENT_MEMORY_SIZE);
  108. /* Adjust offset */
  109. max = (max > JENT_MAX_MEMSIZE_OFFSET) ?
  110. max - JENT_MAX_MEMSIZE_OFFSET : 0;
  111. } else {
  112. max++;
  113. }
  114. max = (max > global_max) ? global_max : max;
  115. /* Clear out the max size */
  116. flags &= ~JENT_MAX_MEMSIZE_MASK;
  117. /* Set the freshly calculated max size */
  118. flags |= JENT_MAX_MEMSIZE_TO_FLAGS(max);
  119. return flags;
  120. }
  121. /***************************************************************************
  122. * Random Number Generation
  123. ***************************************************************************/
  124. /**
  125. * Entry function: Obtain entropy for the caller.
  126. *
  127. * This function invokes the entropy gathering logic as often to generate
  128. * as many bytes as requested by the caller. The entropy gathering logic
  129. * creates 64 bit per invocation.
  130. *
  131. * This function truncates the last 64 bit entropy value output to the exact
  132. * size specified by the caller.
  133. *
  134. * @ec [in] Reference to entropy collector
  135. * @data [out] pointer to buffer for storing random data -- buffer must
  136. * already exist
  137. * @len [in] size of the buffer, specifying also the requested number of random
  138. * in bytes
  139. *
  140. * @return number of bytes returned when request is fulfilled or an error
  141. *
  142. * The following error codes can occur:
  143. * -1 entropy_collector is NULL
  144. * -2 RCT failed
  145. * -3 APT test failed
  146. * -4 The timer cannot be initialized
  147. * -5 LAG failure
  148. */
  149. JENT_PRIVATE_STATIC
  150. ssize_t jent_read_entropy(struct rand_data *ec, char *data, size_t len)
  151. {
  152. char *p = data;
  153. size_t orig_len = len;
  154. int ret = 0;
  155. if (NULL == ec)
  156. return -1;
  157. if (jent_notime_settick(ec))
  158. return -4;
  159. while (len > 0) {
  160. size_t tocopy;
  161. unsigned int health_test_result;
  162. jent_random_data(ec);
  163. if ((health_test_result = jent_health_failure(ec))) {
  164. if (health_test_result & JENT_RCT_FAILURE)
  165. ret = -2;
  166. else if (health_test_result & JENT_APT_FAILURE)
  167. ret = -3;
  168. else
  169. ret = -5;
  170. goto err;
  171. }
  172. if ((DATA_SIZE_BITS / 8) < len)
  173. tocopy = (DATA_SIZE_BITS / 8);
  174. else
  175. tocopy = len;
  176. jent_read_random_block(ec, p, tocopy);
  177. len -= tocopy;
  178. p += tocopy;
  179. }
  180. /*
  181. * Enhanced backtracking support: At this point, the hash state
  182. * contains the digest of the previous Jitter RNG collection round
  183. * which is inserted there by jent_read_random_block with the SHA
  184. * update operation. At the current code location we completed
  185. * one request for a caller and we do not know how long it will
  186. * take until a new request is sent to us. To guarantee enhanced
  187. * backtracking resistance at this point (i.e. ensure that an attacker
  188. * cannot obtain information about prior random numbers we generated),
  189. * but still stirring the hash state with old data the Jitter RNG
  190. * obtains a new message digest from its state and re-inserts it.
  191. * After this operation, the Jitter RNG state is still stirred with
  192. * the old data, but an attacker who gets access to the memory after
  193. * this point cannot deduce the random numbers produced by the
  194. * Jitter RNG prior to this point.
  195. */
  196. /*
  197. * If we use secured memory, where backtracking support may not be
  198. * needed because the state is protected in a different method,
  199. * it is permissible to drop this support. But strongly weigh the
  200. * pros and cons considering that the SHA3 operation is not that
  201. * expensive.
  202. */
  203. #ifndef CONFIG_CRYPTO_CPU_JITTERENTROPY_SECURE_MEMORY
  204. jent_read_random_block(ec, NULL, 0);
  205. #endif
  206. err:
  207. jent_notime_unsettick(ec);
  208. return ret ? ret : (ssize_t)orig_len;
  209. }
  210. static struct rand_data *_jent_entropy_collector_alloc(unsigned int osr,
  211. unsigned int flags);
  212. /**
  213. * Entry function: Obtain entropy for the caller.
  214. *
  215. * This is a service function to jent_read_entropy() with the difference
  216. * that it automatically re-allocates the entropy collector if a health
  217. * test failure is observed. Before reallocation, a new power-on health test
  218. * is performed. The allocation of the new entropy collector automatically
  219. * increases the OSR by one. This is done based on the idea that a health
  220. * test failure indicates that the assumed entropy rate is too high.
  221. *
  222. * Note the function returns with an health test error if the OSR is
  223. * getting too large. If an error is returned by this function, the Jitter RNG
  224. * is not safe to be used on the current system.
  225. *
  226. * @ec [in] Reference to entropy collector - this is a double pointer as
  227. * The entropy collector may be freed and reallocated.
  228. * @data [out] pointer to buffer for storing random data -- buffer must
  229. * already exist
  230. * @len [in] size of the buffer, specifying also the requested number of random
  231. * in bytes
  232. *
  233. * @return see jent_read_entropy()
  234. */
  235. JENT_PRIVATE_STATIC
  236. ssize_t jent_read_entropy_safe(struct rand_data **ec, char *data, size_t len)
  237. {
  238. char *p = data;
  239. size_t orig_len = len;
  240. ssize_t ret = 0;
  241. if (!ec)
  242. return -1;
  243. while (len > 0) {
  244. unsigned int osr, flags, max_mem_set;
  245. ret = jent_read_entropy(*ec, p, len);
  246. switch (ret) {
  247. case -1:
  248. case -4:
  249. return ret;
  250. case -2:
  251. case -3:
  252. case -5:
  253. osr = (*ec)->osr + 1;
  254. flags = (*ec)->flags;
  255. max_mem_set = (*ec)->max_mem_set;
  256. /* generic arbitrary cutoff */
  257. if (osr > 20)
  258. return ret;
  259. /*
  260. * If the caller did not set any specific maximum value
  261. * let the Jitter RNG increase the maximum memory by
  262. * one step.
  263. */
  264. if (!max_mem_set)
  265. flags = jent_update_memsize(flags);
  266. /*
  267. * re-allocate entropy collector with higher OSR and
  268. * memory size
  269. */
  270. jent_entropy_collector_free(*ec);
  271. /* Perform new health test with updated OSR */
  272. if (jent_entropy_init_ex(osr, flags))
  273. return -1;
  274. *ec = _jent_entropy_collector_alloc(osr, flags);
  275. if (!*ec)
  276. return -1;
  277. /* Remember whether caller configured memory size */
  278. (*ec)->max_mem_set = !!max_mem_set;
  279. break;
  280. default:
  281. len -= (size_t)ret;
  282. p += (size_t)ret;
  283. }
  284. }
  285. return (ssize_t)orig_len;
  286. }
  287. /***************************************************************************
  288. * Initialization logic
  289. ***************************************************************************/
  290. /*
  291. * Obtain memory size to allocate for memory access variations.
  292. *
  293. * The maximum variations we can get from the memory access is when we allocate
  294. * a bit more memory than we have as data cache. But allocating as much
  295. * memory as we have as data cache might strain the resources on the system
  296. * more than necessary.
  297. *
  298. * On a lot of systems it is not necessary to need so much memory as the
  299. * variations coming from the general Jitter RNG execution commonly provide
  300. * large amount of variations.
  301. *
  302. * Thus, the default is:
  303. *
  304. * min(JENT_MEMORY_SIZE, data cache size)
  305. *
  306. * In case the data cache size cannot be obtained, use JENT_MEMORY_SIZE.
  307. *
  308. * If the caller provides a maximum memory size, use
  309. * min(provided max memory, data cache size).
  310. */
  311. static inline uint32_t jent_memsize(unsigned int flags)
  312. {
  313. uint32_t memsize, max_memsize;
  314. max_memsize = JENT_FLAGS_TO_MAX_MEMSIZE(flags);
  315. if (max_memsize == 0) {
  316. max_memsize = JENT_MEMORY_SIZE;
  317. } else {
  318. max_memsize = UINT32_C(1) << (max_memsize +
  319. JENT_MAX_MEMSIZE_OFFSET);
  320. }
  321. /* Allocate memory for adding variations based on memory access */
  322. memsize = jent_cache_size_roundup();
  323. /* Limit the memory as defined by caller */
  324. memsize = (memsize > max_memsize) ? max_memsize : memsize;
  325. /* Set a value if none was found */
  326. if (!memsize)
  327. memsize = JENT_MEMORY_SIZE;
  328. return memsize;
  329. }
  330. static int jent_selftest_run = 0;
  331. static struct rand_data
  332. *jent_entropy_collector_alloc_internal(unsigned int osr, unsigned int flags)
  333. {
  334. struct rand_data *entropy_collector;
  335. uint32_t memsize = 0;
  336. /*
  337. * Requesting disabling and forcing of internal timer
  338. * makes no sense.
  339. */
  340. if ((flags & JENT_DISABLE_INTERNAL_TIMER) &&
  341. (flags & JENT_FORCE_INTERNAL_TIMER))
  342. return NULL;
  343. /* Force the self test to be run */
  344. if (!jent_selftest_run && jent_entropy_init_ex(osr, flags))
  345. return NULL;
  346. /*
  347. * If the initial test code concludes to force the internal timer
  348. * and the user requests it not to be used, do not allocate
  349. * the Jitter RNG instance.
  350. */
  351. if (jent_notime_forced() && (flags & JENT_DISABLE_INTERNAL_TIMER))
  352. return NULL;
  353. entropy_collector = jent_zalloc(sizeof(struct rand_data));
  354. if (NULL == entropy_collector)
  355. return NULL;
  356. if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
  357. memsize = jent_memsize(flags);
  358. entropy_collector->mem = (unsigned char *)jent_zalloc(memsize);
  359. #ifdef JENT_RANDOM_MEMACCESS
  360. /*
  361. * Transform the size into a mask - it is assumed that size is
  362. * a power of 2.
  363. */
  364. entropy_collector->memmask = memsize - 1;
  365. #else /* JENT_RANDOM_MEMACCESS */
  366. entropy_collector->memblocksize = memsize / JENT_MEMORY_BLOCKS;
  367. entropy_collector->memblocks = JENT_MEMORY_BLOCKS;
  368. /* sanity check */
  369. if (entropy_collector->memblocksize *
  370. entropy_collector->memblocks != memsize)
  371. goto err;
  372. #endif /* JENT_RANDOM_MEMACCESS */
  373. if (entropy_collector->mem == NULL)
  374. goto err;
  375. entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
  376. }
  377. if (sha3_alloc(&entropy_collector->hash_state))
  378. goto err;
  379. /* Initialize the hash state */
  380. sha3_256_init(entropy_collector->hash_state);
  381. /* verify and set the oversampling rate */
  382. if (osr < JENT_MIN_OSR)
  383. osr = JENT_MIN_OSR;
  384. entropy_collector->osr = osr;
  385. entropy_collector->flags = flags;
  386. if ((flags & JENT_FORCE_FIPS) || jent_fips_enabled())
  387. entropy_collector->fips_enabled = 1;
  388. /* Initialize the APT */
  389. jent_apt_init(entropy_collector, osr);
  390. /* Initialize the Lag Predictor Test */
  391. jent_lag_init(entropy_collector, osr);
  392. /* Was jent_entropy_init run (establishing the common GCD)? */
  393. if (jent_gcd_get(&entropy_collector->jent_common_timer_gcd)) {
  394. /*
  395. * It was not. This should probably be an error, but this
  396. * behavior breaks the test code. Set the gcd to a value that
  397. * won't hurt anything.
  398. */
  399. entropy_collector->jent_common_timer_gcd = 1;
  400. }
  401. /*
  402. * Use timer-less noise source - note, OSR must be set in
  403. * entropy_collector!
  404. */
  405. if (!(flags & JENT_DISABLE_INTERNAL_TIMER)) {
  406. if (jent_notime_enable(entropy_collector, flags))
  407. goto err;
  408. }
  409. return entropy_collector;
  410. err:
  411. if (entropy_collector->mem != NULL)
  412. jent_zfree(entropy_collector->mem, memsize);
  413. jent_zfree(entropy_collector, sizeof(struct rand_data));
  414. return NULL;
  415. }
  416. static struct rand_data *_jent_entropy_collector_alloc(unsigned int osr,
  417. unsigned int flags)
  418. {
  419. struct rand_data *ec = jent_entropy_collector_alloc_internal(osr,
  420. flags);
  421. if (!ec)
  422. return ec;
  423. /* fill the data pad with non-zero values */
  424. if (jent_notime_settick(ec)) {
  425. jent_entropy_collector_free(ec);
  426. return NULL;
  427. }
  428. jent_random_data(ec);
  429. jent_notime_unsettick(ec);
  430. return ec;
  431. }
  432. JENT_PRIVATE_STATIC
  433. struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
  434. unsigned int flags)
  435. {
  436. struct rand_data *ec = _jent_entropy_collector_alloc(osr, flags);
  437. /* Remember that the caller provided a maximum size flag */
  438. if (ec)
  439. ec->max_mem_set = !!JENT_FLAGS_TO_MAX_MEMSIZE(flags);
  440. return ec;
  441. }
  442. JENT_PRIVATE_STATIC
  443. void jent_entropy_collector_free(struct rand_data *entropy_collector)
  444. {
  445. if (entropy_collector != NULL) {
  446. sha3_dealloc(entropy_collector->hash_state);
  447. jent_notime_disable(entropy_collector);
  448. if (entropy_collector->mem != NULL) {
  449. jent_zfree(entropy_collector->mem,
  450. jent_memsize(entropy_collector->flags));
  451. entropy_collector->mem = NULL;
  452. }
  453. jent_zfree(entropy_collector, sizeof(struct rand_data));
  454. }
  455. }
  456. int jent_time_entropy_init(unsigned int osr, unsigned int flags)
  457. {
  458. struct rand_data *ec;
  459. uint64_t *delta_history;
  460. int i, time_backwards = 0, count_stuck = 0, ret = 0;
  461. unsigned int health_test_result;
  462. delta_history = jent_gcd_init(JENT_POWERUP_TESTLOOPCOUNT);
  463. if (!delta_history)
  464. return EMEM;
  465. if (flags & JENT_FORCE_INTERNAL_TIMER)
  466. jent_notime_force();
  467. else
  468. flags |= JENT_DISABLE_INTERNAL_TIMER;
  469. /*
  470. * If the start-up health tests (including the APT and RCT) are not
  471. * run, then the entropy source is not 90B compliant. We could test if
  472. * fips_enabled should be set using the jent_fips_enabled() function,
  473. * but this can be overridden using the JENT_FORCE_FIPS flag, which
  474. * isn't passed in yet. It is better to run the tests on the small
  475. * amount of data that we have, which should not fail unless things
  476. * are really bad.
  477. */
  478. flags |= JENT_FORCE_FIPS;
  479. ec = jent_entropy_collector_alloc_internal(osr, flags);
  480. if (!ec) {
  481. ret = EMEM;
  482. goto out;
  483. }
  484. if (jent_notime_settick(ec)) {
  485. ret = EMEM;
  486. goto out;
  487. }
  488. /* To initialize the prior time. */
  489. jent_measure_jitter(ec, 0, NULL);
  490. /* We could perform statistical tests here, but the problem is
  491. * that we only have a few loop counts to do testing. These
  492. * loop counts may show some slight skew leading to false positives.
  493. */
  494. /*
  495. * We could add a check for system capabilities such as clock_getres or
  496. * check for CONFIG_X86_TSC, but it does not make much sense as the
  497. * following sanity checks verify that we have a high-resolution
  498. * timer.
  499. */
  500. #define CLEARCACHE 100
  501. for (i = -CLEARCACHE; i < JENT_POWERUP_TESTLOOPCOUNT; i++) {
  502. uint64_t start_time = 0, end_time = 0, delta = 0;
  503. unsigned int stuck;
  504. /* Invoke core entropy collection logic */
  505. stuck = jent_measure_jitter(ec, 0, &delta);
  506. end_time = ec->prev_time;
  507. start_time = ec->prev_time - delta;
  508. /* test whether timer works */
  509. if (!start_time || !end_time) {
  510. ret = ENOTIME;
  511. goto out;
  512. }
  513. /*
  514. * test whether timer is fine grained enough to provide
  515. * delta even when called shortly after each other -- this
  516. * implies that we also have a high resolution timer
  517. */
  518. if (!delta || (end_time == start_time)) {
  519. ret = ECOARSETIME;
  520. goto out;
  521. }
  522. /*
  523. * up to here we did not modify any variable that will be
  524. * evaluated later, but we already performed some work. Thus we
  525. * already have had an impact on the caches, branch prediction,
  526. * etc. with the goal to clear it to get the worst case
  527. * measurements.
  528. */
  529. if (i < 0)
  530. continue;
  531. if (stuck)
  532. count_stuck++;
  533. /* test whether we have an increasing timer */
  534. if (!(end_time > start_time))
  535. time_backwards++;
  536. /* Watch for common adjacent GCD values */
  537. jent_gcd_add_value(delta_history, delta, i);
  538. }
  539. /*
  540. * we allow up to three times the time running backwards.
  541. * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
  542. * if such an operation just happens to interfere with our test, it
  543. * should not fail. The value of 3 should cover the NTP case being
  544. * performed during our test run.
  545. */
  546. if (time_backwards > 3) {
  547. ret = ENOMONOTONIC;
  548. goto out;
  549. }
  550. /* First, did we encounter a health test failure? */
  551. if ((health_test_result = jent_health_failure(ec))) {
  552. ret = (health_test_result & JENT_RCT_FAILURE) ? ERCT : EHEALTH;
  553. goto out;
  554. }
  555. ret = jent_gcd_analyze(delta_history, JENT_POWERUP_TESTLOOPCOUNT);
  556. if (ret)
  557. goto out;
  558. /*
  559. * If we have more than 90% stuck results, then this Jitter RNG is
  560. * likely to not work well.
  561. */
  562. if (JENT_STUCK_INIT_THRES(JENT_POWERUP_TESTLOOPCOUNT) < count_stuck)
  563. ret = ESTUCK;
  564. out:
  565. jent_gcd_fini(delta_history, JENT_POWERUP_TESTLOOPCOUNT);
  566. if ((flags & JENT_FORCE_INTERNAL_TIMER) && ec)
  567. jent_notime_unsettick(ec);
  568. jent_entropy_collector_free(ec);
  569. return ret;
  570. }
  571. static inline int jent_entropy_init_common_pre(void)
  572. {
  573. int ret;
  574. jent_notime_block_switch();
  575. jent_health_cb_block_switch();
  576. if (sha3_tester())
  577. return EHASH;
  578. ret = jent_gcd_selftest();
  579. jent_selftest_run = 1;
  580. return ret;
  581. }
  582. static inline int jent_entropy_init_common_post(int ret)
  583. {
  584. /* Unmark the execution of the self tests if they failed. */
  585. if (ret)
  586. jent_selftest_run = 0;
  587. return ret;
  588. }
  589. JENT_PRIVATE_STATIC
  590. int jent_entropy_init(void)
  591. {
  592. int ret = jent_entropy_init_common_pre();
  593. if (ret)
  594. return ret;
  595. ret = jent_time_entropy_init(0, JENT_DISABLE_INTERNAL_TIMER);
  596. #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
  597. if (ret)
  598. ret = jent_time_entropy_init(0, JENT_FORCE_INTERNAL_TIMER);
  599. #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */
  600. return jent_entropy_init_common_post(ret);
  601. }
  602. JENT_PRIVATE_STATIC
  603. int jent_entropy_init_ex(unsigned int osr, unsigned int flags)
  604. {
  605. int ret = jent_entropy_init_common_pre();
  606. if (ret)
  607. return ret;
  608. ret = ENOTIME;
  609. /* Test without internal timer unless caller does not want it */
  610. if (!(flags & JENT_FORCE_INTERNAL_TIMER))
  611. ret = jent_time_entropy_init(osr,
  612. flags | JENT_DISABLE_INTERNAL_TIMER);
  613. #ifdef JENT_CONF_ENABLE_INTERNAL_TIMER
  614. /* Test with internal timer unless caller does not want it */
  615. if (ret && !(flags & JENT_DISABLE_INTERNAL_TIMER))
  616. ret = jent_time_entropy_init(osr,
  617. flags | JENT_FORCE_INTERNAL_TIMER);
  618. #endif /* JENT_CONF_ENABLE_INTERNAL_TIMER */
  619. return jent_entropy_init_common_post(ret);
  620. }
  621. JENT_PRIVATE_STATIC
  622. int jent_entropy_switch_notime_impl(struct jent_notime_thread *new_thread)
  623. {
  624. return jent_notime_switch(new_thread);
  625. }
  626. JENT_PRIVATE_STATIC
  627. int jent_set_fips_failure_callback(jent_fips_failure_cb cb)
  628. {
  629. return jent_set_fips_failure_callback_internal(cb);
  630. }