platform_util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * \file platform_util.h
  3. *
  4. * \brief Common and shared functions used by multiple modules in the Mbed TLS
  5. * library.
  6. */
  7. /*
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  10. */
  11. #ifndef MBEDTLS_PLATFORM_UTIL_H
  12. #define MBEDTLS_PLATFORM_UTIL_H
  13. #include "mbedtls/build_info.h"
  14. #include <stddef.h>
  15. #if defined(MBEDTLS_HAVE_TIME_DATE)
  16. #include "mbedtls/platform_time.h"
  17. #include <time.h>
  18. #endif /* MBEDTLS_HAVE_TIME_DATE */
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* Internal helper macros for deprecating API constants. */
  23. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  24. #if defined(MBEDTLS_DEPRECATED_WARNING)
  25. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  26. MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t;
  27. #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) \
  28. ((mbedtls_deprecated_string_constant_t) (VAL))
  29. MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
  30. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) \
  31. ((mbedtls_deprecated_numeric_constant_t) (VAL))
  32. #else /* MBEDTLS_DEPRECATED_WARNING */
  33. #define MBEDTLS_DEPRECATED
  34. #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
  35. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
  36. #endif /* MBEDTLS_DEPRECATED_WARNING */
  37. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  38. /* Implementation of the check-return facility.
  39. * See the user documentation in mbedtls_config.h.
  40. *
  41. * Do not use this macro directly to annotate function: instead,
  42. * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL
  43. * depending on how important it is to check the return value.
  44. */
  45. #if !defined(MBEDTLS_CHECK_RETURN)
  46. #if defined(__GNUC__)
  47. #define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
  48. #elif defined(_MSC_VER) && _MSC_VER >= 1700
  49. #include <sal.h>
  50. #define MBEDTLS_CHECK_RETURN _Check_return_
  51. #else
  52. #define MBEDTLS_CHECK_RETURN
  53. #endif
  54. #endif
  55. /** Critical-failure function
  56. *
  57. * This macro appearing at the beginning of the declaration of a function
  58. * indicates that its return value should be checked in all applications.
  59. * Omitting the check is very likely to indicate a bug in the application
  60. * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN
  61. * is implemented for the compiler in use.
  62. *
  63. * \note The use of this macro is a work in progress.
  64. * This macro may be added to more functions in the future.
  65. * Such an extension is not considered an API break, provided that
  66. * there are near-unavoidable circumstances under which the function
  67. * can fail. For example, signature/MAC/AEAD verification functions,
  68. * and functions that require a random generator, are considered
  69. * return-check-critical.
  70. */
  71. #define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
  72. /** Ordinary-failure function
  73. *
  74. * This macro appearing at the beginning of the declaration of a function
  75. * indicates that its return value should be generally be checked in portable
  76. * applications. Omitting the check will result in a compile-time warning if
  77. * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use and
  78. * #MBEDTLS_CHECK_RETURN_WARNING is enabled in the compile-time configuration.
  79. *
  80. * You can use #MBEDTLS_IGNORE_RETURN to explicitly ignore the return value
  81. * of a function that is annotated with #MBEDTLS_CHECK_RETURN.
  82. *
  83. * \note The use of this macro is a work in progress.
  84. * This macro will be added to more functions in the future.
  85. * Eventually this should appear before most functions returning
  86. * an error code (as \c int in the \c mbedtls_xxx API or
  87. * as ::psa_status_t in the \c psa_xxx API).
  88. */
  89. #if defined(MBEDTLS_CHECK_RETURN_WARNING)
  90. #define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
  91. #else
  92. #define MBEDTLS_CHECK_RETURN_TYPICAL
  93. #endif
  94. /** Benign-failure function
  95. *
  96. * This macro appearing at the beginning of the declaration of a function
  97. * indicates that it is rarely useful to check its return value.
  98. *
  99. * This macro has an empty expansion. It exists for documentation purposes:
  100. * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function
  101. * has been analyzed for return-check usefulness, whereas the lack of
  102. * an annotation indicates that the function has not been analyzed and its
  103. * return-check usefulness is unknown.
  104. */
  105. #define MBEDTLS_CHECK_RETURN_OPTIONAL
  106. /** \def MBEDTLS_IGNORE_RETURN
  107. *
  108. * Call this macro with one argument, a function call, to suppress a warning
  109. * from #MBEDTLS_CHECK_RETURN due to that function call.
  110. */
  111. #if !defined(MBEDTLS_IGNORE_RETURN)
  112. /* GCC doesn't silence the warning with just (void)(result).
  113. * (void)!(result) is known to work up at least up to GCC 10, as well
  114. * as with Clang and MSVC.
  115. *
  116. * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html
  117. * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result
  118. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
  119. */
  120. #define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
  121. #endif
  122. /* If the following macro is defined, the library is being built by the test
  123. * framework, and the framework is going to provide a replacement
  124. * mbedtls_platform_zeroize() using a preprocessor macro, so the function
  125. * declaration should be omitted. */
  126. #if !defined(MBEDTLS_TEST_DEFINES_ZEROIZE) //no-check-names
  127. /**
  128. * \brief Securely zeroize a buffer
  129. *
  130. * The function is meant to wipe the data contained in a buffer so
  131. * that it can no longer be recovered even if the program memory
  132. * is later compromised. Call this function on sensitive data
  133. * stored on the stack before returning from a function, and on
  134. * sensitive data stored on the heap before freeing the heap
  135. * object.
  136. *
  137. * It is extremely difficult to guarantee that calls to
  138. * mbedtls_platform_zeroize() are not removed by aggressive
  139. * compiler optimizations in a portable way. For this reason, Mbed
  140. * TLS provides the configuration option
  141. * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
  142. * mbedtls_platform_zeroize() to use a suitable implementation for
  143. * their platform and needs
  144. *
  145. * \param buf Buffer to be zeroized
  146. * \param len Length of the buffer in bytes
  147. *
  148. */
  149. void mbedtls_platform_zeroize(void *buf, size_t len);
  150. #endif
  151. /** \brief The type of custom random generator (RNG) callbacks.
  152. *
  153. * Many Mbed TLS functions take two parameters
  154. * `mbedtls_f_rng_t *f_rng, void *p_rng`. The
  155. * library will call \c f_rng to generate
  156. * random values.
  157. *
  158. * \note This is typically one of the following:
  159. * - mbedtls_ctr_drbg_random() with \c p_rng
  160. * pointing to a #mbedtls_ctr_drbg_context;
  161. * - mbedtls_hmac_drbg_random() with \c p_rng
  162. * pointing to a #mbedtls_hmac_drbg_context;
  163. * - mbedtls_psa_get_random() with
  164. * `prng = MBEDTLS_PSA_RANDOM_STATE`.
  165. *
  166. * \note Generally, given a call
  167. * `mbedtls_foo(f_rng, p_rng, ....)`, the RNG callback
  168. * and the context only need to remain valid until
  169. * the call to `mbedtls_foo` returns. However, there
  170. * are a few exceptions where the callback is stored
  171. * in for future use. Check the documentation of
  172. * the calling function.
  173. *
  174. * \warning In a multithreaded environment, calling the
  175. * function should be thread-safe. The standard
  176. * functions provided by the library are thread-safe
  177. * when #MBEDTLS_THREADING_C is enabled.
  178. *
  179. * \warning This function must either provide as many
  180. * bytes as requested of **cryptographic quality**
  181. * random data, or return a negative error code.
  182. *
  183. * \param p_rng The \c p_rng argument that was passed along \c f_rng.
  184. * The library always passes \c p_rng unchanged.
  185. * This is typically a pointer to the random generator
  186. * state, or \c NULL if the custom random generator
  187. * doesn't need a context-specific state.
  188. * \param[out] output On success, this must be filled with \p output_size
  189. * bytes of cryptographic-quality random data.
  190. * \param output_size The number of bytes to output.
  191. *
  192. * \return \c 0 on success, or a negative error code on failure.
  193. * Library functions will generally propagate this
  194. * error code, so \c MBEDTLS_ERR_xxx values are
  195. * recommended. #MBEDTLS_ERR_ENTROPY_SOURCE_FAILED is
  196. * typically sensible for RNG failures.
  197. */
  198. typedef int mbedtls_f_rng_t(void *p_rng,
  199. unsigned char *output, size_t output_size);
  200. #if defined(MBEDTLS_HAVE_TIME_DATE)
  201. /**
  202. * \brief Platform-specific implementation of gmtime_r()
  203. *
  204. * The function is a thread-safe abstraction that behaves
  205. * similarly to the gmtime_r() function from Unix/POSIX.
  206. *
  207. * Mbed TLS will try to identify the underlying platform and
  208. * make use of an appropriate underlying implementation (e.g.
  209. * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
  210. * not possible, then gmtime() will be used. In this case, calls
  211. * from the library to gmtime() will be guarded by the mutex
  212. * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
  213. * enabled. It is recommended that calls from outside the library
  214. * are also guarded by this mutex.
  215. *
  216. * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
  217. * unconditionally use the alternative implementation for
  218. * mbedtls_platform_gmtime_r() supplied by the user at compile time.
  219. *
  220. * \param tt Pointer to an object containing time (in seconds) since the
  221. * epoch to be converted
  222. * \param tm_buf Pointer to an object where the results will be stored
  223. *
  224. * \return Pointer to an object of type struct tm on success, otherwise
  225. * NULL
  226. */
  227. struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
  228. struct tm *tm_buf);
  229. #endif /* MBEDTLS_HAVE_TIME_DATE */
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* MBEDTLS_PLATFORM_UTIL_H */