blob: 74e2a1db6cc96843e1e3c1eea0e0570d8fce0559 [file] [log] [blame]
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -05001/**
2 * \file platform_util.h
3 *
4 * \brief Common and shared functions used by multiple modules in the Mbed TLS
5 * library.
6 */
7/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050010 */
11#ifndef MBEDTLS_PLATFORM_UTIL_H
12#define MBEDTLS_PLATFORM_UTIL_H
13
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010014#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010015#include "mbedtls/config.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010016#else
17#include MBEDTLS_CONFIG_FILE
18#endif
19
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050020#include <stddef.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010021#if defined(MBEDTLS_HAVE_TIME_DATE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010022#include "mbedtls/platform_time.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010023#include <time.h>
24#endif /* MBEDTLS_HAVE_TIME_DATE */
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050025
26#ifdef __cplusplus
27extern "C" {
28#endif
29
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#if defined(MBEDTLS_CHECK_PARAMS)
31
Gilles Peskinec7ad1222019-06-13 16:44:19 +020032#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
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050038#if defined(MBEDTLS_PARAM_FAILED)
39/** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
40 *
41 * This flag can be used to check whether it is safe to assume that
42 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
43 */
44#define MBEDTLS_PARAM_FAILED_ALT
Gilles Peskinec7ad1222019-06-13 16:44:19 +020045
46#elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010047#define MBEDTLS_PARAM_FAILED(cond) assert(cond)
Gilles Peskinec7ad1222019-06-13 16:44:19 +020048#define MBEDTLS_PARAM_FAILED_ALT
49
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050050#else /* MBEDTLS_PARAM_FAILED */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051#define MBEDTLS_PARAM_FAILED(cond) \
52 mbedtls_param_failed( #cond, __FILE__, __LINE__)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050053
54/**
55 * \brief User supplied callback function for parameter validation failure.
56 * See #MBEDTLS_CHECK_PARAMS for context.
57 *
bootstrap-prime7ef96ea2022-05-18 14:08:33 -040058 * This function will be called unless an alternative treatment
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050059 * is defined through the #MBEDTLS_PARAM_FAILED macro.
60 *
61 * This function can return, and the operation will be aborted, or
62 * alternatively, through use of setjmp()/longjmp() can resume
63 * execution in the application code.
64 *
65 * \param failure_condition The assertion that didn't hold.
66 * \param file The file where the assertion failed.
67 * \param line The line in the file where the assertion failed.
68 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069void mbedtls_param_failed(const char *failure_condition,
70 const char *file,
71 int line);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072#endif /* MBEDTLS_PARAM_FAILED */
73
74/* Internal macro meant to be called only from within the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075#define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 if (!(cond)) \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 MBEDTLS_PARAM_FAILED(cond); \
80 return ret; \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050081 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 } while (0)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083
84/* Internal macro meant to be called only from within the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085#define MBEDTLS_INTERNAL_VALIDATE(cond) \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 do { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 if (!(cond)) \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 { \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 MBEDTLS_PARAM_FAILED(cond); \
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 return; \
91 } \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 } while (0)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093
94#else /* MBEDTLS_CHECK_PARAMS */
95
96/* Internal macros meant to be called only from within the library. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097#define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) do { } while (0)
98#define MBEDTLS_INTERNAL_VALIDATE(cond) do { } while (0)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050099
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))
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t;
111#define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) \
112 ((mbedtls_deprecated_string_constant_t) (VAL))
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) \
115 ((mbedtls_deprecated_numeric_constant_t) (VAL))
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116#undef MBEDTLS_DEPRECATED
117#else /* MBEDTLS_DEPRECATED_WARNING */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118#define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
119#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120#endif /* MBEDTLS_DEPRECATED_WARNING */
121#endif /* MBEDTLS_DEPRECATED_REMOVED */
122
Gilles Peskineee0a4432021-09-23 17:28:59 +0200123/* Implementation of the check-return facility.
124 * See the user documentation in config.h.
Gilles Peskine7b8571f2021-07-07 21:02:36 +0200125 *
Gilles Peskineee0a4432021-09-23 17:28:59 +0200126 * 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.
Gilles Peskine7b8571f2021-07-07 21:02:36 +0200129 */
130#if !defined(MBEDTLS_CHECK_RETURN)
131#if defined(__GNUC__)
Gilles Peskinee568eba2021-09-23 17:46:12 +0200132#define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
Gilles Peskine7b8571f2021-07-07 21:02:36 +0200133#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
Gilles Peskineee0a4432021-09-23 17:28:59 +0200141/** Critical-failure function
142 *
143 * This macro appearing at the beginning of the declaration of a function
144 * indicates that its return value should be checked in all applications.
145 * Omitting the check is very likely to indicate a bug in the application
146 * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN
147 * is implemented for the compiler in use.
148 *
149 * \note The use of this macro is a work in progress.
150 * This macro may be added to more functions in the future.
151 * Such an extension is not considered an API break, provided that
152 * there are near-unavoidable circumstances under which the function
153 * can fail. For example, signature/MAC/AEAD verification functions,
154 * and functions that require a random generator, are considered
155 * return-check-critical.
156 */
157#define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
158
159/** Ordinary-failure function
160 *
161 * This macro appearing at the beginning of the declaration of a function
162 * indicates that its return value should be generally be checked in portable
163 * applications. Omitting the check will result in a compile-time warning if
Gilles Peskine8472a102021-09-23 18:07:36 +0200164 * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use and
165 * #MBEDTLS_CHECK_RETURN_WARNING is enabled in the compile-time configuration.
Gilles Peskineee0a4432021-09-23 17:28:59 +0200166 *
Gilles Peskinec2779322021-09-30 18:56:17 +0200167 * You can use #MBEDTLS_IGNORE_RETURN to explicitly ignore the return value
168 * of a function that is annotated with #MBEDTLS_CHECK_RETURN.
169 *
Gilles Peskineee0a4432021-09-23 17:28:59 +0200170 * \note The use of this macro is a work in progress.
171 * This macro will be added to more functions in the future.
172 * Eventually this should appear before most functions returning
173 * an error code (as \c int in the \c mbedtls_xxx API or
174 * as ::psa_status_t in the \c psa_xxx API).
175 */
Gilles Peskine8472a102021-09-23 18:07:36 +0200176#if defined(MBEDTLS_CHECK_RETURN_WARNING)
Gilles Peskineee0a4432021-09-23 17:28:59 +0200177#define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
Gilles Peskine8472a102021-09-23 18:07:36 +0200178#else
179#define MBEDTLS_CHECK_RETURN_TYPICAL
180#endif
Gilles Peskineee0a4432021-09-23 17:28:59 +0200181
182/** Benign-failure function
183 *
184 * This macro appearing at the beginning of the declaration of a function
185 * indicates that it is rarely useful to check its return value.
186 *
187 * This macro has an empty expansion. It exists for documentation purposes:
188 * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function
Tom Cosgrove2b150752022-05-26 11:55:43 +0100189 * has been analyzed for return-check usefulness, whereas the lack of
Gilles Peskineee0a4432021-09-23 17:28:59 +0200190 * an annotation indicates that the function has not been analyzed and its
191 * return-check usefulness is unknown.
192 */
193#define MBEDTLS_CHECK_RETURN_OPTIONAL
194
Mateusz Starzyk15a74202021-08-05 13:56:48 +0200195/** \def MBEDTLS_IGNORE_RETURN
196 *
Gilles Peskinec2779322021-09-30 18:56:17 +0200197 * Call this macro with one argument, a function call, to suppress a warning
198 * from #MBEDTLS_CHECK_RETURN due to that function call.
199 */
200#if !defined(MBEDTLS_IGNORE_RETURN)
Gilles Peskine327cb722021-09-30 18:54:51 +0200201/* GCC doesn't silence the warning with just (void)(result).
Gilles Peskinec79e4ab2021-09-30 20:34:29 +0200202 * (void)!(result) is known to work up at least up to GCC 10, as well
Gilles Peskine327cb722021-09-30 18:54:51 +0200203 * 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
Mateusz Starzyk15a74202021-08-05 13:56:48 +0200208 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209#define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
Gilles Peskinec2779322021-09-30 18:56:17 +0200210#endif
Mateusz Starzyk15a74202021-08-05 13:56:48 +0200211
Tom Cosgrove95b5d792023-09-01 14:34:37 +0100212/* 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
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500217/**
218 * \brief Securely zeroize a buffer
219 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500220 * The function is meant to wipe the data contained in a buffer so
221 * that it can no longer be recovered even if the program memory
222 * is later compromised. Call this function on sensitive data
223 * stored on the stack before returning from a function, and on
224 * sensitive data stored on the heap before freeing the heap
225 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500226 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500227 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500228 * mbedtls_platform_zeroize() are not removed by aggressive
229 * compiler optimizations in a portable way. For this reason, Mbed
230 * TLS provides the configuration option
231 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
232 * mbedtls_platform_zeroize() to use a suitable implementation for
233 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500234 *
235 * \param buf Buffer to be zeroized
236 * \param len Length of the buffer in bytes
237 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500238 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239void mbedtls_platform_zeroize(void *buf, size_t len);
Tom Cosgrove43210b52023-09-01 09:53:42 +0100240#endif
241
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100242#if defined(MBEDTLS_HAVE_TIME_DATE)
243/**
Hanno Beckerc52ef402018-09-05 16:28:59 +0100244 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100245 *
Hanno Beckerc52ef402018-09-05 16:28:59 +0100246 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +0100247 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100248 *
Hanno Becker6a739782018-09-05 15:06:19 +0100249 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +0100250 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +0100251 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
252 * not possible, then gmtime() will be used. In this case, calls
253 * from the library to gmtime() will be guarded by the mutex
254 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
255 * enabled. It is recommended that calls from outside the library
256 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100257 *
Hanno Becker6a739782018-09-05 15:06:19 +0100258 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
259 * unconditionally use the alternative implementation for
260 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100261 *
Hanno Becker272675f2018-09-05 14:03:02 +0100262 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100263 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100264 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100265 *
266 * \return Pointer to an object of type struct tm on success, otherwise
267 * NULL
268 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
270 struct tm *tm_buf);
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100271#endif /* MBEDTLS_HAVE_TIME_DATE */
272
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500273#ifdef __cplusplus
274}
275#endif
276
277#endif /* MBEDTLS_PLATFORM_UTIL_H */