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