Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Platform abstraction layer |
| 3 | * |
| 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 23 | #include "mbedcrypto/config.h" |
| 24 | #else |
| 25 | #include MBEDCRYPTO_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDCRYPTO_PLATFORM_C) |
| 29 | |
| 30 | #include "mbedcrypto/platform.h" |
| 31 | #include "mbedcrypto/platform_util.h" |
| 32 | |
| 33 | #if defined(MBEDCRYPTO_PLATFORM_MEMORY) |
| 34 | #if !defined(MBEDCRYPTO_PLATFORM_STD_CALLOC) |
| 35 | static void *platform_calloc_uninit( size_t n, size_t size ) |
| 36 | { |
| 37 | ((void) n); |
| 38 | ((void) size); |
| 39 | return( NULL ); |
| 40 | } |
| 41 | |
| 42 | #define MBEDCRYPTO_PLATFORM_STD_CALLOC platform_calloc_uninit |
| 43 | #endif /* !MBEDCRYPTO_PLATFORM_STD_CALLOC */ |
| 44 | |
| 45 | #if !defined(MBEDCRYPTO_PLATFORM_STD_FREE) |
| 46 | static void platform_free_uninit( void *ptr ) |
| 47 | { |
| 48 | ((void) ptr); |
| 49 | } |
| 50 | |
| 51 | #define MBEDCRYPTO_PLATFORM_STD_FREE platform_free_uninit |
| 52 | #endif /* !MBEDCRYPTO_PLATFORM_STD_FREE */ |
| 53 | |
| 54 | void * (*mbedcrypto_calloc)( size_t, size_t ) = MBEDCRYPTO_PLATFORM_STD_CALLOC; |
| 55 | void (*mbedcrypto_free)( void * ) = MBEDCRYPTO_PLATFORM_STD_FREE; |
| 56 | |
| 57 | int mbedcrypto_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), |
| 58 | void (*free_func)( void * ) ) |
| 59 | { |
| 60 | mbedcrypto_calloc = calloc_func; |
| 61 | mbedcrypto_free = free_func; |
| 62 | return( 0 ); |
| 63 | } |
| 64 | #endif /* MBEDCRYPTO_PLATFORM_MEMORY */ |
| 65 | |
| 66 | #if defined(_WIN32) |
| 67 | #include <stdarg.h> |
| 68 | int mbedcrypto_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) |
| 69 | { |
| 70 | int ret; |
| 71 | va_list argp; |
| 72 | |
| 73 | /* Avoid calling the invalid parameter handler by checking ourselves */ |
| 74 | if( s == NULL || n == 0 || fmt == NULL ) |
| 75 | return( -1 ); |
| 76 | |
| 77 | va_start( argp, fmt ); |
| 78 | #if defined(_TRUNCATE) && !defined(__MINGW32__) |
| 79 | ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); |
| 80 | #else |
| 81 | ret = _vsnprintf( s, n, fmt, argp ); |
| 82 | if( ret < 0 || (size_t) ret == n ) |
| 83 | { |
| 84 | s[n-1] = '\0'; |
| 85 | ret = -1; |
| 86 | } |
| 87 | #endif |
| 88 | va_end( argp ); |
| 89 | |
| 90 | return( ret ); |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | #if defined(MBEDCRYPTO_PLATFORM_SNPRINTF_ALT) |
| 95 | #if !defined(MBEDCRYPTO_PLATFORM_STD_SNPRINTF) |
| 96 | /* |
| 97 | * Make dummy function to prevent NULL pointer dereferences |
| 98 | */ |
| 99 | static int platform_snprintf_uninit( char * s, size_t n, |
| 100 | const char * format, ... ) |
| 101 | { |
| 102 | ((void) s); |
| 103 | ((void) n); |
| 104 | ((void) format); |
| 105 | return( 0 ); |
| 106 | } |
| 107 | |
| 108 | #define MBEDCRYPTO_PLATFORM_STD_SNPRINTF platform_snprintf_uninit |
| 109 | #endif /* !MBEDCRYPTO_PLATFORM_STD_SNPRINTF */ |
| 110 | |
| 111 | int (*mbedcrypto_snprintf)( char * s, size_t n, |
| 112 | const char * format, |
| 113 | ... ) = MBEDCRYPTO_PLATFORM_STD_SNPRINTF; |
| 114 | |
| 115 | int mbedcrypto_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, |
| 116 | const char * format, |
| 117 | ... ) ) |
| 118 | { |
| 119 | mbedcrypto_snprintf = snprintf_func; |
| 120 | return( 0 ); |
| 121 | } |
| 122 | #endif /* MBEDCRYPTO_PLATFORM_SNPRINTF_ALT */ |
| 123 | |
| 124 | #if defined(MBEDCRYPTO_PLATFORM_PRINTF_ALT) |
| 125 | #if !defined(MBEDCRYPTO_PLATFORM_STD_PRINTF) |
| 126 | /* |
| 127 | * Make dummy function to prevent NULL pointer dereferences |
| 128 | */ |
| 129 | static int platform_printf_uninit( const char *format, ... ) |
| 130 | { |
| 131 | ((void) format); |
| 132 | return( 0 ); |
| 133 | } |
| 134 | |
| 135 | #define MBEDCRYPTO_PLATFORM_STD_PRINTF platform_printf_uninit |
| 136 | #endif /* !MBEDCRYPTO_PLATFORM_STD_PRINTF */ |
| 137 | |
| 138 | int (*mbedcrypto_printf)( const char *, ... ) = MBEDCRYPTO_PLATFORM_STD_PRINTF; |
| 139 | |
| 140 | int mbedcrypto_platform_set_printf( int (*printf_func)( const char *, ... ) ) |
| 141 | { |
| 142 | mbedcrypto_printf = printf_func; |
| 143 | return( 0 ); |
| 144 | } |
| 145 | #endif /* MBEDCRYPTO_PLATFORM_PRINTF_ALT */ |
| 146 | |
| 147 | #if defined(MBEDCRYPTO_PLATFORM_FPRINTF_ALT) |
| 148 | #if !defined(MBEDCRYPTO_PLATFORM_STD_FPRINTF) |
| 149 | /* |
| 150 | * Make dummy function to prevent NULL pointer dereferences |
| 151 | */ |
| 152 | static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) |
| 153 | { |
| 154 | ((void) stream); |
| 155 | ((void) format); |
| 156 | return( 0 ); |
| 157 | } |
| 158 | |
| 159 | #define MBEDCRYPTO_PLATFORM_STD_FPRINTF platform_fprintf_uninit |
| 160 | #endif /* !MBEDCRYPTO_PLATFORM_STD_FPRINTF */ |
| 161 | |
| 162 | int (*mbedcrypto_fprintf)( FILE *, const char *, ... ) = |
| 163 | MBEDCRYPTO_PLATFORM_STD_FPRINTF; |
| 164 | |
| 165 | int mbedcrypto_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) |
| 166 | { |
| 167 | mbedcrypto_fprintf = fprintf_func; |
| 168 | return( 0 ); |
| 169 | } |
| 170 | #endif /* MBEDCRYPTO_PLATFORM_FPRINTF_ALT */ |
| 171 | |
| 172 | #if defined(MBEDCRYPTO_PLATFORM_EXIT_ALT) |
| 173 | #if !defined(MBEDCRYPTO_PLATFORM_STD_EXIT) |
| 174 | /* |
| 175 | * Make dummy function to prevent NULL pointer dereferences |
| 176 | */ |
| 177 | static void platform_exit_uninit( int status ) |
| 178 | { |
| 179 | ((void) status); |
| 180 | } |
| 181 | |
| 182 | #define MBEDCRYPTO_PLATFORM_STD_EXIT platform_exit_uninit |
| 183 | #endif /* !MBEDCRYPTO_PLATFORM_STD_EXIT */ |
| 184 | |
| 185 | void (*mbedcrypto_exit)( int status ) = MBEDCRYPTO_PLATFORM_STD_EXIT; |
| 186 | |
| 187 | int mbedcrypto_platform_set_exit( void (*exit_func)( int status ) ) |
| 188 | { |
| 189 | mbedcrypto_exit = exit_func; |
| 190 | return( 0 ); |
| 191 | } |
| 192 | #endif /* MBEDCRYPTO_PLATFORM_EXIT_ALT */ |
| 193 | |
| 194 | #if defined(MBEDCRYPTO_HAVE_TIME) |
| 195 | |
| 196 | #if defined(MBEDCRYPTO_PLATFORM_TIME_ALT) |
| 197 | #if !defined(MBEDCRYPTO_PLATFORM_STD_TIME) |
| 198 | /* |
| 199 | * Make dummy function to prevent NULL pointer dereferences |
| 200 | */ |
| 201 | static mbedcrypto_time_t platform_time_uninit( mbedcrypto_time_t* timer ) |
| 202 | { |
| 203 | ((void) timer); |
| 204 | return( 0 ); |
| 205 | } |
| 206 | |
| 207 | #define MBEDCRYPTO_PLATFORM_STD_TIME platform_time_uninit |
| 208 | #endif /* !MBEDCRYPTO_PLATFORM_STD_TIME */ |
| 209 | |
| 210 | mbedcrypto_time_t (*mbedcrypto_time)( mbedcrypto_time_t* timer ) = MBEDCRYPTO_PLATFORM_STD_TIME; |
| 211 | |
| 212 | int mbedcrypto_platform_set_time( mbedcrypto_time_t (*time_func)( mbedcrypto_time_t* timer ) ) |
| 213 | { |
| 214 | mbedcrypto_time = time_func; |
| 215 | return( 0 ); |
| 216 | } |
| 217 | #endif /* MBEDCRYPTO_PLATFORM_TIME_ALT */ |
| 218 | |
| 219 | #endif /* MBEDCRYPTO_HAVE_TIME */ |
| 220 | |
| 221 | #if defined(MBEDCRYPTO_ENTROPY_NV_SEED) |
| 222 | #if !defined(MBEDCRYPTO_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDCRYPTO_FS_IO) |
| 223 | /* Default implementations for the platform independent seed functions use |
| 224 | * standard libc file functions to read from and write to a pre-defined filename |
| 225 | */ |
| 226 | int mbedcrypto_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) |
| 227 | { |
| 228 | FILE *file; |
| 229 | size_t n; |
| 230 | |
| 231 | if( ( file = fopen( MBEDCRYPTO_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) |
| 232 | return( -1 ); |
| 233 | |
| 234 | if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) |
| 235 | { |
| 236 | fclose( file ); |
| 237 | mbedcrypto_platform_zeroize( buf, buf_len ); |
| 238 | return( -1 ); |
| 239 | } |
| 240 | |
| 241 | fclose( file ); |
| 242 | return( (int)n ); |
| 243 | } |
| 244 | |
| 245 | int mbedcrypto_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) |
| 246 | { |
| 247 | FILE *file; |
| 248 | size_t n; |
| 249 | |
| 250 | if( ( file = fopen( MBEDCRYPTO_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) |
| 251 | return -1; |
| 252 | |
| 253 | if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) |
| 254 | { |
| 255 | fclose( file ); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | fclose( file ); |
| 260 | return( (int)n ); |
| 261 | } |
| 262 | #endif /* MBEDCRYPTO_PLATFORM_NO_STD_FUNCTIONS */ |
| 263 | |
| 264 | #if defined(MBEDCRYPTO_PLATFORM_NV_SEED_ALT) |
| 265 | #if !defined(MBEDCRYPTO_PLATFORM_STD_NV_SEED_READ) |
| 266 | /* |
| 267 | * Make dummy function to prevent NULL pointer dereferences |
| 268 | */ |
| 269 | static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) |
| 270 | { |
| 271 | ((void) buf); |
| 272 | ((void) buf_len); |
| 273 | return( -1 ); |
| 274 | } |
| 275 | |
| 276 | #define MBEDCRYPTO_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit |
| 277 | #endif /* !MBEDCRYPTO_PLATFORM_STD_NV_SEED_READ */ |
| 278 | |
| 279 | #if !defined(MBEDCRYPTO_PLATFORM_STD_NV_SEED_WRITE) |
| 280 | /* |
| 281 | * Make dummy function to prevent NULL pointer dereferences |
| 282 | */ |
| 283 | static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) |
| 284 | { |
| 285 | ((void) buf); |
| 286 | ((void) buf_len); |
| 287 | return( -1 ); |
| 288 | } |
| 289 | |
| 290 | #define MBEDCRYPTO_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit |
| 291 | #endif /* !MBEDCRYPTO_PLATFORM_STD_NV_SEED_WRITE */ |
| 292 | |
| 293 | int (*mbedcrypto_nv_seed_read)( unsigned char *buf, size_t buf_len ) = |
| 294 | MBEDCRYPTO_PLATFORM_STD_NV_SEED_READ; |
| 295 | int (*mbedcrypto_nv_seed_write)( unsigned char *buf, size_t buf_len ) = |
| 296 | MBEDCRYPTO_PLATFORM_STD_NV_SEED_WRITE; |
| 297 | |
| 298 | int mbedcrypto_platform_set_nv_seed( |
| 299 | int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), |
| 300 | int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) |
| 301 | { |
| 302 | mbedcrypto_nv_seed_read = nv_seed_read_func; |
| 303 | mbedcrypto_nv_seed_write = nv_seed_write_func; |
| 304 | return( 0 ); |
| 305 | } |
| 306 | #endif /* MBEDCRYPTO_PLATFORM_NV_SEED_ALT */ |
| 307 | #endif /* MBEDCRYPTO_ENTROPY_NV_SEED */ |
| 308 | |
| 309 | #if !defined(MBEDCRYPTO_PLATFORM_SETUP_TEARDOWN_ALT) |
| 310 | /* |
| 311 | * Placeholder platform setup that does nothing by default |
| 312 | */ |
| 313 | int mbedcrypto_platform_setup( mbedcrypto_platform_context *ctx ) |
| 314 | { |
| 315 | (void)ctx; |
| 316 | |
| 317 | return( 0 ); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Placeholder platform teardown that does nothing by default |
| 322 | */ |
| 323 | void mbedcrypto_platform_teardown( mbedcrypto_platform_context *ctx ) |
| 324 | { |
| 325 | (void)ctx; |
| 326 | } |
| 327 | #endif /* MBEDCRYPTO_PLATFORM_SETUP_TEARDOWN_ALT */ |
| 328 | |
| 329 | #endif /* MBEDCRYPTO_PLATFORM_C */ |