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