hmac_drbg.h 12 KB

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