Mbed TLS v3.5.0
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
41
42#include "mbedtls/build_info.h"
43
44#if defined(MBEDTLS_HAVE_TIME)
46#endif
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
60/* The older Microsoft Windows common runtime provides non-conforming
61 * implementations of some standard library functions, including snprintf
62 * and vsnprintf. This affects MSVC and MinGW builds.
63 */
64#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
65#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
66#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
67#endif
68
69#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
70#include <stdio.h>
71#include <stdlib.h>
72#if defined(MBEDTLS_HAVE_TIME)
73#include <time.h>
74#endif
75#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
76#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
77#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
78#else
79#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
80#endif
81#endif
82#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
83#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
84#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
85#else
86#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
87#endif
88#endif
89#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
90#define MBEDTLS_PLATFORM_STD_PRINTF printf
91#endif
92#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
93#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
94#endif
95#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
96#define MBEDTLS_PLATFORM_STD_CALLOC calloc
97#endif
98#if !defined(MBEDTLS_PLATFORM_STD_FREE)
99#define MBEDTLS_PLATFORM_STD_FREE free
100#endif
101#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
102#define MBEDTLS_PLATFORM_STD_SETBUF setbuf
103#endif
104#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
105#define MBEDTLS_PLATFORM_STD_EXIT exit
106#endif
107#if !defined(MBEDTLS_PLATFORM_STD_TIME)
108#define MBEDTLS_PLATFORM_STD_TIME time
109#endif
110#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
111#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
112#endif
113#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
114#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
115#endif
116#if defined(MBEDTLS_FS_IO)
117#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
118#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
119#endif
120#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
121#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
122#endif
123#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
124#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
125#endif
126#endif /* MBEDTLS_FS_IO */
127#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
128#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
129#include MBEDTLS_PLATFORM_STD_MEM_HDR
130#endif
131#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
132
133/* Enable certain documented defines only when generating doxygen to avoid
134 * an "unrecognized define" error. */
135#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
136#define MBEDTLS_PLATFORM_STD_CALLOC
137#endif
138
139#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
140#define MBEDTLS_PLATFORM_STD_FREE
141#endif
142
145/*
146 * The function pointers for calloc and free.
147 * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
148 * in mbedtls_config.h for more information about behaviour and requirements.
149 */
150#if defined(MBEDTLS_PLATFORM_MEMORY)
151#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
152 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
153#undef mbedtls_free
154#undef mbedtls_calloc
155#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
156#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
157#else
158/* For size_t */
159#include <stddef.h>
160extern void *mbedtls_calloc(size_t n, size_t size);
161extern void mbedtls_free(void *ptr);
162
172int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
173 void (*free_func)(void *));
174#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
175#else /* !MBEDTLS_PLATFORM_MEMORY */
176#undef mbedtls_free
177#undef mbedtls_calloc
178#define mbedtls_free free
179#define mbedtls_calloc calloc
180#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
181
182/*
183 * The function pointers for fprintf
184 */
185#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
186/* We need FILE * */
187#include <stdio.h>
188extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
189
199int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
200 ...));
201#else
202#undef mbedtls_fprintf
203#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
204#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
205#else
206#define mbedtls_fprintf fprintf
207#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
208#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
209
210/*
211 * The function pointers for printf
212 */
213#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
214extern int (*mbedtls_printf)(const char *format, ...);
215
225int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
226#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
227#undef mbedtls_printf
228#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
229#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
230#else
231#define mbedtls_printf printf
232#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
233#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
234
235/*
236 * The function pointers for snprintf
237 *
238 * The snprintf implementation should conform to C99:
239 * - it *must* always correctly zero-terminate the buffer
240 * (except when n == 0, then it must leave the buffer untouched)
241 * - however it is acceptable to return -1 instead of the required length when
242 * the destination buffer is too short.
243 */
244#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
245/* For Windows (inc. MSYS2), we provide our own fixed implementation */
246int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
247#endif
248
249#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
250extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
251
260int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
261 const char *format, ...));
262#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
263#undef mbedtls_snprintf
264#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
265#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
266#else
267#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
268#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
269#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
270
271/*
272 * The function pointers for vsnprintf
273 *
274 * The vsnprintf implementation should conform to C99:
275 * - it *must* always correctly zero-terminate the buffer
276 * (except when n == 0, then it must leave the buffer untouched)
277 * - however it is acceptable to return -1 instead of the required length when
278 * the destination buffer is too short.
279 */
280#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
281#include <stdarg.h>
282/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
283int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
284#endif
285
286#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
287#include <stdarg.h>
288extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
289
297int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
298 const char *format, va_list arg));
299#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
300#undef mbedtls_vsnprintf
301#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
302#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
303#else
304#define mbedtls_vsnprintf vsnprintf
305#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
306#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
307
308/*
309 * The function pointers for setbuf
310 */
311#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
312#include <stdio.h>
329extern void (*mbedtls_setbuf)(FILE *stream, char *buf);
330
340int mbedtls_platform_set_setbuf(void (*setbuf_func)(
341 FILE *stream, char *buf));
342#else
343#undef mbedtls_setbuf
344#if defined(MBEDTLS_PLATFORM_SETBUF_MACRO)
355#define mbedtls_setbuf MBEDTLS_PLATFORM_SETBUF_MACRO
356#else
357#define mbedtls_setbuf setbuf
358#endif /* MBEDTLS_PLATFORM_SETBUF_MACRO */
359#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
360
361/*
362 * The function pointers for exit
363 */
364#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
365extern void (*mbedtls_exit)(int status);
366
376int mbedtls_platform_set_exit(void (*exit_func)(int status));
377#else
378#undef mbedtls_exit
379#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
380#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
381#else
382#define mbedtls_exit exit
383#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
384#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
385
386/*
387 * The default exit values
388 */
389#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
390#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
391#else
392#define MBEDTLS_EXIT_SUCCESS 0
393#endif
394#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
395#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
396#else
397#define MBEDTLS_EXIT_FAILURE 1
398#endif
399
400/*
401 * The function pointers for reading from and writing a seed file to
402 * Non-Volatile storage (NV) in a platform-independent way
403 *
404 * Only enabled when the NV seed entropy source is enabled
405 */
406#if defined(MBEDTLS_ENTROPY_NV_SEED)
407#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
408/* Internal standard platform definitions */
409int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
410int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
411#endif
412
413#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
414extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
415extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
416
426int mbedtls_platform_set_nv_seed(
427 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
428 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
429 );
430#else
431#undef mbedtls_nv_seed_read
432#undef mbedtls_nv_seed_write
433#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
434 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
435#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
436#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
437#else
438#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
439#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
440#endif
441#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
442#endif /* MBEDTLS_ENTROPY_NV_SEED */
443
444#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
445
453 char MBEDTLS_PRIVATE(dummy);
454}
456
457#else
458#include "platform_alt.h"
459#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
460
492
493#ifdef __cplusplus
494}
495#endif
496
497#endif /* platform.h */
Build-time configuration info.
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:178
#define mbedtls_snprintf
Definition: platform.h:267
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_fprintf
Definition: platform.h:206
#define mbedtls_calloc
Definition: platform.h:179
#define mbedtls_exit
Definition: platform.h:382
#define mbedtls_vsnprintf
Definition: platform.h:304
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
#define mbedtls_printf
Definition: platform.h:231
#define mbedtls_setbuf
Definition: platform.h:357
Mbed TLS Platform time abstraction.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
The platform context structure.
Definition: platform.h:452