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

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