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

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