mbed TLS v2.28.4
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 
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 #undef mbedtls_free
148 #undef mbedtls_calloc
149 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
150 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
151 #else
152 /* For size_t */
153 #include <stddef.h>
154 extern void *mbedtls_calloc(size_t n, size_t size);
155 extern void mbedtls_free(void *ptr);
156 
166 int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
167  void (*free_func)(void *));
168 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
169 #else /* !MBEDTLS_PLATFORM_MEMORY */
170 #undef mbedtls_free
171 #undef mbedtls_calloc
172 #define mbedtls_free free
173 #define mbedtls_calloc calloc
174 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
175 
176 /*
177  * The function pointers for fprintf
178  */
179 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
180 /* We need FILE * */
181 #include <stdio.h>
182 extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
183 
193 int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
194  ...));
195 #else
196 #undef mbedtls_fprintf
197 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
198 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
199 #else
200 #define mbedtls_fprintf fprintf
201 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
202 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
203 
204 /*
205  * The function pointers for printf
206  */
207 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
208 extern int (*mbedtls_printf)(const char *format, ...);
209 
219 int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
220 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
221 #undef mbedtls_printf
222 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
223 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
224 #else
225 #define mbedtls_printf printf
226 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
227 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
228 
229 /*
230  * The function pointers for snprintf
231  *
232  * The snprintf implementation should conform to C99:
233  * - it *must* always correctly zero-terminate the buffer
234  * (except when n == 0, then it must leave the buffer untouched)
235  * - however it is acceptable to return -1 instead of the required length when
236  * the destination buffer is too short.
237  */
238 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
239 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
240 int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
241 #endif
242 
243 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
244 extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
245 
254 int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
255  const char *format, ...));
256 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
257 #undef mbedtls_snprintf
258 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
259 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
260 #else
261 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
262 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
263 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
264 
265 /*
266  * The function pointers for vsnprintf
267  *
268  * The vsnprintf implementation should conform to C99:
269  * - it *must* always correctly zero-terminate the buffer
270  * (except when n == 0, then it must leave the buffer untouched)
271  * - however it is acceptable to return -1 instead of the required length when
272  * the destination buffer is too short.
273  */
274 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
275 #include <stdarg.h>
276 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
277 int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
278 #endif
279 
280 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
281 #include <stdarg.h>
282 extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
283 
291 int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
292  const char *format, va_list arg));
293 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
294 #undef mbedtls_vsnprintf
295 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
296 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
297 #else
298 #define mbedtls_vsnprintf vsnprintf
299 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
300 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
301 
302 /*
303  * The function pointers for exit
304  */
305 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
306 extern void (*mbedtls_exit)(int status);
307 
317 int mbedtls_platform_set_exit(void (*exit_func)(int status));
318 #else
319 #undef mbedtls_exit
320 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
321 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
322 #else
323 #define mbedtls_exit exit
324 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
325 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
326 
327 /*
328  * The default exit values
329  */
330 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
331 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
332 #else
333 #define MBEDTLS_EXIT_SUCCESS 0
334 #endif
335 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
336 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
337 #else
338 #define MBEDTLS_EXIT_FAILURE 1
339 #endif
340 
341 /*
342  * The function pointers for reading from and writing a seed file to
343  * Non-Volatile storage (NV) in a platform-independent way
344  *
345  * Only enabled when the NV seed entropy source is enabled
346  */
347 #if defined(MBEDTLS_ENTROPY_NV_SEED)
348 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
349 /* Internal standard platform definitions */
350 int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
351 int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
352 #endif
353 
354 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
355 extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
356 extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
357 
367 int mbedtls_platform_set_nv_seed(
368  int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
369  int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
370  );
371 #else
372 #undef mbedtls_nv_seed_read
373 #undef mbedtls_nv_seed_write
374 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
375  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
376 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
377 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
378 #else
379 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
380 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
381 #endif
382 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
383 #endif /* MBEDTLS_ENTROPY_NV_SEED */
384 
385 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
386 
393 typedef struct mbedtls_platform_context {
394  char dummy;
395 }
397 
398 #else
399 #include "platform_alt.h"
400 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
401 
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
438 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_calloc
Definition: platform.h:173
#define mbedtls_free
Definition: platform.h:172
#define mbedtls_vsnprintf
Definition: platform.h:298
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:200
The platform context structure.
Definition: platform.h:393
#define mbedtls_exit
Definition: platform.h:323
#define mbedtls_snprintf
Definition: platform.h:261
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_printf
Definition: platform.h:225
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbed TLS Platform time abstraction