ctr_drbg.h 11 KB

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