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