hmac_drbg.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * \file hmac_drbg.h
  3. *
  4. * \brief The HMAC_DRBG pseudorandom generator.
  5. *
  6. * This module implements the HMAC_DRBG pseudorandom generator described
  7. * in <em>NIST SP 800-90A: Recommendation for Random Number Generation Using
  8. * Deterministic Random Bit Generators</em>.
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. */
  14. #ifndef MBEDTLS_HMAC_DRBG_H
  15. #define MBEDTLS_HMAC_DRBG_H
  16. #include "mbedtls/private_access.h"
  17. #include "mbedtls/build_info.h"
  18. #include "mbedtls/md.h"
  19. #if defined(MBEDTLS_THREADING_C)
  20. #include "mbedtls/threading.h"
  21. #endif
  22. /*
  23. * Error codes
  24. */
  25. /** Too many random requested in single call. */
  26. #define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003
  27. /** Input too large (Entropy + additional). */
  28. #define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005
  29. /** Read/write error in file. */
  30. #define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007
  31. /** The entropy source failed. */
  32. #define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009
  33. /**
  34. * \name SECTION: Module settings
  35. *
  36. * The configuration options you can set for this module are in this section.
  37. * Either change them in mbedtls_config.h or define them on the compiler command line.
  38. * \{
  39. */
  40. #if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
  41. #define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  42. #endif
  43. #if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
  44. #define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  45. #endif
  46. #if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
  47. #define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  48. #endif
  49. #if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
  50. #define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  51. #endif
  52. /** \} name SECTION: Module settings */
  53. #define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */
  54. #define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**
  59. * HMAC_DRBG context.
  60. */
  61. typedef struct mbedtls_hmac_drbg_context {
  62. /* Working state: the key K is not stored explicitly,
  63. * but is implied by the HMAC context */
  64. mbedtls_md_context_t MBEDTLS_PRIVATE(md_ctx); /*!< HMAC context (inc. K) */
  65. unsigned char MBEDTLS_PRIVATE(V)[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */
  66. int MBEDTLS_PRIVATE(reseed_counter); /*!< reseed counter */
  67. /* Administrative state */
  68. size_t MBEDTLS_PRIVATE(entropy_len); /*!< entropy bytes grabbed on each (re)seed */
  69. int MBEDTLS_PRIVATE(prediction_resistance); /*!< enable prediction resistance (Automatic
  70. reseed before every random generation) */
  71. int MBEDTLS_PRIVATE(reseed_interval); /*!< reseed interval */
  72. /* Callbacks */
  73. int(*MBEDTLS_PRIVATE(f_entropy))(void *, unsigned char *, size_t); /*!< entropy function */
  74. void *MBEDTLS_PRIVATE(p_entropy); /*!< context for the entropy function */
  75. #if defined(MBEDTLS_THREADING_C)
  76. /* Invariant: the mutex is initialized if and only if
  77. * md_ctx->md_info != NULL. This means that the mutex is initialized
  78. * during the initial seeding in mbedtls_hmac_drbg_seed() or
  79. * mbedtls_hmac_drbg_seed_buf() and freed in mbedtls_ctr_drbg_free().
  80. *
  81. * Note that this invariant may change without notice. Do not rely on it
  82. * and do not access the mutex directly in application code.
  83. */
  84. mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
  85. #endif
  86. } mbedtls_hmac_drbg_context;
  87. /**
  88. * \brief HMAC_DRBG context initialization.
  89. *
  90. * This function makes the context ready for mbedtls_hmac_drbg_seed(),
  91. * mbedtls_hmac_drbg_seed_buf() or mbedtls_hmac_drbg_free().
  92. *
  93. * \note The reseed interval is #MBEDTLS_HMAC_DRBG_RESEED_INTERVAL
  94. * by default. Override this value by calling
  95. * mbedtls_hmac_drbg_set_reseed_interval().
  96. *
  97. * \param ctx HMAC_DRBG context to be initialized.
  98. */
  99. void mbedtls_hmac_drbg_init(mbedtls_hmac_drbg_context *ctx);
  100. /**
  101. * \brief HMAC_DRBG initial seeding.
  102. *
  103. * Set the initial seed and set up the entropy source for future reseeds.
  104. *
  105. * A typical choice for the \p f_entropy and \p p_entropy parameters is
  106. * to use the entropy module:
  107. * - \p f_entropy is mbedtls_entropy_func();
  108. * - \p p_entropy is an instance of ::mbedtls_entropy_context initialized
  109. * with mbedtls_entropy_init() (which registers the platform's default
  110. * entropy sources).
  111. *
  112. * You can provide a personalization string in addition to the
  113. * entropy source, to make this instantiation as unique as possible.
  114. *
  115. * \note By default, the security strength as defined by NIST is:
  116. * - 128 bits if \p md_info is SHA-1;
  117. * - 192 bits if \p md_info is SHA-224;
  118. * - 256 bits if \p md_info is SHA-256, SHA-384 or SHA-512.
  119. * Note that SHA-256 is just as efficient as SHA-224.
  120. * The security strength can be reduced if a smaller
  121. * entropy length is set with
  122. * mbedtls_hmac_drbg_set_entropy_len().
  123. *
  124. * \note The default entropy length is the security strength
  125. * (converted from bits to bytes). You can override
  126. * it by calling mbedtls_hmac_drbg_set_entropy_len().
  127. *
  128. * \note During the initial seeding, this function calls
  129. * the entropy source to obtain a nonce
  130. * whose length is half the entropy length.
  131. */
  132. #if defined(MBEDTLS_THREADING_C)
  133. /**
  134. * \note When Mbed TLS is built with threading support,
  135. * after this function returns successfully,
  136. * it is safe to call mbedtls_hmac_drbg_random()
  137. * from multiple threads. Other operations, including
  138. * reseeding, are not thread-safe.
  139. */
  140. #endif /* MBEDTLS_THREADING_C */
  141. /**
  142. * \param ctx HMAC_DRBG context to be seeded.
  143. * \param md_info MD algorithm to use for HMAC_DRBG.
  144. * \param f_entropy The entropy callback, taking as arguments the
  145. * \p p_entropy context, the buffer to fill, and the
  146. * length of the buffer.
  147. * \p f_entropy is always called with a length that is
  148. * less than or equal to the entropy length.
  149. * \param p_entropy The entropy context to pass to \p f_entropy.
  150. * \param custom The personalization string.
  151. * This can be \c NULL, in which case the personalization
  152. * string is empty regardless of the value of \p len.
  153. * \param len The length of the personalization string.
  154. * This must be at most #MBEDTLS_HMAC_DRBG_MAX_INPUT
  155. * and also at most
  156. * #MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT - \c entropy_len * 3 / 2
  157. * where \c entropy_len is the entropy length
  158. * described above.
  159. *
  160. * \return \c 0 if successful.
  161. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info is
  162. * invalid.
  163. * \return #MBEDTLS_ERR_MD_ALLOC_FAILED if there was not enough
  164. * memory to allocate context data.
  165. * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  166. * if the call to \p f_entropy failed.
  167. */
  168. int mbedtls_hmac_drbg_seed(mbedtls_hmac_drbg_context *ctx,
  169. const mbedtls_md_info_t *md_info,
  170. int (*f_entropy)(void *, unsigned char *, size_t),
  171. void *p_entropy,
  172. const unsigned char *custom,
  173. size_t len);
  174. /**
  175. * \brief Initialisation of simplified HMAC_DRBG (never reseeds).
  176. *
  177. * This function is meant for use in algorithms that need a pseudorandom
  178. * input such as deterministic ECDSA.
  179. */
  180. #if defined(MBEDTLS_THREADING_C)
  181. /**
  182. * \note When Mbed TLS is built with threading support,
  183. * after this function returns successfully,
  184. * it is safe to call mbedtls_hmac_drbg_random()
  185. * from multiple threads. Other operations, including
  186. * reseeding, are not thread-safe.
  187. */
  188. #endif /* MBEDTLS_THREADING_C */
  189. /**
  190. * \param ctx HMAC_DRBG context to be initialised.
  191. * \param md_info MD algorithm to use for HMAC_DRBG.
  192. * \param data Concatenation of the initial entropy string and
  193. * the additional data.
  194. * \param data_len Length of \p data in bytes.
  195. *
  196. * \return \c 0 if successful. or
  197. * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info is
  198. * invalid.
  199. * \return #MBEDTLS_ERR_MD_ALLOC_FAILED if there was not enough
  200. * memory to allocate context data.
  201. */
  202. int mbedtls_hmac_drbg_seed_buf(mbedtls_hmac_drbg_context *ctx,
  203. const mbedtls_md_info_t *md_info,
  204. const unsigned char *data, size_t data_len);
  205. /**
  206. * \brief This function turns prediction resistance on or off.
  207. * The default value is off.
  208. *
  209. * \note If enabled, entropy is gathered at the beginning of
  210. * every call to mbedtls_hmac_drbg_random_with_add()
  211. * or mbedtls_hmac_drbg_random().
  212. * Only use this if your entropy source has sufficient
  213. * throughput.
  214. *
  215. * \param ctx The HMAC_DRBG context.
  216. * \param resistance #MBEDTLS_HMAC_DRBG_PR_ON or #MBEDTLS_HMAC_DRBG_PR_OFF.
  217. */
  218. void mbedtls_hmac_drbg_set_prediction_resistance(mbedtls_hmac_drbg_context *ctx,
  219. int resistance);
  220. /**
  221. * \brief This function sets the amount of entropy grabbed on each
  222. * seed or reseed.
  223. *
  224. * See the documentation of mbedtls_hmac_drbg_seed() for the default value.
  225. *
  226. * \param ctx The HMAC_DRBG context.
  227. * \param len The amount of entropy to grab, in bytes.
  228. */
  229. void mbedtls_hmac_drbg_set_entropy_len(mbedtls_hmac_drbg_context *ctx,
  230. size_t len);
  231. /**
  232. * \brief Set the reseed interval.
  233. *
  234. * The reseed interval is the number of calls to mbedtls_hmac_drbg_random()
  235. * or mbedtls_hmac_drbg_random_with_add() after which the entropy function
  236. * is called again.
  237. *
  238. * The default value is #MBEDTLS_HMAC_DRBG_RESEED_INTERVAL.
  239. *
  240. * \param ctx The HMAC_DRBG context.
  241. * \param interval The reseed interval.
  242. */
  243. void mbedtls_hmac_drbg_set_reseed_interval(mbedtls_hmac_drbg_context *ctx,
  244. int interval);
  245. /**
  246. * \brief This function updates the state of the HMAC_DRBG context.
  247. *
  248. * \note This function is not thread-safe. It is not safe
  249. * to call this function if another thread might be
  250. * concurrently obtaining random numbers from the same
  251. * context or updating or reseeding the same context.
  252. *
  253. * \param ctx The HMAC_DRBG context.
  254. * \param additional The data to update the state with.
  255. * If this is \c NULL, there is no additional data.
  256. * \param add_len Length of \p additional in bytes.
  257. * Unused if \p additional is \c NULL.
  258. *
  259. * \return \c 0 on success, or an error from the underlying
  260. * hash calculation.
  261. */
  262. int mbedtls_hmac_drbg_update(mbedtls_hmac_drbg_context *ctx,
  263. const unsigned char *additional, size_t add_len);
  264. /**
  265. * \brief This function reseeds the HMAC_DRBG context, that is
  266. * extracts data from the entropy source.
  267. *
  268. * \note This function is not thread-safe. It is not safe
  269. * to call this function if another thread might be
  270. * concurrently obtaining random numbers from the same
  271. * context or updating or reseeding the same context.
  272. *
  273. * \param ctx The HMAC_DRBG context.
  274. * \param additional Additional data to add to the state.
  275. * If this is \c NULL, there is no additional data
  276. * and \p len should be \c 0.
  277. * \param len The length of the additional data.
  278. * This must be at most #MBEDTLS_HMAC_DRBG_MAX_INPUT
  279. * and also at most
  280. * #MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT - \c entropy_len
  281. * where \c entropy_len is the entropy length
  282. * (see mbedtls_hmac_drbg_set_entropy_len()).
  283. *
  284. * \return \c 0 if successful.
  285. * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  286. * if a call to the entropy function failed.
  287. */
  288. int mbedtls_hmac_drbg_reseed(mbedtls_hmac_drbg_context *ctx,
  289. const unsigned char *additional, size_t len);
  290. /**
  291. * \brief This function updates an HMAC_DRBG instance with additional
  292. * data and uses it to generate random data.
  293. *
  294. * This function automatically reseeds if the reseed counter is exceeded
  295. * or prediction resistance is enabled.
  296. *
  297. * \note This function is not thread-safe. It is not safe
  298. * to call this function if another thread might be
  299. * concurrently obtaining random numbers from the same
  300. * context or updating or reseeding the same context.
  301. *
  302. * \param p_rng The HMAC_DRBG context. This must be a pointer to a
  303. * #mbedtls_hmac_drbg_context structure.
  304. * \param output The buffer to fill.
  305. * \param output_len The length of the buffer in bytes.
  306. * This must be at most #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
  307. * \param additional Additional data to update with.
  308. * If this is \c NULL, there is no additional data
  309. * and \p add_len should be \c 0.
  310. * \param add_len The length of the additional data.
  311. * This must be at most #MBEDTLS_HMAC_DRBG_MAX_INPUT.
  312. *
  313. * \return \c 0 if successful.
  314. * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  315. * if a call to the entropy source failed.
  316. * \return #MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG if
  317. * \p output_len > #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
  318. * \return #MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG if
  319. * \p add_len > #MBEDTLS_HMAC_DRBG_MAX_INPUT.
  320. */
  321. int mbedtls_hmac_drbg_random_with_add(void *p_rng,
  322. unsigned char *output, size_t output_len,
  323. const unsigned char *additional,
  324. size_t add_len);
  325. /**
  326. * \brief This function uses HMAC_DRBG to generate random data.
  327. *
  328. * This function automatically reseeds if the reseed counter is exceeded
  329. * or prediction resistance is enabled.
  330. */
  331. #if defined(MBEDTLS_THREADING_C)
  332. /**
  333. * \note When Mbed TLS is built with threading support,
  334. * it is safe to call mbedtls_ctr_drbg_random()
  335. * from multiple threads. Other operations, including
  336. * reseeding, are not thread-safe.
  337. */
  338. #endif /* MBEDTLS_THREADING_C */
  339. /**
  340. * \param p_rng The HMAC_DRBG context. This must be a pointer to a
  341. * #mbedtls_hmac_drbg_context structure.
  342. * \param output The buffer to fill.
  343. * \param out_len The length of the buffer in bytes.
  344. * This must be at most #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
  345. *
  346. * \return \c 0 if successful.
  347. * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  348. * if a call to the entropy source failed.
  349. * \return #MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG if
  350. * \p out_len > #MBEDTLS_HMAC_DRBG_MAX_REQUEST.
  351. */
  352. int mbedtls_hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len);
  353. /**
  354. * \brief This function resets HMAC_DRBG context to the state immediately
  355. * after initial call of mbedtls_hmac_drbg_init().
  356. *
  357. * \param ctx The HMAC_DRBG context to free.
  358. */
  359. void mbedtls_hmac_drbg_free(mbedtls_hmac_drbg_context *ctx);
  360. #if defined(MBEDTLS_FS_IO)
  361. /**
  362. * \brief This function writes a seed file.
  363. *
  364. * \param ctx The HMAC_DRBG context.
  365. * \param path The name of the file.
  366. *
  367. * \return \c 0 on success.
  368. * \return #MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR on file error.
  369. * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED on reseed
  370. * failure.
  371. */
  372. int mbedtls_hmac_drbg_write_seed_file(mbedtls_hmac_drbg_context *ctx, const char *path);
  373. /**
  374. * \brief This function reads and updates a seed file. The seed
  375. * is added to this instance.
  376. *
  377. * \param ctx The HMAC_DRBG context.
  378. * \param path The name of the file.
  379. *
  380. * \return \c 0 on success.
  381. * \return #MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR on file error.
  382. * \return #MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED on
  383. * reseed failure.
  384. * \return #MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG if the existing
  385. * seed file is too large.
  386. */
  387. int mbedtls_hmac_drbg_update_seed_file(mbedtls_hmac_drbg_context *ctx, const char *path);
  388. #endif /* MBEDTLS_FS_IO */
  389. #if defined(MBEDTLS_SELF_TEST)
  390. /**
  391. * \brief The HMAC_DRBG Checkup routine.
  392. *
  393. * \return \c 0 if successful.
  394. * \return \c 1 if the test failed.
  395. */
  396. int mbedtls_hmac_drbg_self_test(int verbose);
  397. #endif
  398. #ifdef __cplusplus
  399. }
  400. #endif
  401. #endif /* hmac_drbg.h */