blob: cba02ab3da5ca62435c1684a760525aa3dd5b935 [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 Rodgman16799db2023-11-02 19:47:20 +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
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010015
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050016#include <stddef.h>
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010017#if defined(MBEDTLS_HAVE_TIME_DATE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010018#include "mbedtls/platform_time.h"
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +010019#include <time.h>
20#endif /* MBEDTLS_HAVE_TIME_DATE */
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -050021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026/* Internal macros meant to be called only from within the library. */
Gilles Peskine449bd832023-01-11 14:50:10 +010027#define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret) do { } while (0)
28#define MBEDTLS_INTERNAL_VALIDATE(cond) do { } while (0)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050029
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030/* Internal helper macros for deprecating API constants. */
31#if !defined(MBEDTLS_DEPRECATED_REMOVED)
32#if defined(MBEDTLS_DEPRECATED_WARNING)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Gilles Peskine449bd832023-01-11 14:50:10 +010034MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t;
35#define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) \
36 ((mbedtls_deprecated_string_constant_t) (VAL))
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050037MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
Gilles Peskine449bd832023-01-11 14:50:10 +010038#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) \
39 ((mbedtls_deprecated_numeric_constant_t) (VAL))
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050040#else /* MBEDTLS_DEPRECATED_WARNING */
Brett Warren9e985732021-10-19 22:16:51 +010041#define MBEDTLS_DEPRECATED
Gilles Peskine449bd832023-01-11 14:50:10 +010042#define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL
43#define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050044#endif /* MBEDTLS_DEPRECATED_WARNING */
45#endif /* MBEDTLS_DEPRECATED_REMOVED */
46
Gilles Peskine463adf42021-09-23 17:28:59 +020047/* Implementation of the check-return facility.
48 * See the user documentation in mbedtls_config.h.
Mateusz Starzyke35f8f62021-08-04 15:38:09 +020049 *
Gilles Peskine463adf42021-09-23 17:28:59 +020050 * Do not use this macro directly to annotate function: instead,
51 * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL
52 * depending on how important it is to check the return value.
Mateusz Starzyke35f8f62021-08-04 15:38:09 +020053 */
54#if !defined(MBEDTLS_CHECK_RETURN)
55#if defined(__GNUC__)
Gilles Peskinea33e6932021-09-23 17:46:12 +020056#define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__))
Mateusz Starzyke35f8f62021-08-04 15:38:09 +020057#elif defined(_MSC_VER) && _MSC_VER >= 1700
58#include <sal.h>
59#define MBEDTLS_CHECK_RETURN _Check_return_
60#else
61#define MBEDTLS_CHECK_RETURN
62#endif
63#endif
64
Gilles Peskine463adf42021-09-23 17:28:59 +020065/** Critical-failure function
66 *
67 * This macro appearing at the beginning of the declaration of a function
68 * indicates that its return value should be checked in all applications.
69 * Omitting the check is very likely to indicate a bug in the application
70 * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN
71 * is implemented for the compiler in use.
72 *
73 * \note The use of this macro is a work in progress.
74 * This macro may be added to more functions in the future.
75 * Such an extension is not considered an API break, provided that
76 * there are near-unavoidable circumstances under which the function
77 * can fail. For example, signature/MAC/AEAD verification functions,
78 * and functions that require a random generator, are considered
79 * return-check-critical.
80 */
81#define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN
82
83/** Ordinary-failure function
84 *
85 * This macro appearing at the beginning of the declaration of a function
86 * indicates that its return value should be generally be checked in portable
87 * applications. Omitting the check will result in a compile-time warning if
Gilles Peskine409fbbe2021-09-27 16:17:51 +020088 * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use and
89 * #MBEDTLS_CHECK_RETURN_WARNING is enabled in the compile-time configuration.
Gilles Peskine463adf42021-09-23 17:28:59 +020090 *
Gilles Peskinefcc93d72021-09-30 18:56:17 +020091 * You can use #MBEDTLS_IGNORE_RETURN to explicitly ignore the return value
92 * of a function that is annotated with #MBEDTLS_CHECK_RETURN.
93 *
Gilles Peskine463adf42021-09-23 17:28:59 +020094 * \note The use of this macro is a work in progress.
95 * This macro will be added to more functions in the future.
96 * Eventually this should appear before most functions returning
97 * an error code (as \c int in the \c mbedtls_xxx API or
98 * as ::psa_status_t in the \c psa_xxx API).
99 */
Gilles Peskine9a7d4c22021-09-23 18:07:36 +0200100#if defined(MBEDTLS_CHECK_RETURN_WARNING)
Gilles Peskine463adf42021-09-23 17:28:59 +0200101#define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN
Gilles Peskine9a7d4c22021-09-23 18:07:36 +0200102#else
103#define MBEDTLS_CHECK_RETURN_TYPICAL
104#endif
Gilles Peskine463adf42021-09-23 17:28:59 +0200105
106/** Benign-failure function
107 *
108 * This macro appearing at the beginning of the declaration of a function
109 * indicates that it is rarely useful to check its return value.
110 *
111 * This macro has an empty expansion. It exists for documentation purposes:
112 * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function
Tom Cosgrove1e211442022-05-26 11:51:00 +0100113 * has been analyzed for return-check usefulness, whereas the lack of
Gilles Peskine463adf42021-09-23 17:28:59 +0200114 * an annotation indicates that the function has not been analyzed and its
115 * return-check usefulness is unknown.
116 */
117#define MBEDTLS_CHECK_RETURN_OPTIONAL
118
Mateusz Starzyk5c4ca322021-08-05 13:56:48 +0200119/** \def MBEDTLS_IGNORE_RETURN
120 *
Gilles Peskinefcc93d72021-09-30 18:56:17 +0200121 * Call this macro with one argument, a function call, to suppress a warning
122 * from #MBEDTLS_CHECK_RETURN due to that function call.
123 */
124#if !defined(MBEDTLS_IGNORE_RETURN)
Gilles Peskine252b7582021-09-30 18:54:51 +0200125/* GCC doesn't silence the warning with just (void)(result).
Gilles Peskine2aefc9e2021-09-30 20:34:29 +0200126 * (void)!(result) is known to work up at least up to GCC 10, as well
Gilles Peskine252b7582021-09-30 18:54:51 +0200127 * as with Clang and MSVC.
128 *
129 * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html
130 * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result
131 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34
Mateusz Starzyk5c4ca322021-08-05 13:56:48 +0200132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133#define MBEDTLS_IGNORE_RETURN(result) ((void) !(result))
Gilles Peskinefcc93d72021-09-30 18:56:17 +0200134#endif
Mateusz Starzyk5c4ca322021-08-05 13:56:48 +0200135
Tom Cosgroved9572c02023-09-01 14:34:37 +0100136/* If the following macro is defined, the library is being built by the test
137 * framework, and the framework is going to provide a replacement
138 * mbedtls_platform_zeroize() using a preprocessor macro, so the function
139 * declaration should be omitted. */
140#if !defined(MBEDTLS_TEST_DEFINES_ZEROIZE) //no-check-names
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500141/**
142 * \brief Securely zeroize a buffer
143 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500144 * The function is meant to wipe the data contained in a buffer so
145 * that it can no longer be recovered even if the program memory
146 * is later compromised. Call this function on sensitive data
147 * stored on the stack before returning from a function, and on
148 * sensitive data stored on the heap before freeing the heap
149 * object.
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500150 *
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500151 * It is extremely difficult to guarantee that calls to
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500152 * mbedtls_platform_zeroize() are not removed by aggressive
153 * compiler optimizations in a portable way. For this reason, Mbed
154 * TLS provides the configuration option
155 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
156 * mbedtls_platform_zeroize() to use a suitable implementation for
157 * their platform and needs
Andres Amaya Garcia56e06db2018-04-24 08:37:52 -0500158 *
159 * \param buf Buffer to be zeroized
160 * \param len Length of the buffer in bytes
161 *
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500162 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163void mbedtls_platform_zeroize(void *buf, size_t len);
Tom Cosgrove42b02a92023-09-01 09:53:42 +0100164#endif
165
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100166#if defined(MBEDTLS_HAVE_TIME_DATE)
167/**
Hanno Beckerc52ef402018-09-05 16:28:59 +0100168 * \brief Platform-specific implementation of gmtime_r()
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100169 *
Hanno Beckerc52ef402018-09-05 16:28:59 +0100170 * The function is a thread-safe abstraction that behaves
Hanno Becker03b2bd42018-09-06 09:08:55 +0100171 * similarly to the gmtime_r() function from Unix/POSIX.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100172 *
Hanno Becker6a739782018-09-05 15:06:19 +0100173 * Mbed TLS will try to identify the underlying platform and
Hanno Beckerc52ef402018-09-05 16:28:59 +0100174 * make use of an appropriate underlying implementation (e.g.
Hanno Becker6a739782018-09-05 15:06:19 +0100175 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is
176 * not possible, then gmtime() will be used. In this case, calls
177 * from the library to gmtime() will be guarded by the mutex
178 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
179 * enabled. It is recommended that calls from outside the library
180 * are also guarded by this mutex.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100181 *
Hanno Becker6a739782018-09-05 15:06:19 +0100182 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
183 * unconditionally use the alternative implementation for
184 * mbedtls_platform_gmtime_r() supplied by the user at compile time.
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100185 *
Hanno Becker272675f2018-09-05 14:03:02 +0100186 * \param tt Pointer to an object containing time (in seconds) since the
Hanno Becker48a816f2018-09-05 15:22:22 +0100187 * epoch to be converted
Hanno Becker272675f2018-09-05 14:03:02 +0100188 * \param tm_buf Pointer to an object where the results will be stored
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100189 *
190 * \return Pointer to an object of type struct tm on success, otherwise
191 * NULL
192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt,
194 struct tm *tm_buf);
Andres Amaya Garcia1abb3682018-08-16 21:42:09 +0100195#endif /* MBEDTLS_HAVE_TIME_DATE */
196
Andres Amaya Garcia904e1ef2018-04-17 09:16:30 -0500197#ifdef __cplusplus
198}
199#endif
200
201#endif /* MBEDTLS_PLATFORM_UTIL_H */