hmac_drbg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**
  2. * \file hmac_drbg.h
  3. *
  4. * \brief HMAC_DRBG (NIST SP 800-90A)
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_HMAC_DRBG_H
  26. #define MBEDTLS_HMAC_DRBG_H
  27. #include "md.h"
  28. #if defined(MBEDTLS_THREADING_C)
  29. #include "mbedtls/threading.h"
  30. #endif
  31. /*
  32. * Error codes
  33. */
  34. #define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */
  35. #define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */
  36. #define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */
  37. #define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */
  38. /**
  39. * \name SECTION: Module settings
  40. *
  41. * The configuration options you can set for this module are in this section.
  42. * Either change them in config.h or define them on the compiler command line.
  43. * \{
  44. */
  45. #if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
  46. #define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  47. #endif
  48. #if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
  49. #define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  50. #endif
  51. #if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
  52. #define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  53. #endif
  54. #if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
  55. #define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  56. #endif
  57. /* \} name SECTION: Module settings */
  58. #define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
  59. #define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**
  64. * HMAC_DRBG context.
  65. */
  66. typedef struct
  67. {
  68. /* Working state: the key K is not stored explicitely,
  69. * but is implied by the HMAC context */
  70. mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */
  71. unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */
  72. int reseed_counter; /*!< reseed counter */
  73. /* Administrative state */
  74. size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
  75. int prediction_resistance; /*!< enable prediction resistance (Automatic
  76. reseed before every random generation) */
  77. int reseed_interval; /*!< reseed interval */
  78. /* Callbacks */
  79. int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */
  80. void *p_entropy; /*!< context for the entropy function */
  81. #if defined(MBEDTLS_THREADING_C)
  82. mbedtls_threading_mutex_t mutex;
  83. #endif
  84. } mbedtls_hmac_drbg_context;
  85. /**
  86. * \brief HMAC_DRBG context initialization
  87. * Makes the context ready for mbedtls_hmac_drbg_seed(),
  88. * mbedtls_hmac_drbg_seed_buf() or
  89. * mbedtls_hmac_drbg_free().
  90. *
  91. * \param ctx HMAC_DRBG context to be initialized
  92. */
  93. void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
  94. /**
  95. * \brief HMAC_DRBG initial seeding
  96. * Seed and setup entropy source for future reseeds.
  97. *
  98. * \param ctx HMAC_DRBG context to be seeded
  99. * \param md_info MD algorithm to use for HMAC_DRBG
  100. * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
  101. * length)
  102. * \param p_entropy Entropy context
  103. * \param custom Personalization data (Device specific identifiers)
  104. * (Can be NULL)
  105. * \param len Length of personalization data
  106. *
  107. * \note The "security strength" as defined by NIST is set to:
  108. * 128 bits if md_alg is SHA-1,
  109. * 192 bits if md_alg is SHA-224,
  110. * 256 bits if md_alg is SHA-256 or higher.
  111. * Note that SHA-256 is just as efficient as SHA-224.
  112. *
  113. * \return 0 if successful, or
  114. * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
  115. * MBEDTLS_ERR_MD_ALLOC_FAILED, or
  116. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
  117. */
  118. int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
  119. const mbedtls_md_info_t * md_info,
  120. int (*f_entropy)(void *, unsigned char *, size_t),
  121. void *p_entropy,
  122. const unsigned char *custom,
  123. size_t len );
  124. /**
  125. * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
  126. * (For use with deterministic ECDSA.)
  127. *
  128. * \param ctx HMAC_DRBG context to be initialised
  129. * \param md_info MD algorithm to use for HMAC_DRBG
  130. * \param data Concatenation of entropy string and additional data
  131. * \param data_len Length of data in bytes
  132. *
  133. * \return 0 if successful, or
  134. * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
  135. * MBEDTLS_ERR_MD_ALLOC_FAILED.
  136. */
  137. int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
  138. const mbedtls_md_info_t * md_info,
  139. const unsigned char *data, size_t data_len );
  140. /**
  141. * \brief Enable / disable prediction resistance (Default: Off)
  142. *
  143. * Note: If enabled, entropy is used for ctx->entropy_len before each call!
  144. * Only use this if you have ample supply of good entropy!
  145. *
  146. * \param ctx HMAC_DRBG context
  147. * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF
  148. */
  149. void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
  150. int resistance );
  151. /**
  152. * \brief Set the amount of entropy grabbed on each reseed
  153. * (Default: given by the security strength, which
  154. * depends on the hash used, see \c mbedtls_hmac_drbg_init() )
  155. *
  156. * \param ctx HMAC_DRBG context
  157. * \param len Amount of entropy to grab, in bytes
  158. */
  159. void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,
  160. size_t len );
  161. /**
  162. * \brief Set the reseed interval
  163. * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
  164. *
  165. * \param ctx HMAC_DRBG context
  166. * \param interval Reseed interval
  167. */
  168. void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx,
  169. int interval );
  170. /**
  171. * \brief HMAC_DRBG update state
  172. *
  173. * \param ctx HMAC_DRBG context
  174. * \param additional Additional data to update state with, or NULL
  175. * \param add_len Length of additional data, or 0
  176. *
  177. * \note Additional data is optional, pass NULL and 0 as second
  178. * third argument if no additional data is being used.
  179. */
  180. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  181. const unsigned char *additional, size_t add_len );
  182. /**
  183. * \brief HMAC_DRBG reseeding (extracts data from entropy source)
  184. *
  185. * \param ctx HMAC_DRBG context
  186. * \param additional Additional data to add to state (Can be NULL)
  187. * \param len Length of additional data
  188. *
  189. * \return 0 if successful, or
  190. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  191. */
  192. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  193. const unsigned char *additional, size_t len );
  194. /**
  195. * \brief HMAC_DRBG generate random with additional update input
  196. *
  197. * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
  198. *
  199. * \param p_rng HMAC_DRBG context
  200. * \param output Buffer to fill
  201. * \param output_len Length of the buffer
  202. * \param additional Additional data to update with (can be NULL)
  203. * \param add_len Length of additional data (can be 0)
  204. *
  205. * \return 0 if successful, or
  206. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
  207. * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
  208. * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.
  209. */
  210. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  211. unsigned char *output, size_t output_len,
  212. const unsigned char *additional,
  213. size_t add_len );
  214. /**
  215. * \brief HMAC_DRBG generate random
  216. *
  217. * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
  218. *
  219. * \param p_rng HMAC_DRBG context
  220. * \param output Buffer to fill
  221. * \param out_len Length of the buffer
  222. *
  223. * \return 0 if successful, or
  224. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
  225. * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG
  226. */
  227. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
  228. /**
  229. * \brief Free an HMAC_DRBG context
  230. *
  231. * \param ctx HMAC_DRBG context to free.
  232. */
  233. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );
  234. #if defined(MBEDTLS_FS_IO)
  235. /**
  236. * \brief Write a seed file
  237. *
  238. * \param ctx HMAC_DRBG context
  239. * \param path Name of the file
  240. *
  241. * \return 0 if successful, 1 on file error, or
  242. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  243. */
  244. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
  245. /**
  246. * \brief Read and update a seed file. Seed is added to this
  247. * instance
  248. *
  249. * \param ctx HMAC_DRBG context
  250. * \param path Name of the file
  251. *
  252. * \return 0 if successful, 1 on file error,
  253. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
  254. * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG
  255. */
  256. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
  257. #endif /* MBEDTLS_FS_IO */
  258. #if defined(MBEDTLS_SELF_TEST)
  259. /**
  260. * \brief Checkup routine
  261. *
  262. * \return 0 if successful, or 1 if the test failed
  263. */
  264. int mbedtls_hmac_drbg_self_test( int verbose );
  265. #endif
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif /* hmac_drbg.h */