ctr_drbg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * \file ctr_drbg.h
  3. *
  4. * \brief CTR_DRBG based on AES-256 (NIST SP 800-90)
  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_CTR_DRBG_H
  24. #define MBEDTLS_CTR_DRBG_H
  25. #include "aes.h"
  26. #if defined(MBEDTLS_THREADING_C)
  27. #include "mbedtls/threading.h"
  28. #endif
  29. #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
  30. #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
  31. #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
  32. #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
  33. #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
  34. #define MBEDTLS_CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
  35. #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
  36. #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
  37. /**< The seed length (counter + AES key) */
  38. /**
  39. * \name SECTION: Module settings
  40. *
  41. * The configuration options you can set for this module are in this section.
  42. * Either change them in config.h or define them on the compiler command line.
  43. * \{
  44. */
  45. #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
  46. #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
  47. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
  48. #else
  49. #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
  50. #endif
  51. #endif
  52. #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
  53. #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
  54. #endif
  55. #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
  56. #define MBEDTLS_CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
  57. #endif
  58. #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
  59. #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
  60. #endif
  61. #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
  62. #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
  63. #endif
  64. /* \} name SECTION: Module settings */
  65. #define MBEDTLS_CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
  66. #define MBEDTLS_CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
  67. #ifdef __cplusplus
  68. extern "C" {
  69. #endif
  70. /**
  71. * \brief CTR_DRBG context structure
  72. */
  73. typedef struct
  74. {
  75. unsigned char counter[16]; /*!< counter (V) */
  76. int reseed_counter; /*!< reseed counter */
  77. int prediction_resistance; /*!< enable prediction resistance (Automatic
  78. reseed before every random generation) */
  79. size_t entropy_len; /*!< amount of entropy grabbed on each
  80. (re)seed */
  81. int reseed_interval; /*!< reseed interval */
  82. mbedtls_aes_context aes_ctx; /*!< AES context */
  83. /*
  84. * Callbacks (Entropy)
  85. */
  86. int (*f_entropy)(void *, unsigned char *, size_t);
  87. void *p_entropy; /*!< context for the entropy function */
  88. #if defined(MBEDTLS_THREADING_C)
  89. mbedtls_threading_mutex_t mutex;
  90. #endif
  91. }
  92. mbedtls_ctr_drbg_context;
  93. /**
  94. * \brief CTR_DRBG context initialization
  95. * Makes the context ready for mbedtls_ctr_drbg_seed() or
  96. * mbedtls_ctr_drbg_free().
  97. *
  98. * \param ctx CTR_DRBG context to be initialized
  99. */
  100. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
  101. /**
  102. * \brief CTR_DRBG initial seeding
  103. * Seed and setup entropy source for future reseeds.
  104. *
  105. * Note: Personalization data can be provided in addition to the more generic
  106. * entropy source to make this instantiation as unique as possible.
  107. *
  108. * \param ctx CTR_DRBG context to be seeded
  109. * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
  110. * length)
  111. * \param p_entropy Entropy context
  112. * \param custom Personalization data (Device specific identifiers)
  113. * (Can be NULL)
  114. * \param len Length of personalization data
  115. *
  116. * \return 0 if successful, or
  117. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  118. */
  119. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  120. int (*f_entropy)(void *, unsigned char *, size_t),
  121. void *p_entropy,
  122. const unsigned char *custom,
  123. size_t len );
  124. /**
  125. * \brief Clear CTR_CRBG context data
  126. *
  127. * \param ctx CTR_DRBG context to clear
  128. */
  129. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
  130. /**
  131. * \brief Enable / disable prediction resistance (Default: Off)
  132. *
  133. * Note: If enabled, entropy is used for ctx->entropy_len before each call!
  134. * Only use this if you have ample supply of good entropy!
  135. *
  136. * \param ctx CTR_DRBG context
  137. * \param resistance MBEDTLS_CTR_DRBG_PR_ON or MBEDTLS_CTR_DRBG_PR_OFF
  138. */
  139. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
  140. int resistance );
  141. /**
  142. * \brief Set the amount of entropy grabbed on each (re)seed
  143. * (Default: MBEDTLS_CTR_DRBG_ENTROPY_LEN)
  144. *
  145. * \param ctx CTR_DRBG context
  146. * \param len Amount of entropy to grab
  147. */
  148. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
  149. size_t len );
  150. /**
  151. * \brief Set the reseed interval
  152. * (Default: MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
  153. *
  154. * \param ctx CTR_DRBG context
  155. * \param interval Reseed interval
  156. */
  157. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
  158. int interval );
  159. /**
  160. * \brief CTR_DRBG reseeding (extracts data from entropy source)
  161. *
  162. * \param ctx CTR_DRBG context
  163. * \param additional Additional data to add to state (Can be NULL)
  164. * \param len Length of additional data
  165. *
  166. * \return 0 if successful, or
  167. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  168. */
  169. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  170. const unsigned char *additional, size_t len );
  171. /**
  172. * \brief CTR_DRBG update state
  173. *
  174. * \param ctx CTR_DRBG context
  175. * \param additional Additional data to update state with
  176. * \param add_len Length of additional data
  177. *
  178. * \note If add_len is greater than MBEDTLS_CTR_DRBG_MAX_SEED_INPUT,
  179. * only the first MBEDTLS_CTR_DRBG_MAX_SEED_INPUT bytes are used,
  180. * the remaining ones are silently discarded.
  181. */
  182. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  183. const unsigned char *additional, size_t add_len );
  184. /**
  185. * \brief CTR_DRBG generate random with additional update input
  186. *
  187. * Note: Automatically reseeds if reseed_counter is reached.
  188. *
  189. * \param p_rng CTR_DRBG context
  190. * \param output Buffer to fill
  191. * \param output_len Length of the buffer
  192. * \param additional Additional data to update with (Can be NULL)
  193. * \param add_len Length of additional data
  194. *
  195. * \return 0 if successful, or
  196. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
  197. * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
  198. */
  199. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  200. unsigned char *output, size_t output_len,
  201. const unsigned char *additional, size_t add_len );
  202. /**
  203. * \brief CTR_DRBG generate random
  204. *
  205. * Note: Automatically reseeds if reseed_counter is reached.
  206. *
  207. * \param p_rng CTR_DRBG context
  208. * \param output Buffer to fill
  209. * \param output_len Length of the buffer
  210. *
  211. * \return 0 if successful, or
  212. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
  213. * MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG
  214. */
  215. int mbedtls_ctr_drbg_random( void *p_rng,
  216. unsigned char *output, size_t output_len );
  217. #if defined(MBEDTLS_FS_IO)
  218. /**
  219. * \brief Write a seed file
  220. *
  221. * \param ctx CTR_DRBG context
  222. * \param path Name of the file
  223. *
  224. * \return 0 if successful,
  225. * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
  226. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
  227. */
  228. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  229. /**
  230. * \brief Read and update a seed file. Seed is added to this
  231. * instance
  232. *
  233. * \param ctx CTR_DRBG context
  234. * \param path Name of the file
  235. *
  236. * \return 0 if successful,
  237. * MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
  238. * MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
  239. * MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG
  240. */
  241. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
  242. #endif /* MBEDTLS_FS_IO */
  243. /**
  244. * \brief Checkup routine
  245. *
  246. * \return 0 if successful, or 1 if the test failed
  247. */
  248. int mbedtls_ctr_drbg_self_test( int verbose );
  249. /* Internal functions (do not call directly) */
  250. int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
  251. int (*)(void *, unsigned char *, size_t), void *,
  252. const unsigned char *, size_t, size_t );
  253. #ifdef __cplusplus
  254. }
  255. #endif
  256. #endif /* ctr_drbg.h */