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