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