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