[MBEDTLS] Update to v2.3.0. CORE-11534
[reactos.git] / reactos / sdk / include / reactos / libs / mbedtls / platform.h
1 /**
2 * \file platform.h
3 *
4 * \brief mbed TLS Platform abstraction layer
5 *
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23 #ifndef MBEDTLS_PLATFORM_H
24 #define MBEDTLS_PLATFORM_H
25
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /**
37 * \name SECTION: Module settings
38 *
39 * The configuration options you can set for this module are in this section.
40 * Either change them in config.h or define them on the compiler command line.
41 * \{
42 */
43
44 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <time.h>
48 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
49 #if defined(_WIN32)
50 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
51 #else
52 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
53 #endif
54 #endif
55 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
56 #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
57 #endif
58 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
59 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
60 #endif
61 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
62 #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
63 #endif
64 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
65 #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
66 #endif
67 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
68 #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
69 #endif
70 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
71 #define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
72 #endif
73 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
74 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
75 #endif
76 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
77 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
78 #endif
79 #if defined(MBEDTLS_FS_IO)
80 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
81 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
82 #endif
83 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
84 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
85 #endif
86 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
87 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
88 #endif
89 #endif /* MBEDTLS_FS_IO */
90 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
91 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
92 #include MBEDTLS_PLATFORM_STD_MEM_HDR
93 #endif
94 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
95
96
97 /* \} name SECTION: Module settings */
98
99 /*
100 * The function pointers for calloc and free
101 */
102 #if defined(MBEDTLS_PLATFORM_MEMORY)
103 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
104 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
105 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
106 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
107 #else
108 /* For size_t */
109 #include <stddef.h>
110 extern void * (*mbedtls_calloc)( size_t n, size_t size );
111 extern void (*mbedtls_free)( void *ptr );
112
113 /**
114 * \brief Set your own memory implementation function pointers
115 *
116 * \param calloc_func the calloc function implementation
117 * \param free_func the free function implementation
118 *
119 * \return 0 if successful
120 */
121 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
122 void (*free_func)( void * ) );
123 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
124 #else /* !MBEDTLS_PLATFORM_MEMORY */
125 #define mbedtls_free free
126 #define mbedtls_calloc calloc
127 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
128
129 /*
130 * The function pointers for fprintf
131 */
132 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
133 /* We need FILE * */
134 #include <stdio.h>
135 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
136
137 /**
138 * \brief Set your own fprintf function pointer
139 *
140 * \param fprintf_func the fprintf function implementation
141 *
142 * \return 0
143 */
144 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
145 ... ) );
146 #else
147 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
148 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
149 #else
150 #define mbedtls_fprintf fprintf
151 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
152 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
153
154 /*
155 * The function pointers for printf
156 */
157 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
158 extern int (*mbedtls_printf)( const char *format, ... );
159
160 /**
161 * \brief Set your own printf function pointer
162 *
163 * \param printf_func the printf function implementation
164 *
165 * \return 0
166 */
167 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
168 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
169 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
170 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
171 #else
172 #define mbedtls_printf printf
173 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
174 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
175
176 /*
177 * The function pointers for snprintf
178 *
179 * The snprintf implementation should conform to C99:
180 * - it *must* always correctly zero-terminate the buffer
181 * (except when n == 0, then it must leave the buffer untouched)
182 * - however it is acceptable to return -1 instead of the required length when
183 * the destination buffer is too short.
184 */
185 #if defined(_WIN32)
186 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
187 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
188 #endif
189
190 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
191 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
192
193 /**
194 * \brief Set your own snprintf function pointer
195 *
196 * \param snprintf_func the snprintf function implementation
197 *
198 * \return 0
199 */
200 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
201 const char * format, ... ) );
202 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
203 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
204 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
205 #else
206 #define mbedtls_snprintf snprintf
207 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
208 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
209
210 /*
211 * The function pointers for exit
212 */
213 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
214 extern void (*mbedtls_exit)( int status );
215
216 /**
217 * \brief Set your own exit function pointer
218 *
219 * \param exit_func the exit function implementation
220 *
221 * \return 0
222 */
223 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
224 #else
225 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
226 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
227 #else
228 #define mbedtls_exit exit
229 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
230 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
231
232 /*
233 * The default exit values
234 */
235 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
236 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
237 #else
238 #define MBEDTLS_EXIT_SUCCESS 0
239 #endif
240 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
241 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
242 #else
243 #define MBEDTLS_EXIT_FAILURE 1
244 #endif
245
246 /*
247 * The time_t datatype
248 */
249 #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
250 typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
251 #else
252 /* For time_t */
253 #include <time.h>
254 typedef time_t mbedtls_time_t;
255 #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
256
257 /*
258 * The function pointers for time
259 */
260 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
261 extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time );
262
263 /**
264 * \brief Set your own time function pointer
265 *
266 * \param time_func the time function implementation
267 *
268 * \return 0
269 */
270 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) );
271 #else
272 #if defined(MBEDTLS_PLATFORM_TIME_MACRO)
273 #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO
274 #else
275 #define mbedtls_time time
276 #endif /* MBEDTLS_PLATFORM_TIME_MACRO */
277 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
278
279 /*
280 * The function pointers for reading from and writing a seed file to
281 * Non-Volatile storage (NV) in a platform-independent way
282 *
283 * Only enabled when the NV seed entropy source is enabled
284 */
285 #if defined(MBEDTLS_ENTROPY_NV_SEED)
286 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
287 /* Internal standard platform definitions */
288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
289 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
290 #endif
291
292 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
293 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
294 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
295
296 /**
297 * \brief Set your own seed file writing/reading functions
298 *
299 * \param nv_seed_read_func the seed reading function implementation
300 * \param nv_seed_write_func the seed writing function implementation
301 *
302 * \return 0
303 */
304 int mbedtls_platform_set_nv_seed(
305 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
306 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
307 );
308 #else
309 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
310 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
311 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
312 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
313 #else
314 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
315 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
316 #endif
317 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
318 #endif /* MBEDTLS_ENTROPY_NV_SEED */
319
320 #ifdef __cplusplus
321 }
322 #endif
323
324 #endif /* platform.h */