mbed TLS v3.4.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
40 #include "mbedtls/private_access.h"
41 
42 #include "mbedtls/build_info.h"
43 
44 #if defined(MBEDTLS_HAVE_TIME)
45 #include "mbedtls/platform_time.h"
46 #endif
47 
48 #ifdef __cplusplus
49 extern "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 
136 /*
137  * The function pointers for calloc and free.
138  */
139 #if defined(MBEDTLS_PLATFORM_MEMORY)
140 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
141  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
142 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
143 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
144 #else
145 /* For size_t */
146 #include <stddef.h>
147 extern void *mbedtls_calloc(size_t n, size_t size);
148 extern void mbedtls_free(void *ptr);
149 
159 int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
160  void (*free_func)(void *));
161 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
162 #else /* !MBEDTLS_PLATFORM_MEMORY */
163 #define mbedtls_free free
164 #define mbedtls_calloc calloc
165 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
166 
167 /*
168  * The function pointers for fprintf
169  */
170 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
171 /* We need FILE * */
172 #include <stdio.h>
173 extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
174 
184 int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
185  ...));
186 #else
187 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
188 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
189 #else
190 #define mbedtls_fprintf fprintf
191 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
192 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
193 
194 /*
195  * The function pointers for printf
196  */
197 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
198 extern int (*mbedtls_printf)(const char *format, ...);
199 
209 int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
210 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
211 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
212 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
213 #else
214 #define mbedtls_printf printf
215 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
216 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
217 
218 /*
219  * The function pointers for snprintf
220  *
221  * The snprintf implementation should conform to C99:
222  * - it *must* always correctly zero-terminate the buffer
223  * (except when n == 0, then it must leave the buffer untouched)
224  * - however it is acceptable to return -1 instead of the required length when
225  * the destination buffer is too short.
226  */
227 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
228 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
229 int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
230 #endif
231 
232 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
233 extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
234 
243 int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
244  const char *format, ...));
245 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
246 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
247 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
248 #else
249 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
250 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
251 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
252 
253 /*
254  * The function pointers for vsnprintf
255  *
256  * The vsnprintf implementation should conform to C99:
257  * - it *must* always correctly zero-terminate the buffer
258  * (except when n == 0, then it must leave the buffer untouched)
259  * - however it is acceptable to return -1 instead of the required length when
260  * the destination buffer is too short.
261  */
262 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
263 #include <stdarg.h>
264 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
265 int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
266 #endif
267 
268 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
269 #include <stdarg.h>
270 extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
271 
279 int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
280  const char *format, va_list arg));
281 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
282 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
283 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
284 #else
285 #define mbedtls_vsnprintf vsnprintf
286 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
287 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
288 
289 /*
290  * The function pointers for setbuf
291  */
292 #if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
293 #include <stdio.h>
310 extern void (*mbedtls_setbuf)(FILE *stream, char *buf);
311 
321 int mbedtls_platform_set_setbuf(void (*setbuf_func)(
322  FILE *stream, char *buf));
323 #elif defined(MBEDTLS_PLATFORM_SETBUF_MACRO)
324 
334 #define mbedtls_setbuf MBEDTLS_PLATFORM_SETBUF_MACRO
335 #else
336 #define mbedtls_setbuf setbuf
337 #endif /* MBEDTLS_PLATFORM_SETBUF_ALT / MBEDTLS_PLATFORM_SETBUF_MACRO */
338 
339 /*
340  * The function pointers for exit
341  */
342 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
343 extern void (*mbedtls_exit)(int status);
344 
354 int mbedtls_platform_set_exit(void (*exit_func)(int status));
355 #else
356 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
357 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
358 #else
359 #define mbedtls_exit exit
360 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
361 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
362 
363 /*
364  * The default exit values
365  */
366 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
367 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
368 #else
369 #define MBEDTLS_EXIT_SUCCESS 0
370 #endif
371 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
372 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
373 #else
374 #define MBEDTLS_EXIT_FAILURE 1
375 #endif
376 
377 /*
378  * The function pointers for reading from and writing a seed file to
379  * Non-Volatile storage (NV) in a platform-independent way
380  *
381  * Only enabled when the NV seed entropy source is enabled
382  */
383 #if defined(MBEDTLS_ENTROPY_NV_SEED)
384 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
385 /* Internal standard platform definitions */
386 int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
387 int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
388 #endif
389 
390 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
391 extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
392 extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
393 
403 int mbedtls_platform_set_nv_seed(
404  int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
405  int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
406  );
407 #else
408 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
409  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
410 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
411 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
412 #else
413 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
414 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
415 #endif
416 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
417 #endif /* MBEDTLS_ENTROPY_NV_SEED */
418 
419 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
420 
427 typedef struct mbedtls_platform_context {
428  char MBEDTLS_PRIVATE(dummy);
429 }
431 
432 #else
433 #include "platform_alt.h"
434 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
435 
467 
468 #ifdef __cplusplus
469 }
470 #endif
471 
472 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_calloc
Definition: platform.h:164
#define mbedtls_free
Definition: platform.h:163
#define MBEDTLS_PRIVATE(member)
#define mbedtls_vsnprintf
Definition: platform.h:285
#define mbedtls_fprintf
Definition: platform.h:190
The platform context structure.
Definition: platform.h:427
#define mbedtls_exit
Definition: platform.h:359
Macro wrapper for struct's members.
#define mbedtls_snprintf
Definition: platform.h:249
Build-time configuration info.
#define mbedtls_setbuf
Definition: platform.h:336
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_printf
Definition: platform.h:214
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbed TLS Platform time abstraction