Mbed TLS v2.28.9
platform_util.h
Go to the documentation of this file.
1 
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 
14 #if !defined(MBEDTLS_CONFIG_FILE)
15 #include "mbedtls/config.h"
16 #else
17 #include MBEDTLS_CONFIG_FILE
18 #endif
19 
20 #include <stddef.h>
21 #if defined(MBEDTLS_HAVE_TIME_DATE)
22 #include "mbedtls/platform_time.h"
23 #include <time.h>
24 #endif /* MBEDTLS_HAVE_TIME_DATE */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #if defined(MBEDTLS_CHECK_PARAMS)
31 
32 #if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
33 /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
34  * (which is what our config.h suggests). */
35 #include <assert.h>
36 #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
37 
38 #if defined(MBEDTLS_PARAM_FAILED)
44 #define MBEDTLS_PARAM_FAILED_ALT
45 
46 #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
47 #define MBEDTLS_PARAM_FAILED(cond) assert(cond)
48 #define MBEDTLS_PARAM_FAILED_ALT
49 
50 #else /* MBEDTLS_PARAM_FAILED */
51 #define MBEDTLS_PARAM_FAILED(cond) \
52  mbedtls_param_failed( #cond, __FILE__, __LINE__)
53 
69 void mbedtls_param_failed(const char *failure_condition,
70  const char *file,
71  int line);
72 #endif /* MBEDTLS_PARAM_FAILED */
73 
74 /* Internal macro meant to be called only from within the library. */
75 #define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) \
76  do { \
77  if (!(cond)) \
78  { \
79  MBEDTLS_PARAM_FAILED(cond); \
80  return ret; \
81  } \
82  } while (0)
83 
84 /* Internal macro meant to be called only from within the library. */
85 #define MBEDTLS_INTERNAL_VALIDATE(cond) \
86  do { \
87  if (!(cond)) \
88  { \
89  MBEDTLS_PARAM_FAILED(cond); \
90  return; \
91  } \
92  } while (0)
93 
94 #else /* MBEDTLS_CHECK_PARAMS */
95 
96 /* Internal macros meant to be called only from within the library. */
97 #define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) do { } while (0)
98 #define MBEDTLS_INTERNAL_VALIDATE(cond) do { } while (0)
99 
100 #endif /* MBEDTLS_CHECK_PARAMS */
101 
102 /* Internal helper macros for deprecating API constants. */
103 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
104 #if defined(MBEDTLS_DEPRECATED_WARNING)
105 /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
106  * to avoid conflict with other headers which define and use
107  * it, too. We might want to move all these definitions here at
108  * some point for uniformity. */
109 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
110 MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t;
111 #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) \
112  ((mbedtls_deprecated_string_constant_t) (VAL))
113 MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
114 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) \
115  ((mbedtls_deprecated_numeric_constant_t) (VAL))
116 #undef MBEDTLS_DEPRECATED
117 #else /* MBEDTLS_DEPRECATED_WARNING */
118 #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
119 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
120 #endif /* MBEDTLS_DEPRECATED_WARNING */
121 #endif /* MBEDTLS_DEPRECATED_REMOVED */
122 
123 /* Implementation of the check-return facility.
124  * See the user documentation in config.h.
125  *
126  * Do not use this macro directly to annotate function: instead,
127  * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL
128  * depending on how important it is to check the return value.
129  */
130 #if !defined(MBEDTLS_CHECK_RETURN)
131 #if defined(__GNUC__)
132 #define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
133 #elif defined(_MSC_VER) && _MSC_VER >= 1700
134 #include <sal.h>
135 #define MBEDTLS_CHECK_RETURN _Check_return_
136 #else
137 #define MBEDTLS_CHECK_RETURN
138 #endif
139 #endif
140 
157 #define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
158 
176 #if defined(MBEDTLS_CHECK_RETURN_WARNING)
177 #define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
178 #else
179 #define MBEDTLS_CHECK_RETURN_TYPICAL
180 #endif
181 
193 #define MBEDTLS_CHECK_RETURN_OPTIONAL
194 
200 #if !defined(MBEDTLS_IGNORE_RETURN)
201 /* GCC doesn't silence the warning with just (void)(result).
202  * (void)!(result) is known to work up at least up to GCC 10, as well
203  * as with Clang and MSVC.
204  *
205  * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html
206  * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result
207  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
208  */
209 #define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
210 #endif
211 
212 /* If the following macro is defined, the library is being built by the test
213  * framework, and the framework is going to provide a replacement
214  * mbedtls_platform_zeroize() using a preprocessor macro, so the function
215  * declaration should be omitted. */
216 #if !defined(MBEDTLS_TEST_DEFINES_ZEROIZE) //no-check-names
239 void mbedtls_platform_zeroize(void *buf, size_t len);
240 #endif
241 
242 #if defined(MBEDTLS_HAVE_TIME_DATE)
270  struct tm *tm_buf);
271 #endif /* MBEDTLS_HAVE_TIME_DATE */
272 
273 #ifdef __cplusplus
274 }
275 #endif
276 
277 #endif /* MBEDTLS_PLATFORM_UTIL_H */
#define MBEDTLS_DEPRECATED
Definition: aes.h:625
Configuration options (set of defines)
Mbed TLS Platform time abstraction.
time_t mbedtls_time_t
Definition: platform_time.h:31
struct tm * mbedtls_platform_gmtime_r(const mbedtls_time_t *tt, struct tm *tm_buf)
Platform-specific implementation of gmtime_r()