mbed TLS v2.6.0
platform.h
Go to the documentation of this file.
1 
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 #if defined(MBEDTLS_HAVE_TIME)
35 #include "mbedtls/platform_time.h"
36 #endif
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
50 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <time.h>
54 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
55 #if defined(_WIN32)
56 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
57 #else
58 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
59 #endif
60 #endif
61 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
62 #define MBEDTLS_PLATFORM_STD_PRINTF printf
63 #endif
64 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
65 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
66 #endif
67 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
68 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
69 #endif
70 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
71 #define MBEDTLS_PLATFORM_STD_FREE free
72 #endif
73 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
74 #define MBEDTLS_PLATFORM_STD_EXIT exit
75 #endif
76 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
77 #define MBEDTLS_PLATFORM_STD_TIME time
78 #endif
79 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
80 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
81 #endif
82 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
83 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
84 #endif
85 #if defined(MBEDTLS_FS_IO)
86 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
87 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
88 #endif
89 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
90 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
91 #endif
92 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
93 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
94 #endif
95 #endif /* MBEDTLS_FS_IO */
96 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
97 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
98 #include MBEDTLS_PLATFORM_STD_MEM_HDR
99 #endif
100 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
101 
102 
103 /* \} name SECTION: Module settings */
104 
105 /*
106  * The function pointers for calloc and free
107  */
108 #if defined(MBEDTLS_PLATFORM_MEMORY)
109 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
110  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
111 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
112 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
113 #else
114 /* For size_t */
115 #include <stddef.h>
116 extern void * (*mbedtls_calloc)( size_t n, size_t size );
117 extern void (*mbedtls_free)( void *ptr );
118 
127 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
128  void (*free_func)( void * ) );
129 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
130 #else /* !MBEDTLS_PLATFORM_MEMORY */
131 #define mbedtls_free free
132 #define mbedtls_calloc calloc
133 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
134 
135 /*
136  * The function pointers for fprintf
137  */
138 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
139 /* We need FILE * */
140 #include <stdio.h>
141 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
142 
150 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
151  ... ) );
152 #else
153 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
154 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
155 #else
156 #define mbedtls_fprintf fprintf
157 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
158 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
159 
160 /*
161  * The function pointers for printf
162  */
163 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
164 extern int (*mbedtls_printf)( const char *format, ... );
165 
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
177 #else
178 #define mbedtls_printf printf
179 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
180 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
181 
182 /*
183  * The function pointers for snprintf
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.
190  */
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 
196 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
197 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
198 
206 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
207  const char * format, ... ) );
208 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
209 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
210 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
211 #else
212 #define mbedtls_snprintf snprintf
213 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
214 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
215 
216 /*
217  * The function pointers for exit
218  */
219 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
220 extern void (*mbedtls_exit)( int status );
221 
229 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
230 #else
231 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
232 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
233 #else
234 #define mbedtls_exit exit
235 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
236 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
237 
238 /*
239  * The default exit values
240  */
241 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
242 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
243 #else
244 #define MBEDTLS_EXIT_SUCCESS 0
245 #endif
246 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
247 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
248 #else
249 #define MBEDTLS_EXIT_FAILURE 1
250 #endif
251 
252 /*
253  * The function pointers for reading from and writing a seed file to
254  * Non-Volatile storage (NV) in a platform-independent way
255  *
256  * Only enabled when the NV seed entropy source is enabled
257  */
258 #if defined(MBEDTLS_ENTROPY_NV_SEED)
259 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
260 /* Internal standard platform definitions */
261 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
262 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
263 #endif
264 
265 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
266 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
267 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
268 
277 int mbedtls_platform_set_nv_seed(
278  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
279  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
280  );
281 #else
282 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
283  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
284 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
285 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
286 #else
287 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
288 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
289 #endif
290 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
291 #endif /* MBEDTLS_ENTROPY_NV_SEED */
292 
293 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
294 
301 typedef struct {
302  char dummy;
303 }
305 
306 #else
307 #include "platform_alt.h"
308 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
309 
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 
345 #endif /* platform.h */
char dummy
Placeholder member as empty structs are not portable.
Definition: platform.h:302
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
Perform any platform teardown operations.
#define mbedtls_free
Definition: platform.h:131
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:156
Platform context structure.
Definition: platform.h:301
#define mbedtls_exit
Definition: platform.h:234
#define mbedtls_snprintf
Definition: platform.h:212
#define mbedtls_printf
Definition: platform.h:178
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
Perform any platform initialisation operations.
mbed TLS Platform time abstraction