Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

hmac_drbg.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 );
  9. #include <stdint.h>
  10. #include <ipxe/sha1.h>
  11. /** Use SHA-1 as the underlying hash algorithm
  12. *
  13. * HMAC_DRBG using SHA-1 is an Approved algorithm in ANS X9.82.
  14. */
  15. #define hmac_drbg_algorithm sha1_algorithm
  16. /** Maximum security strength
  17. *
  18. * The maximum security strength of HMAC_DRBG using SHA-1 is 128 bits
  19. * (according to the list of maximum security strengths documented in
  20. * NIST SP 800-57 Part 1 Section 5.6.1 Table 3).
  21. */
  22. #define HMAC_DRBG_MAX_SECURITY_STRENGTH 128
  23. /** Security strength
  24. *
  25. * For the sake of implementation simplicity, only a single security
  26. * strength is supported, which is the maximum security strength
  27. * supported by the algorithm.
  28. */
  29. #define HMAC_DRBG_SECURITY_STRENGTH HMAC_DRBG_MAX_SECURITY_STRENGTH
  30. /** Underlying hash algorithm output length (in bytes) */
  31. #define HMAC_DRBG_OUTLEN_BYTES SHA1_DIGEST_SIZE
  32. /** Required minimum entropy for instantiate and reseed
  33. *
  34. * The minimum required entropy for HMAC_DRBG is equal to the security
  35. * strength according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
  36. * (NIST SP 800-90 Section 10.1 Table 2).
  37. */
  38. #define HMAC_DRBG_MIN_ENTROPY_BYTES ( HMAC_DRBG_SECURITY_STRENGTH / 8 )
  39. /** Minimum entropy input length
  40. *
  41. * The minimum entropy input length for HMAC_DRBG is equal to the
  42. * security strength according to ANS X9.82 Part 3-2007 Section 10.2.1
  43. * Table 2 (NIST SP 800-90 Section 10.1 Table 2).
  44. */
  45. #define HMAC_DRBG_MIN_ENTROPY_LEN_BYTES ( HMAC_DRBG_SECURITY_STRENGTH / 8 )
  46. /** Maximum entropy input length
  47. *
  48. * The maximum entropy input length for HMAC_DRBG is 2^35 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. * We choose to allow up to 32 bytes.
  53. */
  54. #define HMAC_DRBG_MAX_ENTROPY_LEN_BYTES 32
  55. /** Maximum personalisation string length
  56. *
  57. * The maximum permitted personalisation string length for HMAC_DRBG
  58. * is 2^35 bits according to ANS X9.82 Part 3-2007 Section 10.2.1
  59. * Table 1 (NIST SP 800-90 Section 10.1 Table 2).
  60. *
  61. * We choose to allow up to 2^32-1 bytes (i.e. 2^35-8 bits).
  62. */
  63. #define HMAC_DRBG_MAX_PERSONAL_LEN_BYTES 0xffffffffUL
  64. /** Maximum additional input length
  65. *
  66. * The maximum permitted additional input length for HMAC_DRBG is 2^35
  67. * bits according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 1
  68. * (NIST SP 800-90 Section 10.1 Table 2).
  69. *
  70. * We choose to allow up to 2^32-1 bytes (i.e. 2^35-8 bits).
  71. */
  72. #define HMAC_DRBG_MAX_ADDITIONAL_LEN_BYTES 0xffffffffUL
  73. /** Maximum length of generated pseudorandom data per request
  74. *
  75. * The maximum number of bits per request for HMAC_DRBG is 2^19 bits
  76. * according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 1 (NIST SP
  77. * 800-90 Section 10.1 Table 2).
  78. *
  79. * We choose to allow up to 2^16-1 bytes (i.e. 2^19-8 bits).
  80. */
  81. #define HMAC_DRBG_MAX_GENERATED_LEN_BYTES 0x0000ffffUL
  82. /** Reseed interval
  83. *
  84. * The maximum permitted reseed interval for HMAC_DRBG using SHA-1 is
  85. * 2^48 according to ANS X9.82 Part 3-2007 Section 10.2.1 Table 2
  86. * (NIST SP 800-90 Section 10.1 Table 2). However, the sample
  87. * implementation given in ANS X9.82 Part 3-2007 Annex E.2.1 (NIST SP
  88. * 800-90 Appendix F.2) shows a reseed interval of 10000.
  89. *
  90. * We choose a very conservative reseed interval.
  91. */
  92. #define HMAC_DRBG_RESEED_INTERVAL 1024
  93. /** Underlying hash algorithm context size (in bytes) */
  94. #define HMAC_DRBG_CTX_SIZE SHA1_CTX_SIZE
  95. /**
  96. * HMAC_DRBG internal state
  97. *
  98. * This structure is defined by ANS X9.82 Part 3-2007 Section
  99. * 10.2.2.2.1 (NIST SP 800-90 Section 10.1.2.1).
  100. *
  101. * The "administrative information" portions (security_strength and
  102. * prediction_resistance) are design-time constants and so are not
  103. * present as fields in this structure.
  104. */
  105. struct hmac_drbg_state {
  106. /** Current value
  107. *
  108. * "The value V of outlen bits, which is updated each time
  109. * another outlen bits of output are produced"
  110. */
  111. uint8_t value[HMAC_DRBG_OUTLEN_BYTES];
  112. /** Current key
  113. *
  114. * "The outlen-bit Key, which is updated at least once each
  115. * time that the DRBG mechanism generates pseudorandom bits."
  116. */
  117. uint8_t key[HMAC_DRBG_OUTLEN_BYTES];
  118. /** Reseed counter
  119. *
  120. * "A counter (reseed_counter) that indicates the number of
  121. * requests for pseudorandom bits since instantiation or
  122. * reseeding"
  123. */
  124. unsigned int reseed_counter;
  125. };
  126. extern void hmac_drbg_instantiate ( struct hmac_drbg_state *state,
  127. const void *entropy, size_t entropy_len,
  128. const void *personal, size_t personal_len );
  129. extern void hmac_drbg_reseed ( struct hmac_drbg_state *state,
  130. const void *entropy, size_t entropy_len,
  131. const void *additional, size_t additional_len );
  132. extern int hmac_drbg_generate ( struct hmac_drbg_state *state,
  133. const void *additional, size_t additional_len,
  134. void *data, size_t len );
  135. #endif /* _IPXE_HMAC_DRBG_H */