hmac_drbg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "mbedtls/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. * \note Additional data is optional, pass NULL and 0 as second
  177. * third argument if no additional data is being used.
  178. */
  179. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  180. const unsigned char *additional, size_t add_len );
  181. /**
  182. * \brief HMAC_DRBG reseeding (extracts data from entropy source)
  183. *
  184. * \param ctx HMAC_DRBG context
  185. * \param additional Additional data to add to state (Can be NULL)
  186. * \param len Length of additional data
  187. *
  188. * \return 0 if successful, or
  189. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  190. */
  191. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  192. const unsigned char *additional, size_t len );
  193. /**
  194. * \brief HMAC_DRBG generate random with additional update input
  195. *
  196. * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
  197. *
  198. * \param p_rng HMAC_DRBG context
  199. * \param output Buffer to fill
  200. * \param output_len Length of the buffer
  201. * \param additional Additional data to update with (can be NULL)
  202. * \param add_len Length of additional data (can be 0)
  203. *
  204. * \return 0 if successful, or
  205. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
  206. * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or
  207. * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.
  208. */
  209. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  210. unsigned char *output, size_t output_len,
  211. const unsigned char *additional,
  212. size_t add_len );
  213. /**
  214. * \brief HMAC_DRBG generate random
  215. *
  216. * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.
  217. *
  218. * \param p_rng HMAC_DRBG context
  219. * \param output Buffer to fill
  220. * \param out_len Length of the buffer
  221. *
  222. * \return 0 if successful, or
  223. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
  224. * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG
  225. */
  226. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
  227. /**
  228. * \brief Free an HMAC_DRBG context
  229. *
  230. * \param ctx HMAC_DRBG context to free.
  231. */
  232. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );
  233. #if defined(MBEDTLS_FS_IO)
  234. /**
  235. * \brief Write a seed file
  236. *
  237. * \param ctx HMAC_DRBG context
  238. * \param path Name of the file
  239. *
  240. * \return 0 if successful, 1 on file error, or
  241. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
  242. */
  243. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
  244. /**
  245. * \brief Read and update a seed file. Seed is added to this
  246. * instance
  247. *
  248. * \param ctx HMAC_DRBG context
  249. * \param path Name of the file
  250. *
  251. * \return 0 if successful, 1 on file error,
  252. * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or
  253. * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG
  254. */
  255. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
  256. #endif /* MBEDTLS_FS_IO */
  257. #if defined(MBEDTLS_SELF_TEST)
  258. /**
  259. * \brief Checkup routine
  260. *
  261. * \return 0 if successful, or 1 if the test failed
  262. */
  263. int mbedtls_hmac_drbg_self_test( int verbose );
  264. #endif
  265. #ifdef __cplusplus
  266. }
  267. #endif
  268. #endif /* hmac_drbg.h */