BA / Mbed OS BaBoRo1
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 
00032 #if defined(MBEDTLS_ENTROPY_NV_SEED) && \
00033     !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
00034 /* Implementation that should never be optimized out by the compiler */
00035 static void mbedtls_zeroize( void *v, size_t n ) {
00036     volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
00037 }
00038 #endif
00039 
00040 #if defined(MBEDTLS_PLATFORM_MEMORY)
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 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
00062 void (*mbedtls_free)( void * )     = MBEDTLS_PLATFORM_STD_FREE;
00063 
00064 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
00065                               void (*free_func)( void * ) )
00066 {
00067     mbedtls_calloc = calloc_func;
00068     mbedtls_free = free_func;
00069     return( 0 );
00070 }
00071 #endif /* MBEDTLS_PLATFORM_MEMORY */
00072 
00073 #if defined(_WIN32)
00074 #include <stdarg.h>
00075 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
00076 {
00077     int ret;
00078     va_list argp;
00079 
00080     /* Avoid calling the invalid parameter handler by checking ourselves */
00081     if( s == NULL || n == 0 || fmt == NULL )
00082         return( -1 );
00083 
00084     va_start( argp, fmt );
00085 #if defined(_TRUNCATE)
00086     ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
00087 #else
00088     ret = _vsnprintf( s, n, fmt, argp );
00089     if( ret < 0 || (size_t) ret == n )
00090     {
00091         s[n-1] = '\0';
00092         ret = -1;
00093     }
00094 #endif
00095     va_end( argp );
00096 
00097     return( ret );
00098 }
00099 #endif
00100 
00101 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
00102 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
00103 /*
00104  * Make dummy function to prevent NULL pointer dereferences
00105  */
00106 static int platform_snprintf_uninit( char * s, size_t n,
00107                                      const char * format, ... )
00108 {
00109     ((void) s);
00110     ((void) n);
00111     ((void) format);
00112     return( 0 );
00113 }
00114 
00115 #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
00116 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
00117 
00118 int (*mbedtls_snprintf)( char * s, size_t n,
00119                           const char * format,
00120                           ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
00121 
00122 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
00123                                                  const char * format,
00124                                                  ... ) )
00125 {
00126     mbedtls_snprintf = snprintf_func;
00127     return( 0 );
00128 }
00129 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
00130 
00131 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
00132 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
00133 /*
00134  * Make dummy function to prevent NULL pointer dereferences
00135  */
00136 static int platform_printf_uninit( const char *format, ... )
00137 {
00138     ((void) format);
00139     return( 0 );
00140 }
00141 
00142 #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
00143 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
00144 
00145 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
00146 
00147 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
00148 {
00149     mbedtls_printf = printf_func;
00150     return( 0 );
00151 }
00152 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
00153 
00154 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
00155 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
00156 /*
00157  * Make dummy function to prevent NULL pointer dereferences
00158  */
00159 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
00160 {
00161     ((void) stream);
00162     ((void) format);
00163     return( 0 );
00164 }
00165 
00166 #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
00167 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
00168 
00169 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
00170                                         MBEDTLS_PLATFORM_STD_FPRINTF;
00171 
00172 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
00173 {
00174     mbedtls_fprintf = fprintf_func;
00175     return( 0 );
00176 }
00177 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
00178 
00179 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
00180 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
00181 /*
00182  * Make dummy function to prevent NULL pointer dereferences
00183  */
00184 static void platform_exit_uninit( int status )
00185 {
00186     ((void) status);
00187 }
00188 
00189 #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
00190 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
00191 
00192 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
00193 
00194 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
00195 {
00196     mbedtls_exit = exit_func;
00197     return( 0 );
00198 }
00199 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
00200 
00201 #if defined(MBEDTLS_HAVE_TIME)
00202 
00203 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
00204 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
00205 /*
00206  * Make dummy function to prevent NULL pointer dereferences
00207  */
00208 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
00209 {
00210     ((void) timer);
00211     return( 0 );
00212 }
00213 
00214 #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
00215 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
00216 
00217 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
00218 
00219 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
00220 {
00221     mbedtls_time = time_func;
00222     return( 0 );
00223 }
00224 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
00225 
00226 #endif /* MBEDTLS_HAVE_TIME */
00227 
00228 #if defined(MBEDTLS_ENTROPY_NV_SEED)
00229 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
00230 /* Default implementations for the platform independent seed functions use
00231  * standard libc file functions to read from and write to a pre-defined filename
00232  */
00233 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
00234 {
00235     FILE *file;
00236     size_t n;
00237 
00238     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
00239         return( -1 );
00240 
00241     if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
00242     {
00243         fclose( file );
00244         mbedtls_zeroize( buf, buf_len );
00245         return( -1 );
00246     }
00247 
00248     fclose( file );
00249     return( (int)n );
00250 }
00251 
00252 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
00253 {
00254     FILE *file;
00255     size_t n;
00256 
00257     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
00258         return -1;
00259 
00260     if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
00261     {
00262         fclose( file );
00263         return -1;
00264     }
00265 
00266     fclose( file );
00267     return( (int)n );
00268 }
00269 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
00270 
00271 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
00272 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
00273 /*
00274  * Make dummy function to prevent NULL pointer dereferences
00275  */
00276 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
00277 {
00278     ((void) buf);
00279     ((void) buf_len);
00280     return( -1 );
00281 }
00282 
00283 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
00284 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
00285 
00286 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
00287 /*
00288  * Make dummy function to prevent NULL pointer dereferences
00289  */
00290 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
00291 {
00292     ((void) buf);
00293     ((void) buf_len);
00294     return( -1 );
00295 }
00296 
00297 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
00298 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
00299 
00300 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
00301             MBEDTLS_PLATFORM_STD_NV_SEED_READ;
00302 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
00303             MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
00304 
00305 int mbedtls_platform_set_nv_seed(
00306         int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
00307         int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
00308 {
00309     mbedtls_nv_seed_read = nv_seed_read_func;
00310     mbedtls_nv_seed_write = nv_seed_write_func;
00311     return( 0 );
00312 }
00313 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
00314 #endif /* MBEDTLS_ENTROPY_NV_SEED */
00315 
00316 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
00317 /*
00318  * Placeholder platform setup that does nothing by default
00319  */
00320 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
00321 {
00322     (void)ctx;
00323 
00324     return( 0 );
00325 }
00326 
00327 /*
00328  * Placeholder platform teardown that does nothing by default
00329  */
00330 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
00331 {
00332     (void)ctx;
00333 }
00334 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
00335 
00336 #endif /* MBEDTLS_PLATFORM_C */