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