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 /* 8 * Copyright (C) 2018, Arm Limited, All Rights Reserved 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. 22 * 23 * This file is part of Mbed TLS (https://tls.mbed.org) 24 */ 25 #ifndef MBEDTLS_PLATFORM_UTIL_H 26 #define MBEDTLS_PLATFORM_UTIL_H 27 28 #if !defined(MBEDTLS_CONFIG_FILE) 29 #include "config.h" 30 #else 31 #include MBEDTLS_CONFIG_FILE 32 #endif 33 34 #include <stddef.h> 35 #if defined(MBEDTLS_HAVE_TIME_DATE) 36 #include "platform_time.h" 37 #include <time.h> 38 #endif /* MBEDTLS_HAVE_TIME_DATE */ 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 #if defined(MBEDTLS_CHECK_PARAMS) 45 46 #if defined(MBEDTLS_CHECK_PARAMS_ASSERT) 47 /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert 48 * (which is what our config.h suggests). */ 49 #include <assert.h> 50 #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */ 51 52 #if defined(MBEDTLS_PARAM_FAILED) 53 /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. 54 * 55 * This flag can be used to check whether it is safe to assume that 56 * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). 57 */ 58 #define MBEDTLS_PARAM_FAILED_ALT 59 60 #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT) 61 #define MBEDTLS_PARAM_FAILED( cond ) assert( cond ) 62 #define MBEDTLS_PARAM_FAILED_ALT 63 64 #else /* MBEDTLS_PARAM_FAILED */ 65 #define MBEDTLS_PARAM_FAILED( cond ) \ 66 mbedtls_param_failed( #cond, __FILE__, __LINE__ ) 67 68 /** 69 * \brief User supplied callback function for parameter validation failure. 70 * See #MBEDTLS_CHECK_PARAMS for context. 71 * 72 * This function will be called unless an alternative treatement 73 * is defined through the #MBEDTLS_PARAM_FAILED macro. 74 * 75 * This function can return, and the operation will be aborted, or 76 * alternatively, through use of setjmp()/longjmp() can resume 77 * execution in the application code. 78 * 79 * \param failure_condition The assertion that didn't hold. 80 * \param file The file where the assertion failed. 81 * \param line The line in the file where the assertion failed. 82 */ 83 void mbedtls_param_failed( const char *failure_condition, 84 const char *file, 85 int line ); 86 #endif /* MBEDTLS_PARAM_FAILED */ 87 88 /* Internal macro meant to be called only from within the library. */ 89 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ 90 do { \ 91 if( !(cond) ) \ 92 { \ 93 MBEDTLS_PARAM_FAILED( cond ); \ 94 return( ret ); \ 95 } \ 96 } while( 0 ) 97 98 /* Internal macro meant to be called only from within the library. */ 99 #define MBEDTLS_INTERNAL_VALIDATE( cond ) \ 100 do { \ 101 if( !(cond) ) \ 102 { \ 103 MBEDTLS_PARAM_FAILED( cond ); \ 104 return; \ 105 } \ 106 } while( 0 ) 107 108 #else /* MBEDTLS_CHECK_PARAMS */ 109 110 /* Internal macros meant to be called only from within the library. */ 111 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) 112 #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) 113 114 #endif /* MBEDTLS_CHECK_PARAMS */ 115 116 /* Internal helper macros for deprecating API constants. */ 117 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 118 #if defined(MBEDTLS_DEPRECATED_WARNING) 119 /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here 120 * to avoid conflict with other headers which define and use 121 * it, too. We might want to move all these definitions here at 122 * some point for uniformity. */ 123 #define MBEDTLS_DEPRECATED __attribute__((deprecated)) 124 MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; 125 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ 126 ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) 127 MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; 128 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ 129 ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) 130 #undef MBEDTLS_DEPRECATED 131 #else /* MBEDTLS_DEPRECATED_WARNING */ 132 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL 133 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL 134 #endif /* MBEDTLS_DEPRECATED_WARNING */ 135 #endif /* MBEDTLS_DEPRECATED_REMOVED */ 136 137 /** 138 * \brief Securely zeroize a buffer 139 * 140 * The function is meant to wipe the data contained in a buffer so 141 * that it can no longer be recovered even if the program memory 142 * is later compromised. Call this function on sensitive data 143 * stored on the stack before returning from a function, and on 144 * sensitive data stored on the heap before freeing the heap 145 * object. 146 * 147 * It is extremely difficult to guarantee that calls to 148 * mbedtls_platform_zeroize() are not removed by aggressive 149 * compiler optimizations in a portable way. For this reason, Mbed 150 * TLS provides the configuration option 151 * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure 152 * mbedtls_platform_zeroize() to use a suitable implementation for 153 * their platform and needs 154 * 155 * \param buf Buffer to be zeroized 156 * \param len Length of the buffer in bytes 157 * 158 */ 159 void mbedtls_platform_zeroize( void *buf, size_t len ); 160 161 #if defined(MBEDTLS_HAVE_TIME_DATE) 162 /** 163 * \brief Platform-specific implementation of gmtime_r() 164 * 165 * The function is a thread-safe abstraction that behaves 166 * similarly to the gmtime_r() function from Unix/POSIX. 167 * 168 * Mbed TLS will try to identify the underlying platform and 169 * make use of an appropriate underlying implementation (e.g. 170 * gmtime_r() for POSIX and gmtime_s() for Windows). If this is 171 * not possible, then gmtime() will be used. In this case, calls 172 * from the library to gmtime() will be guarded by the mutex 173 * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is 174 * enabled. It is recommended that calls from outside the library 175 * are also guarded by this mutex. 176 * 177 * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will 178 * unconditionally use the alternative implementation for 179 * mbedtls_platform_gmtime_r() supplied by the user at compile time. 180 * 181 * \param tt Pointer to an object containing time (in seconds) since the 182 * epoch to be converted 183 * \param tm_buf Pointer to an object where the results will be stored 184 * 185 * \return Pointer to an object of type struct tm on success, otherwise 186 * NULL 187 */ 188 struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, 189 struct tm *tm_buf ); 190 #endif /* MBEDTLS_HAVE_TIME_DATE */ 191 192 #ifdef __cplusplus 193 } 194 #endif 195 196 #endif /* MBEDTLS_PLATFORM_UTIL_H */ 197