mbed TLS v2.28.0
platform.h
Go to the documentation of this file.
1
15/*
16 * Copyright The Mbed TLS Contributors
17 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 */
31#ifndef MBEDTLS_PLATFORM_H
32#define MBEDTLS_PLATFORM_H
33
34#if !defined(MBEDTLS_CONFIG_FILE)
35#include "mbedtls/config.h"
36#else
37#include MBEDTLS_CONFIG_FILE
38#endif
39
40#if defined(MBEDTLS_HAVE_TIME)
42#endif
43
45#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
47#define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
61/* The older Microsoft Windows common runtime provides non-conforming
62 * implementations of some standard library functions, including snprintf
63 * and vsnprintf. This affects MSVC and MinGW builds.
64 */
65#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
66#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
67#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
68#endif
69
70#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
71#include <stdio.h>
72#include <stdlib.h>
73#include <time.h>
74#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
75#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
76#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
77#else
78#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
79#endif
80#endif
81#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
82#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
83#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
84#else
85#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
86#endif
87#endif
88#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
89#define MBEDTLS_PLATFORM_STD_PRINTF printf
90#endif
91#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
92#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
93#endif
94#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
95#define MBEDTLS_PLATFORM_STD_CALLOC calloc
96#endif
97#if !defined(MBEDTLS_PLATFORM_STD_FREE)
98#define MBEDTLS_PLATFORM_STD_FREE free
99#endif
100#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
101#define MBEDTLS_PLATFORM_STD_EXIT exit
102#endif
103#if !defined(MBEDTLS_PLATFORM_STD_TIME)
104#define MBEDTLS_PLATFORM_STD_TIME time
105#endif
106#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
107#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
108#endif
109#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
110#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
111#endif
112#if defined(MBEDTLS_FS_IO)
113#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
114#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
115#endif
116#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
117#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
118#endif
119#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
120#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
121#endif
122#endif /* MBEDTLS_FS_IO */
123#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
124#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
125#include MBEDTLS_PLATFORM_STD_MEM_HDR
126#endif
127#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
128
129
130/* \} name SECTION: Module settings */
131
132/*
133 * The function pointers for calloc and free.
134 */
135#if defined(MBEDTLS_PLATFORM_MEMORY)
136#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
137 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
138#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
139#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
140#else
141/* For size_t */
142#include <stddef.h>
143extern void *mbedtls_calloc( size_t n, size_t size );
144extern void mbedtls_free( void *ptr );
145
155int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
156 void (*free_func)( void * ) );
157#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
158#else /* !MBEDTLS_PLATFORM_MEMORY */
159#define mbedtls_free free
160#define mbedtls_calloc calloc
161#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
162
163/*
164 * The function pointers for fprintf
165 */
166#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
167/* We need FILE * */
168#include <stdio.h>
169extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
170
180int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
181 ... ) );
182#else
183#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
184#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
185#else
186#define mbedtls_fprintf fprintf
187#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
188#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
189
190/*
191 * The function pointers for printf
192 */
193#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
194extern int (*mbedtls_printf)( const char *format, ... );
195
205int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
206#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
207#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
208#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
209#else
210#define mbedtls_printf printf
211#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
212#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
213
214/*
215 * The function pointers for snprintf
216 *
217 * The snprintf implementation should conform to C99:
218 * - it *must* always correctly zero-terminate the buffer
219 * (except when n == 0, then it must leave the buffer untouched)
220 * - however it is acceptable to return -1 instead of the required length when
221 * the destination buffer is too short.
222 */
223#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
224/* For Windows (inc. MSYS2), we provide our own fixed implementation */
225int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
226#endif
227
228#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
229extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
230
239int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
240 const char * format, ... ) );
241#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
242#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
243#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
244#else
245#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
246#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
247#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
248
249/*
250 * The function pointers for vsnprintf
251 *
252 * The vsnprintf implementation should conform to C99:
253 * - it *must* always correctly zero-terminate the buffer
254 * (except when n == 0, then it must leave the buffer untouched)
255 * - however it is acceptable to return -1 instead of the required length when
256 * the destination buffer is too short.
257 */
258#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
259#include <stdarg.h>
260/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
261int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
262#endif
263
264#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
265#include <stdarg.h>
266extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
267
275int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
276 const char * format, va_list arg ) );
277#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
278#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
279#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
280#else
281#define mbedtls_vsnprintf vsnprintf
282#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
283#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
284
285/*
286 * The function pointers for exit
287 */
288#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
289extern void (*mbedtls_exit)( int status );
290
300int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
301#else
302#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
303#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
304#else
305#define mbedtls_exit exit
306#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
307#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
308
309/*
310 * The default exit values
311 */
312#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
313#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
314#else
315#define MBEDTLS_EXIT_SUCCESS 0
316#endif
317#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
318#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
319#else
320#define MBEDTLS_EXIT_FAILURE 1
321#endif
322
323/*
324 * The function pointers for reading from and writing a seed file to
325 * Non-Volatile storage (NV) in a platform-independent way
326 *
327 * Only enabled when the NV seed entropy source is enabled
328 */
329#if defined(MBEDTLS_ENTROPY_NV_SEED)
330#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
331/* Internal standard platform definitions */
332int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
333int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
334#endif
335
336#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
337extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
338extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
339
349int mbedtls_platform_set_nv_seed(
350 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
351 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
352 );
353#else
354#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
355 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
356#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
357#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
358#else
359#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
360#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
361#endif
362#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
363#endif /* MBEDTLS_ENTROPY_NV_SEED */
364
365#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
366
374{
375 char dummy;
376}
378
379#else
380#include "platform_alt.h"
381#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
382
414
415#ifdef __cplusplus
416}
417#endif
418
419#endif /* platform.h */
Configuration options (set of defines)
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:159
#define mbedtls_snprintf
Definition: platform.h:245
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_fprintf
Definition: platform.h:186
#define mbedtls_calloc
Definition: platform.h:160
#define mbedtls_exit
Definition: platform.h:305
#define mbedtls_vsnprintf
Definition: platform.h:281
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
#define mbedtls_printf
Definition: platform.h:210
mbed TLS Platform time abstraction
The platform context structure.
Definition: platform.h:374