mbed TLS v2.28.1
Loading...
Searching...
No Matches
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#if defined(MBEDTLS_HAVE_TIME)
74#include <time.h>
75#endif
76#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
77#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
78#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
79#else
80#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
81#endif
82#endif
83#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
84#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
85#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
86#else
87#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
88#endif
89#endif
90#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
91#define MBEDTLS_PLATFORM_STD_PRINTF printf
92#endif
93#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
94#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
95#endif
96#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
97#define MBEDTLS_PLATFORM_STD_CALLOC calloc
98#endif
99#if !defined(MBEDTLS_PLATFORM_STD_FREE)
100#define MBEDTLS_PLATFORM_STD_FREE free
101#endif
102#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
103#define MBEDTLS_PLATFORM_STD_EXIT exit
104#endif
105#if !defined(MBEDTLS_PLATFORM_STD_TIME)
106#define MBEDTLS_PLATFORM_STD_TIME time
107#endif
108#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
109#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
110#endif
111#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
112#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
113#endif
114#if defined(MBEDTLS_FS_IO)
115#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
116#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
117#endif
118#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
119#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
120#endif
121#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
122#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
123#endif
124#endif /* MBEDTLS_FS_IO */
125#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
126#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
127#include MBEDTLS_PLATFORM_STD_MEM_HDR
128#endif
129#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
130
131
134/*
135 * The function pointers for calloc and free.
136 */
137#if defined(MBEDTLS_PLATFORM_MEMORY)
138#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
139 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
140#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
141#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
142#else
143/* For size_t */
144#include <stddef.h>
145extern void *mbedtls_calloc( size_t n, size_t size );
146extern void mbedtls_free( void *ptr );
147
157int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
158 void (*free_func)( void * ) );
159#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
160#else /* !MBEDTLS_PLATFORM_MEMORY */
161#define mbedtls_free free
162#define mbedtls_calloc calloc
163#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
164
165/*
166 * The function pointers for fprintf
167 */
168#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
169/* We need FILE * */
170#include <stdio.h>
171extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
172
182int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
183 ... ) );
184#else
185#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
186#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
187#else
188#define mbedtls_fprintf fprintf
189#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
190#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
191
192/*
193 * The function pointers for printf
194 */
195#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
196extern int (*mbedtls_printf)( const char *format, ... );
197
207int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
208#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
209#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
210#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
211#else
212#define mbedtls_printf printf
213#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
214#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
215
216/*
217 * The function pointers for snprintf
218 *
219 * The snprintf implementation should conform to C99:
220 * - it *must* always correctly zero-terminate the buffer
221 * (except when n == 0, then it must leave the buffer untouched)
222 * - however it is acceptable to return -1 instead of the required length when
223 * the destination buffer is too short.
224 */
225#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
226/* For Windows (inc. MSYS2), we provide our own fixed implementation */
227int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
228#endif
229
230#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
231extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
232
241int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
242 const char * format, ... ) );
243#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
244#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
245#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
246#else
247#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
248#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
249#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
250
251/*
252 * The function pointers for vsnprintf
253 *
254 * The vsnprintf implementation should conform to C99:
255 * - it *must* always correctly zero-terminate the buffer
256 * (except when n == 0, then it must leave the buffer untouched)
257 * - however it is acceptable to return -1 instead of the required length when
258 * the destination buffer is too short.
259 */
260#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
261#include <stdarg.h>
262/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
263int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
264#endif
265
266#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
267#include <stdarg.h>
268extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
269
277int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
278 const char * format, va_list arg ) );
279#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
280#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
281#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
282#else
283#define mbedtls_vsnprintf vsnprintf
284#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
285#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
286
287/*
288 * The function pointers for exit
289 */
290#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
291extern void (*mbedtls_exit)( int status );
292
302int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
303#else
304#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
305#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
306#else
307#define mbedtls_exit exit
308#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
309#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
310
311/*
312 * The default exit values
313 */
314#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
315#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
316#else
317#define MBEDTLS_EXIT_SUCCESS 0
318#endif
319#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
320#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
321#else
322#define MBEDTLS_EXIT_FAILURE 1
323#endif
324
325/*
326 * The function pointers for reading from and writing a seed file to
327 * Non-Volatile storage (NV) in a platform-independent way
328 *
329 * Only enabled when the NV seed entropy source is enabled
330 */
331#if defined(MBEDTLS_ENTROPY_NV_SEED)
332#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
333/* Internal standard platform definitions */
334int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
335int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
336#endif
337
338#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
339extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
340extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
341
351int mbedtls_platform_set_nv_seed(
352 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
353 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
354 );
355#else
356#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
357 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
358#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
359#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
360#else
361#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
362#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
363#endif
364#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
365#endif /* MBEDTLS_ENTROPY_NV_SEED */
366
367#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
368
376{
377 char dummy;
378}
380
381#else
382#include "platform_alt.h"
383#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
384
416
417#ifdef __cplusplus
418}
419#endif
420
421#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:161
#define mbedtls_snprintf
Definition: platform.h:247
#define mbedtls_fprintf
Definition: platform.h:188
#define mbedtls_calloc
Definition: platform.h:162
#define mbedtls_exit
Definition: platform.h:307
#define mbedtls_vsnprintf
Definition: platform.h:283
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
#define mbedtls_printf
Definition: platform.h:212
mbed TLS Platform time abstraction
The platform context structure.
Definition: platform.h:376