platform_util.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 (C) 2018, Arm Limited, All Rights Reserved
  9. * SPDX-License-Identifier: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. * This file is part of Mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_PLATFORM_UTIL_H
  26. #define MBEDTLS_PLATFORM_UTIL_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. #if defined(MBEDTLS_HAVE_TIME_DATE)
  34. #include "platform_time.h"
  35. #include <time.h>
  36. #endif /* MBEDTLS_HAVE_TIME_DATE */
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #if defined(MBEDTLS_CHECK_PARAMS)
  41. #if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
  42. /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
  43. * (which is what our config.h suggests). */
  44. #include <assert.h>
  45. #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
  46. #if defined(MBEDTLS_PARAM_FAILED)
  47. /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
  48. *
  49. * This flag can be used to check whether it is safe to assume that
  50. * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
  51. */
  52. #define MBEDTLS_PARAM_FAILED_ALT
  53. #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
  54. #define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
  55. #define MBEDTLS_PARAM_FAILED_ALT
  56. #else /* MBEDTLS_PARAM_FAILED */
  57. #define MBEDTLS_PARAM_FAILED( cond ) \
  58. mbedtls_param_failed( #cond, __FILE__, __LINE__ )
  59. /**
  60. * \brief User supplied callback function for parameter validation failure.
  61. * See #MBEDTLS_CHECK_PARAMS for context.
  62. *
  63. * This function will be called unless an alternative treatement
  64. * is defined through the #MBEDTLS_PARAM_FAILED macro.
  65. *
  66. * This function can return, and the operation will be aborted, or
  67. * alternatively, through use of setjmp()/longjmp() can resume
  68. * execution in the application code.
  69. *
  70. * \param failure_condition The assertion that didn't hold.
  71. * \param file The file where the assertion failed.
  72. * \param line The line in the file where the assertion failed.
  73. */
  74. void mbedtls_param_failed( const char *failure_condition,
  75. const char *file,
  76. int line );
  77. #endif /* MBEDTLS_PARAM_FAILED */
  78. /* Internal macro meant to be called only from within the library. */
  79. #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
  80. do { \
  81. if( !(cond) ) \
  82. { \
  83. MBEDTLS_PARAM_FAILED( cond ); \
  84. return( ret ); \
  85. } \
  86. } while( 0 )
  87. /* Internal macro meant to be called only from within the library. */
  88. #define MBEDTLS_INTERNAL_VALIDATE( cond ) \
  89. do { \
  90. if( !(cond) ) \
  91. { \
  92. MBEDTLS_PARAM_FAILED( cond ); \
  93. return; \
  94. } \
  95. } while( 0 )
  96. #else /* MBEDTLS_CHECK_PARAMS */
  97. /* Internal macros meant to be called only from within the library. */
  98. #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
  99. #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
  100. #endif /* MBEDTLS_CHECK_PARAMS */
  101. /* Internal helper macros for deprecating API constants. */
  102. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  103. #if defined(MBEDTLS_DEPRECATED_WARNING)
  104. /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
  105. * to avoid conflict with other headers which define and use
  106. * it, too. We might want to move all these definitions here at
  107. * some point for uniformity. */
  108. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  109. MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
  110. #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
  111. ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
  112. MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
  113. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
  114. ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
  115. #undef MBEDTLS_DEPRECATED
  116. #else /* MBEDTLS_DEPRECATED_WARNING */
  117. #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
  118. #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
  119. #endif /* MBEDTLS_DEPRECATED_WARNING */
  120. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  121. /**
  122. * \brief Securely zeroize a buffer
  123. *
  124. * The function is meant to wipe the data contained in a buffer so
  125. * that it can no longer be recovered even if the program memory
  126. * is later compromised. Call this function on sensitive data
  127. * stored on the stack before returning from a function, and on
  128. * sensitive data stored on the heap before freeing the heap
  129. * object.
  130. *
  131. * It is extremely difficult to guarantee that calls to
  132. * mbedtls_platform_zeroize() are not removed by aggressive
  133. * compiler optimizations in a portable way. For this reason, Mbed
  134. * TLS provides the configuration option
  135. * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
  136. * mbedtls_platform_zeroize() to use a suitable implementation for
  137. * their platform and needs
  138. *
  139. * \param buf Buffer to be zeroized
  140. * \param len Length of the buffer in bytes
  141. *
  142. */
  143. void mbedtls_platform_zeroize( void *buf, size_t len );
  144. #if defined(MBEDTLS_HAVE_TIME_DATE)
  145. /**
  146. * \brief Platform-specific implementation of gmtime_r()
  147. *
  148. * The function is a thread-safe abstraction that behaves
  149. * similarly to the gmtime_r() function from Unix/POSIX.
  150. *
  151. * Mbed TLS will try to identify the underlying platform and
  152. * make use of an appropriate underlying implementation (e.g.
  153. * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
  154. * not possible, then gmtime() will be used. In this case, calls
  155. * from the library to gmtime() will be guarded by the mutex
  156. * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
  157. * enabled. It is recommended that calls from outside the library
  158. * are also guarded by this mutex.
  159. *
  160. * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
  161. * unconditionally use the alternative implementation for
  162. * mbedtls_platform_gmtime_r() supplied by the user at compile time.
  163. *
  164. * \param tt Pointer to an object containing time (in seconds) since the
  165. * epoch to be converted
  166. * \param tm_buf Pointer to an object where the results will be stored
  167. *
  168. * \return Pointer to an object of type struct tm on success, otherwise
  169. * NULL
  170. */
  171. struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
  172. struct tm *tm_buf );
  173. #endif /* MBEDTLS_HAVE_TIME_DATE */
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif /* MBEDTLS_PLATFORM_UTIL_H */