Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file platform.h |
| 3 | * |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 4 | * \brief This file contains the definitions and functions of the |
| 5 | * Mbed TLS platform abstraction layer. |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 6 | * |
| 7 | * The platform abstraction layer removes the need for the library |
| 8 | * to directly link to standard C library functions or operating |
| 9 | * system services, making the library easier to port and embed. |
| 10 | * Application developers and users of the library can provide their own |
| 11 | * implementations of these functions, or implementations specific to |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 12 | * their platform, which can be statically linked to the library or |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 13 | * dynamically configured at runtime. |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 14 | */ |
| 15 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 16 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 30 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #ifndef MBEDTLS_PLATFORM_H |
| 32 | #define MBEDTLS_PLATFORM_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 33 | #include "mbedtls/private_access.h" |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 34 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 35 | #include "mbedtls/build_info.h" |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 36 | |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 37 | #if defined(MBEDTLS_HAVE_TIME) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 38 | # include "mbedtls/platform_time.h" |
Simon Butcher | b5b6af2 | 2016-07-13 14:46:18 +0100 | [diff] [blame] | 39 | #endif |
| 40 | |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 45 | /** |
| 46 | * \name SECTION: Module settings |
| 47 | * |
| 48 | * The configuration options you can set for this module are in this section. |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 49 | * Either change them in mbedtls_config.h or define them on the compiler command |
| 50 | * line. |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 51 | * \{ |
| 52 | */ |
| 53 | |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 54 | /* The older Microsoft Windows common runtime provides non-conforming |
| 55 | * implementations of some standard library functions, including snprintf |
| 56 | * and vsnprintf. This affects MSVC and MinGW builds. |
| 57 | */ |
Krzysztof Stachowiak | 2cdb6b4 | 2018-10-22 10:43:56 +0200 | [diff] [blame] | 58 | #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 59 | # define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF |
| 60 | # define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 63 | #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 64 | # include <stdio.h> |
| 65 | # include <stdlib.h> |
| 66 | # include <time.h> |
| 67 | # if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) |
| 68 | # if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 69 | /** The default \c snprintf function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 70 | # define MBEDTLS_PLATFORM_STD_SNPRINTF \ |
| 71 | mbedtls_platform_win32_snprintf |
| 72 | # else |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 73 | /** The default \c snprintf function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 74 | # define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf |
| 75 | # endif |
| 76 | # endif |
| 77 | # if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF) |
| 78 | # if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 79 | /** The default \c vsnprintf function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 80 | # define MBEDTLS_PLATFORM_STD_VSNPRINTF \ |
| 81 | mbedtls_platform_win32_vsnprintf |
| 82 | # else |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 83 | /** The default \c vsnprintf function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 84 | # define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf |
| 85 | # endif |
| 86 | # endif |
| 87 | # if !defined(MBEDTLS_PLATFORM_STD_PRINTF) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 88 | /** The default \c printf function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 89 | # define MBEDTLS_PLATFORM_STD_PRINTF printf |
| 90 | # endif |
| 91 | # if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 92 | /** The default \c fprintf function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 93 | # define MBEDTLS_PLATFORM_STD_FPRINTF fprintf |
| 94 | # endif |
| 95 | # if !defined(MBEDTLS_PLATFORM_STD_CALLOC) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 96 | /** The default \c calloc function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 97 | # define MBEDTLS_PLATFORM_STD_CALLOC calloc |
| 98 | # endif |
| 99 | # if !defined(MBEDTLS_PLATFORM_STD_FREE) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 100 | /** The default \c free function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 101 | # define MBEDTLS_PLATFORM_STD_FREE free |
| 102 | # endif |
| 103 | # if !defined(MBEDTLS_PLATFORM_STD_EXIT) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 104 | /** The default \c exit function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 105 | # define MBEDTLS_PLATFORM_STD_EXIT exit |
| 106 | # endif |
| 107 | # if !defined(MBEDTLS_PLATFORM_STD_TIME) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 108 | /** The default \c time function to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 109 | # define MBEDTLS_PLATFORM_STD_TIME time |
| 110 | # endif |
| 111 | # if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 112 | /** The default exit value to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 113 | # define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS |
| 114 | # endif |
| 115 | # if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE) |
Mateusz Starzyk | 16fec33 | 2021-07-22 16:43:35 +0200 | [diff] [blame] | 116 | /** The default exit value to use. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 117 | # define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE |
| 118 | # endif |
| 119 | # if defined(MBEDTLS_FS_IO) |
| 120 | # if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) |
| 121 | # define MBEDTLS_PLATFORM_STD_NV_SEED_READ \ |
| 122 | mbedtls_platform_std_nv_seed_read |
| 123 | # endif |
| 124 | # if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) |
| 125 | # define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE \ |
| 126 | mbedtls_platform_std_nv_seed_write |
| 127 | # endif |
| 128 | # if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE) |
| 129 | # define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile" |
| 130 | # endif |
| 131 | # endif /* MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 133 | # if defined(MBEDTLS_PLATFORM_STD_MEM_HDR) |
| 134 | # include MBEDTLS_PLATFORM_STD_MEM_HDR |
| 135 | # endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 137 | |
| 138 | /* \} name SECTION: Module settings */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 139 | |
| 140 | /* |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 141 | * The function pointers for calloc and free. |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 142 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | #if defined(MBEDTLS_PLATFORM_MEMORY) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 144 | # if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \ |
| 145 | defined(MBEDTLS_PLATFORM_CALLOC_MACRO) |
| 146 | # define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO |
| 147 | # define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO |
| 148 | # else |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 149 | /* For size_t */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 150 | # include <stddef.h> |
| 151 | extern void *mbedtls_calloc(size_t n, size_t size); |
| 152 | extern void mbedtls_free(void *ptr); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 153 | |
| 154 | /** |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 155 | * \brief This function dynamically sets the memory-management |
| 156 | * functions used by the library, during runtime. |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 157 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 158 | * \param calloc_func The \c calloc function implementation. |
| 159 | * \param free_func The \c free function implementation. |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 160 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 161 | * \return \c 0. |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 162 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 163 | int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t), |
| 164 | void (*free_func)(void *)); |
| 165 | # endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | #else /* !MBEDTLS_PLATFORM_MEMORY */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 167 | # define mbedtls_free free |
| 168 | # define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 169 | #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * The function pointers for fprintf |
| 173 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 175 | /* We need FILE * */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 176 | # include <stdio.h> |
| 177 | extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...); |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 178 | |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 179 | /** |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 180 | * \brief This function dynamically configures the fprintf |
| 181 | * function that is called when the |
| 182 | * mbedtls_fprintf() function is invoked by the library. |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 183 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 184 | * \param fprintf_func The \c fprintf function implementation. |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 185 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 186 | * \return \c 0. |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 187 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 188 | int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, |
| 189 | const char *, |
| 190 | ...)); |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 191 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 192 | # if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO) |
| 193 | # define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO |
| 194 | # else |
| 195 | # define mbedtls_fprintf fprintf |
| 196 | # endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 200 | * The function pointers for printf |
| 201 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 203 | extern int (*mbedtls_printf)(const char *format, ...); |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 206 | * \brief This function dynamically configures the snprintf |
| 207 | * function that is called when the mbedtls_snprintf() |
| 208 | * function is invoked by the library. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 209 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 210 | * \param printf_func The \c printf function implementation. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 211 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 212 | * \return \c 0 on success. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 213 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 214 | int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...)); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 216 | # if defined(MBEDTLS_PLATFORM_PRINTF_MACRO) |
| 217 | # define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO |
| 218 | # else |
| 219 | # define mbedtls_printf printf |
| 220 | # endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * The function pointers for snprintf |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 225 | * |
| 226 | * The snprintf implementation should conform to C99: |
| 227 | * - it *must* always correctly zero-terminate the buffer |
| 228 | * (except when n == 0, then it must leave the buffer untouched) |
| 229 | * - however it is acceptable to return -1 instead of the required length when |
| 230 | * the destination buffer is too short. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 231 | */ |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 232 | #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF) |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 233 | /* For Windows (inc. MSYS2), we provide our own fixed implementation */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 234 | 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] | 235 | #endif |
| 236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 238 | extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...); |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 239 | |
| 240 | /** |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 241 | * \brief This function allows configuring a custom |
| 242 | * \c snprintf function pointer. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 243 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 244 | * \param snprintf_func The \c snprintf function implementation. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 245 | * |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 246 | * \return \c 0 on success. |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 247 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 248 | int mbedtls_platform_set_snprintf( |
| 249 | int (*snprintf_func)(char *s, size_t n, const char *format, ...)); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 251 | # if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) |
| 252 | # define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO |
| 253 | # else |
| 254 | # define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF |
| 255 | # endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ |
Rich Evans | 16f8cd8 | 2015-02-06 16:14:34 +0000 | [diff] [blame] | 257 | |
| 258 | /* |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 259 | * The function pointers for vsnprintf |
| 260 | * |
| 261 | * The vsnprintf implementation should conform to C99: |
| 262 | * - it *must* always correctly zero-terminate the buffer |
| 263 | * (except when n == 0, then it must leave the buffer untouched) |
| 264 | * - however it is acceptable to return -1 instead of the required length when |
| 265 | * the destination buffer is too short. |
| 266 | */ |
| 267 | #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 268 | # include <stdarg.h> |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 269 | /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 270 | int mbedtls_platform_win32_vsnprintf(char *s, |
| 271 | size_t n, |
| 272 | const char *fmt, |
| 273 | va_list arg); |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 274 | #endif |
| 275 | |
| 276 | #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 277 | # include <stdarg.h> |
| 278 | extern int (*mbedtls_vsnprintf)(char *s, |
| 279 | size_t n, |
| 280 | const char *format, |
| 281 | va_list arg); |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 282 | |
| 283 | /** |
| 284 | * \brief Set your own snprintf function pointer |
| 285 | * |
Krzysztof Stachowiak | 1555716 | 2018-09-24 14:15:46 +0200 | [diff] [blame] | 286 | * \param vsnprintf_func The \c vsnprintf function implementation |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 287 | * |
Krzysztof Stachowiak | 1555716 | 2018-09-24 14:15:46 +0200 | [diff] [blame] | 288 | * \return \c 0 |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 289 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 290 | int mbedtls_platform_set_vsnprintf( |
| 291 | int (*vsnprintf_func)(char *s, size_t n, const char *format, va_list arg)); |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 292 | #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 293 | # if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO) |
| 294 | # define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO |
| 295 | # else |
| 296 | # define mbedtls_vsnprintf vsnprintf |
| 297 | # endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */ |
k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 298 | #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */ |
| 299 | |
| 300 | /* |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 301 | * The function pointers for exit |
| 302 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | #if defined(MBEDTLS_PLATFORM_EXIT_ALT) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 304 | extern void (*mbedtls_exit)(int status); |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 305 | |
| 306 | /** |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 307 | * \brief This function dynamically configures the exit |
| 308 | * function that is called when the mbedtls_exit() |
| 309 | * function is invoked by the library. |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 310 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 311 | * \param exit_func The \c exit function implementation. |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 312 | * |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 313 | * \return \c 0 on success. |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 314 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 315 | int mbedtls_platform_set_exit(void (*exit_func)(int status)); |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 316 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 317 | # if defined(MBEDTLS_PLATFORM_EXIT_MACRO) |
| 318 | # define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO |
| 319 | # else |
| 320 | # define mbedtls_exit exit |
| 321 | # endif /* MBEDTLS_PLATFORM_EXIT_MACRO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 322 | #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 323 | |
Janos Follath | 9194744 | 2016-03-18 13:49:27 +0000 | [diff] [blame] | 324 | /* |
| 325 | * The default exit values |
| 326 | */ |
| 327 | #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 328 | # define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS |
Janos Follath | 9194744 | 2016-03-18 13:49:27 +0000 | [diff] [blame] | 329 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 330 | # define MBEDTLS_EXIT_SUCCESS 0 |
Janos Follath | 9194744 | 2016-03-18 13:49:27 +0000 | [diff] [blame] | 331 | #endif |
| 332 | #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 333 | # define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE |
Janos Follath | 9194744 | 2016-03-18 13:49:27 +0000 | [diff] [blame] | 334 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 335 | # define MBEDTLS_EXIT_FAILURE 1 |
Janos Follath | 9194744 | 2016-03-18 13:49:27 +0000 | [diff] [blame] | 336 | #endif |
| 337 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 338 | /* |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 339 | * The function pointers for reading from and writing a seed file to |
| 340 | * Non-Volatile storage (NV) in a platform-independent way |
| 341 | * |
| 342 | * Only enabled when the NV seed entropy source is enabled |
| 343 | */ |
| 344 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 345 | # if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 346 | /* Internal standard platform definitions */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 347 | int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len); |
| 348 | int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len); |
| 349 | # endif |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 350 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 351 | # if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) |
| 352 | extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len); |
| 353 | extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 354 | |
| 355 | /** |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 356 | * \brief This function allows configuring custom seed file writing and |
| 357 | * reading functions. |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 358 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 359 | * \param nv_seed_read_func The seed reading function implementation. |
| 360 | * \param nv_seed_write_func The seed writing function implementation. |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 361 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 362 | * \return \c 0 on success. |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 363 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 364 | int mbedtls_platform_set_nv_seed(int (*nv_seed_read_func)(unsigned char *buf, |
| 365 | size_t buf_len), |
| 366 | int (*nv_seed_write_func)(unsigned char *buf, |
| 367 | size_t buf_len)); |
| 368 | # else |
| 369 | # if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \ |
| 370 | defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO) |
| 371 | # define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO |
| 372 | # define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO |
| 373 | # else |
| 374 | # define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read |
| 375 | # define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write |
| 376 | # endif |
| 377 | # endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 378 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 379 | |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 380 | #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 381 | |
Andres Amaya Garcia | 64b02cd | 2017-07-12 11:32:40 +0100 | [diff] [blame] | 382 | /** |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 383 | * \brief The platform context structure. |
Andres Amaya Garcia | 64b02cd | 2017-07-12 11:32:40 +0100 | [diff] [blame] | 384 | * |
| 385 | * \note This structure may be used to assist platform-specific |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 386 | * setup or teardown operations. |
Andres Amaya Garcia | 64b02cd | 2017-07-12 11:32:40 +0100 | [diff] [blame] | 387 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 388 | typedef struct mbedtls_platform_context { |
| 389 | char MBEDTLS_PRIVATE(dummy); /**< A placeholder member, as empty structs are |
| 390 | not portable. */ |
| 391 | } mbedtls_platform_context; |
Andres Amaya Garcia | 64b02cd | 2017-07-12 11:32:40 +0100 | [diff] [blame] | 392 | |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 393 | #else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 394 | # include "platform_alt.h" |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 395 | #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 396 | |
| 397 | /** |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 398 | * \brief This function performs any platform-specific initialization |
| 399 | * operations. |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 400 | * |
| 401 | * \note This function should be called before any other library functions. |
| 402 | * |
| 403 | * Its implementation is platform-specific, and unless |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 404 | * platform-specific code is provided, it does nothing. |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 405 | * |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 406 | * \note The usage and necessity of this function is dependent on the |
| 407 | * platform. |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 408 | * |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 409 | * \param ctx The platform context. |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 410 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 411 | * \return \c 0 on success. |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 412 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 413 | int mbedtls_platform_setup(mbedtls_platform_context *ctx); |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 414 | /** |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 415 | * \brief This function performs any platform teardown operations. |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 416 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 417 | * \note This function should be called after every other Mbed TLS module |
| 418 | * has been correctly freed using the appropriate free function. |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 419 | * |
Rose Zadik | 332658d | 2018-01-25 22:02:53 +0000 | [diff] [blame] | 420 | * Its implementation is platform-specific, and unless |
| 421 | * platform-specific code is provided, it does nothing. |
Simon Butcher | d3be27a | 2017-07-21 02:08:00 +0200 | [diff] [blame] | 422 | * |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 423 | * \note The usage and necessity of this function is dependent on the |
| 424 | * platform. |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 425 | * |
Rose Zadik | 9464d7b | 2018-04-16 15:28:35 +0100 | [diff] [blame] | 426 | * \param ctx The platform context. |
Rose Zadik | 4bca2b0 | 2018-03-27 13:12:52 +0100 | [diff] [blame] | 427 | * |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 428 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 429 | void mbedtls_platform_teardown(mbedtls_platform_context *ctx); |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 430 | |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 431 | #ifdef __cplusplus |
| 432 | } |
| 433 | #endif |
| 434 | |
| 435 | #endif /* platform.h */ |