| 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" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 31 | #include "mbedtls/platform_util.h" | 
| Manuel Pégourié-Gonnard | a268da9 | 2017-12-20 12:52:49 +0100 | [diff] [blame] | 32 |  | 
| Hanno Becker | cfa2e33 | 2018-10-11 10:26:55 +0100 | [diff] [blame] | 33 | /* The compile time configuration of memory allocation via the macros | 
|  | 34 | * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime | 
|  | 35 | * configuration via mbedtls_platform_set_calloc_free(). So, omit everything | 
|  | 36 | * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */ | 
|  | 37 | #if defined(MBEDTLS_PLATFORM_MEMORY) &&                 \ | 
|  | 38 | !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&        \ | 
|  | 39 | defined(MBEDTLS_PLATFORM_FREE_MACRO) ) | 
|  | 40 |  | 
| 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 |  | 
| Roberto Vargas | 7decfe8 | 2018-06-04 13:54:09 +0100 | [diff] [blame] | 61 | static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; | 
|  | 62 | static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE; | 
|  | 63 |  | 
|  | 64 | void * mbedtls_calloc( size_t nmemb, size_t size ) | 
|  | 65 | { | 
|  | 66 | return (*mbedtls_calloc_func)( nmemb, size ); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | void mbedtls_free( void * ptr ) | 
|  | 70 | { | 
|  | 71 | (*mbedtls_free_func)( ptr ); | 
|  | 72 | } | 
| Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 73 |  | 
| Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 74 | 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] | 75 | void (*free_func)( void * ) ) | 
|  | 76 | { | 
| Roberto Vargas | 7decfe8 | 2018-06-04 13:54:09 +0100 | [diff] [blame] | 77 | mbedtls_calloc_func = calloc_func; | 
|  | 78 | mbedtls_free_func = free_func; | 
| Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 79 | return( 0 ); | 
|  | 80 | } | 
| Hanno Becker | cfa2e33 | 2018-10-11 10:26:55 +0100 | [diff] [blame] | 81 | #endif /* MBEDTLS_PLATFORM_MEMORY && | 
|  | 82 | !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && | 
|  | 83 | defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */ | 
| Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 84 |  | 
| Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 85 | #if defined(_WIN32) | 
|  | 86 | #include <stdarg.h> | 
|  | 87 | int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) | 
|  | 88 | { | 
|  | 89 | int ret; | 
|  | 90 | va_list argp; | 
|  | 91 |  | 
| Manuel Pégourié-Gonnard | f659d2c | 2015-06-26 17:45:00 +0200 | [diff] [blame] | 92 | /* Avoid calling the invalid parameter handler by checking ourselves */ | 
|  | 93 | if( s == NULL || n == 0 || fmt == NULL ) | 
|  | 94 | return( -1 ); | 
|  | 95 |  | 
| Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 96 | va_start( argp, fmt ); | 
| Ron Eldor | bc18eb3 | 2017-09-06 17:49:10 +0300 | [diff] [blame] | 97 | #if defined(_TRUNCATE) && !defined(__MINGW32__) | 
| Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 98 | ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); | 
| Manuel Pégourié-Gonnard | a4f055f | 2015-07-08 16:46:13 +0200 | [diff] [blame] | 99 | #else | 
|  | 100 | ret = _vsnprintf( s, n, fmt, argp ); | 
|  | 101 | if( ret < 0 || (size_t) ret == n ) | 
|  | 102 | { | 
|  | 103 | s[n-1] = '\0'; | 
|  | 104 | ret = -1; | 
|  | 105 | } | 
|  | 106 | #endif | 
| Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 107 | va_end( argp ); | 
|  | 108 |  | 
|  | 109 | return( ret ); | 
|  | 110 | } | 
|  | 111 | #endif | 
|  | 112 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) | 
|  | 114 | #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 115 | /* | 
|  | 116 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 117 | */ | 
|  | 118 | static int platform_snprintf_uninit( char * s, size_t n, | 
|  | 119 | const char * format, ... ) | 
|  | 120 | { | 
|  | 121 | ((void) s); | 
|  | 122 | ((void) n); | 
| Manuel Pégourié-Gonnard | dccb80b | 2015-06-03 10:20:33 +0100 | [diff] [blame] | 123 | ((void) format); | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 124 | return( 0 ); | 
|  | 125 | } | 
|  | 126 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit | 
|  | 128 | #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 129 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | int (*mbedtls_snprintf)( char * s, size_t n, | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 131 | const char * format, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | 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] | 135 | const char * format, | 
|  | 136 | ... ) ) | 
|  | 137 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | mbedtls_snprintf = snprintf_func; | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 139 | return( 0 ); | 
|  | 140 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ | 
| Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 142 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) | 
|  | 144 | #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 145 | /* | 
|  | 146 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 147 | */ | 
|  | 148 | static int platform_printf_uninit( const char *format, ... ) | 
|  | 149 | { | 
|  | 150 | ((void) format); | 
|  | 151 | return( 0 ); | 
|  | 152 | } | 
|  | 153 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit | 
|  | 155 | #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 156 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 158 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 160 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | mbedtls_printf = printf_func; | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 162 | return( 0 ); | 
|  | 163 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 165 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) | 
|  | 167 | #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 168 | /* | 
|  | 169 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 170 | */ | 
|  | 171 | static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) | 
|  | 172 | { | 
|  | 173 | ((void) stream); | 
|  | 174 | ((void) format); | 
|  | 175 | return( 0 ); | 
|  | 176 | } | 
|  | 177 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit | 
|  | 179 | #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 180 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | int (*mbedtls_fprintf)( FILE *, const char *, ... ) = | 
|  | 182 | MBEDTLS_PLATFORM_STD_FPRINTF; | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 183 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 185 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | mbedtls_fprintf = fprintf_func; | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 187 | return( 0 ); | 
|  | 188 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ | 
| Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 190 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | #if defined(MBEDTLS_PLATFORM_EXIT_ALT) | 
|  | 192 | #if !defined(MBEDTLS_PLATFORM_STD_EXIT) | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 193 | /* | 
|  | 194 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 195 | */ | 
|  | 196 | static void platform_exit_uninit( int status ) | 
|  | 197 | { | 
|  | 198 | ((void) status); | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit | 
|  | 202 | #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 203 |  | 
| Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 204 | void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 205 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 207 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | mbedtls_exit = exit_func; | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 209 | return( 0 ); | 
|  | 210 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ | 
| Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 212 |  | 
| Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 213 | #if defined(MBEDTLS_HAVE_TIME) | 
|  | 214 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 215 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) | 
|  | 216 | #if !defined(MBEDTLS_PLATFORM_STD_TIME) | 
|  | 217 | /* | 
|  | 218 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 219 | */ | 
|  | 220 | static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) | 
|  | 221 | { | 
|  | 222 | ((void) timer); | 
| Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 223 | return( 0 ); | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
|  | 226 | #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit | 
|  | 227 | #endif /* !MBEDTLS_PLATFORM_STD_TIME */ | 
|  | 228 |  | 
| Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 229 | mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 230 |  | 
| Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 231 | 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] | 232 | { | 
|  | 233 | mbedtls_time = time_func; | 
|  | 234 | return( 0 ); | 
|  | 235 | } | 
|  | 236 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ | 
|  | 237 |  | 
| Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 238 | #endif /* MBEDTLS_HAVE_TIME */ | 
|  | 239 |  | 
| Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 240 | #if defined(MBEDTLS_ENTROPY_NV_SEED) | 
|  | 241 | #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) | 
|  | 242 | /* Default implementations for the platform independent seed functions use | 
|  | 243 | * standard libc file functions to read from and write to a pre-defined filename | 
|  | 244 | */ | 
|  | 245 | int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) | 
|  | 246 | { | 
|  | 247 | FILE *file; | 
|  | 248 | size_t n; | 
|  | 249 |  | 
|  | 250 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) | 
| Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 251 | return( -1 ); | 
| Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 252 |  | 
|  | 253 | if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) | 
|  | 254 | { | 
|  | 255 | fclose( file ); | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 256 | mbedtls_platform_zeroize( buf, buf_len ); | 
| Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 257 | return( -1 ); | 
| Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | fclose( file ); | 
| Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 261 | return( (int)n ); | 
| Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
|  | 264 | int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) | 
|  | 265 | { | 
|  | 266 | FILE *file; | 
|  | 267 | size_t n; | 
|  | 268 |  | 
|  | 269 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) | 
|  | 270 | return -1; | 
|  | 271 |  | 
|  | 272 | if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) | 
|  | 273 | { | 
|  | 274 | fclose( file ); | 
|  | 275 | return -1; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | fclose( file ); | 
| Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 279 | return( (int)n ); | 
| Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 280 | } | 
|  | 281 | #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ | 
|  | 282 |  | 
|  | 283 | #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) | 
|  | 284 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) | 
|  | 285 | /* | 
|  | 286 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 287 | */ | 
|  | 288 | static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) | 
|  | 289 | { | 
|  | 290 | ((void) buf); | 
|  | 291 | ((void) buf_len); | 
|  | 292 | return( -1 ); | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit | 
|  | 296 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ | 
|  | 297 |  | 
|  | 298 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) | 
|  | 299 | /* | 
|  | 300 | * Make dummy function to prevent NULL pointer dereferences | 
|  | 301 | */ | 
|  | 302 | static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) | 
|  | 303 | { | 
|  | 304 | ((void) buf); | 
|  | 305 | ((void) buf_len); | 
|  | 306 | return( -1 ); | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit | 
|  | 310 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ | 
|  | 311 |  | 
|  | 312 | int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = | 
|  | 313 | MBEDTLS_PLATFORM_STD_NV_SEED_READ; | 
|  | 314 | int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = | 
|  | 315 | MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; | 
|  | 316 |  | 
|  | 317 | int mbedtls_platform_set_nv_seed( | 
|  | 318 | int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), | 
|  | 319 | int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) | 
|  | 320 | { | 
|  | 321 | mbedtls_nv_seed_read = nv_seed_read_func; | 
|  | 322 | mbedtls_nv_seed_write = nv_seed_write_func; | 
|  | 323 | return( 0 ); | 
|  | 324 | } | 
|  | 325 | #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ | 
|  | 326 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ | 
|  | 327 |  | 
| Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 328 | #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) | 
| Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 329 | /* | 
| Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 330 | * Placeholder platform setup that does nothing by default | 
| Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 331 | */ | 
| Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 332 | int mbedtls_platform_setup( mbedtls_platform_context *ctx ) | 
| Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 333 | { | 
|  | 334 | (void)ctx; | 
|  | 335 |  | 
|  | 336 | return( 0 ); | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | /* | 
| Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 340 | * Placeholder platform teardown that does nothing by default | 
| Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 341 | */ | 
| Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 342 | void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) | 
| Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 343 | { | 
|  | 344 | (void)ctx; | 
|  | 345 | } | 
| Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 346 | #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ | 
| Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 347 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | #endif /* MBEDTLS_PLATFORM_C */ |