2 * \file platform_util.h
4 * \brief Common and shared functions used by multiple modules in the Mbed TLS
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #ifndef MBEDTLS_PLATFORM_UTIL_H
24 #define MBEDTLS_PLATFORM_UTIL_H
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
29 #include MBEDTLS_CONFIG_FILE
33 #if defined(MBEDTLS_HAVE_TIME_DATE)
34 #include "mbedtls/platform_time.h"
36 #endif /* MBEDTLS_HAVE_TIME_DATE */
42 #if defined(MBEDTLS_CHECK_PARAMS)
44 #if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
45 /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
46 * (which is what our config.h suggests). */
48 #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
50 #if defined(MBEDTLS_PARAM_FAILED)
51 /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
53 * This flag can be used to check whether it is safe to assume that
54 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
56 #define MBEDTLS_PARAM_FAILED_ALT
58 #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
59 #define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
60 #define MBEDTLS_PARAM_FAILED_ALT
62 #else /* MBEDTLS_PARAM_FAILED */
63 #define MBEDTLS_PARAM_FAILED( cond ) \
64 mbedtls_param_failed( #cond, __FILE__, __LINE__ )
67 * \brief User supplied callback function for parameter validation failure.
68 * See #MBEDTLS_CHECK_PARAMS for context.
70 * This function will be called unless an alternative treatement
71 * is defined through the #MBEDTLS_PARAM_FAILED macro.
73 * This function can return, and the operation will be aborted, or
74 * alternatively, through use of setjmp()/longjmp() can resume
75 * execution in the application code.
77 * \param failure_condition The assertion that didn't hold.
78 * \param file The file where the assertion failed.
79 * \param line The line in the file where the assertion failed.
81 void mbedtls_param_failed(const char *failure_condition
,
84 #endif /* MBEDTLS_PARAM_FAILED */
86 /* Internal macro meant to be called only from within the library. */
87 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \
91 MBEDTLS_PARAM_FAILED( cond ); \
96 /* Internal macro meant to be called only from within the library. */
97 #define MBEDTLS_INTERNAL_VALIDATE( cond ) \
101 MBEDTLS_PARAM_FAILED( cond ); \
106 #else /* MBEDTLS_CHECK_PARAMS */
108 /* Internal macros meant to be called only from within the library. */
109 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 )
110 #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 )
112 #endif /* MBEDTLS_CHECK_PARAMS */
114 /* Internal helper macros for deprecating API constants. */
115 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
116 #if defined(MBEDTLS_DEPRECATED_WARNING)
117 /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
118 * to avoid conflict with other headers which define and use
119 * it, too. We might want to move all these definitions here at
120 * some point for uniformity. */
121 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
122 MBEDTLS_DEPRECATED
typedef char const *mbedtls_deprecated_string_constant_t
;
123 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \
124 ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
125 MBEDTLS_DEPRECATED
typedef int mbedtls_deprecated_numeric_constant_t
;
126 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \
127 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
128 #undef MBEDTLS_DEPRECATED
129 #else /* MBEDTLS_DEPRECATED_WARNING */
130 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
131 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
132 #endif /* MBEDTLS_DEPRECATED_WARNING */
133 #endif /* MBEDTLS_DEPRECATED_REMOVED */
136 * \brief Securely zeroize a buffer
138 * The function is meant to wipe the data contained in a buffer so
139 * that it can no longer be recovered even if the program memory
140 * is later compromised. Call this function on sensitive data
141 * stored on the stack before returning from a function, and on
142 * sensitive data stored on the heap before freeing the heap
145 * It is extremely difficult to guarantee that calls to
146 * mbedtls_platform_zeroize() are not removed by aggressive
147 * compiler optimizations in a portable way. For this reason, Mbed
148 * TLS provides the configuration option
149 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
150 * mbedtls_platform_zeroize() to use a suitable implementation for
151 * their platform and needs
153 * \param buf Buffer to be zeroized
154 * \param len Length of the buffer in bytes
157 void mbedtls_platform_zeroize(void *buf
, size_t len
);
159 #if defined(MBEDTLS_HAVE_TIME_DATE)
161 * \brief Platform-specific implementation of gmtime_r()
163 * The function is a thread-safe abstraction that behaves
164 * similarly to the gmtime_r() function from Unix/POSIX.
166 * Mbed TLS will try to identify the underlying platform and
167 * make use of an appropriate underlying implementation (e.g.
168 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
169 * not possible, then gmtime() will be used. In this case, calls
170 * from the library to gmtime() will be guarded by the mutex
171 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
172 * enabled. It is recommended that calls from outside the library
173 * are also guarded by this mutex.
175 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
176 * unconditionally use the alternative implementation for
177 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
179 * \param tt Pointer to an object containing time (in seconds) since the
180 * epoch to be converted
181 * \param tm_buf Pointer to an object where the results will be stored
183 * \return Pointer to an object of type struct tm on success, otherwise
186 struct tm
*mbedtls_platform_gmtime_r(const mbedtls_time_t
*tt
,
188 #endif /* MBEDTLS_HAVE_TIME_DATE */
194 #endif /* MBEDTLS_PLATFORM_UTIL_H */