Mbed TLS v2.28.5
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
53 
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 /* Enable certain documented defines only when generating doxygen to avoid
139  * an "unrecognized define" error. */
140 #if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
141 #define MBEDTLS_PLATFORM_STD_CALLOC
142 #endif
143 
144 #if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
145 #define MBEDTLS_PLATFORM_STD_FREE
146 #endif
147 
150 /*
151  * The function pointers for calloc and free.
152  * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
153  * in mbedtls_config.h for more information about behaviour and requirements.
154  */
155 #if defined(MBEDTLS_PLATFORM_MEMORY)
156 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
157  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
158 #undef mbedtls_free
159 #undef mbedtls_calloc
160 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
161 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
162 #else
163 /* For size_t */
164 #include <stddef.h>
165 extern void *mbedtls_calloc(size_t n, size_t size);
166 extern void mbedtls_free(void *ptr);
167 
177 int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
178  void (*free_func)(void *));
179 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
180 #else /* !MBEDTLS_PLATFORM_MEMORY */
181 #undef mbedtls_free
182 #undef mbedtls_calloc
183 #define mbedtls_free free
184 #define mbedtls_calloc calloc
185 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
186 
187 /*
188  * The function pointers for fprintf
189  */
190 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
191 /* We need FILE * */
192 #include <stdio.h>
193 extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
194 
204 int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
205  ...));
206 #else
207 #undef mbedtls_fprintf
208 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
209 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
210 #else
211 #define mbedtls_fprintf fprintf
212 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
213 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
214 
215 /*
216  * The function pointers for printf
217  */
218 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
219 extern int (*mbedtls_printf)(const char *format, ...);
220 
230 int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
231 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
232 #undef mbedtls_printf
233 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
234 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
235 #else
236 #define mbedtls_printf printf
237 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
238 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
239 
240 /*
241  * The function pointers for snprintf
242  *
243  * The snprintf implementation should conform to C99:
244  * - it *must* always correctly zero-terminate the buffer
245  * (except when n == 0, then it must leave the buffer untouched)
246  * - however it is acceptable to return -1 instead of the required length when
247  * the destination buffer is too short.
248  */
249 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
250 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
251 int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
252 #endif
253 
254 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
255 extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
256 
265 int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
266  const char *format, ...));
267 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
268 #undef mbedtls_snprintf
269 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
270 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
271 #else
272 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
273 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
274 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
275 
276 /*
277  * The function pointers for vsnprintf
278  *
279  * The vsnprintf implementation should conform to C99:
280  * - it *must* always correctly zero-terminate the buffer
281  * (except when n == 0, then it must leave the buffer untouched)
282  * - however it is acceptable to return -1 instead of the required length when
283  * the destination buffer is too short.
284  */
285 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
286 #include <stdarg.h>
287 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
288 int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
289 #endif
290 
291 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
292 #include <stdarg.h>
293 extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
294 
302 int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
303  const char *format, va_list arg));
304 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
305 #undef mbedtls_vsnprintf
306 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
307 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
308 #else
309 #define mbedtls_vsnprintf vsnprintf
310 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
311 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
312 
313 /*
314  * The function pointers for exit
315  */
316 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
317 extern void (*mbedtls_exit)(int status);
318 
328 int mbedtls_platform_set_exit(void (*exit_func)(int status));
329 #else
330 #undef mbedtls_exit
331 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
332 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
333 #else
334 #define mbedtls_exit exit
335 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
336 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
337 
338 /*
339  * The default exit values
340  */
341 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
342 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
343 #else
344 #define MBEDTLS_EXIT_SUCCESS 0
345 #endif
346 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
347 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
348 #else
349 #define MBEDTLS_EXIT_FAILURE 1
350 #endif
351 
352 /*
353  * The function pointers for reading from and writing a seed file to
354  * Non-Volatile storage (NV) in a platform-independent way
355  *
356  * Only enabled when the NV seed entropy source is enabled
357  */
358 #if defined(MBEDTLS_ENTROPY_NV_SEED)
359 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
360 /* Internal standard platform definitions */
361 int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
362 int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
363 #endif
364 
365 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
366 extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
367 extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
368 
378 int mbedtls_platform_set_nv_seed(
379  int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
380  int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
381  );
382 #else
383 #undef mbedtls_nv_seed_read
384 #undef mbedtls_nv_seed_write
385 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
386  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
387 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
388 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
389 #else
390 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
391 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
392 #endif
393 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
394 #endif /* MBEDTLS_ENTROPY_NV_SEED */
395 
396 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
397 
404 typedef struct mbedtls_platform_context {
405  char dummy;
406 }
408 
409 #else
410 #include "platform_alt.h"
411 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
412 
444 
445 #ifdef __cplusplus
446 }
447 #endif
448 
449 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_calloc
Definition: platform.h:184
#define mbedtls_free
Definition: platform.h:183
#define mbedtls_vsnprintf
Definition: platform.h:309
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:211
The platform context structure.
Definition: platform.h:404
#define mbedtls_exit
Definition: platform.h:334
#define mbedtls_snprintf
Definition: platform.h:272
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_printf
Definition: platform.h:236
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
Mbed TLS Platform time abstraction.