mbed TLS v2.28.2
platform.h
Go to the documentation of this file.
1 
22 /*
23  * Copyright The Mbed TLS Contributors
24  * SPDX-License-Identifier: Apache-2.0
25  *
26  * Licensed under the Apache License, Version 2.0 (the "License"); you may
27  * not use this file except in compliance with the License.
28  * You may obtain a copy of the License at
29  *
30  * http://www.apache.org/licenses/LICENSE-2.0
31  *
32  * Unless required by applicable law or agreed to in writing, software
33  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35  * See the License for the specific language governing permissions and
36  * limitations under the License.
37  */
38 #ifndef MBEDTLS_PLATFORM_H
39 #define MBEDTLS_PLATFORM_H
40 
41 #if !defined(MBEDTLS_CONFIG_FILE)
42 #include "mbedtls/config.h"
43 #else
44 #include MBEDTLS_CONFIG_FILE
45 #endif
46 
47 #if defined(MBEDTLS_HAVE_TIME)
48 #include "mbedtls/platform_time.h"
49 #endif
50 
52 #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
54 #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
68 /* The older Microsoft Windows common runtime provides non-conforming
69  * implementations of some standard library functions, including snprintf
70  * and vsnprintf. This affects MSVC and MinGW builds.
71  */
72 #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
73 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
74 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
75 #endif
76 
77 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
78 #include <stdio.h>
79 #include <stdlib.h>
80 #if defined(MBEDTLS_HAVE_TIME)
81 #include <time.h>
82 #endif
83 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
84 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
85 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
86 #else
87 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
88 #endif
89 #endif
90 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
91 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
92 #define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
93 #else
94 #define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
95 #endif
96 #endif
97 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
98 #define MBEDTLS_PLATFORM_STD_PRINTF printf
99 #endif
100 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
101 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
102 #endif
103 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
104 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
105 #endif
106 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
107 #define MBEDTLS_PLATFORM_STD_FREE free
108 #endif
109 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
110 #define MBEDTLS_PLATFORM_STD_EXIT exit
111 #endif
112 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
113 #define MBEDTLS_PLATFORM_STD_TIME time
114 #endif
115 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
116 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
117 #endif
118 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
119 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
120 #endif
121 #if defined(MBEDTLS_FS_IO)
122 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
123 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
124 #endif
125 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
126 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
127 #endif
128 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
129 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
130 #endif
131 #endif /* MBEDTLS_FS_IO */
132 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
133 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
134 #include MBEDTLS_PLATFORM_STD_MEM_HDR
135 #endif
136 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
137 
138 
141 /*
142  * The function pointers for calloc and free.
143  */
144 #if defined(MBEDTLS_PLATFORM_MEMORY)
145 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
146  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
147 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
148 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
149 #else
150 /* For size_t */
151 #include <stddef.h>
152 extern void *mbedtls_calloc( size_t n, size_t size );
153 extern void mbedtls_free( void *ptr );
154 
164 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
165  void (*free_func)( void * ) );
166 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
167 #else /* !MBEDTLS_PLATFORM_MEMORY */
168 #define mbedtls_free free
169 #define mbedtls_calloc calloc
170 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
171 
172 /*
173  * The function pointers for fprintf
174  */
175 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
176 /* We need FILE * */
177 #include <stdio.h>
178 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
179 
189 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
190  ... ) );
191 #else
192 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
193 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
194 #else
195 #define mbedtls_fprintf fprintf
196 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
197 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
198 
199 /*
200  * The function pointers for printf
201  */
202 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
203 extern int (*mbedtls_printf)( const char *format, ... );
204 
214 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
215 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
216 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
217 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
218 #else
219 #define mbedtls_printf printf
220 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
221 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
222 
223 /*
224  * The function pointers for snprintf
225  *
226  * The snprintf implementation should conform to C99:
227  * - it *must* always correctly zero-terminate the buffer
228  * (except when n == 0, then it must leave the buffer untouched)
229  * - however it is acceptable to return -1 instead of the required length when
230  * the destination buffer is too short.
231  */
232 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
233 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
234 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
235 #endif
236 
237 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
238 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
239 
248 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
249  const char * format, ... ) );
250 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
251 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
252 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
253 #else
254 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
255 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
256 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
257 
258 /*
259  * The function pointers for vsnprintf
260  *
261  * The vsnprintf implementation should conform to C99:
262  * - it *must* always correctly zero-terminate the buffer
263  * (except when n == 0, then it must leave the buffer untouched)
264  * - however it is acceptable to return -1 instead of the required length when
265  * the destination buffer is too short.
266  */
267 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
268 #include <stdarg.h>
269 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
270 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
271 #endif
272 
273 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
274 #include <stdarg.h>
275 extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
276 
284 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
285  const char * format, va_list arg ) );
286 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
287 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
288 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
289 #else
290 #define mbedtls_vsnprintf vsnprintf
291 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
292 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
293 
294 /*
295  * The function pointers for exit
296  */
297 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
298 extern void (*mbedtls_exit)( int status );
299 
309 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
310 #else
311 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
312 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
313 #else
314 #define mbedtls_exit exit
315 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
316 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
317 
318 /*
319  * The default exit values
320  */
321 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
322 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
323 #else
324 #define MBEDTLS_EXIT_SUCCESS 0
325 #endif
326 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
327 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
328 #else
329 #define MBEDTLS_EXIT_FAILURE 1
330 #endif
331 
332 /*
333  * The function pointers for reading from and writing a seed file to
334  * Non-Volatile storage (NV) in a platform-independent way
335  *
336  * Only enabled when the NV seed entropy source is enabled
337  */
338 #if defined(MBEDTLS_ENTROPY_NV_SEED)
339 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
340 /* Internal standard platform definitions */
341 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
342 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
343 #endif
344 
345 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
346 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
347 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
348 
358 int mbedtls_platform_set_nv_seed(
359  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
360  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
361  );
362 #else
363 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
364  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
365 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
366 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
367 #else
368 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
369 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
370 #endif
371 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
372 #endif /* MBEDTLS_ENTROPY_NV_SEED */
373 
374 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
375 
383 {
384  char dummy;
385 }
387 
388 #else
389 #include "platform_alt.h"
390 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
391 
423 
424 #ifdef __cplusplus
425 }
426 #endif
427 
428 #endif /* platform.h */
Configuration options (set of defines)
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:168
#define mbedtls_snprintf
Definition: platform.h:254
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_fprintf
Definition: platform.h:195
#define mbedtls_calloc
Definition: platform.h:169
#define mbedtls_exit
Definition: platform.h:314
#define mbedtls_vsnprintf
Definition: platform.h:290
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
#define mbedtls_printf
Definition: platform.h:219
mbed TLS Platform time abstraction
The platform context structure.
Definition: platform.h:383