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