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