ctr_drbg.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "aes.h"
  30. #if defined(MBEDTLS_THREADING_C)
  31. #include "mbedtls/threading.h"
  32. #endif
  33. #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
  34. #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< The requested random buffer length is too big. */
  35. #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< The input (entropy + additional data) is too large. */
  36. #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read or write error in file. */
  37. #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< The block size used by the cipher. */
  38. #define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< The key size used by the cipher. */
  39. #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 ) /**< The key size for the DRBG operation, in bits. */
  40. #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE ) /**< The seed length, calculated as (counter + AES key). */
  41. /**
  42. * \name SECTION: Module settings
  43. *
  44. * The configuration options you can set for this module are in this section.
  45. * Either change them in config.h or define them using the compiler command
  46. * line.
  47. * \{
  48. */
  49. #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
  50. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
  51. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
  52. /**< The amount of entropy used per seed by default:
  53. * <ul><li>48 with SHA-512.</li>
  54. * <li>32 with SHA-256.</li></ul>
  55. */
  56. #else
  57. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
  58. /**< Amount of entropy used per seed by default:
  59. * <ul><li>48 with SHA-512.</li>
  60. * <li>32 with SHA-256.</li></ul>
  61. */
  62. #endif
  63. #endif
  64. #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
  65. #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
  66. /**< The interval before reseed is performed by default. */
  67. #endif
  68. #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
  69. #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
  70. /**< The maximum number of additional input Bytes. */
  71. #endif
  72. #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
  73. #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
  74. /**< The maximum number of requested Bytes per call. */
  75. #endif
  76. #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
  77. #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
  78. /**< The maximum size of seed or reseed buffer. */
  79. #endif
  80. /* \} name SECTION: Module settings */
  81. #define MBEDTLS_CTR_DRBG_PR_OFF 0
  82. /**< Prediction resistance is disabled. */
  83. #define MBEDTLS_CTR_DRBG_PR_ON 1
  84. /**< Prediction resistance is enabled. */
  85. #ifdef __cplusplus
  86. extern "C" {
  87. #endif
  88. /**
  89. * \brief The CTR_DRBG context structure.
  90. */
  91. typedef struct
  92. {
  93. unsigned char counter[16]; /*!< The counter (V). */
  94. int reseed_counter; /*!< The reseed counter. */
  95. int prediction_resistance; /*!< This determines whether prediction
  96. resistance is enabled, that is
  97. whether to systematically reseed before
  98. each random generation. */
  99. size_t entropy_len; /*!< The amount of entropy grabbed on each
  100. seed or reseed operation. */
  101. int reseed_interval; /*!< The reseed interval. */
  102. mbedtls_aes_context aes_ctx; /*!< The AES context. */
  103. /*
  104. * Callbacks (Entropy)
  105. */
  106. int (*f_entropy)(void *, unsigned char *, size_t);
  107. /*!< The entropy callback function. */
  108. void *p_entropy; /*!< The context for the entropy function. */
  109. #if defined(MBEDTLS_THREADING_C)
  110. mbedtls_threading_mutex_t mutex;
  111. #endif
  112. }
  113. mbedtls_ctr_drbg_context;
  114. /**
  115. * \brief This function initializes the CTR_DRBG context,
  116. * and prepares it for mbedtls_ctr_drbg_seed()
  117. * or mbedtls_ctr_drbg_free().
  118. *
  119. * \param ctx The CTR_DRBG context to initialize.
  120. */
  121. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
  122. /**
  123. * \brief This function seeds and sets up the CTR_DRBG
  124. * entropy source for future reseeds.
  125. *
  126. * \note Personalization data can be provided in addition to the more generic
  127. * entropy source, to make this instantiation as unique as possible.
  128. *
  129. * \param ctx The CTR_DRBG context to seed.
  130. * \param f_entropy The entropy callback, taking as arguments the
  131. * \p p_entropy context, the buffer to fill, and the
  132. length of the buffer.
  133. * \param p_entropy The entropy context.
  134. * \param custom Personalization data, that is device-specific
  135. identifiers. Can be NULL.
  136. * \param len The length of the personalization data.
  137. *
  138. * \return \c 0 on success, or
  139. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
  140. */
  141. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  142. int (*f_entropy)(void *, unsigned char *, size_t),
  143. void *p_entropy,
  144. const unsigned char *custom,
  145. size_t len );
  146. /**
  147. * \brief This function clears CTR_CRBG context data.
  148. *
  149. * \param ctx The CTR_DRBG context to clear.
  150. */
  151. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
  152. /**
  153. * \brief This function turns prediction resistance on or off.
  154. * The default value is off.
  155. *
  156. * \note If enabled, entropy is gathered at the beginning of
  157. * every call to mbedtls_ctr_drbg_random_with_add().
  158. * Only use this if your entropy source has sufficient
  159. * throughput.
  160. *
  161. * \param ctx The CTR_DRBG context.
  162. * \param resistance #MBEDTLS_CTR_DRBG_PR_ON or #MBEDTLS_CTR_DRBG_PR_OFF.
  163. */
  164. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
  165. int resistance );
  166. /**
  167. * \brief This function sets the amount of entropy grabbed on each
  168. * seed or reseed. The default value is
  169. * #MBEDTLS_CTR_DRBG_ENTROPY_LEN.
  170. *
  171. * \param ctx The CTR_DRBG context.
  172. * \param len The amount of entropy to grab.
  173. */
  174. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
  175. size_t len );
  176. /**
  177. * \brief This function sets the reseed interval.
  178. * The default value is #MBEDTLS_CTR_DRBG_RESEED_INTERVAL.
  179. *
  180. * \param ctx The CTR_DRBG context.
  181. * \param interval The reseed interval.
  182. */
  183. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
  184. int interval );
  185. /**
  186. * \brief This function reseeds the CTR_DRBG context, that is
  187. * extracts data from the entropy source.
  188. *
  189. * \param ctx The CTR_DRBG context.
  190. * \param additional Additional data to add to the state. Can be NULL.
  191. * \param len The length of the additional data.
  192. *
  193. * \return \c 0 on success, or
  194. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on failure.
  195. */
  196. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  197. const unsigned char *additional, size_t len );
  198. /**
  199. * \brief This function updates the state of the CTR_DRBG context.
  200. *
  201. * \param ctx The CTR_DRBG context.
  202. * \param additional The data to update the state with.
  203. * \param add_len Length of \p additional data.
  204. *
  205. * \note If \p add_len is greater than #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
  206. * only the first #MBEDTLS_CTR_DRBG_MAX_SEED_INPUT Bytes are used.
  207. * The remaining Bytes are silently discarded.
  208. */
  209. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  210. const unsigned char *additional, size_t add_len );
  211. /**
  212. * \brief This function updates a CTR_DRBG instance with additional
  213. * data and uses it to generate random data.
  214. *
  215. * \note The function automatically reseeds if the reseed counter is exceeded.
  216. *
  217. * \param p_rng The CTR_DRBG context. This must be a pointer to a
  218. * #mbedtls_ctr_drbg_context structure.
  219. * \param output The buffer to fill.
  220. * \param output_len The length of the buffer.
  221. * \param additional Additional data to update. Can be NULL.
  222. * \param add_len The length of the additional data.
  223. *
  224. * \return \c 0 on success, or
  225. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  226. * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
  227. */
  228. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  229. unsigned char *output, size_t output_len,
  230. const unsigned char *additional, size_t add_len );
  231. /**
  232. * \brief This function uses CTR_DRBG to generate random data.
  233. *
  234. * \note The function automatically reseeds if the reseed counter is exceeded.
  235. *
  236. * \param p_rng The CTR_DRBG context. This must be a pointer to a
  237. * #mbedtls_ctr_drbg_context structure.
  238. * \param output The buffer to fill.
  239. * \param output_len The length of the buffer.
  240. *
  241. * \return \c 0 on success, or
  242. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  243. * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
  244. */
  245. int mbedtls_ctr_drbg_random( void *p_rng,
  246. unsigned char *output, size_t output_len );
  247. #if defined(MBEDTLS_FS_IO)
  248. /**
  249. * \brief This function writes a seed file.
  250. *
  251. * \param ctx The CTR_DRBG context.
  252. * \param path The name of the file.
  253. *
  254. * \return \c 0 on success,
  255. * #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
  256. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED on
  257. * failure.
  258. */
  259. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  260. /**
  261. * \brief This function reads and updates a seed file. The seed
  262. * is added to this instance.
  263. *
  264. * \param ctx The CTR_DRBG context.
  265. * \param path The name of the file.
  266. *
  267. * \return \c 0 on success,
  268. * #MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
  269. * #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  270. * #MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG on failure.
  271. */
  272. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  273. #endif /* MBEDTLS_FS_IO */
  274. /**
  275. * \brief The CTR_DRBG checkup routine.
  276. *
  277. * \return \c 0 on success, or \c 1 on failure.
  278. */
  279. int mbedtls_ctr_drbg_self_test( int verbose );
  280. /* Internal functions (do not call directly) */
  281. int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
  282. int (*)(void *, unsigned char *, size_t), void *,
  283. const unsigned char *, size_t, size_t );
  284. #ifdef __cplusplus
  285. }
  286. #endif
  287. #endif /* ctr_drbg.h */