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 |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 47 | */ |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 53 | #endif |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_PLATFORM_C) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/platform.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 58 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | a268da9 | 2017-12-20 12:52:49 +0100 | [diff] [blame] | 59 | |
Hanno Becker | cfa2e33 | 2018-10-11 10:26:55 +0100 | [diff] [blame] | 60 | /* The compile time configuration of memory allocation via the macros |
| 61 | * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime |
| 62 | * configuration via mbedtls_platform_set_calloc_free(). So, omit everything |
| 63 | * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */ |
| 64 | #if defined(MBEDTLS_PLATFORM_MEMORY) && \ |
| 65 | !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \ |
| 66 | defined(MBEDTLS_PLATFORM_FREE_MACRO) ) |
| 67 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 68 | #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) |
| 69 | static void *platform_calloc_uninit( size_t n, size_t size ) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 70 | { |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 71 | ((void) n); |
| 72 | ((void) size); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 73 | return( NULL ); |
| 74 | } |
| 75 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 76 | #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit |
| 77 | #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 78 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | #if !defined(MBEDTLS_PLATFORM_STD_FREE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 80 | static void platform_free_uninit( void *ptr ) |
| 81 | { |
| 82 | ((void) ptr); |
| 83 | } |
| 84 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit |
| 86 | #endif /* !MBEDTLS_PLATFORM_STD_FREE */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 87 | |
Roberto Vargas | 7decfe8 | 2018-06-04 13:54:09 +0100 | [diff] [blame] | 88 | static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; |
| 89 | static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE; |
| 90 | |
| 91 | void * mbedtls_calloc( size_t nmemb, size_t size ) |
| 92 | { |
| 93 | return (*mbedtls_calloc_func)( nmemb, size ); |
| 94 | } |
| 95 | |
| 96 | void mbedtls_free( void * ptr ) |
| 97 | { |
| 98 | (*mbedtls_free_func)( ptr ); |
| 99 | } |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 100 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 101 | 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] | 102 | void (*free_func)( void * ) ) |
| 103 | { |
Roberto Vargas | 7decfe8 | 2018-06-04 13:54:09 +0100 | [diff] [blame] | 104 | mbedtls_calloc_func = calloc_func; |
| 105 | mbedtls_free_func = free_func; |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 106 | return( 0 ); |
| 107 | } |
Hanno Becker | cfa2e33 | 2018-10-11 10:26:55 +0100 | [diff] [blame] | 108 | #endif /* MBEDTLS_PLATFORM_MEMORY && |
| 109 | !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && |
| 110 | defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 112 | #if defined(_WIN32) |
| 113 | #include <stdarg.h> |
| 114 | int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) |
| 115 | { |
| 116 | int ret; |
| 117 | va_list argp; |
| 118 | |
Manuel Pégourié-Gonnard | f659d2c | 2015-06-26 17:45:00 +0200 | [diff] [blame] | 119 | /* Avoid calling the invalid parameter handler by checking ourselves */ |
| 120 | if( s == NULL || n == 0 || fmt == NULL ) |
| 121 | return( -1 ); |
| 122 | |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 123 | va_start( argp, fmt ); |
Ron Eldor | bc18eb3 | 2017-09-06 17:49:10 +0300 | [diff] [blame] | 124 | #if defined(_TRUNCATE) && !defined(__MINGW32__) |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 125 | ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); |
Manuel Pégourié-Gonnard | a4f055f | 2015-07-08 16:46:13 +0200 | [diff] [blame] | 126 | #else |
| 127 | ret = _vsnprintf( s, n, fmt, argp ); |
| 128 | if( ret < 0 || (size_t) ret == n ) |
| 129 | { |
| 130 | s[n-1] = '\0'; |
| 131 | ret = -1; |
| 132 | } |
| 133 | #endif |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 134 | va_end( argp ); |
| 135 | |
| 136 | return( ret ); |
| 137 | } |
| 138 | #endif |
| 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) |
| 141 | #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 142 | /* |
| 143 | * Make dummy function to prevent NULL pointer dereferences |
| 144 | */ |
| 145 | static int platform_snprintf_uninit( char * s, size_t n, |
| 146 | const char * format, ... ) |
| 147 | { |
| 148 | ((void) s); |
| 149 | ((void) n); |
Manuel Pégourié-Gonnard | dccb80b | 2015-06-03 10:20:33 +0100 | [diff] [blame] | 150 | ((void) format); |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 151 | return( 0 ); |
| 152 | } |
| 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit |
| 155 | #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 156 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | int (*mbedtls_snprintf)( char * s, size_t n, |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 158 | const char * format, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | 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] | 162 | const char * format, |
| 163 | ... ) ) |
| 164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_snprintf = snprintf_func; |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 166 | return( 0 ); |
| 167 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) |
| 171 | #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 172 | /* |
| 173 | * Make dummy function to prevent NULL pointer dereferences |
| 174 | */ |
| 175 | static int platform_printf_uninit( const char *format, ... ) |
| 176 | { |
| 177 | ((void) format); |
| 178 | return( 0 ); |
| 179 | } |
| 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit |
| 182 | #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ |
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_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; |
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 | int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_printf = printf_func; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 189 | return( 0 ); |
| 190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 192 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) |
| 194 | #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 195 | /* |
| 196 | * Make dummy function to prevent NULL pointer dereferences |
| 197 | */ |
| 198 | static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) |
| 199 | { |
| 200 | ((void) stream); |
| 201 | ((void) format); |
| 202 | return( 0 ); |
| 203 | } |
| 204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit |
| 206 | #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | int (*mbedtls_fprintf)( FILE *, const char *, ... ) = |
| 209 | MBEDTLS_PLATFORM_STD_FPRINTF; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 212 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | mbedtls_fprintf = fprintf_func; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 214 | return( 0 ); |
| 215 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | #if defined(MBEDTLS_PLATFORM_EXIT_ALT) |
| 219 | #if !defined(MBEDTLS_PLATFORM_STD_EXIT) |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 220 | /* |
| 221 | * Make dummy function to prevent NULL pointer dereferences |
| 222 | */ |
| 223 | static void platform_exit_uninit( int status ) |
| 224 | { |
| 225 | ((void) status); |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit |
| 229 | #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 230 | |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 231 | void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 234 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | mbedtls_exit = exit_func; |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 236 | return( 0 ); |
| 237 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 239 | |
Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 240 | #if defined(MBEDTLS_HAVE_TIME) |
| 241 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 242 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
| 243 | #if !defined(MBEDTLS_PLATFORM_STD_TIME) |
| 244 | /* |
| 245 | * Make dummy function to prevent NULL pointer dereferences |
| 246 | */ |
| 247 | static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) |
| 248 | { |
| 249 | ((void) timer); |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 250 | return( 0 ); |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit |
| 254 | #endif /* !MBEDTLS_PLATFORM_STD_TIME */ |
| 255 | |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 256 | mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 257 | |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 258 | 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] | 259 | { |
| 260 | mbedtls_time = time_func; |
| 261 | return( 0 ); |
| 262 | } |
| 263 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ |
| 264 | |
Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 265 | #endif /* MBEDTLS_HAVE_TIME */ |
| 266 | |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 267 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 268 | #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) |
| 269 | /* Default implementations for the platform independent seed functions use |
| 270 | * standard libc file functions to read from and write to a pre-defined filename |
| 271 | */ |
| 272 | int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) |
| 273 | { |
| 274 | FILE *file; |
| 275 | size_t n; |
| 276 | |
| 277 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) |
Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 278 | return( -1 ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 279 | |
| 280 | if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) |
| 281 | { |
| 282 | fclose( file ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 283 | mbedtls_platform_zeroize( buf, buf_len ); |
Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 284 | return( -1 ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | fclose( file ); |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 288 | return( (int)n ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) |
| 292 | { |
| 293 | FILE *file; |
| 294 | size_t n; |
| 295 | |
| 296 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) |
| 297 | return -1; |
| 298 | |
| 299 | if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) |
| 300 | { |
| 301 | fclose( file ); |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | fclose( file ); |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 306 | return( (int)n ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 307 | } |
| 308 | #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ |
| 309 | |
| 310 | #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) |
| 311 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) |
| 312 | /* |
| 313 | * Make dummy function to prevent NULL pointer dereferences |
| 314 | */ |
| 315 | static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) |
| 316 | { |
| 317 | ((void) buf); |
| 318 | ((void) buf_len); |
| 319 | return( -1 ); |
| 320 | } |
| 321 | |
| 322 | #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit |
| 323 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ |
| 324 | |
| 325 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) |
| 326 | /* |
| 327 | * Make dummy function to prevent NULL pointer dereferences |
| 328 | */ |
| 329 | static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) |
| 330 | { |
| 331 | ((void) buf); |
| 332 | ((void) buf_len); |
| 333 | return( -1 ); |
| 334 | } |
| 335 | |
| 336 | #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit |
| 337 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ |
| 338 | |
| 339 | int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = |
| 340 | MBEDTLS_PLATFORM_STD_NV_SEED_READ; |
| 341 | int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = |
| 342 | MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; |
| 343 | |
| 344 | int mbedtls_platform_set_nv_seed( |
| 345 | int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), |
| 346 | int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) |
| 347 | { |
| 348 | mbedtls_nv_seed_read = nv_seed_read_func; |
| 349 | mbedtls_nv_seed_write = nv_seed_write_func; |
| 350 | return( 0 ); |
| 351 | } |
| 352 | #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ |
| 353 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 354 | |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 355 | #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 356 | /* |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 357 | * Placeholder platform setup that does nothing by default |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 358 | */ |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 359 | int mbedtls_platform_setup( mbedtls_platform_context *ctx ) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 360 | { |
| 361 | (void)ctx; |
| 362 | |
| 363 | return( 0 ); |
| 364 | } |
| 365 | |
| 366 | /* |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 367 | * Placeholder platform teardown that does nothing by default |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 368 | */ |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 369 | void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 370 | { |
| 371 | (void)ctx; |
| 372 | } |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 373 | #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | #endif /* MBEDTLS_PLATFORM_C */ |