takashi kadono / Mbed OS Nucleo446_SSD1331

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers platform_util.c Source File

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/threading.h"
00039 
00040 #include <stddef.h>
00041 #include <string.h>
00042 
00043 #if !defined(MBEDTLS_PLATFORM_ZEROIZE_ALT)
00044 /*
00045  * This implementation should never be optimized out by the compiler
00046  *
00047  * This implementation for mbedtls_platform_zeroize() was inspired from Colin
00048  * Percival's blog article at:
00049  *
00050  * http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
00051  *
00052  * It uses a volatile function pointer to the standard memset(). Because the
00053  * pointer is volatile the compiler expects it to change at
00054  * any time and will not optimize out the call that could potentially perform
00055  * other operations on the input buffer instead of just setting it to 0.
00056  * Nevertheless, as pointed out by davidtgoldblatt on Hacker News
00057  * (refer to http://www.daemonology.net/blog/2014-09-05-erratum.html for
00058  * details), optimizations of the following form are still possible:
00059  *
00060  * if( memset_func != memset )
00061  *     memset_func( buf, 0, len );
00062  *
00063  * Note that it is extremely difficult to guarantee that
00064  * mbedtls_platform_zeroize() will not be optimized out by aggressive compilers
00065  * in a portable way. For this reason, Mbed TLS also provides the configuration
00066  * option MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
00067  * mbedtls_platform_zeroize() to use a suitable implementation for their
00068  * platform and needs.
00069  */
00070 static void * (* const volatile memset_func)( void *, int, size_t ) = memset;
00071 
00072 void mbedtls_platform_zeroize( void *buf, size_t len )
00073 {
00074     memset_func( buf, 0, len );
00075 }
00076 #endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
00077 
00078 #if defined(MBEDTLS_HAVE_TIME_DATE) && !defined(MBEDTLS_PLATFORM_GMTIME_R_ALT)
00079 #include <time.h>
00080 #if !defined(_WIN32) && (defined(unix) || \
00081     defined(__unix) || defined(__unix__) || (defined(__APPLE__) && \
00082     defined(__MACH__)))
00083 #include <unistd.h>
00084 #endif /* !_WIN32 && (unix || __unix || __unix__ ||
00085         * (__APPLE__ && __MACH__)) */
00086 
00087 #if !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) ||     \
00088        ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) &&                     \
00089          _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) )
00090 /*
00091  * This is a convenience shorthand macro to avoid checking the long
00092  * preprocessor conditions above. Ideally, we could expose this macro in
00093  * platform_util.h and simply use it in platform_util.c, threading.c and
00094  * threading.h. However, this macro is not part of the Mbed TLS public API, so
00095  * we keep it private by only defining it in this file
00096  */
00097 #if ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) )
00098 #define PLATFORM_UTIL_USE_GMTIME
00099 #endif /* ! ( defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) ) */
00100 
00101 #endif /* !( ( defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L ) ||     \
00102              ( defined(_POSIX_THREAD_SAFE_FUNCTIONS ) &&                     \
00103                 _POSIX_THREAD_SAFE_FUNCTIONS >= 20112L ) ) */
00104 
00105 struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
00106                                       struct tm *tm_buf )
00107 {
00108 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
00109     return( ( gmtime_s( tm_buf, tt ) == 0 ) ? tm_buf : NULL );
00110 #elif !defined(PLATFORM_UTIL_USE_GMTIME)
00111     return( gmtime_r( tt, tm_buf ) );
00112 #else
00113     struct tm *lt;
00114 
00115 #if defined(MBEDTLS_THREADING_C)
00116     if( mbedtls_mutex_lock( &mbedtls_threading_gmtime_mutex ) != 0 )
00117         return( NULL );
00118 #endif /* MBEDTLS_THREADING_C */
00119 
00120     lt = gmtime( tt );
00121 
00122     if( lt != NULL )
00123     {
00124         memcpy( tm_buf, lt, sizeof( struct tm ) );
00125     }
00126 
00127 #if defined(MBEDTLS_THREADING_C)
00128     if( mbedtls_mutex_unlock( &mbedtls_threading_gmtime_mutex ) != 0 )
00129         return( NULL );
00130 #endif /* MBEDTLS_THREADING_C */
00131 
00132     return( ( lt == NULL ) ? NULL : tm_buf );
00133 #endif /* _WIN32 && !EFIX64 && !EFI32 */
00134 }
00135 #endif /* MBEDTLS_HAVE_TIME_DATE && MBEDTLS_PLATFORM_GMTIME_R_ALT */