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. */ |
| 39 | #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) |
| 40 | #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) |
| 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) |
| 45 | /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here |
| 46 | * to avoid conflict with other headers which define and use |
| 47 | * it, too. We might want to move all these definitions here at |
| 48 | * some point for uniformity. */ |
| 49 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 50 | MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; |
| 51 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ |
| 52 | ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) |
| 53 | MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; |
| 54 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ |
| 55 | ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) |
| 56 | #undef MBEDTLS_DEPRECATED |
| 57 | #else /* MBEDTLS_DEPRECATED_WARNING */ |
| 58 | #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL |
| 59 | #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL |
| 60 | #endif /* MBEDTLS_DEPRECATED_WARNING */ |
| 61 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 62 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 63 | /** |
| 64 | * \brief Securely zeroize a buffer |
| 65 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 66 | * The function is meant to wipe the data contained in a buffer so |
| 67 | * that it can no longer be recovered even if the program memory |
| 68 | * is later compromised. Call this function on sensitive data |
| 69 | * stored on the stack before returning from a function, and on |
| 70 | * sensitive data stored on the heap before freeing the heap |
| 71 | * object. |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 72 | * |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 73 | * It is extremely difficult to guarantee that calls to |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 74 | * mbedtls_platform_zeroize() are not removed by aggressive |
| 75 | * compiler optimizations in a portable way. For this reason, Mbed |
| 76 | * TLS provides the configuration option |
| 77 | * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure |
| 78 | * mbedtls_platform_zeroize() to use a suitable implementation for |
| 79 | * their platform and needs |
Andres Amaya Garcia | 56e06db | 2018-04-24 08:37:52 -0500 | [diff] [blame] | 80 | * |
| 81 | * \param buf Buffer to be zeroized |
| 82 | * \param len Length of the buffer in bytes |
| 83 | * |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 84 | */ |
| 85 | void mbedtls_platform_zeroize( void *buf, size_t len ); |
| 86 | |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 87 | #if defined(MBEDTLS_HAVE_TIME_DATE) |
| 88 | /** |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 89 | * \brief Platform-specific implementation of gmtime_r() |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 90 | * |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 91 | * The function is a thread-safe abstraction that behaves |
Hanno Becker | 03b2bd4 | 2018-09-06 09:08:55 +0100 | [diff] [blame] | 92 | * similarly to the gmtime_r() function from Unix/POSIX. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 93 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 94 | * Mbed TLS will try to identify the underlying platform and |
Hanno Becker | c52ef40 | 2018-09-05 16:28:59 +0100 | [diff] [blame] | 95 | * make use of an appropriate underlying implementation (e.g. |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 96 | * gmtime_r() for POSIX and gmtime_s() for Windows). If this is |
| 97 | * not possible, then gmtime() will be used. In this case, calls |
| 98 | * from the library to gmtime() will be guarded by the mutex |
| 99 | * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is |
| 100 | * enabled. It is recommended that calls from outside the library |
| 101 | * are also guarded by this mutex. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 102 | * |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 103 | * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will |
| 104 | * unconditionally use the alternative implementation for |
| 105 | * mbedtls_platform_gmtime_r() supplied by the user at compile time. |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 106 | * |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 107 | * \param tt Pointer to an object containing time (in seconds) since the |
Hanno Becker | 48a816f | 2018-09-05 15:22:22 +0100 | [diff] [blame] | 108 | * epoch to be converted |
Hanno Becker | 272675f | 2018-09-05 14:03:02 +0100 | [diff] [blame] | 109 | * \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] | 110 | * |
| 111 | * \return Pointer to an object of type struct tm on success, otherwise |
| 112 | * NULL |
| 113 | */ |
Hanno Becker | 6a73978 | 2018-09-05 15:06:19 +0100 | [diff] [blame] | 114 | struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, |
| 115 | struct tm *tm_buf ); |
Andres Amaya Garcia | 1abb368 | 2018-08-16 21:42:09 +0100 | [diff] [blame] | 116 | #endif /* MBEDTLS_HAVE_TIME_DATE */ |
| 117 | |
Andres Amaya Garcia | 904e1ef | 2018-04-17 09:16:30 -0500 | [diff] [blame] | 118 | #ifdef __cplusplus |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | #endif /* MBEDTLS_PLATFORM_UTIL_H */ |