jitterentropy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Non-physical true random number generator based on timing jitter --
  3. * Jitter RNG standalone code.
  4. *
  5. * Copyright Stephan Mueller <smueller@chronox.de>, 2015 - 2023
  6. *
  7. * Design
  8. * ======
  9. *
  10. * See https://www.chronox.de/jent.html
  11. *
  12. * License
  13. * =======
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, and the entire permission notice in its entirety,
  20. * including the disclaimer of warranties.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * ALTERNATIVELY, this product may be distributed under the terms of
  29. * the GNU General Public License, in which case the provisions of the GPL2 are
  30. * required INSTEAD OF the above restrictions. (This clause is
  31. * necessary due to a potential bad interaction between the GPL and
  32. * the restrictions contained in a BSD-style copyright.)
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
  37. * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
  38. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  39. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  40. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  41. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  42. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44. * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
  45. * DAMAGE.
  46. */
  47. /*
  48. * This Jitterentropy RNG is based on the jitterentropy library
  49. * version 3.4.0 provided at https://www.chronox.de/jent.html
  50. */
  51. #ifdef __OPTIMIZE__
  52. #error "The CPU Jitter random number generator must not be compiled with optimizations. See documentation. Use the compiler switch -O0 for compiling jitterentropy.c."
  53. #endif
  54. typedef unsigned long long __u64;
  55. typedef long long __s64;
  56. typedef unsigned int __u32;
  57. typedef unsigned char u8;
  58. #define NULL ((void *) 0)
  59. /* The entropy pool */
  60. struct rand_data {
  61. /* SHA3-256 is used as conditioner */
  62. #define DATA_SIZE_BITS 256
  63. /* all data values that are vital to maintain the security
  64. * of the RNG are marked as SENSITIVE. A user must not
  65. * access that information while the RNG executes its loops to
  66. * calculate the next random value. */
  67. void *hash_state; /* SENSITIVE hash state entropy pool */
  68. __u64 prev_time; /* SENSITIVE Previous time stamp */
  69. __u64 last_delta; /* SENSITIVE stuck test */
  70. __s64 last_delta2; /* SENSITIVE stuck test */
  71. unsigned int flags; /* Flags used to initialize */
  72. unsigned int osr; /* Oversample rate */
  73. #define JENT_MEMORY_ACCESSLOOPS 128
  74. #define JENT_MEMORY_SIZE \
  75. (CONFIG_CRYPTO_JITTERENTROPY_MEMORY_BLOCKS * \
  76. CONFIG_CRYPTO_JITTERENTROPY_MEMORY_BLOCKSIZE)
  77. unsigned char *mem; /* Memory access location with size of
  78. * memblocks * memblocksize */
  79. unsigned int memlocation; /* Pointer to byte in *mem */
  80. unsigned int memblocks; /* Number of memory blocks in *mem */
  81. unsigned int memblocksize; /* Size of one memory block in bytes */
  82. unsigned int memaccessloops; /* Number of memory accesses per random
  83. * bit generation */
  84. /* Repetition Count Test */
  85. unsigned int rct_count; /* Number of stuck values */
  86. /* Adaptive Proportion Test cutoff values */
  87. unsigned int apt_cutoff; /* Intermittent health test failure */
  88. unsigned int apt_cutoff_permanent; /* Permanent health test failure */
  89. #define JENT_APT_WINDOW_SIZE 512 /* Data window size */
  90. /* LSB of time stamp to process */
  91. #define JENT_APT_LSB 16
  92. #define JENT_APT_WORD_MASK (JENT_APT_LSB - 1)
  93. unsigned int apt_observations; /* Number of collected observations */
  94. unsigned int apt_count; /* APT counter */
  95. unsigned int apt_base; /* APT base reference */
  96. unsigned int health_failure; /* Record health failure */
  97. unsigned int apt_base_set:1; /* APT base reference set? */
  98. };
  99. /* Flags that can be used to initialize the RNG */
  100. #define JENT_DISABLE_MEMORY_ACCESS (1<<2) /* Disable memory access for more
  101. * entropy, saves MEMORY_SIZE RAM for
  102. * entropy collector */
  103. /* -- error codes for init function -- */
  104. #define JENT_ENOTIME 1 /* Timer service not available */
  105. #define JENT_ECOARSETIME 2 /* Timer too coarse for RNG */
  106. #define JENT_ENOMONOTONIC 3 /* Timer is not monotonic increasing */
  107. #define JENT_EVARVAR 5 /* Timer does not produce variations of
  108. * variations (2nd derivation of time is
  109. * zero). */
  110. #define JENT_ESTUCK 8 /* Too many stuck results during init. */
  111. #define JENT_EHEALTH 9 /* Health test failed during initialization */
  112. #define JENT_ERCT 10 /* RCT failed during initialization */
  113. #define JENT_EHASH 11 /* Hash self test failed */
  114. #define JENT_EMEM 12 /* Can't allocate memory for initialization */
  115. #define JENT_RCT_FAILURE 1 /* Failure in RCT health test. */
  116. #define JENT_APT_FAILURE 2 /* Failure in APT health test. */
  117. #define JENT_PERMANENT_FAILURE_SHIFT 16
  118. #define JENT_PERMANENT_FAILURE(x) (x << JENT_PERMANENT_FAILURE_SHIFT)
  119. #define JENT_RCT_FAILURE_PERMANENT JENT_PERMANENT_FAILURE(JENT_RCT_FAILURE)
  120. #define JENT_APT_FAILURE_PERMANENT JENT_PERMANENT_FAILURE(JENT_APT_FAILURE)
  121. /*
  122. * The output n bits can receive more than n bits of min entropy, of course,
  123. * but the fixed output of the conditioning function can only asymptotically
  124. * approach the output size bits of min entropy, not attain that bound. Random
  125. * maps will tend to have output collisions, which reduces the creditable
  126. * output entropy (that is what SP 800-90B Section 3.1.5.1.2 attempts to bound).
  127. *
  128. * The value "64" is justified in Appendix A.4 of the current 90C draft,
  129. * and aligns with NIST's in "epsilon" definition in this document, which is
  130. * that a string can be considered "full entropy" if you can bound the min
  131. * entropy in each bit of output to at least 1-epsilon, where epsilon is
  132. * required to be <= 2^(-32).
  133. */
  134. #define JENT_ENTROPY_SAFETY_FACTOR 64
  135. #include <linux/fips.h>
  136. #include <linux/minmax.h>
  137. #include "jitterentropy.h"
  138. /***************************************************************************
  139. * Adaptive Proportion Test
  140. *
  141. * This test complies with SP800-90B section 4.4.2.
  142. ***************************************************************************/
  143. /*
  144. * See the SP 800-90B comment #10b for the corrected cutoff for the SP 800-90B
  145. * APT.
  146. * https://www.untruth.org/~josh/sp80090b/UL%20SP800-90B-final%20comments%20v1.9%2020191212.pdf
  147. * In the syntax of R, this is C = 2 + qbinom(1 − 2^(−30), 511, 2^(-1/osr)).
  148. * (The original formula wasn't correct because the first symbol must
  149. * necessarily have been observed, so there is no chance of observing 0 of these
  150. * symbols.)
  151. *
  152. * For the alpha < 2^-53, R cannot be used as it uses a float data type without
  153. * arbitrary precision. A SageMath script is used to calculate those cutoff
  154. * values.
  155. *
  156. * For any value above 14, this yields the maximal allowable value of 512
  157. * (by FIPS 140-2 IG 7.19 Resolution # 16, we cannot choose a cutoff value that
  158. * renders the test unable to fail).
  159. */
  160. static const unsigned int jent_apt_cutoff_lookup[15] = {
  161. 325, 422, 459, 477, 488, 494, 499, 502,
  162. 505, 507, 508, 509, 510, 511, 512 };
  163. static const unsigned int jent_apt_cutoff_permanent_lookup[15] = {
  164. 355, 447, 479, 494, 502, 507, 510, 512,
  165. 512, 512, 512, 512, 512, 512, 512 };
  166. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  167. static void jent_apt_init(struct rand_data *ec, unsigned int osr)
  168. {
  169. /*
  170. * Establish the apt_cutoff based on the presumed entropy rate of
  171. * 1/osr.
  172. */
  173. if (osr >= ARRAY_SIZE(jent_apt_cutoff_lookup)) {
  174. ec->apt_cutoff = jent_apt_cutoff_lookup[
  175. ARRAY_SIZE(jent_apt_cutoff_lookup) - 1];
  176. ec->apt_cutoff_permanent = jent_apt_cutoff_permanent_lookup[
  177. ARRAY_SIZE(jent_apt_cutoff_permanent_lookup) - 1];
  178. } else {
  179. ec->apt_cutoff = jent_apt_cutoff_lookup[osr - 1];
  180. ec->apt_cutoff_permanent =
  181. jent_apt_cutoff_permanent_lookup[osr - 1];
  182. }
  183. }
  184. /*
  185. * Reset the APT counter
  186. *
  187. * @ec [in] Reference to entropy collector
  188. */
  189. static void jent_apt_reset(struct rand_data *ec, unsigned int delta_masked)
  190. {
  191. /* Reset APT counter */
  192. ec->apt_count = 0;
  193. ec->apt_base = delta_masked;
  194. ec->apt_observations = 0;
  195. }
  196. /*
  197. * Insert a new entropy event into APT
  198. *
  199. * @ec [in] Reference to entropy collector
  200. * @delta_masked [in] Masked time delta to process
  201. */
  202. static void jent_apt_insert(struct rand_data *ec, unsigned int delta_masked)
  203. {
  204. /* Initialize the base reference */
  205. if (!ec->apt_base_set) {
  206. ec->apt_base = delta_masked;
  207. ec->apt_base_set = 1;
  208. return;
  209. }
  210. if (delta_masked == ec->apt_base) {
  211. ec->apt_count++;
  212. /* Note, ec->apt_count starts with one. */
  213. if (ec->apt_count >= ec->apt_cutoff_permanent)
  214. ec->health_failure |= JENT_APT_FAILURE_PERMANENT;
  215. else if (ec->apt_count >= ec->apt_cutoff)
  216. ec->health_failure |= JENT_APT_FAILURE;
  217. }
  218. ec->apt_observations++;
  219. if (ec->apt_observations >= JENT_APT_WINDOW_SIZE)
  220. jent_apt_reset(ec, delta_masked);
  221. }
  222. /***************************************************************************
  223. * Stuck Test and its use as Repetition Count Test
  224. *
  225. * The Jitter RNG uses an enhanced version of the Repetition Count Test
  226. * (RCT) specified in SP800-90B section 4.4.1. Instead of counting identical
  227. * back-to-back values, the input to the RCT is the counting of the stuck
  228. * values during the generation of one Jitter RNG output block.
  229. *
  230. * The RCT is applied with an alpha of 2^{-30} compliant to FIPS 140-2 IG 9.8.
  231. *
  232. * During the counting operation, the Jitter RNG always calculates the RCT
  233. * cut-off value of C. If that value exceeds the allowed cut-off value,
  234. * the Jitter RNG output block will be calculated completely but discarded at
  235. * the end. The caller of the Jitter RNG is informed with an error code.
  236. ***************************************************************************/
  237. /*
  238. * Repetition Count Test as defined in SP800-90B section 4.4.1
  239. *
  240. * @ec [in] Reference to entropy collector
  241. * @stuck [in] Indicator whether the value is stuck
  242. */
  243. static void jent_rct_insert(struct rand_data *ec, int stuck)
  244. {
  245. if (stuck) {
  246. ec->rct_count++;
  247. /*
  248. * The cutoff value is based on the following consideration:
  249. * alpha = 2^-30 or 2^-60 as recommended in SP800-90B.
  250. * In addition, we require an entropy value H of 1/osr as this
  251. * is the minimum entropy required to provide full entropy.
  252. * Note, we collect (DATA_SIZE_BITS + ENTROPY_SAFETY_FACTOR)*osr
  253. * deltas for inserting them into the entropy pool which should
  254. * then have (close to) DATA_SIZE_BITS bits of entropy in the
  255. * conditioned output.
  256. *
  257. * Note, ec->rct_count (which equals to value B in the pseudo
  258. * code of SP800-90B section 4.4.1) starts with zero. Hence
  259. * we need to subtract one from the cutoff value as calculated
  260. * following SP800-90B. Thus C = ceil(-log_2(alpha)/H) = 30*osr
  261. * or 60*osr.
  262. */
  263. if ((unsigned int)ec->rct_count >= (60 * ec->osr)) {
  264. ec->rct_count = -1;
  265. ec->health_failure |= JENT_RCT_FAILURE_PERMANENT;
  266. } else if ((unsigned int)ec->rct_count >= (30 * ec->osr)) {
  267. ec->rct_count = -1;
  268. ec->health_failure |= JENT_RCT_FAILURE;
  269. }
  270. } else {
  271. /* Reset RCT */
  272. ec->rct_count = 0;
  273. }
  274. }
  275. static inline __u64 jent_delta(__u64 prev, __u64 next)
  276. {
  277. #define JENT_UINT64_MAX (__u64)(~((__u64) 0))
  278. return (prev < next) ? (next - prev) :
  279. (JENT_UINT64_MAX - prev + 1 + next);
  280. }
  281. /*
  282. * Stuck test by checking the:
  283. * 1st derivative of the jitter measurement (time delta)
  284. * 2nd derivative of the jitter measurement (delta of time deltas)
  285. * 3rd derivative of the jitter measurement (delta of delta of time deltas)
  286. *
  287. * All values must always be non-zero.
  288. *
  289. * @ec [in] Reference to entropy collector
  290. * @current_delta [in] Jitter time delta
  291. *
  292. * @return
  293. * 0 jitter measurement not stuck (good bit)
  294. * 1 jitter measurement stuck (reject bit)
  295. */
  296. static int jent_stuck(struct rand_data *ec, __u64 current_delta)
  297. {
  298. __u64 delta2 = jent_delta(ec->last_delta, current_delta);
  299. __u64 delta3 = jent_delta(ec->last_delta2, delta2);
  300. ec->last_delta = current_delta;
  301. ec->last_delta2 = delta2;
  302. /*
  303. * Insert the result of the comparison of two back-to-back time
  304. * deltas.
  305. */
  306. jent_apt_insert(ec, current_delta);
  307. if (!current_delta || !delta2 || !delta3) {
  308. /* RCT with a stuck bit */
  309. jent_rct_insert(ec, 1);
  310. return 1;
  311. }
  312. /* RCT with a non-stuck bit */
  313. jent_rct_insert(ec, 0);
  314. return 0;
  315. }
  316. /*
  317. * Report any health test failures
  318. *
  319. * @ec [in] Reference to entropy collector
  320. *
  321. * @return a bitmask indicating which tests failed
  322. * 0 No health test failure
  323. * 1 RCT failure
  324. * 2 APT failure
  325. * 1<<JENT_PERMANENT_FAILURE_SHIFT RCT permanent failure
  326. * 2<<JENT_PERMANENT_FAILURE_SHIFT APT permanent failure
  327. */
  328. static unsigned int jent_health_failure(struct rand_data *ec)
  329. {
  330. /* Test is only enabled in FIPS mode */
  331. if (!fips_enabled)
  332. return 0;
  333. return ec->health_failure;
  334. }
  335. /***************************************************************************
  336. * Noise sources
  337. ***************************************************************************/
  338. /*
  339. * Update of the loop count used for the next round of
  340. * an entropy collection.
  341. *
  342. * Input:
  343. * @bits is the number of low bits of the timer to consider
  344. * @min is the number of bits we shift the timer value to the right at
  345. * the end to make sure we have a guaranteed minimum value
  346. *
  347. * @return Newly calculated loop counter
  348. */
  349. static __u64 jent_loop_shuffle(unsigned int bits, unsigned int min)
  350. {
  351. __u64 time = 0;
  352. __u64 shuffle = 0;
  353. unsigned int i = 0;
  354. unsigned int mask = (1<<bits) - 1;
  355. jent_get_nstime(&time);
  356. /*
  357. * We fold the time value as much as possible to ensure that as many
  358. * bits of the time stamp are included as possible.
  359. */
  360. for (i = 0; ((DATA_SIZE_BITS + bits - 1) / bits) > i; i++) {
  361. shuffle ^= time & mask;
  362. time = time >> bits;
  363. }
  364. /*
  365. * We add a lower boundary value to ensure we have a minimum
  366. * RNG loop count.
  367. */
  368. return (shuffle + (1<<min));
  369. }
  370. /*
  371. * CPU Jitter noise source -- this is the noise source based on the CPU
  372. * execution time jitter
  373. *
  374. * This function injects the individual bits of the time value into the
  375. * entropy pool using a hash.
  376. *
  377. * ec [in] entropy collector
  378. * time [in] time stamp to be injected
  379. * stuck [in] Is the time stamp identified as stuck?
  380. *
  381. * Output:
  382. * updated hash context in the entropy collector or error code
  383. */
  384. static int jent_condition_data(struct rand_data *ec, __u64 time, int stuck)
  385. {
  386. #define SHA3_HASH_LOOP (1<<3)
  387. struct {
  388. int rct_count;
  389. unsigned int apt_observations;
  390. unsigned int apt_count;
  391. unsigned int apt_base;
  392. } addtl = {
  393. ec->rct_count,
  394. ec->apt_observations,
  395. ec->apt_count,
  396. ec->apt_base
  397. };
  398. return jent_hash_time(ec->hash_state, time, (u8 *)&addtl, sizeof(addtl),
  399. SHA3_HASH_LOOP, stuck);
  400. }
  401. /*
  402. * Memory Access noise source -- this is a noise source based on variations in
  403. * memory access times
  404. *
  405. * This function performs memory accesses which will add to the timing
  406. * variations due to an unknown amount of CPU wait states that need to be
  407. * added when accessing memory. The memory size should be larger than the L1
  408. * caches as outlined in the documentation and the associated testing.
  409. *
  410. * The L1 cache has a very high bandwidth, albeit its access rate is usually
  411. * slower than accessing CPU registers. Therefore, L1 accesses only add minimal
  412. * variations as the CPU has hardly to wait. Starting with L2, significant
  413. * variations are added because L2 typically does not belong to the CPU any more
  414. * and therefore a wider range of CPU wait states is necessary for accesses.
  415. * L3 and real memory accesses have even a wider range of wait states. However,
  416. * to reliably access either L3 or memory, the ec->mem memory must be quite
  417. * large which is usually not desirable.
  418. *
  419. * @ec [in] Reference to the entropy collector with the memory access data -- if
  420. * the reference to the memory block to be accessed is NULL, this noise
  421. * source is disabled
  422. * @loop_cnt [in] if a value not equal to 0 is set, use the given value
  423. * number of loops to perform the LFSR
  424. */
  425. static void jent_memaccess(struct rand_data *ec, __u64 loop_cnt)
  426. {
  427. unsigned int wrap = 0;
  428. __u64 i = 0;
  429. #define MAX_ACC_LOOP_BIT 7
  430. #define MIN_ACC_LOOP_BIT 0
  431. __u64 acc_loop_cnt =
  432. jent_loop_shuffle(MAX_ACC_LOOP_BIT, MIN_ACC_LOOP_BIT);
  433. if (NULL == ec || NULL == ec->mem)
  434. return;
  435. wrap = ec->memblocksize * ec->memblocks;
  436. /*
  437. * testing purposes -- allow test app to set the counter, not
  438. * needed during runtime
  439. */
  440. if (loop_cnt)
  441. acc_loop_cnt = loop_cnt;
  442. for (i = 0; i < (ec->memaccessloops + acc_loop_cnt); i++) {
  443. unsigned char *tmpval = ec->mem + ec->memlocation;
  444. /*
  445. * memory access: just add 1 to one byte,
  446. * wrap at 255 -- memory access implies read
  447. * from and write to memory location
  448. */
  449. *tmpval = (*tmpval + 1) & 0xff;
  450. /*
  451. * Addition of memblocksize - 1 to pointer
  452. * with wrap around logic to ensure that every
  453. * memory location is hit evenly
  454. */
  455. ec->memlocation = ec->memlocation + ec->memblocksize - 1;
  456. ec->memlocation = ec->memlocation % wrap;
  457. }
  458. }
  459. /***************************************************************************
  460. * Start of entropy processing logic
  461. ***************************************************************************/
  462. /*
  463. * This is the heart of the entropy generation: calculate time deltas and
  464. * use the CPU jitter in the time deltas. The jitter is injected into the
  465. * entropy pool.
  466. *
  467. * WARNING: ensure that ->prev_time is primed before using the output
  468. * of this function! This can be done by calling this function
  469. * and not using its result.
  470. *
  471. * @ec [in] Reference to entropy collector
  472. *
  473. * @return result of stuck test
  474. */
  475. static int jent_measure_jitter(struct rand_data *ec, __u64 *ret_current_delta)
  476. {
  477. __u64 time = 0;
  478. __u64 current_delta = 0;
  479. int stuck;
  480. /* Invoke one noise source before time measurement to add variations */
  481. jent_memaccess(ec, 0);
  482. /*
  483. * Get time stamp and calculate time delta to previous
  484. * invocation to measure the timing variations
  485. */
  486. jent_get_nstime(&time);
  487. current_delta = jent_delta(ec->prev_time, time);
  488. ec->prev_time = time;
  489. /* Check whether we have a stuck measurement. */
  490. stuck = jent_stuck(ec, current_delta);
  491. /* Now call the next noise sources which also injects the data */
  492. if (jent_condition_data(ec, current_delta, stuck))
  493. stuck = 1;
  494. /* return the raw entropy value */
  495. if (ret_current_delta)
  496. *ret_current_delta = current_delta;
  497. return stuck;
  498. }
  499. /*
  500. * Generator of one 64 bit random number
  501. * Function fills rand_data->hash_state
  502. *
  503. * @ec [in] Reference to entropy collector
  504. */
  505. static void jent_gen_entropy(struct rand_data *ec)
  506. {
  507. unsigned int k = 0, safety_factor = 0;
  508. if (fips_enabled)
  509. safety_factor = JENT_ENTROPY_SAFETY_FACTOR;
  510. /* priming of the ->prev_time value */
  511. jent_measure_jitter(ec, NULL);
  512. while (!jent_health_failure(ec)) {
  513. /* If a stuck measurement is received, repeat measurement */
  514. if (jent_measure_jitter(ec, NULL))
  515. continue;
  516. /*
  517. * We multiply the loop value with ->osr to obtain the
  518. * oversampling rate requested by the caller
  519. */
  520. if (++k >= ((DATA_SIZE_BITS + safety_factor) * ec->osr))
  521. break;
  522. }
  523. }
  524. /*
  525. * Entry function: Obtain entropy for the caller.
  526. *
  527. * This function invokes the entropy gathering logic as often to generate
  528. * as many bytes as requested by the caller. The entropy gathering logic
  529. * creates 64 bit per invocation.
  530. *
  531. * This function truncates the last 64 bit entropy value output to the exact
  532. * size specified by the caller.
  533. *
  534. * @ec [in] Reference to entropy collector
  535. * @data [in] pointer to buffer for storing random data -- buffer must already
  536. * exist
  537. * @len [in] size of the buffer, specifying also the requested number of random
  538. * in bytes
  539. *
  540. * @return 0 when request is fulfilled or an error
  541. *
  542. * The following error codes can occur:
  543. * -1 entropy_collector is NULL or the generation failed
  544. * -2 Intermittent health failure
  545. * -3 Permanent health failure
  546. */
  547. int jent_read_entropy(struct rand_data *ec, unsigned char *data,
  548. unsigned int len)
  549. {
  550. unsigned char *p = data;
  551. if (!ec)
  552. return -1;
  553. while (len > 0) {
  554. unsigned int tocopy, health_test_result;
  555. jent_gen_entropy(ec);
  556. health_test_result = jent_health_failure(ec);
  557. if (health_test_result > JENT_PERMANENT_FAILURE_SHIFT) {
  558. /*
  559. * At this point, the Jitter RNG instance is considered
  560. * as a failed instance. There is no rerun of the
  561. * startup test any more, because the caller
  562. * is assumed to not further use this instance.
  563. */
  564. return -3;
  565. } else if (health_test_result) {
  566. /*
  567. * Perform startup health tests and return permanent
  568. * error if it fails.
  569. */
  570. if (jent_entropy_init(0, 0, NULL, ec)) {
  571. /* Mark the permanent error */
  572. ec->health_failure &=
  573. JENT_RCT_FAILURE_PERMANENT |
  574. JENT_APT_FAILURE_PERMANENT;
  575. return -3;
  576. }
  577. return -2;
  578. }
  579. tocopy = min(DATA_SIZE_BITS / 8, len);
  580. if (jent_read_random_block(ec->hash_state, p, tocopy))
  581. return -1;
  582. len -= tocopy;
  583. p += tocopy;
  584. }
  585. return 0;
  586. }
  587. /***************************************************************************
  588. * Initialization logic
  589. ***************************************************************************/
  590. struct rand_data *jent_entropy_collector_alloc(unsigned int osr,
  591. unsigned int flags,
  592. void *hash_state)
  593. {
  594. struct rand_data *entropy_collector;
  595. entropy_collector = jent_zalloc(sizeof(struct rand_data));
  596. if (!entropy_collector)
  597. return NULL;
  598. if (!(flags & JENT_DISABLE_MEMORY_ACCESS)) {
  599. /* Allocate memory for adding variations based on memory
  600. * access
  601. */
  602. entropy_collector->mem = jent_kvzalloc(JENT_MEMORY_SIZE);
  603. if (!entropy_collector->mem) {
  604. jent_zfree(entropy_collector);
  605. return NULL;
  606. }
  607. entropy_collector->memblocksize =
  608. CONFIG_CRYPTO_JITTERENTROPY_MEMORY_BLOCKSIZE;
  609. entropy_collector->memblocks =
  610. CONFIG_CRYPTO_JITTERENTROPY_MEMORY_BLOCKS;
  611. entropy_collector->memaccessloops = JENT_MEMORY_ACCESSLOOPS;
  612. }
  613. /* verify and set the oversampling rate */
  614. if (osr == 0)
  615. osr = 1; /* H_submitter = 1 / osr */
  616. entropy_collector->osr = osr;
  617. entropy_collector->flags = flags;
  618. entropy_collector->hash_state = hash_state;
  619. /* Initialize the APT */
  620. jent_apt_init(entropy_collector, osr);
  621. /* fill the data pad with non-zero values */
  622. jent_gen_entropy(entropy_collector);
  623. return entropy_collector;
  624. }
  625. void jent_entropy_collector_free(struct rand_data *entropy_collector)
  626. {
  627. jent_kvzfree(entropy_collector->mem, JENT_MEMORY_SIZE);
  628. entropy_collector->mem = NULL;
  629. jent_zfree(entropy_collector);
  630. }
  631. int jent_entropy_init(unsigned int osr, unsigned int flags, void *hash_state,
  632. struct rand_data *p_ec)
  633. {
  634. /*
  635. * If caller provides an allocated ec, reuse it which implies that the
  636. * health test entropy data is used to further still the available
  637. * entropy pool.
  638. */
  639. struct rand_data *ec = p_ec;
  640. int i, time_backwards = 0, ret = 0, ec_free = 0;
  641. unsigned int health_test_result;
  642. if (!ec) {
  643. ec = jent_entropy_collector_alloc(osr, flags, hash_state);
  644. if (!ec)
  645. return JENT_EMEM;
  646. ec_free = 1;
  647. } else {
  648. /* Reset the APT */
  649. jent_apt_reset(ec, 0);
  650. /* Ensure that a new APT base is obtained */
  651. ec->apt_base_set = 0;
  652. /* Reset the RCT */
  653. ec->rct_count = 0;
  654. /* Reset intermittent, leave permanent health test result */
  655. ec->health_failure &= (~JENT_RCT_FAILURE);
  656. ec->health_failure &= (~JENT_APT_FAILURE);
  657. }
  658. /* We could perform statistical tests here, but the problem is
  659. * that we only have a few loop counts to do testing. These
  660. * loop counts may show some slight skew and we produce
  661. * false positives.
  662. *
  663. * Moreover, only old systems show potentially problematic
  664. * jitter entropy that could potentially be caught here. But
  665. * the RNG is intended for hardware that is available or widely
  666. * used, but not old systems that are long out of favor. Thus,
  667. * no statistical tests.
  668. */
  669. /*
  670. * We could add a check for system capabilities such as clock_getres or
  671. * check for CONFIG_X86_TSC, but it does not make much sense as the
  672. * following sanity checks verify that we have a high-resolution
  673. * timer.
  674. */
  675. /*
  676. * TESTLOOPCOUNT needs some loops to identify edge systems. 100 is
  677. * definitely too little.
  678. *
  679. * SP800-90B requires at least 1024 initial test cycles.
  680. */
  681. #define TESTLOOPCOUNT 1024
  682. #define CLEARCACHE 100
  683. for (i = 0; (TESTLOOPCOUNT + CLEARCACHE) > i; i++) {
  684. __u64 start_time = 0, end_time = 0, delta = 0;
  685. /* Invoke core entropy collection logic */
  686. jent_measure_jitter(ec, &delta);
  687. end_time = ec->prev_time;
  688. start_time = ec->prev_time - delta;
  689. /* test whether timer works */
  690. if (!start_time || !end_time) {
  691. ret = JENT_ENOTIME;
  692. goto out;
  693. }
  694. /*
  695. * test whether timer is fine grained enough to provide
  696. * delta even when called shortly after each other -- this
  697. * implies that we also have a high resolution timer
  698. */
  699. if (!delta || (end_time == start_time)) {
  700. ret = JENT_ECOARSETIME;
  701. goto out;
  702. }
  703. /*
  704. * up to here we did not modify any variable that will be
  705. * evaluated later, but we already performed some work. Thus we
  706. * already have had an impact on the caches, branch prediction,
  707. * etc. with the goal to clear it to get the worst case
  708. * measurements.
  709. */
  710. if (i < CLEARCACHE)
  711. continue;
  712. /* test whether we have an increasing timer */
  713. if (!(end_time > start_time))
  714. time_backwards++;
  715. }
  716. /*
  717. * we allow up to three times the time running backwards.
  718. * CLOCK_REALTIME is affected by adjtime and NTP operations. Thus,
  719. * if such an operation just happens to interfere with our test, it
  720. * should not fail. The value of 3 should cover the NTP case being
  721. * performed during our test run.
  722. */
  723. if (time_backwards > 3) {
  724. ret = JENT_ENOMONOTONIC;
  725. goto out;
  726. }
  727. /* Did we encounter a health test failure? */
  728. health_test_result = jent_health_failure(ec);
  729. if (health_test_result) {
  730. ret = (health_test_result & JENT_RCT_FAILURE) ? JENT_ERCT :
  731. JENT_EHEALTH;
  732. goto out;
  733. }
  734. out:
  735. if (ec_free)
  736. jent_entropy_collector_free(ec);
  737. return ret;
  738. }