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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef _IPXE_HMAC_DRBG_H
  2. #define _IPXE_HMAC_DRBG_H
  3. /** @file
  4. *
  5. * HMAC_DRBG algorithm
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/crypto.h>
  11. /** Declare an HMAC_DRBG algorithm
  12. *
  13. * @v hash Underlying hash algorithm
  14. * @v max_security_strength Maxmimum security strength
  15. * @v out_len_bits Output block length, in bits
  16. * @ret hmac_drbg HMAC_DRBG algorithm
  17. */
  18. #define HMAC_DRBG( hash, max_security_strength, out_len_bits ) \
  19. ( hash, max_security_strength, out_len_bits )
  20. /** HMAC_DRBG using SHA-1
  21. *
  22. * The maximum security strength of HMAC_DRBG using SHA-1 is 128 bits
  23. * according to the list of maximum security strengths documented in
  24. * NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
  25. *
  26. * The output block length of HMAC_DRBG using SHA-1 is 160 bits
  27. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  28. * 800-90 Section 10.1 Table 2).
  29. */
  30. #define HMAC_DRBG_SHA1 HMAC_DRBG ( &sha1_algorithm, 128, 160 )
  31. /** HMAC_DRBG using SHA-224
  32. *
  33. * The maximum security strength of HMAC_DRBG using SHA-224 is 192
  34. * bits according to the list of maximum security strengths documented
  35. * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
  36. *
  37. * The output block length of HMAC_DRBG using SHA-224 is 224 bits
  38. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  39. * 800-90 Section 10.1 Table 2).
  40. */
  41. #define HMAC_DRBG_SHA224 HMAC_DRBG ( &sha224_algorithm, 192, 224 )
  42. /** HMAC_DRBG using SHA-256
  43. *
  44. * The maximum security strength of HMAC_DRBG using SHA-256 is 256
  45. * bits according to the list of maximum security strengths documented
  46. * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
  47. *
  48. * The output block length of HMAC_DRBG using SHA-256 is 256 bits
  49. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  50. * 800-90 Section 10.1 Table 2).
  51. */
  52. #define HMAC_DRBG_SHA256 HMAC_DRBG ( &sha256_algorithm, 256, 256 )
  53. /** HMAC_DRBG using SHA-384
  54. *
  55. * The maximum security strength of HMAC_DRBG using SHA-384 is 256
  56. * bits according to the list of maximum security strengths documented
  57. * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
  58. *
  59. * The output block length of HMAC_DRBG using SHA-384 is 384 bits
  60. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  61. * 800-90 Section 10.1 Table 2).
  62. */
  63. #define HMAC_DRBG_SHA384 HMAC_DRBG ( &sha384_algorithm, 256, 384 )
  64. /** HMAC_DRBG using SHA-512
  65. *
  66. * The maximum security strength of HMAC_DRBG using SHA-512 is 256
  67. * bits according to the list of maximum security strengths documented
  68. * in NIST SP 800-57 Part 1 Section 5.6.1 Table 3.
  69. *
  70. * The output block length of HMAC_DRBG using SHA-512 is 512 bits
  71. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  72. * 800-90 Section 10.1 Table 2).
  73. */
  74. #define HMAC_DRBG_SHA512 HMAC_DRBG ( &sha512_algorithm, 256, 512 )
  75. /** Underlying hash algorithm
  76. *
  77. * @v hmac_drbg HMAC_DRBG algorithm
  78. * @ret hash Underlying hash algorithm
  79. */
  80. #define HMAC_DRBG_HASH( hmac_drbg ) \
  81. HMAC_DRBG_EXTRACT_HASH hmac_drbg
  82. #define HMAC_DRBG_EXTRACT_HASH( hash, max_security_strength, out_len_bits ) \
  83. hash
  84. /** Maximum security strength
  85. *
  86. * @v hmac_drbg HMAC_DRBG algorithm
  87. * @ret max_security_strength Maxmimum security strength
  88. */
  89. #define HMAC_DRBG_MAX_SECURITY_STRENGTH( hmac_drbg ) \
  90. HMAC_DRBG_EXTRACT_MAX_SECURITY_STRENGTH hmac_drbg
  91. #define HMAC_DRBG_EXTRACT_MAX_SECURITY_STRENGTH( hash, max_security_strength, \
  92. out_len_bits ) \
  93. max_security_strength
  94. /** Output block length, in bits
  95. *
  96. * @v hmac_drbg HMAC_DRBG algorithm
  97. * @ret out_len_bits Output block length, in bits
  98. */
  99. #define HMAC_DRBG_OUTLEN_BITS( hmac_drbg ) \
  100. HMAC_DRBG_EXTRACT_OUTLEN_BITS hmac_drbg
  101. #define HMAC_DRBG_EXTRACT_OUTLEN_BITS( hash, max_security_strength, \
  102. out_len_bits ) \
  103. out_len_bits
  104. /** Output block length, in bytes
  105. *
  106. * @v hmac_drbg HMAC_DRBG algorithm
  107. * @ret out_len_bytes Output block length, in bytes
  108. */
  109. #define HMAC_DRBG_OUTLEN_BYTES( hmac_drbg ) \
  110. ( HMAC_DRBG_OUTLEN_BITS ( hmac_drbg ) / 8 )
  111. /** Maximum output block length, in bytes
  112. *
  113. * The maximum output block length for HMAC_DRBG is 512 bits for
  114. * SHA-512 according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
  115. * (NIST SP 800-90 Section 10.1 Table 2).
  116. */
  117. #define HMAC_DRBG_MAX_OUTLEN_BYTES HMAC_DRBG_OUTLEN_BYTES ( HMAC_DRBG_SHA512 )
  118. /** Required minimum entropy for instantiate and reseed
  119. *
  120. * @v security_strength Security strength
  121. * @ret min_entropy Required minimum entropy
  122. *
  123. * The minimum required entropy for HMAC_DRBG is equal to the security
  124. * strength according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
  125. * (NIST SP 800-90 Section 10.1 Table 2).
  126. */
  127. #define HMAC_DRBG_MIN_ENTROPY( security_strength ) (security_strength)
  128. /** Minimum entropy input length
  129. *
  130. * @v security_strength Security strength
  131. * @ret min_entropy_len_bytes Required minimum entropy length (in bytes)
  132. *
  133. * The minimum entropy input length for HMAC_DRBG is equal to the
  134. * security strength according to ANS X9.82 Part 3-2007 Section 10.2.1
  135. * Table 2 (NIST SP 800-90 Section 10.1 Table 2).
  136. */
  137. #define HMAC_DRBG_MIN_ENTROPY_LEN_BYTES( security_strength ) \
  138. ( (security_strength) / 8 )
  139. /** Maximum entropy input length
  140. *
  141. * The maximum entropy input length for HMAC_DRBG is 2^35 bits
  142. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  143. * 800-90 Section 10.1 Table 2).
  144. *
  145. * We choose to allow up to 32 bytes.
  146. */
  147. #define HMAC_DRBG_MAX_ENTROPY_LEN_BYTES 32
  148. /** Maximum personalisation string length
  149. *
  150. * The maximum permitted personalisation string length for HMAC_DRBG
  151. * is 2^35 bits according to ANS X9.82 Part 3-2007 Section 10.2.1
  152. * Table 1 (NIST SP 800-90 Section 10.1 Table 2).
  153. *
  154. * We choose to allow up to 2^32-1 bytes (i.e. 2^35-8 bits).
  155. */
  156. #define HMAC_DRBG_MAX_PERSONAL_LEN_BYTES 0xffffffffUL
  157. /** Maximum additional input length
  158. *
  159. * The maximum permitted additional input length for HMAC_DRBG is 2^35
  160. * bits according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 1
  161. * (NIST SP 800-90 Section 10.1 Table 2).
  162. *
  163. * We choose to allow up to 2^32-1 bytes (i.e. 2^35-8 bits).
  164. */
  165. #define HMAC_DRBG_MAX_ADDITIONAL_LEN_BYTES 0xffffffffUL
  166. /** Maximum length of generated pseudorandom data per request
  167. *
  168. * The maximum number of bits per request for HMAC_DRBG is 2^19 bits
  169. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 1 (NIST SP
  170. * 800-90 Section 10.1 Table 2).
  171. *
  172. * We choose to allow up to 2^16-1 bytes (i.e. 2^19-8 bits).
  173. */
  174. #define HMAC_DRBG_MAX_GENERATED_LEN_BYTES 0x0000ffffUL
  175. /** Reseed interval
  176. *
  177. * The maximum permitted reseed interval for HMAC_DRBG is 2^48
  178. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2 (NIST SP
  179. * 800-90 Section 10.1 Table 2). However, the sample implementation
  180. * given in ANS X9.82 Part 3-2007 Annex E.2.1 (NIST SP 800-90 Appendix
  181. * F.2) shows a reseed interval of 10000.
  182. *
  183. * We choose a very conservative reseed interval.
  184. */
  185. #define HMAC_DRBG_RESEED_INTERVAL 1024
  186. /**
  187. * HMAC_DRBG internal state
  188. *
  189. * This structure is defined by ANS X9.82 Part 3-2007 Section
  190. * 10.2.2.2.1 (NIST SP 800-90 Section 10.1.2.1).
  191. *
  192. * The "administrative information" portions (security_strength and
  193. * prediction_resistance) are design-time constants and so are not
  194. * present as fields in this structure.
  195. */
  196. struct hmac_drbg_state {
  197. /** Current value
  198. *
  199. * "The value V of outlen bits, which is updated each time
  200. * another outlen bits of output are produced"
  201. */
  202. uint8_t value[HMAC_DRBG_MAX_OUTLEN_BYTES];
  203. /** Current key
  204. *
  205. * "The outlen-bit Key, which is updated at least once each
  206. * time that the DRBG mechanism generates pseudorandom bits."
  207. */
  208. uint8_t key[HMAC_DRBG_MAX_OUTLEN_BYTES];
  209. /** Reseed counter
  210. *
  211. * "A counter (reseed_counter) that indicates the number of
  212. * requests for pseudorandom bits since instantiation or
  213. * reseeding"
  214. */
  215. unsigned int reseed_counter;
  216. };
  217. extern void hmac_drbg_instantiate ( struct digest_algorithm *hash,
  218. struct hmac_drbg_state *state,
  219. const void *entropy, size_t entropy_len,
  220. const void *personal, size_t personal_len );
  221. extern void hmac_drbg_reseed ( struct digest_algorithm *hash,
  222. struct hmac_drbg_state *state,
  223. const void *entropy, size_t entropy_len,
  224. const void *additional, size_t additional_len );
  225. extern int hmac_drbg_generate ( struct digest_algorithm *hash,
  226. struct hmac_drbg_state *state,
  227. const void *additional, size_t additional_len,
  228. void *data, size_t len );
  229. #endif /* _IPXE_HMAC_DRBG_H */