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.h 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #ifndef _IPXE_ENTROPY_H
  2. #define _IPXE_ENTROPY_H
  3. /** @file
  4. *
  5. * Entropy source
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <ipxe/api.h>
  13. #include <ipxe/hash_df.h>
  14. #include <ipxe/sha256.h>
  15. #include <config/entropy.h>
  16. /**
  17. * Calculate static inline entropy API function name
  18. *
  19. * @v _prefix Subsystem prefix
  20. * @v _api_func API function
  21. * @ret _subsys_func Subsystem API function
  22. */
  23. #define ENTROPY_INLINE( _subsys, _api_func ) \
  24. SINGLE_API_INLINE ( ENTROPY_PREFIX_ ## _subsys, _api_func )
  25. /**
  26. * Provide a entropy API implementation
  27. *
  28. * @v _prefix Subsystem prefix
  29. * @v _api_func API function
  30. * @v _func Implementing function
  31. */
  32. #define PROVIDE_ENTROPY( _subsys, _api_func, _func ) \
  33. PROVIDE_SINGLE_API ( ENTROPY_PREFIX_ ## _subsys, _api_func, _func )
  34. /**
  35. * Provide a static inline entropy API implementation
  36. *
  37. * @v _prefix Subsystem prefix
  38. * @v _api_func API function
  39. */
  40. #define PROVIDE_ENTROPY_INLINE( _subsys, _api_func ) \
  41. PROVIDE_SINGLE_API_INLINE ( ENTROPY_PREFIX_ ## _subsys, _api_func )
  42. /** A noise sample */
  43. typedef uint8_t noise_sample_t;
  44. /** An entropy sample */
  45. typedef uint8_t entropy_sample_t;
  46. /* Include all architecture-independent entropy API headers */
  47. #include <ipxe/null_entropy.h>
  48. #include <ipxe/efi/efi_entropy.h>
  49. #include <ipxe/linux/linux_entropy.h>
  50. /* Include all architecture-dependent entropy API headers */
  51. #include <bits/entropy.h>
  52. /**
  53. * Enable entropy gathering
  54. *
  55. * @ret rc Return status code
  56. */
  57. int entropy_enable ( void );
  58. /**
  59. * Disable entropy gathering
  60. *
  61. */
  62. void entropy_disable ( void );
  63. /**
  64. * min-entropy per sample
  65. *
  66. * @ret min_entropy min-entropy of each sample
  67. *
  68. * min-entropy is defined in ANS X9.82 Part 1-2006 Section 8.3 and in
  69. * NIST SP 800-90 Appendix C.3 as
  70. *
  71. * H_min = -log2 ( p_max )
  72. *
  73. * where p_max is the probability of the most likely sample value.
  74. *
  75. * This must be a compile-time constant.
  76. */
  77. double min_entropy_per_sample ( void );
  78. /**
  79. * Get noise sample
  80. *
  81. * @ret noise Noise sample
  82. * @ret rc Return status code
  83. *
  84. * This is the GetNoise function defined in ANS X9.82 Part 2
  85. * (October 2011 Draft) Section 6.5.2.
  86. */
  87. int get_noise ( noise_sample_t *noise );
  88. extern int get_entropy_input_tmp ( unsigned int num_samples,
  89. uint8_t *tmp, size_t tmp_len );
  90. /** Use SHA-256 as the underlying hash algorithm for Hash_df
  91. *
  92. * Hash_df using SHA-256 is an Approved algorithm in ANS X9.82.
  93. */
  94. #define entropy_hash_df_algorithm sha256_algorithm
  95. /** Underlying hash algorithm output length (in bytes) */
  96. #define ENTROPY_HASH_DF_OUTLEN_BYTES SHA256_DIGEST_SIZE
  97. /**
  98. * Obtain entropy input
  99. *
  100. * @v min_entropy_bits Minimum amount of entropy, in bits
  101. * @v data Data buffer
  102. * @v min_len Minimum length of entropy input, in bytes
  103. * @v max_len Maximum length of entropy input, in bytes
  104. * @ret len Length of entropy input, in bytes, or negative error
  105. *
  106. * This is the implementation of the Get_entropy_input function (using
  107. * an entropy source as the source of entropy input and condensing
  108. * each entropy source output after each GetEntropy call) as defined
  109. * in ANS X9.82 Part 4 (April 2011 Draft) Section 13.3.4.2.
  110. *
  111. * To minimise code size, the number of samples required is calculated
  112. * at compilation time.
  113. */
  114. static inline __attribute__ (( always_inline )) int
  115. get_entropy_input ( unsigned int min_entropy_bits, void *data, size_t min_len,
  116. size_t max_len ) {
  117. size_t tmp_len = ( ( ( min_entropy_bits * 2 ) + 7 ) / 8 );
  118. uint8_t tmp_buf[ tmp_len ];
  119. uint8_t *tmp = ( ( tmp_len > max_len ) ? tmp_buf : data );
  120. double min_samples;
  121. unsigned int num_samples;
  122. unsigned int n;
  123. int rc;
  124. /* Sanity checks */
  125. linker_assert ( ( min_entropy_per_sample() <=
  126. ( 8 * sizeof ( noise_sample_t ) ) ),
  127. min_entropy_per_sample_is_impossibly_high );
  128. linker_assert ( ( min_entropy_bits <= ( 8 * max_len ) ),
  129. entropy_buffer_too_small );
  130. /* Round up minimum entropy to an integral number of bytes */
  131. min_entropy_bits = ( ( min_entropy_bits + 7 ) & ~7 );
  132. /* Calculate number of samples required to contain sufficient entropy */
  133. min_samples = ( ( min_entropy_bits * 1.0 ) / min_entropy_per_sample() );
  134. /* Round up to a whole number of samples. We don't have the
  135. * ceil() function available, so do the rounding by hand.
  136. */
  137. num_samples = min_samples;
  138. if ( num_samples < min_samples )
  139. num_samples++;
  140. linker_assert ( ( num_samples >= min_samples ), rounding_error );
  141. /* Floating-point operations are not allowed in iPXE since we
  142. * never set up a suitable environment. Abort the build
  143. * unless the calculated number of samples is a compile-time
  144. * constant.
  145. */
  146. linker_assert ( __builtin_constant_p ( num_samples ),
  147. num_samples_not_constant );
  148. /* (Unnumbered). The output length of the hash function shall
  149. * meet or exceed the security strength indicated by the
  150. * min_entropy parameter.
  151. */
  152. linker_assert ( ( ( 8 * ENTROPY_HASH_DF_OUTLEN_BYTES ) >=
  153. min_entropy_bits ), hash_df_algorithm_too_weak );
  154. /* 1. If ( min_length > max_length ), then return ( FAILURE, Null ) */
  155. linker_assert ( ( min_len <= max_len ), min_len_greater_than_max_len );
  156. /* 2. n = 2 * min_entropy */
  157. n = ( 2 * min_entropy_bits );
  158. /* 3. entropy_total = 0
  159. * 4. tmp = a fixed n-bit value, such as 0^n
  160. * 5. While ( entropy_total < min_entropy )
  161. * 5.1. ( status, entropy_bitstring, assessed_entropy )
  162. * = GetEntropy()
  163. * 5.2. If status indicates an error, return ( status, Null )
  164. * 5.3. nonce = MakeNextNonce()
  165. * 5.4. tmp = tmp XOR df ( ( nonce || entropy_bitstring ), n )
  166. * 5.5. entropy_total = entropy_total + assessed_entropy
  167. *
  168. * (The implementation of these steps is inside the function
  169. * get_entropy_input_tmp().)
  170. */
  171. linker_assert ( __builtin_constant_p ( tmp_len ),
  172. tmp_len_not_constant );
  173. linker_assert ( ( n == ( 8 * tmp_len ) ), tmp_len_mismatch );
  174. if ( ( rc = get_entropy_input_tmp ( num_samples, tmp, tmp_len ) ) != 0 )
  175. return rc;
  176. /* 6. If ( n < min_length ), then tmp = tmp || 0^(min_length-n)
  177. * 7. If ( n > max_length ), then tmp = df ( tmp, max_length )
  178. * 8. Return ( SUCCESS, tmp )
  179. */
  180. if ( tmp_len < min_len ) {
  181. /* (Data is already in-place.) */
  182. linker_assert ( ( data == tmp ), data_not_inplace );
  183. memset ( ( data + tmp_len ), 0, ( min_len - tmp_len ) );
  184. return min_len;
  185. } else if ( tmp_len > max_len ) {
  186. linker_assert ( ( tmp == tmp_buf ), data_inplace );
  187. hash_df ( &entropy_hash_df_algorithm, tmp, tmp_len,
  188. data, max_len );
  189. return max_len;
  190. } else {
  191. /* (Data is already in-place.) */
  192. linker_assert ( ( data == tmp ), data_not_inplace );
  193. return tmp_len;
  194. }
  195. }
  196. #endif /* _IPXE_ENTROPY_H */