hmac_drbg.h 11 KB

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