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