ctr_drbg.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * \file ctr_drbg.h
  3. *
  4. * \brief CTR_DRBG is based on AES-256, as defined in <em>NIST SP 800-90A:
  5. * Recommendation for Random Number Generation Using Deterministic
  6. * Random Bit Generators</em>.
  7. *
  8. */
  9. /*
  10. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * This file is part of Mbed TLS (https://tls.mbed.org)
  26. */
  27. #ifndef MBEDTLS_CTR_DRBG_H
  28. #define MBEDTLS_CTR_DRBG_H
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34. #include "aes.h"
  35. #if defined(MBEDTLS_THREADING_C)
  36. #include "threading.h"
  37. #endif
  38. #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
  39. #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
  40. #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
  41. #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
  42. #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
  43. #define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher. */
  44. #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
  45. #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE ) /**< The seed length, calculated as (counter + AES key). */
  46. /**
  47. * \name SECTION: Module settings
  48. *
  49. * The configuration options you can set for this module are in this section.
  50. * Either change them in config.h or define them using the compiler command
  51. * line.
  52. * \{
  53. */
  54. #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
  55. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
  56. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
  57. /**< The amount of entropy used per seed by default:
  58. * <ul><li>48 with SHA-512.</li>
  59. * <li>32 with SHA-256.</li></ul>
  60. */
  61. #else
  62. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
  63. /**< Amount of entropy used per seed by default:
  64. * <ul><li>48 with SHA-512.</li>
  65. * <li>32 with SHA-256.</li></ul>
  66. */
  67. #endif
  68. #endif
  69. #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
  70. #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
  71. /**< The interval before reseed is performed by default. */
  72. #endif
  73. #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
  74. #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
  75. /**< The maximum number of additional input Bytes. */
  76. #endif
  77. #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
  78. #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
  79. /**< The maximum number of requested Bytes per call. */
  80. #endif
  81. #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
  82. #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
  83. /**< The maximum size of seed or reseed buffer. */
  84. #endif
  85. /* \} name SECTION: Module settings */
  86. #define MBEDTLS_CTR_DRBG_PR_OFF 0
  87. /**< Prediction resistance is disabled. */
  88. #define MBEDTLS_CTR_DRBG_PR_ON 1
  89. /**< Prediction resistance is enabled. */
  90. #ifdef __cplusplus
  91. extern "C" {
  92. #endif
  93. /**
  94. * \brief The CTR_DRBG context structure.
  95. */
  96. typedef struct
  97. {
  98. unsigned char counter[16]; /*!< The counter (V). */
  99. int reseed_counter; /*!< The reseed counter. */
  100. int prediction_resistance; /*!< This determines whether prediction
  101. resistance is enabled, that is
  102. whether to systematically reseed before
  103. each random generation. */
  104. size_t entropy_len; /*!< The amount of entropy grabbed on each
  105. seed or reseed operation. */
  106. int reseed_interval; /*!< The reseed interval. */
  107. mbedtls_aes_context aes_ctx; /*!< The AES context. */
  108. /*
  109. * Callbacks (Entropy)
  110. */
  111. int (*f_entropy)(void *, unsigned char *, size_t);
  112. /*!< The entropy callback function. */
  113. void *p_entropy; /*!< The context for the entropy function. */
  114. #if defined(MBEDTLS_THREADING_C)
  115. mbedtls_threading_mutex_t mutex;
  116. #endif
  117. }
  118. mbedtls_ctr_drbg_context;
  119. /**
  120. * \brief This function initializes the CTR_DRBG context,
  121. * and prepares it for mbedtls_ctr_drbg_seed()
  122. * or mbedtls_ctr_drbg_free().
  123. *
  124. * \param ctx The CTR_DRBG context to initialize.
  125. */
  126. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
  127. /**
  128. * \brief This function seeds and sets up the CTR_DRBG
  129. * entropy source for future reseeds.
  130. *
  131. * \note Personalization data can be provided in addition to the more generic
  132. * entropy source, to make this instantiation as unique as possible.
  133. *
  134. * \param ctx The CTR_DRBG context to seed.
  135. * \param f_entropy The entropy callback, taking as arguments the
  136. * \p p_entropy context, the buffer to fill, and the
  137. length of the buffer.
  138. * \param p_entropy The entropy context.
  139. * \param custom Personalization data, that is device-specific
  140. identifiers. Can be NULL.
  141. * \param len The length of the personalization data.
  142. *
  143. * \return \c 0 on success, or
  144. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
  145. */
  146. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  147. int (*f_entropy)(void *, unsigned char *, size_t),
  148. void *p_entropy,
  149. const unsigned char *custom,
  150. size_t len );
  151. /**
  152. * \brief This function clears CTR_CRBG context data.
  153. *
  154. * \param ctx The CTR_DRBG context to clear.
  155. */
  156. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
  157. /**
  158. * \brief This function turns prediction resistance on or off.
  159. * The default value is off.
  160. *
  161. * \note If enabled, entropy is gathered at the beginning of
  162. * every call to mbedtls_ctr_drbg_random_with_add().
  163. * Only use this if your entropy source has sufficient
  164. * throughput.
  165. *
  166. * \param ctx The CTR_DRBG context.
  167. * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
  168. */
  169. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
  170. int resistance );
  171. /**
  172. * \brief This function sets the amount of entropy grabbed on each
  173. * seed or reseed. The default value is
  174. * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
  175. *
  176. * \param ctx The CTR_DRBG context.
  177. * \param len The amount of entropy to grab.
  178. */
  179. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
  180. size_t len );
  181. /**
  182. * \brief This function sets the reseed interval.
  183. * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
  184. *
  185. * \param ctx The CTR_DRBG context.
  186. * \param interval The reseed interval.
  187. */
  188. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
  189. int interval );
  190. /**
  191. * \brief This function reseeds the CTR_DRBG context, that is
  192. * extracts data from the entropy source.
  193. *
  194. * \param ctx The CTR_DRBG context.
  195. * \param additional Additional data to add to the state. Can be NULL.
  196. * \param len The length of the additional data.
  197. *
  198. * \return \c 0 on success, or
  199. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
  200. */
  201. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  202. const unsigned char *additional, size_t len );
  203. /**
  204. * \brief This function updates the state of the CTR_DRBG context.
  205. *
  206. * \param ctx The CTR_DRBG context.
  207. * \param additional The data to update the state with.
  208. * \param add_len Length of \p additional in bytes. This must be at
  209. * most #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
  210. *
  211. * \return \c 0 on success.
  212. * \return #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG if
  213. * \p add_len is more than
  214. * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT.
  215. * \return An error from the underlying AES cipher on failure.
  216. */
  217. int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
  218. const unsigned char *additional,
  219. size_t add_len );
  220. /**
  221. * \brief This function updates the state of the CTR_DRBG context.
  222. *
  223. * \warning This function cannot report errors. You should use
  224. * mbedtls_ctr_drbg_update_ret() instead.
  225. *
  226. * \note If \p add_len is greater than
  227. * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT, only the first
  228. * #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
  229. * The remaining Bytes are silently discarded.
  230. *
  231. * \param ctx The CTR_DRBG context.
  232. * \param additional The data to update the state with.
  233. * \param add_len Length of \p additional data.
  234. */
  235. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  236. const unsigned char *additional,
  237. size_t add_len );
  238. /**
  239. * \brief This function updates a CTR_DRBG instance with additional
  240. * data and uses it to generate random data.
  241. *
  242. * \note The function automatically reseeds if the reseed counter is exceeded.
  243. *
  244. * \param p_rng The CTR_DRBG context. This must be a pointer to a
  245. * #mbedtls_ctr_drbg_context structure.
  246. * \param output The buffer to fill.
  247. * \param output_len The length of the buffer.
  248. * \param additional Additional data to update. Can be NULL.
  249. * \param add_len The length of the additional data.
  250. *
  251. * \return \c 0 on success, or
  252. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  253. * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
  254. */
  255. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  256. unsigned char *output, size_t output_len,
  257. const unsigned char *additional, size_t add_len );
  258. /**
  259. * \brief This function uses CTR_DRBG to generate random data.
  260. *
  261. * \note The function automatically reseeds if the reseed counter is exceeded.
  262. *
  263. * \param p_rng The CTR_DRBG context. This must be a pointer to a
  264. * #mbedtls_ctr_drbg_context structure.
  265. * \param output The buffer to fill.
  266. * \param output_len The length of the buffer.
  267. *
  268. * \return \c 0 on success, or
  269. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  270. * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
  271. */
  272. int mbedtls_ctr_drbg_random( void *p_rng,
  273. unsigned char *output, size_t output_len );
  274. #if defined(MBEDTLS_FS_IO)
  275. /**
  276. * \brief This function writes a seed file.
  277. *
  278. * \param ctx The CTR_DRBG context.
  279. * \param path The name of the file.
  280. *
  281. * \return \c 0 on success,
  282. * #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
  283. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
  284. * failure.
  285. */
  286. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  287. /**
  288. * \brief This function reads and updates a seed file. The seed
  289. * is added to this instance.
  290. *
  291. * \param ctx The CTR_DRBG context.
  292. * \param path The name of the file.
  293. *
  294. * \return \c 0 on success,
  295. * #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
  296. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  297. * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
  298. */
  299. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  300. #endif /* MBEDTLS_FS_IO */
  301. /**
  302. * \brief The CTR_DRBG checkup routine.
  303. *
  304. * \return \c 0 on success, or \c 1 on failure.
  305. */
  306. int mbedtls_ctr_drbg_self_test( int verbose );
  307. /* Internal functions (do not call directly) */
  308. int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
  309. int (*)(void *, unsigned char *, size_t), void *,
  310. const unsigned char *, size_t, size_t );
  311. #ifdef __cplusplus
  312. }
  313. #endif
  314. #endif /* ctr_drbg.h */