Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
platform_util.c
00001 /* 00002 * Common and shared functions used by multiple modules in the Mbed TLS 00003 * library. 00004 * 00005 * Copyright (C) 2018, Arm Limited, All Rights Reserved 00006 * SPDX-License-Identifier: Apache-2.0 00007 * 00008 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00009 * not use this file except in compliance with the License. 00010 * You may obtain a copy of the License at 00011 * 00012 * http://www.apache.org/licenses/LICENSE-2.0 00013 * 00014 * Unless required by applicable law or agreed to in writing, software 00015 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00016 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00017 * See the License for the specific language governing permissions and 00018 * limitations under the License. 00019 * 00020 * This file is part of Mbed TLS (https://tls.mbed.org) 00021 */ 00022 00023 /* 00024 * Ensure gmtime_r is available even with -std=c99; must be defined before 00025 * config.h, which pulls in glibc's features.h. Harmless on other platforms. 00026 */ 00027 #if !defined(_POSIX_C_SOURCE) 00028 #define _POSIX_C_SOURCE 200112L 00029 #endif 00030 00031 #if !defined(MBEDTLS_CONFIG_FILE) 00032 #include "mbedtls/config.h" 00033 #else 00034 #include MBEDTLS_CONFIG_FILE 00035 #endif 00036 00037 #include "mbedtls/platform_util.h" 00038 #include "mbedtls/platform.h" 00039 #include "mbedtls/threading.h" 00040 00041 #include <stddef.h> 00042 #include <string.h> 00043 00044 #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT) 00045 /* 00046 * This implementation should never be optimized out by the compiler 00047 * 00048 * This implementation for mbedtls_platform_zeroize() was inspired from Colin 00049 * Percival's blog article at: 00050 * 00051 * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html 00052 * 00053 * It uses a volatile function pointer to the standard memset(). Because the 00054 * pointer is volatile the compiler expects it to change at 00055 * any time and will not optimize out the call that could potentially perform 00056 * other operations on the input buffer instead of just setting it to 0. 00057 * Nevertheless, as pointed out by davidtgoldblatt on Hacker News 00058 * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for 00059 * details), optimizations of the following form are still possible: 00060 * 00061 * if( memset_func != memset ) 00062 * memset_func( buf, 0, len ); 00063 * 00064 * Note that it is extremely difficult to guarantee that 00065 * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers 00066 * in a portable way. For this reason, Mbed TLS also provides the configuration 00067 * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure 00068 * mbedtls_platform_zeroize() to use a suitable implementation for their 00069 * platform and needs. 00070 */ 00071 static void * (* const volatile memset_func)( void *, int, size_t ) = memset; 00072 00073 void mbedtls_platform_zeroize( void *buf, size_t len ) 00074 { 00075 MBEDTLS_INTERNAL_VALIDATE( len == 0 || buf != NULL ); 00076 00077 if( len > 0 ) 00078 memset_func( buf, 0, len ); 00079 } 00080 #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */ 00081 00082 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT) 00083 #include <time.h> 00084 #if !defined(_WIN32) && (defined(unix) || \ 00085 defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \ 00086 defined(__MACH__))) 00087 #include <unistd.h> 00088 #endif /* !_WIN32 && (unix || __unix || __unix__ || 00089 * (__APPLE__ && __MACH__)) */ 00090 00091 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ 00092 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ 00093 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) 00094 /* 00095 * This is a convenience shorthand macro to avoid checking the long 00096 * preprocessor conditions above. Ideally, we could expose this macro in 00097 * platform_util.h and simply use it in platform_util.c, threading.c and 00098 * threading.h. However, this macro is not part of the Mbed TLS public API, so 00099 * we keep it private by only defining it in this file 00100 */ 00101 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) 00102 #define PLATFORM_UTIL_USE_GMTIME 00103 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */ 00104 00105 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) || \ 00106 ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) && \ 00107 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */ 00108 00109 struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, 00110 struct tm *tm_buf ) 00111 { 00112 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) 00113 return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL ); 00114 #elif !defined(PLATFORM_UTIL_USE_GMTIME) 00115 return( gmtime_r( tt, tm_buf ) ); 00116 #else 00117 struct tm *lt; 00118 00119 #if defined(MBEDTLS_THREADING_C) 00120 if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 ) 00121 return( NULL ); 00122 #endif /* MBEDTLS_THREADING_C */ 00123 00124 lt = gmtime( tt ); 00125 00126 if( lt != NULL ) 00127 { 00128 memcpy( tm_buf, lt, sizeof( struct tm ) ); 00129 } 00130 00131 #if defined(MBEDTLS_THREADING_C) 00132 if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 ) 00133 return( NULL ); 00134 #endif /* MBEDTLS_THREADING_C */ 00135 00136 return( ( lt == NULL ) ? NULL : tm_buf ); 00137 #endif /* _WIN32 && !EFIX64 && !EFI32 */ 00138 } 00139 #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */
Generated on Tue Jul 12 2022 13:54:41 by
