Mbed OS Reference
Loading...
Searching...
No Matches
connectivity/mbedtls/include/mbedtls/platform.h
Go to the documentation of this file.
1/**
2 * \file platform.h
3 *
4 * \brief This file contains the definitions and functions of the
5 * Mbed TLS platform abstraction layer.
6 *
7 * The platform abstraction layer removes the need for the library
8 * to directly link to standard C library functions or operating
9 * system services, making the library easier to port and embed.
10 * Application developers and users of the library can provide their own
11 * implementations of these functions, or implementations specific to
12 * their platform, which can be statically linked to the library or
13 * dynamically configured at runtime.
14 */
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
44/**
45 * \addtogroup mbedtls
46 * \{
47 * \defgroup mbedtls_platform_module Platform Abstraction Layer
48 * \{
49 */
50
51#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070 /**< Hardware accelerator failed */
52#define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072 /**< The requested feature is not supported by the platform */
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58/**
59 * \name SECTION: Module settings
60 *
61 * The configuration options you can set for this module are in this section.
62 * Either change them in config.h or define them on the compiler command line.
63 * \{
64 */
65
66/* The older Microsoft Windows common runtime provides non-conforming
67 * implementations of some standard library functions, including snprintf
68 * and vsnprintf. This affects MSVC and MinGW builds.
69 */
70#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
71#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
72#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
73#endif
74
75#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
76#include <stdio.h>
77#include <stdlib.h>
78#include <time.h>
79#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
80#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
81#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */
82#else
83#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
84#endif
85#endif
86#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
87#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
88#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
89#else
90#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
91#endif
92#endif
93#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
94#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
95#endif
96#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
97#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
98#endif
99#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
100#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
101#endif
102#if !defined(MBEDTLS_PLATFORM_STD_FREE)
103#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
104#endif
105#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
106#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
107#endif
108#if !defined(MBEDTLS_PLATFORM_STD_TIME)
109#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
110#endif
111#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
112#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
113#endif
114#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
115#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
116#endif
117#if defined(MBEDTLS_FS_IO)
118#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
119#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
120#endif
121#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
122#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
123#endif
124#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
125#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
126#endif
127#endif /* MBEDTLS_FS_IO */
128#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
129#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
130#include MBEDTLS_PLATFORM_STD_MEM_HDR
131#endif
132#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
133
134
135/** \} name SECTION: Module settings */
136
137/*
138 * The function pointers for calloc and free.
139 */
140#if defined(MBEDTLS_PLATFORM_MEMORY)
141#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
142 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
143#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
144#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
145#else
146/* For size_t */
147#include <stddef.h>
148extern void *mbedtls_calloc( size_t n, size_t size );
149extern void mbedtls_free( void *ptr );
150
151/**
152 * \brief This function dynamically sets the memory-management
153 * functions used by the library, during runtime.
154 *
155 * \param calloc_func The \c calloc function implementation.
156 * \param free_func The \c free function implementation.
157 *
158 * \return \c 0.
159 */
160int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
161 void (*free_func)( void * ) );
162#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
163#else /* !MBEDTLS_PLATFORM_MEMORY */
164#define mbedtls_free free
165#define mbedtls_calloc calloc
166#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
167
168/*
169 * The function pointers for fprintf
170 */
171#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
172/* We need FILE * */
173#include <stdio.h>
174extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
175
176/**
177 * \brief This function dynamically configures the fprintf
178 * function that is called when the
179 * mbedtls_fprintf() function is invoked by the library.
180 *
181 * \param fprintf_func The \c fprintf function implementation.
182 *
183 * \return \c 0.
184 */
185int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
186 ... ) );
187#else
188#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
189#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
190#else
191#define mbedtls_fprintf fprintf
192#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
193#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
194
195/*
196 * The function pointers for printf
197 */
198#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
199extern int (*mbedtls_printf)( const char *format, ... );
200
201/**
202 * \brief This function dynamically configures the snprintf
203 * function that is called when the mbedtls_snprintf()
204 * function is invoked by the library.
205 *
206 * \param printf_func The \c printf function implementation.
207 *
208 * \return \c 0 on success.
209 */
210int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
211#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
212#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
213#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
214#else
215#define mbedtls_printf printf
216#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
217#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
218
219/*
220 * The function pointers for snprintf
221 *
222 * The snprintf implementation should conform to C99:
223 * - it *must* always correctly zero-terminate the buffer
224 * (except when n == 0, then it must leave the buffer untouched)
225 * - however it is acceptable to return -1 instead of the required length when
226 * the destination buffer is too short.
227 */
228#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
229/* For Windows (inc. MSYS2), we provide our own fixed implementation */
230int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
231#endif
232
233#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
234extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
235
236/**
237 * \brief This function allows configuring a custom
238 * \c snprintf function pointer.
239 *
240 * \param snprintf_func The \c snprintf function implementation.
241 *
242 * \return \c 0 on success.
243 */
244int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
245 const char * format, ... ) );
246#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
247#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
248#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
249#else
250#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
251#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
252#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
253
254/*
255 * The function pointers for vsnprintf
256 *
257 * The vsnprintf implementation should conform to C99:
258 * - it *must* always correctly zero-terminate the buffer
259 * (except when n == 0, then it must leave the buffer untouched)
260 * - however it is acceptable to return -1 instead of the required length when
261 * the destination buffer is too short.
262 */
263#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
264#include <stdarg.h>
265/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
266int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
267#endif
268
269#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
270#include <stdarg.h>
271extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
272
273/**
274 * \brief Set your own snprintf function pointer
275 *
276 * \param vsnprintf_func The \c vsnprintf function implementation
277 *
278 * \return \c 0
279 */
280int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
281 const char * format, va_list arg ) );
282#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
283#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
284#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
285#else
286#define mbedtls_vsnprintf vsnprintf
287#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
288#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
289
290/*
291 * The function pointers for exit
292 */
293#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
294extern void (*mbedtls_exit)( int status );
295
296/**
297 * \brief This function dynamically configures the exit
298 * function that is called when the mbedtls_exit()
299 * function is invoked by the library.
300 *
301 * \param exit_func The \c exit function implementation.
302 *
303 * \return \c 0 on success.
304 */
305int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
306#else
307#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
308#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
309#else
310#define mbedtls_exit exit
311#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
312#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
313
314/*
315 * The default exit values
316 */
317#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
318#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
319#else
320#define MBEDTLS_EXIT_SUCCESS 0
321#endif
322#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
323#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
324#else
325#define MBEDTLS_EXIT_FAILURE 1
326#endif
327
328/*
329 * The function pointers for reading from and writing a seed file to
330 * Non-Volatile storage (NV) in a platform-independent way
331 *
332 * Only enabled when the NV seed entropy source is enabled
333 */
334#if defined(MBEDTLS_ENTROPY_NV_SEED)
335#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
336/* Internal standard platform definitions */
337int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
338int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
339#endif
340
341#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
342extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
343extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
344
345/**
346 * \brief This function allows configuring custom seed file writing and
347 * reading functions.
348 *
349 * \param nv_seed_read_func The seed reading function implementation.
350 * \param nv_seed_write_func The seed writing function implementation.
351 *
352 * \return \c 0 on success.
353 */
354int mbedtls_platform_set_nv_seed(
355 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
356 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
357 );
358#else
359#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
360 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
361#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
362#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
363#else
364#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
365#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
366#endif
367#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
368#endif /* MBEDTLS_ENTROPY_NV_SEED */
369
370#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
371
372/**
373 * \brief The platform context structure.
374 *
375 * \note This structure may be used to assist platform-specific
376 * setup or teardown operations.
377 */
379{
380 char dummy; /**< A placeholder member, as empty structs are not portable. */
381}
383
384#else
385#include "platform_alt.h"
386#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
387
388/**
389 * \brief This function performs any platform-specific initialization
390 * operations.
391 *
392 * \note This function should be called before any other library functions.
393 *
394 * Its implementation is platform-specific, and unless
395 * platform-specific code is provided, it does nothing.
396 *
397 * \note The usage and necessity of this function is dependent on the platform.
398 *
399 * \param ctx The platform context.
400 *
401 * \return \c 0 on success.
402 */
404/**
405 * \brief This function performs any platform teardown operations.
406 *
407 * \note This function should be called after every other Mbed TLS module
408 * has been correctly freed using the appropriate free function.
409 *
410 * Its implementation is platform-specific, and unless
411 * platform-specific code is provided, it does nothing.
412 *
413 * \note The usage and necessity of this function is dependent on the platform.
414 *
415 * \param ctx The platform context.
416 *
417 */
419
420#ifdef __cplusplus
421}
422#endif
423
424/// \}
425/// \}
426
427#endif /* platform.h */
Configuration options (set of defines)
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbed TLS Platform time abstraction
char dummy
A placeholder member, as empty structs are not portable.