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