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.c Source File

platform.c

00001 /*
00002  *  Platform abstraction layer
00003  *
00004  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 
00022 #if !defined(MBEDTLS_CONFIG_FILE)
00023 #include "mbedtls/config.h"
00024 #else
00025 #include MBEDTLS_CONFIG_FILE
00026 #endif
00027 
00028 #if defined(MBEDTLS_PLATFORM_C)
00029 
00030 #include "mbedtls/platform.h"
00031 #include "mbedtls/platform_util.h"
00032 
00033 /* The compile time configuration of memory allocation via the macros
00034  * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
00035  * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
00036  * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
00037 #if defined(MBEDTLS_PLATFORM_MEMORY) &&                 \
00038     !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&        \
00039        defined(MBEDTLS_PLATFORM_FREE_MACRO) )
00040 
00041 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
00042 static void *platform_calloc_uninit( size_t n, size_t size )
00043 {
00044     ((void) n);
00045     ((void) size);
00046     return( NULL );
00047 }
00048 
00049 #define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
00050 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
00051 
00052 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
00053 static void platform_free_uninit( void *ptr )
00054 {
00055     ((void) ptr);
00056 }
00057 
00058 #define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
00059 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
00060 
00061 static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
00062 static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
00063 
00064 void * mbedtls_calloc( size_t nmemb, size_t size )
00065 {
00066     return (*mbedtls_calloc_func)( nmemb, size );
00067 }
00068 
00069 void mbedtls_free( void * ptr )
00070 {
00071     (*mbedtls_free_func)( ptr );
00072 }
00073 
00074 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
00075                               void (*free_func)( void * ) )
00076 {
00077     mbedtls_calloc_func = calloc_func;
00078     mbedtls_free_func = free_func;
00079     return( 0 );
00080 }
00081 #endif /* MBEDTLS_PLATFORM_MEMORY &&
00082           !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
00083              defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
00084 
00085 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
00086 #include <stdarg.h>
00087 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
00088 {
00089     int ret;
00090     va_list argp;
00091 
00092     va_start( argp, fmt );
00093     ret = mbedtls_vsnprintf( s, n, fmt, argp );
00094     va_end( argp );
00095 
00096     return( ret );
00097 }
00098 #endif
00099 
00100 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
00101 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
00102 /*
00103  * Make dummy function to prevent NULL pointer dereferences
00104  */
00105 static int platform_snprintf_uninit( char * s, size_t n,
00106                                      const char * format, ... )
00107 {
00108     ((void) s);
00109     ((void) n);
00110     ((void) format);
00111     return( 0 );
00112 }
00113 
00114 #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
00115 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
00116 
00117 int (*mbedtls_snprintf)( char * s, size_t n,
00118                           const char * format,
00119                           ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
00120 
00121 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
00122                                                  const char * format,
00123                                                  ... ) )
00124 {
00125     mbedtls_snprintf = snprintf_func;
00126     return( 0 );
00127 }
00128 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
00129 
00130 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
00131 #include <stdarg.h>
00132 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
00133 {
00134     int ret;
00135 
00136     /* Avoid calling the invalid parameter handler by checking ourselves */
00137     if( s == NULL || n == 0 || fmt == NULL )
00138         return( -1 );
00139 
00140 #if defined(_TRUNCATE)
00141     ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg );
00142 #else
00143     ret = vsnprintf( s, n, fmt, arg );
00144     if( ret < 0 || (size_t) ret == n )
00145     {
00146         s[n-1] = '\0';
00147         ret = -1;
00148     }
00149 #endif
00150 
00151     return( ret );
00152 }
00153 #endif
00154 
00155 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
00156 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
00157 /*
00158  * Make dummy function to prevent NULL pointer dereferences
00159  */
00160 static int platform_vsnprintf_uninit( char * s, size_t n,
00161                                      const char * format, va_list arg )
00162 {
00163     ((void) s);
00164     ((void) n);
00165     ((void) format);
00166     ((void) arg);
00167     return( -1 );
00168 }
00169 
00170 #define MBEDTLS_PLATFORM_STD_VSNPRINTF    platform_vsnprintf_uninit
00171 #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
00172 
00173 int (*mbedtls_vsnprintf)( char * s, size_t n,
00174                           const char * format,
00175                           va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
00176 
00177 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
00178                                                  const char * format,
00179                                                  va_list arg ) )
00180 {
00181     mbedtls_vsnprintf = vsnprintf_func;
00182     return( 0 );
00183 }
00184 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
00185 
00186 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
00187 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
00188 /*
00189  * Make dummy function to prevent NULL pointer dereferences
00190  */
00191 static int platform_printf_uninit( const char *format, ... )
00192 {
00193     ((void) format);
00194     return( 0 );
00195 }
00196 
00197 #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
00198 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
00199 
00200 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
00201 
00202 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
00203 {
00204     mbedtls_printf = printf_func;
00205     return( 0 );
00206 }
00207 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
00208 
00209 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
00210 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
00211 /*
00212  * Make dummy function to prevent NULL pointer dereferences
00213  */
00214 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
00215 {
00216     ((void) stream);
00217     ((void) format);
00218     return( 0 );
00219 }
00220 
00221 #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
00222 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
00223 
00224 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
00225                                         MBEDTLS_PLATFORM_STD_FPRINTF;
00226 
00227 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
00228 {
00229     mbedtls_fprintf = fprintf_func;
00230     return( 0 );
00231 }
00232 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
00233 
00234 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
00235 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
00236 /*
00237  * Make dummy function to prevent NULL pointer dereferences
00238  */
00239 static void platform_exit_uninit( int status )
00240 {
00241     ((void) status);
00242 }
00243 
00244 #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
00245 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
00246 
00247 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
00248 
00249 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
00250 {
00251     mbedtls_exit = exit_func;
00252     return( 0 );
00253 }
00254 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
00255 
00256 #if defined(MBEDTLS_HAVE_TIME)
00257 
00258 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
00259 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
00260 /*
00261  * Make dummy function to prevent NULL pointer dereferences
00262  */
00263 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
00264 {
00265     ((void) timer);
00266     return( 0 );
00267 }
00268 
00269 #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
00270 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
00271 
00272 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
00273 
00274 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
00275 {
00276     mbedtls_time = time_func;
00277     return( 0 );
00278 }
00279 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
00280 
00281 #endif /* MBEDTLS_HAVE_TIME */
00282 
00283 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00284 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
00285 /* Default implementations for the platform independent seed functions use
00286  * standard libc file functions to read from and write to a pre-defined filename
00287  */
00288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
00289 {
00290     FILE *file;
00291     size_t n;
00292 
00293     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
00294         return( -1 );
00295 
00296     if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
00297     {
00298         fclose( file );
00299         mbedtls_platform_zeroize( buf, buf_len );
00300         return( -1 );
00301     }
00302 
00303     fclose( file );
00304     return( (int)n );
00305 }
00306 
00307 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
00308 {
00309     FILE *file;
00310     size_t n;
00311 
00312     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
00313         return -1;
00314 
00315     if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
00316     {
00317         fclose( file );
00318         return -1;
00319     }
00320 
00321     fclose( file );
00322     return( (int)n );
00323 }
00324 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
00325 
00326 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
00327 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
00328 /*
00329  * Make dummy function to prevent NULL pointer dereferences
00330  */
00331 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
00332 {
00333     ((void) buf);
00334     ((void) buf_len);
00335     return( -1 );
00336 }
00337 
00338 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
00339 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
00340 
00341 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
00342 /*
00343  * Make dummy function to prevent NULL pointer dereferences
00344  */
00345 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
00346 {
00347     ((void) buf);
00348     ((void) buf_len);
00349     return( -1 );
00350 }
00351 
00352 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
00353 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
00354 
00355 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
00356             MBEDTLS_PLATFORM_STD_NV_SEED_READ;
00357 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
00358             MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
00359 
00360 int mbedtls_platform_set_nv_seed(
00361         int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
00362         int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
00363 {
00364     mbedtls_nv_seed_read = nv_seed_read_func;
00365     mbedtls_nv_seed_write = nv_seed_write_func;
00366     return( 0 );
00367 }
00368 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
00369 #endif /* MBEDTLS_ENTROPY_NV_SEED */
00370 
00371 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
00372 /*
00373  * Placeholder platform setup that does nothing by default
00374  */
00375 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
00376 {
00377     (void)ctx;
00378 
00379     return( 0 );
00380 }
00381 
00382 /*
00383  * Placeholder platform teardown that does nothing by default
00384  */
00385 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
00386 {
00387     (void)ctx;
00388 }
00389 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
00390 
00391 #endif /* MBEDTLS_PLATFORM_C */