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.

entropy.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Entropy source
  27. *
  28. * This algorithm is designed to comply with ANS X9.82 Part 4 (April
  29. * 2011 Draft) Section 13.3. This standard is unfortunately not
  30. * freely available.
  31. */
  32. #include <stdint.h>
  33. #include <assert.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <ipxe/crypto.h>
  37. #include <ipxe/hash_df.h>
  38. #include <ipxe/entropy.h>
  39. /* Disambiguate the various error causes */
  40. #define EPIPE_REPETITION_COUNT_TEST \
  41. __einfo_error ( EINFO_EPIPE_REPETITION_COUNT_TEST )
  42. #define EINFO_EPIPE_REPETITION_COUNT_TEST \
  43. __einfo_uniqify ( EINFO_EPIPE, 0x01, "Repetition count test failed" )
  44. #define EPIPE_ADAPTIVE_PROPORTION_TEST \
  45. __einfo_error ( EINFO_EPIPE_ADAPTIVE_PROPORTION_TEST )
  46. #define EINFO_EPIPE_ADAPTIVE_PROPORTION_TEST \
  47. __einfo_uniqify ( EINFO_EPIPE, 0x02, "Adaptive proportion test failed" )
  48. /**
  49. * Calculate cutoff value for the repetition count test
  50. *
  51. * @ret cutoff Cutoff value
  52. *
  53. * This is the cutoff value for the Repetition Count Test defined in
  54. * ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.2.
  55. */
  56. static inline __attribute__ (( always_inline )) unsigned int
  57. repetition_count_cutoff ( void ) {
  58. double max_repetitions;
  59. unsigned int cutoff;
  60. /* The cutoff formula for the repetition test is:
  61. *
  62. * C = ( 1 + ( -log2(W) / H_min ) )
  63. *
  64. * where W is set at 2^(-30) (in ANS X9.82 Part 2 (October
  65. * 2011 Draft) Section 8.5.2.1.3.1).
  66. */
  67. max_repetitions = ( 1 + ( 30 / min_entropy_per_sample() ) );
  68. /* Round up to a whole number of repetitions. We don't have
  69. * the ceil() function available, so do the rounding by hand.
  70. */
  71. cutoff = max_repetitions;
  72. if ( cutoff < max_repetitions )
  73. cutoff++;
  74. linker_assert ( ( cutoff >= max_repetitions ), rounding_error );
  75. /* Floating-point operations are not allowed in iPXE since we
  76. * never set up a suitable environment. Abort the build
  77. * unless the calculated number of repetitions is a
  78. * compile-time constant.
  79. */
  80. linker_assert ( __builtin_constant_p ( cutoff ),
  81. repetition_count_cutoff_not_constant );
  82. return cutoff;
  83. }
  84. /**
  85. * Perform repetition count test
  86. *
  87. * @v sample Noise sample
  88. * @ret rc Return status code
  89. *
  90. * This is the Repetition Count Test defined in ANS X9.82 Part 2
  91. * (October 2011 Draft) Section 8.5.2.1.2.
  92. */
  93. static int repetition_count_test ( noise_sample_t sample ) {
  94. static noise_sample_t most_recent_sample;
  95. static unsigned int repetition_count = 0;
  96. /* A = the most recently seen sample value
  97. * B = the number of times that value A has been seen in a row
  98. * C = the cutoff value above which the repetition test should fail
  99. */
  100. /* 1. For each new sample processed:
  101. *
  102. * (Note that the test for "repetition_count > 0" ensures that
  103. * the initial value of most_recent_sample is treated as being
  104. * undefined.)
  105. */
  106. if ( ( sample == most_recent_sample ) && ( repetition_count > 0 ) ) {
  107. /* a) If the new sample = A, then B is incremented by one. */
  108. repetition_count++;
  109. /* i. If B >= C, then an error condition is raised
  110. * due to a failure of the test
  111. */
  112. if ( repetition_count >= repetition_count_cutoff() )
  113. return -EPIPE_REPETITION_COUNT_TEST;
  114. } else {
  115. /* b) Else:
  116. * i. A = new sample
  117. */
  118. most_recent_sample = sample;
  119. /* ii. B = 1 */
  120. repetition_count = 1;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * Window size for the adaptive proportion test
  126. *
  127. * ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.1 allows
  128. * five possible window sizes: 16, 64, 256, 4096 and 65536.
  129. *
  130. * We expect to generate relatively few (<256) entropy samples during
  131. * a typical iPXE run; the use of a large window size would mean that
  132. * the test would never complete a single cycle. We use a window size
  133. * of 64, which is the smallest window size that permits values of
  134. * H_min down to one bit per sample.
  135. */
  136. #define ADAPTIVE_PROPORTION_WINDOW_SIZE 64
  137. /**
  138. * Combine adaptive proportion test window size and min-entropy
  139. *
  140. * @v n N (window size)
  141. * @v h H (min-entropy)
  142. * @ret n_h (N,H) combined value
  143. */
  144. #define APC_N_H( n, h ) ( ( (n) << 8 ) | (h) )
  145. /**
  146. * Define a row of the adaptive proportion cutoff table
  147. *
  148. * @v h H (min-entropy)
  149. * @v c16 Cutoff for N=16
  150. * @v c64 Cutoff for N=64
  151. * @v c256 Cutoff for N=256
  152. * @v c4096 Cutoff for N=4096
  153. * @v c65536 Cutoff for N=65536
  154. */
  155. #define APC_TABLE_ROW( h, c16, c64, c256, c4096, c65536) \
  156. case APC_N_H ( 16, h ) : return c16; \
  157. case APC_N_H ( 64, h ) : return c64; \
  158. case APC_N_H ( 256, h ) : return c256; \
  159. case APC_N_H ( 4096, h ) : return c4096; \
  160. case APC_N_H ( 65536, h ) : return c65536;
  161. /** Value used to represent "N/A" in adaptive proportion cutoff table */
  162. #define APC_NA 0
  163. /**
  164. * Look up value in adaptive proportion test cutoff table
  165. *
  166. * @v n N (window size)
  167. * @v h H (min-entropy)
  168. * @ret cutoff Cutoff
  169. *
  170. * This is the table of cutoff values defined in ANS X9.82 Part 2
  171. * (October 2011 Draft) Section 8.5.2.1.3.1.2.
  172. */
  173. static inline __attribute__ (( always_inline )) unsigned int
  174. adaptive_proportion_cutoff_lookup ( unsigned int n, unsigned int h ) {
  175. switch ( APC_N_H ( n, h ) ) {
  176. APC_TABLE_ROW ( 1, APC_NA, 51, 168, 2240, 33537 );
  177. APC_TABLE_ROW ( 2, APC_NA, 35, 100, 1193, 17053 );
  178. APC_TABLE_ROW ( 3, 10, 24, 61, 643, 8705 );
  179. APC_TABLE_ROW ( 4, 8, 16, 38, 354, 4473 );
  180. APC_TABLE_ROW ( 5, 6, 12, 25, 200, 2321 );
  181. APC_TABLE_ROW ( 6, 5, 9, 17, 117, 1220 );
  182. APC_TABLE_ROW ( 7, 4, 7, 15, 71, 653 );
  183. APC_TABLE_ROW ( 8, 4, 5, 9, 45, 358 );
  184. APC_TABLE_ROW ( 9, 3, 4, 7, 30, 202 );
  185. APC_TABLE_ROW ( 10, 3, 4, 5, 21, 118 );
  186. APC_TABLE_ROW ( 11, 2, 3, 4, 15, 71 );
  187. APC_TABLE_ROW ( 12, 2, 3, 4, 11, 45 );
  188. APC_TABLE_ROW ( 13, 2, 2, 3, 9, 30 );
  189. APC_TABLE_ROW ( 14, 2, 2, 3, 7, 21 );
  190. APC_TABLE_ROW ( 15, 1, 2, 2, 6, 15 );
  191. APC_TABLE_ROW ( 16, 1, 2, 2, 5, 11 );
  192. APC_TABLE_ROW ( 17, 1, 1, 2, 4, 9 );
  193. APC_TABLE_ROW ( 18, 1, 1, 2, 4, 7 );
  194. APC_TABLE_ROW ( 19, 1, 1, 1, 3, 6 );
  195. APC_TABLE_ROW ( 20, 1, 1, 1, 3, 5 );
  196. default:
  197. return APC_NA;
  198. }
  199. }
  200. /**
  201. * Calculate cutoff value for the adaptive proportion test
  202. *
  203. * @ret cutoff Cutoff value
  204. *
  205. * This is the cutoff value for the Adaptive Proportion Test defined
  206. * in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.1.2.
  207. */
  208. static inline __attribute__ (( always_inline )) unsigned int
  209. adaptive_proportion_cutoff ( void ) {
  210. unsigned int h;
  211. unsigned int n;
  212. unsigned int cutoff;
  213. /* Look up cutoff value in cutoff table */
  214. n = ADAPTIVE_PROPORTION_WINDOW_SIZE;
  215. h = min_entropy_per_sample();
  216. cutoff = adaptive_proportion_cutoff_lookup ( n, h );
  217. /* Fail unless cutoff value is a build-time constant */
  218. linker_assert ( __builtin_constant_p ( cutoff ),
  219. adaptive_proportion_cutoff_not_constant );
  220. /* Fail if cutoff value is N/A */
  221. linker_assert ( ( cutoff != APC_NA ),
  222. adaptive_proportion_cutoff_not_applicable );
  223. return cutoff;
  224. }
  225. /**
  226. * Perform adaptive proportion test
  227. *
  228. * @v sample Noise sample
  229. * @ret rc Return status code
  230. *
  231. * This is the Adaptive Proportion Test for the Most Common Value
  232. * defined in ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.3.
  233. */
  234. static int adaptive_proportion_test ( noise_sample_t sample ) {
  235. static noise_sample_t current_counted_sample;
  236. static unsigned int sample_count = ADAPTIVE_PROPORTION_WINDOW_SIZE;
  237. static unsigned int repetition_count;
  238. /* A = the sample value currently being counted
  239. * B = the number of samples examined in this run of the test so far
  240. * N = the total number of samples that must be observed in
  241. * one run of the test, also known as the "window size" of
  242. * the test
  243. * B = the current number of times that S (sic) has been seen
  244. * in the W (sic) samples examined so far
  245. * C = the cutoff value above which the repetition test should fail
  246. * W = the probability of a false positive: 2^-30
  247. */
  248. /* 1. The entropy source draws the current sample from the
  249. * noise source.
  250. *
  251. * (Nothing to do; we already have the current sample.)
  252. */
  253. /* 2. If S = N, then a new run of the test begins: */
  254. if ( sample_count == ADAPTIVE_PROPORTION_WINDOW_SIZE ) {
  255. /* a. A = the current sample */
  256. current_counted_sample = sample;
  257. /* b. S = 0 */
  258. sample_count = 0;
  259. /* c. B = 0 */
  260. repetition_count = 0;
  261. } else {
  262. /* Else: (the test is already running)
  263. * a. S = S + 1
  264. */
  265. sample_count++;
  266. /* b. If A = the current sample, then: */
  267. if ( sample == current_counted_sample ) {
  268. /* i. B = B + 1 */
  269. repetition_count++;
  270. /* ii. If S (sic) > C then raise an error
  271. * condition, because the test has
  272. * detected a failure
  273. */
  274. if ( repetition_count > adaptive_proportion_cutoff() )
  275. return -EPIPE_ADAPTIVE_PROPORTION_TEST;
  276. }
  277. }
  278. return 0;
  279. }
  280. /**
  281. * Get entropy sample
  282. *
  283. * @ret entropy Entropy sample
  284. * @ret rc Return status code
  285. *
  286. * This is the GetEntropy function defined in ANS X9.82 Part 2
  287. * (October 2011 Draft) Section 6.5.1.
  288. */
  289. static int get_entropy ( entropy_sample_t *entropy ) {
  290. static int rc = 0;
  291. noise_sample_t noise;
  292. /* Any failure is permanent */
  293. if ( rc != 0 )
  294. return rc;
  295. /* Get noise sample */
  296. if ( ( rc = get_noise ( &noise ) ) != 0 )
  297. return rc;
  298. /* Perform Repetition Count Test and Adaptive Proportion Test
  299. * as mandated by ANS X9.82 Part 2 (October 2011 Draft)
  300. * Section 8.5.2.1.1.
  301. */
  302. if ( ( rc = repetition_count_test ( noise ) ) != 0 )
  303. return rc;
  304. if ( ( rc = adaptive_proportion_test ( noise ) ) != 0 )
  305. return rc;
  306. /* We do not use any optional conditioning component */
  307. *entropy = noise;
  308. return 0;
  309. }
  310. /**
  311. * Calculate number of samples required for startup tests
  312. *
  313. * @ret num_samples Number of samples required
  314. *
  315. * ANS X9.82 Part 2 (October 2011 Draft) Section 8.5.2.1.5 requires
  316. * that at least one full cycle of the continuous tests must be
  317. * performed at start-up.
  318. */
  319. static inline __attribute__ (( always_inline )) unsigned int
  320. startup_test_count ( void ) {
  321. unsigned int num_samples;
  322. /* At least max(N,C) samples shall be generated by the noise
  323. * source for start-up testing.
  324. */
  325. num_samples = repetition_count_cutoff();
  326. if ( num_samples < adaptive_proportion_cutoff() )
  327. num_samples = adaptive_proportion_cutoff();
  328. linker_assert ( __builtin_constant_p ( num_samples ),
  329. startup_test_count_not_constant );
  330. return num_samples;
  331. }
  332. /**
  333. * Create next nonce value
  334. *
  335. * @ret nonce Nonce
  336. *
  337. * This is the MakeNextNonce function defined in ANS X9.82 Part 4
  338. * (April 2011 Draft) Section 13.3.4.2.
  339. */
  340. static uint32_t make_next_nonce ( void ) {
  341. static uint32_t nonce;
  342. /* The simplest implementation of a nonce uses a large counter */
  343. nonce++;
  344. return nonce;
  345. }
  346. /**
  347. * Obtain entropy input temporary buffer
  348. *
  349. * @v num_samples Number of entropy samples
  350. * @v tmp Temporary buffer
  351. * @v tmp_len Length of temporary buffer
  352. * @ret rc Return status code
  353. *
  354. * This is (part of) the implementation of the Get_entropy_input
  355. * function (using an entropy source as the source of entropy input
  356. * and condensing each entropy source output after each GetEntropy
  357. * call) as defined in ANS X9.82 Part 4 (April 2011 Draft) Section
  358. * 13.3.4.2.
  359. *
  360. * To minimise code size, the number of samples required is calculated
  361. * at compilation time.
  362. */
  363. int get_entropy_input_tmp ( unsigned int num_samples, uint8_t *tmp,
  364. size_t tmp_len ) {
  365. static unsigned int startup_tested = 0;
  366. struct {
  367. uint32_t nonce;
  368. entropy_sample_t sample;
  369. } __attribute__ (( packed )) data;;
  370. uint8_t df_buf[tmp_len];
  371. unsigned int i;
  372. int rc;
  373. /* Enable entropy gathering */
  374. if ( ( rc = entropy_enable() ) != 0 )
  375. return rc;
  376. /* Perform mandatory startup tests, if not yet performed */
  377. for ( ; startup_tested < startup_test_count() ; startup_tested++ ) {
  378. if ( ( rc = get_entropy ( &data.sample ) ) != 0 )
  379. goto err_get_entropy;
  380. }
  381. /* 3. entropy_total = 0
  382. *
  383. * (Nothing to do; the number of entropy samples required has
  384. * already been precalculated.)
  385. */
  386. /* 4. tmp = a fixed n-bit value, such as 0^n */
  387. memset ( tmp, 0, tmp_len );
  388. /* 5. While ( entropy_total < min_entropy ) */
  389. while ( num_samples-- ) {
  390. /* 5.1. ( status, entropy_bitstring, assessed_entropy )
  391. * = GetEntropy()
  392. * 5.2. If status indicates an error, return ( status, Null )
  393. */
  394. if ( ( rc = get_entropy ( &data.sample ) ) != 0 )
  395. goto err_get_entropy;
  396. /* 5.3. nonce = MakeNextNonce() */
  397. data.nonce = make_next_nonce();
  398. /* 5.4. tmp = tmp XOR
  399. * df ( ( nonce || entropy_bitstring ), n )
  400. */
  401. hash_df ( &entropy_hash_df_algorithm, &data, sizeof ( data ),
  402. df_buf, sizeof ( df_buf ) );
  403. for ( i = 0 ; i < tmp_len ; i++ )
  404. tmp[i] ^= df_buf[i];
  405. /* 5.5. entropy_total = entropy_total + assessed_entropy
  406. *
  407. * (Nothing to do; the number of entropy samples
  408. * required has already been precalculated.)
  409. */
  410. }
  411. /* Disable entropy gathering */
  412. entropy_disable();
  413. return 0;
  414. err_get_entropy:
  415. entropy_disable();
  416. return rc;
  417. }