Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers platform_util.h Source File

platform_util.h

Go to the documentation of this file.
00001 /**
00002  * \file platform_util.h
00003  *
00004  * \brief Common and shared functions used by multiple modules in the Mbed TLS
00005  *        library.
00006  */
00007 /*
00008  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
00009  *  SPDX-License-Identifier: Apache-2.0
00010  *
00011  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00012  *  not use this file except in compliance with the License.
00013  *  You may obtain a copy of the License at
00014  *
00015  *  http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  *  Unless required by applicable law or agreed to in writing, software
00018  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00019  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  *  See the License for the specific language governing permissions and
00021  *  limitations under the License.
00022  *
00023  *  This file is part of Mbed TLS (https://tls.mbed.org)
00024  */
00025 #ifndef MBEDTLS_PLATFORM_UTIL_H
00026 #define MBEDTLS_PLATFORM_UTIL_H
00027 
00028 #if !defined(MBEDTLS_CONFIG_FILE)
00029 #include "mbedtls/config.h"
00030 #else
00031 #include MBEDTLS_CONFIG_FILE
00032 #endif
00033 
00034 #include <stddef.h>
00035 #if defined(MBEDTLS_HAVE_TIME_DATE)
00036 #include "mbedtls/platform_time.h"
00037 #include <time.h>
00038 #endif /* MBEDTLS_HAVE_TIME_DATE */
00039 
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043 
00044 #if defined(MBEDTLS_CHECK_PARAMS)
00045 
00046 #if defined(MBEDTLS_CHECK_PARAMS_ASSERT)
00047 /* Allow the user to define MBEDTLS_PARAM_FAILED to something like assert
00048  * (which is what our config.h suggests). */
00049 #include <assert.h>
00050 #endif /* MBEDTLS_CHECK_PARAMS_ASSERT */
00051 
00052 #if defined(MBEDTLS_PARAM_FAILED)
00053 /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h.
00054  *
00055  * This flag can be used to check whether it is safe to assume that
00056  * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed().
00057  */
00058 #define MBEDTLS_PARAM_FAILED_ALT
00059 
00060 #elif defined(MBEDTLS_CHECK_PARAMS_ASSERT)
00061 #define MBEDTLS_PARAM_FAILED( cond ) assert( cond )
00062 #define MBEDTLS_PARAM_FAILED_ALT
00063 
00064 #else /* MBEDTLS_PARAM_FAILED */
00065 #define MBEDTLS_PARAM_FAILED( cond ) \
00066     mbedtls_param_failed( #cond, __FILE__, __LINE__ )
00067 
00068 /**
00069  * \brief       User supplied callback function for parameter validation failure.
00070  *              See #MBEDTLS_CHECK_PARAMS for context.
00071  *
00072  *              This function will be called unless an alternative treatement
00073  *              is defined through the #MBEDTLS_PARAM_FAILED macro.
00074  *
00075  *              This function can return, and the operation will be aborted, or
00076  *              alternatively, through use of setjmp()/longjmp() can resume
00077  *              execution in the application code.
00078  *
00079  * \param failure_condition The assertion that didn't hold.
00080  * \param file  The file where the assertion failed.
00081  * \param line  The line in the file where the assertion failed.
00082  */
00083 void mbedtls_param_failed( const char *failure_condition,
00084                            const char *file,
00085                            int line );
00086 #endif /* MBEDTLS_PARAM_FAILED */
00087 
00088 /* Internal macro meant to be called only from within the library. */
00089 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret )  \
00090     do {                                            \
00091         if( !(cond) )                               \
00092         {                                           \
00093             MBEDTLS_PARAM_FAILED( cond );           \
00094             return( ret );                          \
00095         }                                           \
00096     } while( 0 )
00097 
00098 /* Internal macro meant to be called only from within the library. */
00099 #define MBEDTLS_INTERNAL_VALIDATE( cond )           \
00100     do {                                            \
00101         if( !(cond) )                               \
00102         {                                           \
00103             MBEDTLS_PARAM_FAILED( cond );           \
00104             return;                                 \
00105         }                                           \
00106     } while( 0 )
00107 
00108 #else /* MBEDTLS_CHECK_PARAMS */
00109 
00110 /* Internal macros meant to be called only from within the library. */
00111 #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret )  do { } while( 0 )
00112 #define MBEDTLS_INTERNAL_VALIDATE( cond )           do { } while( 0 )
00113 
00114 #endif /* MBEDTLS_CHECK_PARAMS */
00115 
00116 /* Internal helper macros for deprecating API constants. */
00117 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00118 #if defined(MBEDTLS_DEPRECATED_WARNING)
00119 /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here
00120  * to avoid conflict with other headers which define and use
00121  * it, too. We might want to move all these definitions here at
00122  * some point for uniformity. */
00123 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
00124 MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t;
00125 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL )       \
00126     ( (mbedtls_deprecated_string_constant_t) ( VAL ) )
00127 MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t;
00128 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL )       \
00129     ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) )
00130 #undef MBEDTLS_DEPRECATED
00131 #else /* MBEDTLS_DEPRECATED_WARNING */
00132 #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL
00133 #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL
00134 #endif /* MBEDTLS_DEPRECATED_WARNING */
00135 #endif /* MBEDTLS_DEPRECATED_REMOVED */
00136 
00137 /**
00138  * \brief       Securely zeroize a buffer
00139  *
00140  *              The function is meant to wipe the data contained in a buffer so
00141  *              that it can no longer be recovered even if the program memory
00142  *              is later compromised. Call this function on sensitive data
00143  *              stored on the stack before returning from a function, and on
00144  *              sensitive data stored on the heap before freeing the heap
00145  *              object.
00146  *
00147  *              It is extremely difficult to guarantee that calls to
00148  *              mbedtls_platform_zeroize() are not removed by aggressive
00149  *              compiler optimizations in a portable way. For this reason, Mbed
00150  *              TLS provides the configuration option
00151  *              MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure
00152  *              mbedtls_platform_zeroize() to use a suitable implementation for
00153  *              their platform and needs
00154  *
00155  * \param buf   Buffer to be zeroized
00156  * \param len   Length of the buffer in bytes
00157  *
00158  */
00159 void mbedtls_platform_zeroize( void *buf, size_t len );
00160 
00161 #if defined(MBEDTLS_HAVE_TIME_DATE)
00162 /**
00163  * \brief      Platform-specific implementation of gmtime_r()
00164  *
00165  *             The function is a thread-safe abstraction that behaves
00166  *             similarly to the gmtime_r() function from Unix/POSIX.
00167  *
00168  *             Mbed TLS will try to identify the underlying platform and
00169  *             make use of an appropriate underlying implementation (e.g.
00170  *             gmtime_r() for POSIX and gmtime_s() for Windows). If this is
00171  *             not possible, then gmtime() will be used. In this case, calls
00172  *             from the library to gmtime() will be guarded by the mutex
00173  *             mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is
00174  *             enabled. It is recommended that calls from outside the library
00175  *             are also guarded by this mutex.
00176  *
00177  *             If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will
00178  *             unconditionally use the alternative implementation for
00179  *             mbedtls_platform_gmtime_r() supplied by the user at compile time.
00180  *
00181  * \param tt     Pointer to an object containing time (in seconds) since the
00182  *               epoch to be converted
00183  * \param tm_buf Pointer to an object where the results will be stored
00184  *
00185  * \return      Pointer to an object of type struct tm on success, otherwise
00186  *              NULL
00187  */
00188 struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
00189                                       struct tm *tm_buf );
00190 #endif /* MBEDTLS_HAVE_TIME_DATE */
00191 
00192 #ifdef __cplusplus
00193 }
00194 #endif
00195 
00196 #endif /* MBEDTLS_PLATFORM_UTIL_H */