Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

drbg.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef _IPXE_DRBG_H
  2. #define _IPXE_DRBG_H
  3. /** @file
  4. *
  5. * DRBG mechanism
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/sha256.h>
  11. #include <ipxe/hmac_drbg.h>
  12. /** Choose HMAC_DRBG using SHA-256
  13. *
  14. * HMAC_DRBG using SHA-256 is an Approved algorithm in ANS X9.82.
  15. */
  16. #define HMAC_DRBG_ALGORITHM HMAC_DRBG_SHA256
  17. /** Maximum security strength */
  18. #define DRBG_MAX_SECURITY_STRENGTH \
  19. HMAC_DRBG_MAX_SECURITY_STRENGTH ( HMAC_DRBG_ALGORITHM )
  20. /** Security strength
  21. *
  22. * We choose to operate at a strength of 128 bits.
  23. */
  24. #define DRBG_SECURITY_STRENGTH 128
  25. /** Minimum entropy input length */
  26. #define DRBG_MIN_ENTROPY_LEN_BYTES \
  27. HMAC_DRBG_MIN_ENTROPY_LEN_BYTES ( DRBG_SECURITY_STRENGTH )
  28. /** Maximum entropy input length */
  29. #define DRBG_MAX_ENTROPY_LEN_BYTES HMAC_DRBG_MAX_ENTROPY_LEN_BYTES
  30. /** Maximum personalisation string length */
  31. #define DRBG_MAX_PERSONAL_LEN_BYTES HMAC_DRBG_MAX_PERSONAL_LEN_BYTES
  32. /** Maximum additional input length */
  33. #define DRBG_MAX_ADDITIONAL_LEN_BYTES HMAC_DRBG_MAX_ADDITIONAL_LEN_BYTES
  34. /** Maximum length of generated pseudorandom data per request */
  35. #define DRBG_MAX_GENERATED_LEN_BYTES HMAC_DRBG_MAX_GENERATED_LEN_BYTES
  36. /** A Deterministic Random Bit Generator */
  37. struct drbg_state {
  38. /** Algorithm internal state */
  39. struct hmac_drbg_state internal;
  40. /** Reseed required flag */
  41. int reseed_required;
  42. /** State is valid */
  43. int valid;
  44. };
  45. /**
  46. * Instantiate DRBG algorithm
  47. *
  48. * @v state Algorithm state
  49. * @v entropy Entropy input
  50. * @v entropy_len Length of entropy input
  51. * @v personal Personalisation string
  52. * @v personal_len Length of personalisation string
  53. *
  54. * This is the Instantiate_algorithm function defined in ANS X9.82
  55. * Part 3-2007 Section 9.2 (NIST SP 800-90 Section 9.1).
  56. */
  57. static inline void drbg_instantiate_algorithm ( struct drbg_state *state,
  58. const void *entropy,
  59. size_t entropy_len,
  60. const void *personal,
  61. size_t personal_len ) {
  62. hmac_drbg_instantiate ( HMAC_DRBG_HASH ( HMAC_DRBG_ALGORITHM ),
  63. &state->internal, entropy, entropy_len,
  64. personal, personal_len );
  65. }
  66. /**
  67. * Reseed DRBG algorithm
  68. *
  69. * @v state Algorithm state
  70. * @v entropy Entropy input
  71. * @v entropy_len Length of entropy input
  72. * @v additional Additional input
  73. * @v additional_len Length of additional input
  74. *
  75. * This is the Reseed_algorithm function defined in ANS X9.82
  76. * Part 3-2007 Section 9.3 (NIST SP 800-90 Section 9.2).
  77. */
  78. static inline void drbg_reseed_algorithm ( struct drbg_state *state,
  79. const void *entropy,
  80. size_t entropy_len,
  81. const void *additional,
  82. size_t additional_len ) {
  83. hmac_drbg_reseed ( HMAC_DRBG_HASH ( HMAC_DRBG_ALGORITHM ),
  84. &state->internal, entropy, entropy_len,
  85. additional, additional_len );
  86. }
  87. /**
  88. * Generate pseudorandom bits using DRBG algorithm
  89. *
  90. * @v state Algorithm state
  91. * @v additional Additional input
  92. * @v additional_len Length of additional input
  93. * @v data Output buffer
  94. * @v len Length of output buffer
  95. * @ret rc Return status code
  96. *
  97. * This is the Generate_algorithm function defined in ANS X9.82
  98. * Part 3-2007 Section 9.4 (NIST SP 800-90 Section 9.3).
  99. *
  100. * Note that the only permitted error is "reseed required".
  101. */
  102. static inline int drbg_generate_algorithm ( struct drbg_state *state,
  103. const void *additional,
  104. size_t additional_len,
  105. void *data, size_t len ) {
  106. return hmac_drbg_generate ( HMAC_DRBG_HASH ( HMAC_DRBG_ALGORITHM ),
  107. &state->internal, additional,
  108. additional_len, data, len );
  109. }
  110. extern int drbg_instantiate ( struct drbg_state *state, const void *personal,
  111. size_t personal_len );
  112. extern int drbg_reseed ( struct drbg_state *state, const void *additional,
  113. size_t additional_len );
  114. extern int drbg_generate ( struct drbg_state *state, const void *additional,
  115. size_t additional_len, int prediction_resist,
  116. void *data, size_t len );
  117. extern void drbg_uninstantiate ( struct drbg_state *state );
  118. #endif /* _IPXE_DRBG_H */