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