Mbed TLS v4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
platform.h
Go to the documentation of this file.
1 
22 /*
23  * Copyright The Mbed TLS Contributors
24  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
25  */
26 #ifndef MBEDTLS_PLATFORM_H
27 #define MBEDTLS_PLATFORM_H
28 #include "mbedtls/private_access.h"
29 
31 
32 #if defined(MBEDTLS_HAVE_TIME)
33 #include "mbedtls/platform_time.h"
34 #endif
35 
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
50 /* The older Microsoft Windows common runtime provides non-conforming
51  * implementations of some standard library functions, including snprintf
52  * and vsnprintf. This affects MSVC and MinGW builds.
53  */
54 #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
55 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
56 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
57 #endif
58 
59 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
60 #include <stdio.h>
61 #include <stdlib.h>
62 #if defined(MBEDTLS_HAVE_TIME)
63 #include <time.h>
64 #endif
65 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
66 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
67 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
68 #else
69 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
70 #endif
71 #endif
72 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
73 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
74 #define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
75 #else
76 #define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
77 #endif
78 #endif
79 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
80 #define MBEDTLS_PLATFORM_STD_PRINTF printf
81 #endif
82 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
83 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
84 #endif
85 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
86 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
87 #endif
88 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
89 #define MBEDTLS_PLATFORM_STD_FREE free
90 #endif
91 #if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
92 #define MBEDTLS_PLATFORM_STD_SETBUF setbuf
93 #endif
94 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
95 #define MBEDTLS_PLATFORM_STD_EXIT exit
96 #endif
97 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
98 #define MBEDTLS_PLATFORM_STD_TIME time
99 #endif
100 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
101 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
102 #endif
103 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
104 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
105 #endif
106 #if defined(MBEDTLS_FS_IO)
107 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
108 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
109 #endif
110 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
111 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
112 #endif
113 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
114 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
115 #endif
116 #endif /* MBEDTLS_FS_IO */
117 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
118 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
119 #include MBEDTLS_PLATFORM_STD_MEM_HDR
120 #endif
121 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
122 
123 /* Enable certain documented defines only when generating doxygen to avoid
124  * an "unrecognized define" error. */
125 #if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
126 #define MBEDTLS_PLATFORM_STD_CALLOC
127 #endif
128 
129 #if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
130 #define MBEDTLS_PLATFORM_STD_FREE
131 #endif
132 
135 /*
136  * The function pointers for calloc and free.
137  * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
138  * in mbedtls_config.h for more information about behaviour and requirements.
139  */
140 #if defined(MBEDTLS_PLATFORM_MEMORY)
141 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
142  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
143 #undef mbedtls_free
144 #undef mbedtls_calloc
145 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
146 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
147 #else
148 /* For size_t */
149 #include <stddef.h>
150 extern void *mbedtls_calloc(size_t n, size_t size);
151 extern void mbedtls_free(void *ptr);
152 
162 int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
163  void (*free_func)(void *));
164 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
165 #else /* !MBEDTLS_PLATFORM_MEMORY */
166 #undef mbedtls_free
167 #undef mbedtls_calloc
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 #undef mbedtls_fprintf
193 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
194 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
195 #else
196 #define mbedtls_fprintf fprintf
197 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
198 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
199 
200 /*
201  * The function pointers for printf
202  */
203 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
204 extern int (*mbedtls_printf)(const char *format, ...);
205 
215 int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
216 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
217 #undef mbedtls_printf
218 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
219 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
220 #else
221 #define mbedtls_printf printf
222 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
223 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
224 
225 /*
226  * The function pointers for snprintf
227  *
228  * The snprintf implementation should conform to C99:
229  * - it *must* always correctly zero-terminate the buffer
230  * (except when n == 0, then it must leave the buffer untouched)
231  * - however it is acceptable to return -1 instead of the required length when
232  * the destination buffer is too short.
233  */
234 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
235 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
236 int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
237 #endif
238 
239 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
240 extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
241 
250 int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
251  const char *format, ...));
252 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
253 #undef mbedtls_snprintf
254 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
255 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
256 #else
257 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
258 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
259 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
260 
261 /*
262  * The function pointers for vsnprintf
263  *
264  * The vsnprintf implementation should conform to C99:
265  * - it *must* always correctly zero-terminate the buffer
266  * (except when n == 0, then it must leave the buffer untouched)
267  * - however it is acceptable to return -1 instead of the required length when
268  * the destination buffer is too short.
269  */
270 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
271 #include <stdarg.h>
272 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
273 int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
274 #endif
275 
276 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
277 #include <stdarg.h>
278 extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
279 
287 int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
288  const char *format, va_list arg));
289 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
290 #undef mbedtls_vsnprintf
291 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
292 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
293 #else
294 #define mbedtls_vsnprintf vsnprintf
295 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
296 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
297 
298 /*
299  * The function pointers for setbuf
300  */
301 #if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
302 #include <stdio.h>
319 extern void (*mbedtls_setbuf)(FILE *stream, char *buf);
320 
330 int mbedtls_platform_set_setbuf(void (*setbuf_func)(
331  FILE *stream, char *buf));
332 #else
333 #undef mbedtls_setbuf
334 #if defined(MBEDTLS_PLATFORM_SETBUF_MACRO)
335 
345 #define mbedtls_setbuf MBEDTLS_PLATFORM_SETBUF_MACRO
346 #else
347 #define mbedtls_setbuf setbuf
348 #endif /* MBEDTLS_PLATFORM_SETBUF_MACRO */
349 #endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
350 
351 /*
352  * The function pointers for exit
353  */
354 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
355 extern void (*mbedtls_exit)(int status);
356 
366 int mbedtls_platform_set_exit(void (*exit_func)(int status));
367 #else
368 #undef mbedtls_exit
369 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
370 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
371 #else
372 #define mbedtls_exit exit
373 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
374 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
375 
376 /*
377  * The default exit values
378  */
379 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
380 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
381 #else
382 #define MBEDTLS_EXIT_SUCCESS 0
383 #endif
384 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
385 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
386 #else
387 #define MBEDTLS_EXIT_FAILURE 1
388 #endif
389 
390 /*
391  * The function pointers for reading from and writing a seed file to
392  * Non-Volatile storage (NV) in a platform-independent way
393  *
394  * Only enabled when the NV seed entropy source is enabled
395  */
396 #if defined(MBEDTLS_ENTROPY_NV_SEED)
397 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
398 /* Internal standard platform definitions */
399 int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
400 int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
401 #endif
402 
403 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
404 extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
405 extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
406 
417  int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
418  int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
419  );
420 #else
421 #undef mbedtls_nv_seed_read
422 #undef mbedtls_nv_seed_write
423 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
424  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
425 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
426 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
427 #else
428 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
429 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
430 #endif
431 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
432 #endif /* MBEDTLS_ENTROPY_NV_SEED */
433 
434 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
435 
442 typedef struct mbedtls_platform_context {
443  char MBEDTLS_PRIVATE(dummy);
444 }
446 
447 #else
448 #include "platform_alt.h"
449 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
450 
487  size_t *estimate_bits,
488  unsigned char *output, size_t output_size);
489 
521 
522 #ifdef __cplusplus
523 }
524 #endif
525 
526 #endif /* platform.h */
uint32_t psa_driver_get_entropy_flags_t
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_calloc
Definition: platform.h:146
#define mbedtls_free
Definition: platform.h:145
Definitions for PSA random and entropy drivers.
int mbedtls_platform_set_exit(void(*exit_func)(int status))
This function dynamically configures the exit function that is called when the mbedtls_exit() functio...
void(* mbedtls_setbuf)(FILE *stream, char *buf)
Function pointer to call for setbuf() functionality (changing the internal buffering on stdio calls)...
int mbedtls_platform_set_printf(int(*printf_func)(const char *,...))
This function dynamically configures the snprintf function that is called when the mbedtls_snprintf()...
int mbedtls_platform_set_vsnprintf(int(*vsnprintf_func)(char *s, size_t n, const char *format, va_list arg))
Set your own snprintf function pointer.
int mbedtls_platform_set_fprintf(int(*fprintf_func)(FILE *stream, const char *,...))
This function dynamically configures the fprintf function that is called when the mbedtls_fprintf() f...
int(* mbedtls_printf)(const char *format,...)
int(* mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg)
int(* mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len)
struct mbedtls_platform_context mbedtls_platform_context
int mbedtls_platform_set_snprintf(int(*snprintf_func)(char *s, size_t n, const char *format,...))
This function allows configuring a custom snprintf function pointer.
int(* mbedtls_fprintf)(FILE *stream, const char *format,...)
void(* mbedtls_exit)(int status)
int mbedtls_platform_set_nv_seed(int(*nv_seed_read_func)(unsigned char *buf, size_t buf_len), int(*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
This function allows configuring custom seed file writing and reading functions.
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
Build-time configuration info.
int(* mbedtls_snprintf)(char *s, size_t n, const char *format,...)
Mbed TLS Platform time abstraction.
int mbedtls_platform_set_setbuf(void(*setbuf_func)(FILE *stream, char *buf))
Dynamically configure the function that is called when the mbedtls_setbuf() function is called by the...
int mbedtls_platform_get_entropy(psa_driver_get_entropy_flags_t flags, size_t *estimate_bits, unsigned char *output, size_t output_size)
User defined callback function that is used from the entropy module to gather entropy data from some ...
int(* mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len)