ctr_drbg.h 14 KB

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