| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 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 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 8 | *  Copyright The Mbed TLS Contributors | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 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. | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 22 | */ | 
|  | 23 | #ifndef MBEDTLS_PLATFORM_UTIL_H | 
|  | 24 | #define MBEDTLS_PLATFORM_UTIL_H | 
|  | 25 |  | 
| Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 27 |  | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 28 | #include <stddef.h> | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 29 | #if defined(MBEDTLS_HAVE_TIME_DATE) | 
| Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform_time.h" | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 31 | #include <time.h> | 
|  | 32 | #endif /* MBEDTLS_HAVE_TIME_DATE */ | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 33 |  | 
|  | 34 | #ifdef __cplusplus | 
|  | 35 | extern "C" { | 
|  | 36 | #endif | 
|  | 37 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 38 | /* Internal macros meant to be called only from within the library. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | #define MBEDTLS_INTERNAL_VALIDATE_RET(cond, ret)  do { } while (0) | 
|  | 40 | #define MBEDTLS_INTERNAL_VALIDATE(cond)           do { } while (0) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 41 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 42 | /* Internal helper macros for deprecating API constants. */ | 
|  | 43 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) | 
|  | 44 | #if defined(MBEDTLS_DEPRECATED_WARNING) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 45 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | MBEDTLS_DEPRECATED typedef char const *mbedtls_deprecated_string_constant_t; | 
|  | 47 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL)       \ | 
|  | 48 | ((mbedtls_deprecated_string_constant_t) (VAL)) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 49 | MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL)       \ | 
|  | 51 | ((mbedtls_deprecated_numeric_constant_t) (VAL)) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 52 | #else /* MBEDTLS_DEPRECATED_WARNING */ | 
| Brett Warren | 9e98573 | 2021-10-19 22:16:51 +0100 | [diff] [blame] | 53 | #define MBEDTLS_DEPRECATED | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT(VAL) VAL | 
|  | 55 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT(VAL) VAL | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 56 | #endif /* MBEDTLS_DEPRECATED_WARNING */ | 
|  | 57 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ | 
|  | 58 |  | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 59 | /* Implementation of the check-return facility. | 
|  | 60 | * See the user documentation in mbedtls_config.h. | 
| Mateusz Starzyk | e35f8f6 | 2021-08-04 15:38:09 +0200 | [diff] [blame] | 61 | * | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 62 | * Do not use this macro directly to annotate function: instead, | 
|  | 63 | * use one of MBEDTLS_CHECK_RETURN_CRITICAL or MBEDTLS_CHECK_RETURN_TYPICAL | 
|  | 64 | * depending on how important it is to check the return value. | 
| Mateusz Starzyk | e35f8f6 | 2021-08-04 15:38:09 +0200 | [diff] [blame] | 65 | */ | 
|  | 66 | #if !defined(MBEDTLS_CHECK_RETURN) | 
|  | 67 | #if defined(__GNUC__) | 
| Gilles Peskine | a33e693 | 2021-09-23 17:46:12 +0200 | [diff] [blame] | 68 | #define MBEDTLS_CHECK_RETURN __attribute__((__warn_unused_result__)) | 
| Mateusz Starzyk | e35f8f6 | 2021-08-04 15:38:09 +0200 | [diff] [blame] | 69 | #elif defined(_MSC_VER) && _MSC_VER >= 1700 | 
|  | 70 | #include <sal.h> | 
|  | 71 | #define MBEDTLS_CHECK_RETURN _Check_return_ | 
|  | 72 | #else | 
|  | 73 | #define MBEDTLS_CHECK_RETURN | 
|  | 74 | #endif | 
|  | 75 | #endif | 
|  | 76 |  | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 77 | /** Critical-failure function | 
|  | 78 | * | 
|  | 79 | * This macro appearing at the beginning of the declaration of a function | 
|  | 80 | * indicates that its return value should be checked in all applications. | 
|  | 81 | * Omitting the check is very likely to indicate a bug in the application | 
|  | 82 | * and will result in a compile-time warning if #MBEDTLS_CHECK_RETURN | 
|  | 83 | * is implemented for the compiler in use. | 
|  | 84 | * | 
|  | 85 | * \note  The use of this macro is a work in progress. | 
|  | 86 | *        This macro may be added to more functions in the future. | 
|  | 87 | *        Such an extension is not considered an API break, provided that | 
|  | 88 | *        there are near-unavoidable circumstances under which the function | 
|  | 89 | *        can fail. For example, signature/MAC/AEAD verification functions, | 
|  | 90 | *        and functions that require a random generator, are considered | 
|  | 91 | *        return-check-critical. | 
|  | 92 | */ | 
|  | 93 | #define MBEDTLS_CHECK_RETURN_CRITICAL MBEDTLS_CHECK_RETURN | 
|  | 94 |  | 
|  | 95 | /** Ordinary-failure function | 
|  | 96 | * | 
|  | 97 | * This macro appearing at the beginning of the declaration of a function | 
|  | 98 | * indicates that its return value should be generally be checked in portable | 
|  | 99 | * applications. Omitting the check will result in a compile-time warning if | 
| Gilles Peskine | 409fbbe | 2021-09-27 16:17:51 +0200 | [diff] [blame] | 100 | * #MBEDTLS_CHECK_RETURN is implemented for the compiler in use and | 
|  | 101 | * #MBEDTLS_CHECK_RETURN_WARNING is enabled in the compile-time configuration. | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 102 | * | 
| Gilles Peskine | fcc93d7 | 2021-09-30 18:56:17 +0200 | [diff] [blame] | 103 | * You can use #MBEDTLS_IGNORE_RETURN to explicitly ignore the return value | 
|  | 104 | * of a function that is annotated with #MBEDTLS_CHECK_RETURN. | 
|  | 105 | * | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 106 | * \note  The use of this macro is a work in progress. | 
|  | 107 | *        This macro will be added to more functions in the future. | 
|  | 108 | *        Eventually this should appear before most functions returning | 
|  | 109 | *        an error code (as \c int in the \c mbedtls_xxx API or | 
|  | 110 | *        as ::psa_status_t in the \c psa_xxx API). | 
|  | 111 | */ | 
| Gilles Peskine | 9a7d4c2 | 2021-09-23 18:07:36 +0200 | [diff] [blame] | 112 | #if defined(MBEDTLS_CHECK_RETURN_WARNING) | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 113 | #define MBEDTLS_CHECK_RETURN_TYPICAL MBEDTLS_CHECK_RETURN | 
| Gilles Peskine | 9a7d4c2 | 2021-09-23 18:07:36 +0200 | [diff] [blame] | 114 | #else | 
|  | 115 | #define MBEDTLS_CHECK_RETURN_TYPICAL | 
|  | 116 | #endif | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 117 |  | 
|  | 118 | /** Benign-failure function | 
|  | 119 | * | 
|  | 120 | * This macro appearing at the beginning of the declaration of a function | 
|  | 121 | * indicates that it is rarely useful to check its return value. | 
|  | 122 | * | 
|  | 123 | * This macro has an empty expansion. It exists for documentation purposes: | 
|  | 124 | * a #MBEDTLS_CHECK_RETURN_OPTIONAL annotation indicates that the function | 
| Tom Cosgrove | 1e21144 | 2022-05-26 11:51:00 +0100 | [diff] [blame] | 125 | * has been analyzed for return-check usefulness, whereas the lack of | 
| Gilles Peskine | 463adf4 | 2021-09-23 17:28:59 +0200 | [diff] [blame] | 126 | * an annotation indicates that the function has not been analyzed and its | 
|  | 127 | * return-check usefulness is unknown. | 
|  | 128 | */ | 
|  | 129 | #define MBEDTLS_CHECK_RETURN_OPTIONAL | 
|  | 130 |  | 
| Mateusz Starzyk | 5c4ca32 | 2021-08-05 13:56:48 +0200 | [diff] [blame] | 131 | /** \def MBEDTLS_IGNORE_RETURN | 
|  | 132 | * | 
| Gilles Peskine | fcc93d7 | 2021-09-30 18:56:17 +0200 | [diff] [blame] | 133 | * Call this macro with one argument, a function call, to suppress a warning | 
|  | 134 | * from #MBEDTLS_CHECK_RETURN due to that function call. | 
|  | 135 | */ | 
|  | 136 | #if !defined(MBEDTLS_IGNORE_RETURN) | 
| Gilles Peskine | 252b758 | 2021-09-30 18:54:51 +0200 | [diff] [blame] | 137 | /* GCC doesn't silence the warning with just (void)(result). | 
| Gilles Peskine | 2aefc9e | 2021-09-30 20:34:29 +0200 | [diff] [blame] | 138 | * (void)!(result) is known to work up at least up to GCC 10, as well | 
| Gilles Peskine | 252b758 | 2021-09-30 18:54:51 +0200 | [diff] [blame] | 139 | * as with Clang and MSVC. | 
|  | 140 | * | 
|  | 141 | * https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Non_002dbugs.html | 
|  | 142 | * https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result | 
|  | 143 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425#c34 | 
| Mateusz Starzyk | 5c4ca32 | 2021-08-05 13:56:48 +0200 | [diff] [blame] | 144 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | #define MBEDTLS_IGNORE_RETURN(result) ((void) !(result)) | 
| Gilles Peskine | fcc93d7 | 2021-09-30 18:56:17 +0200 | [diff] [blame] | 146 | #endif | 
| Mateusz Starzyk | 5c4ca32 | 2021-08-05 13:56:48 +0200 | [diff] [blame] | 147 |  | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 148 | /** | 
|  | 149 | * \brief       Securely zeroize a buffer | 
|  | 150 | * | 
| Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 151 | *              The function is meant to wipe the data contained in a buffer so | 
|  | 152 | *              that it can no longer be recovered even if the program memory | 
|  | 153 | *              is later compromised. Call this function on sensitive data | 
|  | 154 | *              stored on the stack before returning from a function, and on | 
|  | 155 | *              sensitive data stored on the heap before freeing the heap | 
|  | 156 | *              object. | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 157 | * | 
| Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 158 | *              It is extremely difficult to guarantee that calls to | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 159 | *              mbedtls_platform_zeroize() are not removed by aggressive | 
|  | 160 | *              compiler optimizations in a portable way. For this reason, Mbed | 
|  | 161 | *              TLS provides the configuration option | 
|  | 162 | *              MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure | 
|  | 163 | *              mbedtls_platform_zeroize() to use a suitable implementation for | 
|  | 164 | *              their platform and needs | 
| Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 165 | * | 
|  | 166 | * \param buf   Buffer to be zeroized | 
|  | 167 | * \param len   Length of the buffer in bytes | 
|  | 168 | * | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 169 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | void mbedtls_platform_zeroize(void *buf, size_t len); | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 171 |  | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 172 | #if defined(MBEDTLS_HAVE_TIME_DATE) | 
|  | 173 | /** | 
| Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 174 | * \brief      Platform-specific implementation of gmtime_r() | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 175 | * | 
| Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 176 | *             The function is a thread-safe abstraction that behaves | 
| Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 177 | *             similarly to the gmtime_r() function from Unix/POSIX. | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 178 | * | 
| Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 179 | *             Mbed TLS will try to identify the underlying platform and | 
| Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 180 | *             make use of an appropriate underlying implementation (e.g. | 
| Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 181 | *             gmtime_r() for POSIX and gmtime_s() for Windows). If this is | 
|  | 182 | *             not possible, then gmtime() will be used. In this case, calls | 
|  | 183 | *             from the library to gmtime() will be guarded by the mutex | 
|  | 184 | *             mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is | 
|  | 185 | *             enabled. It is recommended that calls from outside the library | 
|  | 186 | *             are also guarded by this mutex. | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 187 | * | 
| Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 188 | *             If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will | 
|  | 189 | *             unconditionally use the alternative implementation for | 
|  | 190 | *             mbedtls_platform_gmtime_r() supplied by the user at compile time. | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 191 | * | 
| Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 192 | * \param tt     Pointer to an object containing time (in seconds) since the | 
| Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 193 | *               epoch to be converted | 
| Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 194 | * \param tm_buf Pointer to an object where the results will be stored | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 195 | * | 
|  | 196 | * \return      Pointer to an object of type struct tm on success, otherwise | 
|  | 197 | *              NULL | 
|  | 198 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | struct tm *mbedtls_platform_gmtime_r(const mbedtls_time_t *tt, | 
|  | 200 | struct tm *tm_buf); | 
| Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 201 | #endif /* MBEDTLS_HAVE_TIME_DATE */ | 
|  | 202 |  | 
| Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 203 | #ifdef __cplusplus | 
|  | 204 | } | 
|  | 205 | #endif | 
|  | 206 |  | 
|  | 207 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |