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 7.1KB

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