Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Platform abstraction layer |
| 3 | * |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | a268da9 | 2017-12-20 12:52:49 +0100 | [diff] [blame] | 32 | #if defined(MBEDTLS_ENTROPY_NV_SEED) && \ |
| 33 | !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) |
| 34 | /* Implementation that should never be optimized out by the compiler */ |
| 35 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 36 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
| 37 | } |
| 38 | #endif |
| 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_PLATFORM_MEMORY) |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 41 | #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) |
| 42 | static void *platform_calloc_uninit( size_t n, size_t size ) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 43 | { |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 44 | ((void) n); |
| 45 | ((void) size); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 46 | return( NULL ); |
| 47 | } |
| 48 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 49 | #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit |
| 50 | #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if !defined(MBEDTLS_PLATFORM_STD_FREE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 53 | static void platform_free_uninit( void *ptr ) |
| 54 | { |
| 55 | ((void) ptr); |
| 56 | } |
| 57 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit |
| 59 | #endif /* !MBEDTLS_PLATFORM_STD_FREE */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 61 | void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 64 | int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 65 | void (*free_func)( void * ) ) |
| 66 | { |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 67 | mbedtls_calloc = calloc_func; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | mbedtls_free = free_func; |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 69 | return( 0 ); |
| 70 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #endif /* MBEDTLS_PLATFORM_MEMORY */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 73 | #if defined(_WIN32) |
| 74 | #include <stdarg.h> |
| 75 | int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) |
| 76 | { |
| 77 | int ret; |
| 78 | va_list argp; |
| 79 | |
Manuel Pégourié-Gonnard | f659d2c | 2015-06-26 17:45:00 +0200 | [diff] [blame] | 80 | /* Avoid calling the invalid parameter handler by checking ourselves */ |
| 81 | if( s == NULL || n == 0 || fmt == NULL ) |
| 82 | return( -1 ); |
| 83 | |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 84 | va_start( argp, fmt ); |
Ron Eldor | bc18eb3 | 2017-09-06 17:49:10 +0300 | [diff] [blame] | 85 | #if defined(_TRUNCATE) && !defined(__MINGW32__) |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 86 | ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); |
Manuel Pégourié-Gonnard | a4f055f | 2015-07-08 16:46:13 +0200 | [diff] [blame] | 87 | #else |
| 88 | ret = _vsnprintf( s, n, fmt, argp ); |
| 89 | if( ret < 0 || (size_t) ret == n ) |
| 90 | { |
| 91 | s[n-1] = '\0'; |
| 92 | ret = -1; |
| 93 | } |
| 94 | #endif |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 95 | va_end( argp ); |
| 96 | |
| 97 | return( ret ); |
| 98 | } |
| 99 | #endif |
| 100 | |
Krzysztof Stachowiak | 25d34a4 | 2018-03-20 16:17:51 +0100 | [diff] [blame] | 101 | #if defined(MBEDTLS_PLATFORM_TF_SNPRINTF) |
| 102 | |
| 103 | static void unsigned_dec_print( char **s, size_t n, size_t *chars_printed, |
| 104 | unsigned int unum ) |
| 105 | { |
| 106 | /* Enough for a 32-bit unsigned decimal integer (4294967295). */ |
| 107 | unsigned char num_buf[10]; |
| 108 | int i = 0, rem; |
| 109 | |
| 110 | do |
| 111 | { |
| 112 | rem = unum % 10; |
| 113 | num_buf[i++] = '0' + rem; |
| 114 | } while( unum /= 10 ); |
| 115 | |
| 116 | while( --i >= 0 ) |
| 117 | { |
| 118 | if( *chars_printed < n ) |
| 119 | *(*s)++ = num_buf[i]; |
| 120 | (*chars_printed)++; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | int tf_snprintf( char *s, size_t n, const char *fmt, ... ) |
| 125 | { |
| 126 | va_list args; |
| 127 | int num; |
| 128 | unsigned int unum; |
| 129 | size_t chars_printed = 0; |
| 130 | |
| 131 | if( n == 1 ) |
| 132 | { |
| 133 | /* Buffer is too small to actually write anything else. */ |
| 134 | *s = '\0'; |
| 135 | n = 0; |
| 136 | } |
| 137 | else if( n >= 2 ) |
| 138 | { |
| 139 | /* Reserve space for the terminator character. */ |
| 140 | n--; |
| 141 | } |
| 142 | |
| 143 | va_start( args, fmt ); |
| 144 | while( *fmt ) { |
| 145 | |
| 146 | if( *fmt == '%' ) |
| 147 | { |
| 148 | fmt++; |
| 149 | /* Check the format specifier. */ |
| 150 | switch( *fmt ) |
| 151 | { |
| 152 | case 'i': |
| 153 | case 'd': |
| 154 | num = va_arg( args, int ); |
| 155 | |
| 156 | if( num < 0 ) |
| 157 | { |
| 158 | if( chars_printed < n ) |
| 159 | *s++ = '-'; |
| 160 | chars_printed++; |
| 161 | |
| 162 | unum = (unsigned int)-num; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | unum = (unsigned int)num; |
| 167 | } |
| 168 | |
| 169 | unsigned_dec_print( &s, n, &chars_printed, unum ); |
| 170 | break; |
| 171 | case 'u': |
| 172 | unum = va_arg( args, unsigned int ); |
| 173 | unsigned_dec_print( &s, n, &chars_printed, unum ); |
| 174 | break; |
| 175 | default: |
| 176 | /* Return error on any other specifier. */ |
| 177 | chars_printed = -1; |
| 178 | goto exit; |
| 179 | } |
| 180 | fmt++; |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | if( chars_printed < n ) |
| 185 | *s++ = *fmt; |
| 186 | fmt++; |
| 187 | chars_printed++; |
| 188 | } |
| 189 | |
| 190 | exit: |
| 191 | va_end( args ); |
| 192 | |
| 193 | if( n > 0 ) |
| 194 | *s = '\0'; |
| 195 | |
| 196 | return( chars_printed ); |
| 197 | } |
| 198 | |
| 199 | #if defined(MBEDTLS_PLATFORM_STD_SNPRINTF) |
| 200 | #undef MBEDTLS_PLATFORM_STD_SNPRINTF |
| 201 | #endif |
| 202 | #define MBEDTLS_PLATFORM_STD_SNPRINTF tf_snprintf; |
| 203 | |
| 204 | #endif /* MBEDTLS_PLATFORM_TF_SNPRINTF */ |
| 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) |
| 207 | #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 208 | /* |
| 209 | * Make dummy function to prevent NULL pointer dereferences |
| 210 | */ |
| 211 | static int platform_snprintf_uninit( char * s, size_t n, |
| 212 | const char * format, ... ) |
| 213 | { |
| 214 | ((void) s); |
| 215 | ((void) n); |
Manuel Pégourié-Gonnard | dccb80b | 2015-06-03 10:20:33 +0100 | [diff] [blame] | 216 | ((void) format); |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 217 | return( 0 ); |
| 218 | } |
| 219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit |
| 221 | #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | int (*mbedtls_snprintf)( char * s, size_t n, |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 224 | const char * format, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 228 | const char * format, |
| 229 | ... ) ) |
| 230 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | mbedtls_snprintf = snprintf_func; |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 232 | return( 0 ); |
| 233 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) |
| 237 | #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 238 | /* |
| 239 | * Make dummy function to prevent NULL pointer dereferences |
| 240 | */ |
| 241 | static int platform_printf_uninit( const char *format, ... ) |
| 242 | { |
| 243 | ((void) format); |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit |
| 248 | #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 251 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 253 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_printf = printf_func; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 255 | return( 0 ); |
| 256 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) |
| 260 | #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 261 | /* |
| 262 | * Make dummy function to prevent NULL pointer dereferences |
| 263 | */ |
| 264 | static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) |
| 265 | { |
| 266 | ((void) stream); |
| 267 | ((void) format); |
| 268 | return( 0 ); |
| 269 | } |
| 270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit |
| 272 | #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | int (*mbedtls_fprintf)( FILE *, const char *, ... ) = |
| 275 | MBEDTLS_PLATFORM_STD_FPRINTF; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 278 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | mbedtls_fprintf = fprintf_func; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 280 | return( 0 ); |
| 281 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | #if defined(MBEDTLS_PLATFORM_EXIT_ALT) |
| 285 | #if !defined(MBEDTLS_PLATFORM_STD_EXIT) |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 286 | /* |
| 287 | * Make dummy function to prevent NULL pointer dereferences |
| 288 | */ |
| 289 | static void platform_exit_uninit( int status ) |
| 290 | { |
| 291 | ((void) status); |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit |
| 295 | #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 296 | |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 297 | void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 300 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | mbedtls_exit = exit_func; |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 302 | return( 0 ); |
| 303 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 305 | |
Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 306 | #if defined(MBEDTLS_HAVE_TIME) |
| 307 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 308 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
| 309 | #if !defined(MBEDTLS_PLATFORM_STD_TIME) |
| 310 | /* |
| 311 | * Make dummy function to prevent NULL pointer dereferences |
| 312 | */ |
| 313 | static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) |
| 314 | { |
| 315 | ((void) timer); |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 316 | return( 0 ); |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit |
| 320 | #endif /* !MBEDTLS_PLATFORM_STD_TIME */ |
| 321 | |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 322 | mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 323 | |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 324 | int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) ) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 325 | { |
| 326 | mbedtls_time = time_func; |
| 327 | return( 0 ); |
| 328 | } |
| 329 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ |
| 330 | |
Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 331 | #endif /* MBEDTLS_HAVE_TIME */ |
| 332 | |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 333 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 334 | #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) |
| 335 | /* Default implementations for the platform independent seed functions use |
| 336 | * standard libc file functions to read from and write to a pre-defined filename |
| 337 | */ |
| 338 | int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) |
| 339 | { |
| 340 | FILE *file; |
| 341 | size_t n; |
| 342 | |
| 343 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) |
Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 344 | return( -1 ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 345 | |
| 346 | if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) |
| 347 | { |
| 348 | fclose( file ); |
Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 349 | mbedtls_zeroize( buf, buf_len ); |
| 350 | return( -1 ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | fclose( file ); |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 354 | return( (int)n ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) |
| 358 | { |
| 359 | FILE *file; |
| 360 | size_t n; |
| 361 | |
| 362 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) |
| 363 | return -1; |
| 364 | |
| 365 | if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) |
| 366 | { |
| 367 | fclose( file ); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | fclose( file ); |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 372 | return( (int)n ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 373 | } |
| 374 | #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ |
| 375 | |
| 376 | #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) |
| 377 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) |
| 378 | /* |
| 379 | * Make dummy function to prevent NULL pointer dereferences |
| 380 | */ |
| 381 | static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) |
| 382 | { |
| 383 | ((void) buf); |
| 384 | ((void) buf_len); |
| 385 | return( -1 ); |
| 386 | } |
| 387 | |
| 388 | #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit |
| 389 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ |
| 390 | |
| 391 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) |
| 392 | /* |
| 393 | * Make dummy function to prevent NULL pointer dereferences |
| 394 | */ |
| 395 | static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) |
| 396 | { |
| 397 | ((void) buf); |
| 398 | ((void) buf_len); |
| 399 | return( -1 ); |
| 400 | } |
| 401 | |
| 402 | #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit |
| 403 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ |
| 404 | |
| 405 | int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = |
| 406 | MBEDTLS_PLATFORM_STD_NV_SEED_READ; |
| 407 | int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = |
| 408 | MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; |
| 409 | |
| 410 | int mbedtls_platform_set_nv_seed( |
| 411 | int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), |
| 412 | int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) |
| 413 | { |
| 414 | mbedtls_nv_seed_read = nv_seed_read_func; |
| 415 | mbedtls_nv_seed_write = nv_seed_write_func; |
| 416 | return( 0 ); |
| 417 | } |
| 418 | #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ |
| 419 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 420 | |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 421 | #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 422 | /* |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 423 | * Placeholder platform setup that does nothing by default |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 424 | */ |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 425 | int mbedtls_platform_setup( mbedtls_platform_context *ctx ) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 426 | { |
| 427 | (void)ctx; |
| 428 | |
| 429 | return( 0 ); |
| 430 | } |
| 431 | |
| 432 | /* |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 433 | * Placeholder platform teardown that does nothing by default |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 434 | */ |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 435 | void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 436 | { |
| 437 | (void)ctx; |
| 438 | } |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 439 | #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 441 | #endif /* MBEDTLS_PLATFORM_C */ |