hmac_drbg.h 19 KB

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