1 /* 2 * Platform abstraction layer 3 * 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6 * 7 * This file is provided under the Apache License 2.0, or the 8 * GNU General Public License v2.0 or later. 9 * 10 * ********** 11 * Apache License 2.0: 12 * 13 * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 * not use this file except in compliance with the License. 15 * You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, software 20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 * See the License for the specific language governing permissions and 23 * limitations under the License. 24 * 25 * ********** 26 * 27 * ********** 28 * GNU General Public License v2.0 or later: 29 * 30 * This program is free software; you can redistribute it and/or modify 31 * it under the terms of the GNU General Public License as published by 32 * the Free Software Foundation; either version 2 of the License, or 33 * (at your option) any later version. 34 * 35 * This program is distributed in the hope that it will be useful, 36 * but WITHOUT ANY WARRANTY; without even the implied warranty of 37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 38 * GNU General Public License for more details. 39 * 40 * You should have received a copy of the GNU General Public License along 41 * with this program; if not, write to the Free Software Foundation, Inc., 42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 43 * 44 * ********** 45 * 46 * This file is part of mbed TLS (https://tls.mbed.org) 47 */ 48 49 #if !defined(MBEDTLS_CONFIG_FILE) 50 #include "mbedtls/config.h" 51 #else 52 #include MBEDTLS_CONFIG_FILE 53 #endif 54 55 #if defined(MBEDTLS_PLATFORM_C) 56 57 #include "mbedtls/platform.h" 58 59 #if defined(MBEDTLS_ENTROPY_NV_SEED) && \ 60 !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) 61 /* Implementation that should never be optimized out by the compiler */ 62 static void mbedtls_zeroize( void *v, size_t n ) { 63 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; 64 } 65 #endif 66 67 /* The compile time configuration of memory allocation via the macros 68 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime 69 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything 70 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */ 71 #if defined(MBEDTLS_PLATFORM_MEMORY) && \ 72 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \ 73 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) 74 75 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) 76 static void *platform_calloc_uninit( size_t n, size_t size ) 77 { 78 ((void) n); 79 ((void) size); 80 return( NULL ); 81 } 82 83 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit 84 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ 85 86 #if !defined(MBEDTLS_PLATFORM_STD_FREE) 87 static void platform_free_uninit( void *ptr ) 88 { 89 ((void) ptr); 90 } 91 92 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit 93 #endif /* !MBEDTLS_PLATFORM_STD_FREE */ 94 95 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; 96 void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; 97 98 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), 99 void (*free_func)( void * ) ) 100 { 101 mbedtls_calloc = calloc_func; 102 mbedtls_free = free_func; 103 return( 0 ); 104 } 105 #endif /* MBEDTLS_PLATFORM_MEMORY && 106 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && 107 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */ 108 109 #if defined(_WIN32) 110 #include <stdarg.h> 111 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) 112 { 113 int ret; 114 va_list argp; 115 116 /* Avoid calling the invalid parameter handler by checking ourselves */ 117 if( s == NULL || n == 0 || fmt == NULL ) 118 return( -1 ); 119 120 va_start( argp, fmt ); 121 #if defined(_TRUNCATE) && !defined(__MINGW32__) 122 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); 123 #else 124 ret = _vsnprintf( s, n, fmt, argp ); 125 if( ret < 0 || (size_t) ret == n ) 126 { 127 s[n-1] = '\0'; 128 ret = -1; 129 } 130 #endif 131 va_end( argp ); 132 133 return( ret ); 134 } 135 #endif 136 137 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) 138 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) 139 /* 140 * Make dummy function to prevent NULL pointer dereferences 141 */ 142 static int platform_snprintf_uninit( char * s, size_t n, 143 const char * format, ... ) 144 { 145 ((void) s); 146 ((void) n); 147 ((void) format); 148 return( 0 ); 149 } 150 151 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit 152 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ 153 154 int (*mbedtls_snprintf)( char * s, size_t n, 155 const char * format, 156 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; 157 158 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, 159 const char * format, 160 ... ) ) 161 { 162 mbedtls_snprintf = snprintf_func; 163 return( 0 ); 164 } 165 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ 166 167 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) 168 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) 169 /* 170 * Make dummy function to prevent NULL pointer dereferences 171 */ 172 static int platform_printf_uninit( const char *format, ... ) 173 { 174 ((void) format); 175 return( 0 ); 176 } 177 178 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit 179 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ 180 181 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; 182 183 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) 184 { 185 mbedtls_printf = printf_func; 186 return( 0 ); 187 } 188 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ 189 190 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) 191 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) 192 /* 193 * Make dummy function to prevent NULL pointer dereferences 194 */ 195 static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) 196 { 197 ((void) stream); 198 ((void) format); 199 return( 0 ); 200 } 201 202 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit 203 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ 204 205 int (*mbedtls_fprintf)( FILE *, const char *, ... ) = 206 MBEDTLS_PLATFORM_STD_FPRINTF; 207 208 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) 209 { 210 mbedtls_fprintf = fprintf_func; 211 return( 0 ); 212 } 213 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ 214 215 #if defined(MBEDTLS_PLATFORM_EXIT_ALT) 216 #if !defined(MBEDTLS_PLATFORM_STD_EXIT) 217 /* 218 * Make dummy function to prevent NULL pointer dereferences 219 */ 220 static void platform_exit_uninit( int status ) 221 { 222 ((void) status); 223 } 224 225 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit 226 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ 227 228 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; 229 230 int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) 231 { 232 mbedtls_exit = exit_func; 233 return( 0 ); 234 } 235 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ 236 237 #if defined(MBEDTLS_HAVE_TIME) 238 239 #if defined(MBEDTLS_PLATFORM_TIME_ALT) 240 #if !defined(MBEDTLS_PLATFORM_STD_TIME) 241 /* 242 * Make dummy function to prevent NULL pointer dereferences 243 */ 244 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) 245 { 246 ((void) timer); 247 return( 0 ); 248 } 249 250 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit 251 #endif /* !MBEDTLS_PLATFORM_STD_TIME */ 252 253 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; 254 255 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) ) 256 { 257 mbedtls_time = time_func; 258 return( 0 ); 259 } 260 #endif /* MBEDTLS_PLATFORM_TIME_ALT */ 261 262 #endif /* MBEDTLS_HAVE_TIME */ 263 264 #if defined(MBEDTLS_ENTROPY_NV_SEED) 265 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) 266 /* Default implementations for the platform independent seed functions use 267 * standard libc file functions to read from and write to a pre-defined filename 268 */ 269 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) 270 { 271 FILE *file; 272 size_t n; 273 274 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) 275 return( -1 ); 276 277 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) 278 { 279 fclose( file ); 280 mbedtls_zeroize( buf, buf_len ); 281 return( -1 ); 282 } 283 284 fclose( file ); 285 return( (int)n ); 286 } 287 288 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) 289 { 290 FILE *file; 291 size_t n; 292 293 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) 294 return -1; 295 296 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) 297 { 298 fclose( file ); 299 return -1; 300 } 301 302 fclose( file ); 303 return( (int)n ); 304 } 305 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ 306 307 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) 308 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) 309 /* 310 * Make dummy function to prevent NULL pointer dereferences 311 */ 312 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) 313 { 314 ((void) buf); 315 ((void) buf_len); 316 return( -1 ); 317 } 318 319 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit 320 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ 321 322 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) 323 /* 324 * Make dummy function to prevent NULL pointer dereferences 325 */ 326 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) 327 { 328 ((void) buf); 329 ((void) buf_len); 330 return( -1 ); 331 } 332 333 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit 334 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ 335 336 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = 337 MBEDTLS_PLATFORM_STD_NV_SEED_READ; 338 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = 339 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; 340 341 int mbedtls_platform_set_nv_seed( 342 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), 343 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) 344 { 345 mbedtls_nv_seed_read = nv_seed_read_func; 346 mbedtls_nv_seed_write = nv_seed_write_func; 347 return( 0 ); 348 } 349 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ 350 #endif /* MBEDTLS_ENTROPY_NV_SEED */ 351 352 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) 353 /* 354 * Placeholder platform setup that does nothing by default 355 */ 356 int mbedtls_platform_setup( mbedtls_platform_context *ctx ) 357 { 358 (void)ctx; 359 360 return( 0 ); 361 } 362 363 /* 364 * Placeholder platform teardown that does nothing by default 365 */ 366 void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) 367 { 368 (void)ctx; 369 } 370 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ 371 372 #endif /* MBEDTLS_PLATFORM_C */ 373