4 * \brief This file contains the definitions and functions of the
\r
5 * Mbed TLS platform abstraction layer.
\r
7 * The platform abstraction layer removes the need for the library
\r
8 * to directly link to standard C library functions or operating
\r
9 * system services, making the library easier to port and embed.
\r
10 * Application developers and users of the library can provide their own
\r
11 * implementations of these functions, or implementations specific to
\r
12 * their platform, which can be statically linked to the library or
\r
13 * dynamically configured at runtime.
\r
16 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
\r
17 * SPDX-License-Identifier: Apache-2.0
\r
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
\r
20 * not use this file except in compliance with the License.
\r
21 * You may obtain a copy of the License at
\r
23 * http://www.apache.org/licenses/LICENSE-2.0
\r
25 * Unless required by applicable law or agreed to in writing, software
\r
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\r
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
28 * See the License for the specific language governing permissions and
\r
29 * limitations under the License.
\r
31 * This file is part of Mbed TLS (https://tls.mbed.org)
\r
33 #ifndef MBEDTLS_PLATFORM_H
\r
34 #define MBEDTLS_PLATFORM_H
\r
36 #if !defined(MBEDTLS_CONFIG_FILE)
\r
39 #include MBEDTLS_CONFIG_FILE
\r
42 #if defined(MBEDTLS_HAVE_TIME)
\r
43 #include "platform_time.h"
\r
46 #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070 /**< Hardware accelerator failed */
\r
47 #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072 /**< The requested feature is not supported by the platform */
\r
54 * \name SECTION: Module settings
\r
56 * The configuration options you can set for this module are in this section.
\r
57 * Either change them in config.h or define them on the compiler command line.
\r
61 /* The older Microsoft Windows common runtime provides non-conforming
\r
62 * implementations of some standard library functions, including snprintf
\r
63 * and vsnprintf. This affects MSVC and MinGW builds.
\r
65 #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
\r
66 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
\r
67 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
\r
70 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
\r
74 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
\r
75 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
\r
76 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */
\r
78 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
\r
81 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
\r
82 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
\r
83 #define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
\r
85 #define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
\r
88 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
\r
89 #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
\r
91 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
\r
92 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
\r
94 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
\r
95 #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
\r
97 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
\r
98 #define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
\r
100 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
\r
101 #define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
\r
103 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
\r
104 #define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
\r
106 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
\r
107 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
\r
109 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
\r
110 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
\r
112 #if defined(MBEDTLS_FS_IO)
\r
113 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
\r
114 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
\r
116 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
\r
117 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
\r
119 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
\r
120 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
\r
122 #endif /* MBEDTLS_FS_IO */
\r
123 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
\r
124 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
\r
125 #include MBEDTLS_PLATFORM_STD_MEM_HDR
\r
127 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
\r
130 /* \} name SECTION: Module settings */
\r
133 * The function pointers for calloc and free.
\r
135 #if defined(MBEDTLS_PLATFORM_MEMORY)
\r
136 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
\r
137 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
\r
138 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
\r
139 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
\r
142 #include <stddef.h>
\r
143 extern void *mbedtls_calloc( size_t n, size_t size );
\r
144 extern void mbedtls_free( void *ptr );
\r
147 * \brief This function dynamically sets the memory-management
\r
148 * functions used by the library, during runtime.
\r
150 * \param calloc_func The \c calloc function implementation.
\r
151 * \param free_func The \c free function implementation.
\r
155 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
\r
156 void (*free_func)( void * ) );
\r
157 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
\r
158 #else /* !MBEDTLS_PLATFORM_MEMORY */
\r
159 #define mbedtls_free free
\r
160 #define mbedtls_calloc calloc
\r
161 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
\r
164 * The function pointers for fprintf
\r
166 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
\r
167 /* We need FILE * */
\r
169 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
\r
172 * \brief This function dynamically configures the fprintf
\r
173 * function that is called when the
\r
174 * mbedtls_fprintf() function is invoked by the library.
\r
176 * \param fprintf_func The \c fprintf function implementation.
\r
180 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
\r
183 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
\r
184 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
\r
186 #define mbedtls_fprintf fprintf
\r
187 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
\r
188 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
\r
191 * The function pointers for printf
\r
193 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
\r
194 extern int (*mbedtls_printf)( const char *format, ... );
\r
197 * \brief This function dynamically configures the snprintf
\r
198 * function that is called when the mbedtls_snprintf()
\r
199 * function is invoked by the library.
\r
201 * \param printf_func The \c printf function implementation.
\r
203 * \return \c 0 on success.
\r
205 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
\r
206 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
\r
207 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
\r
208 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
\r
210 #define mbedtls_printf printf
\r
211 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
\r
212 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
\r
215 * The function pointers for snprintf
\r
217 * The snprintf implementation should conform to C99:
\r
218 * - it *must* always correctly zero-terminate the buffer
\r
219 * (except when n == 0, then it must leave the buffer untouched)
\r
220 * - however it is acceptable to return -1 instead of the required length when
\r
221 * the destination buffer is too short.
\r
223 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
\r
224 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
\r
225 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
\r
228 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
\r
229 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
\r
232 * \brief This function allows configuring a custom
\r
233 * \c snprintf function pointer.
\r
235 * \param snprintf_func The \c snprintf function implementation.
\r
237 * \return \c 0 on success.
\r
239 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
\r
240 const char * format, ... ) );
\r
241 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
\r
242 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
\r
243 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
\r
245 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
\r
246 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
\r
247 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
\r
250 * The function pointers for vsnprintf
\r
252 * The vsnprintf implementation should conform to C99:
\r
253 * - it *must* always correctly zero-terminate the buffer
\r
254 * (except when n == 0, then it must leave the buffer untouched)
\r
255 * - however it is acceptable to return -1 instead of the required length when
\r
256 * the destination buffer is too short.
\r
258 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
\r
259 #include <stdarg.h>
\r
260 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
\r
261 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
\r
264 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
\r
265 #include <stdarg.h>
\r
266 extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
\r
269 * \brief Set your own snprintf function pointer
\r
271 * \param vsnprintf_func The \c vsnprintf function implementation
\r
275 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
\r
276 const char * format, va_list arg ) );
\r
277 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
\r
278 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
\r
279 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
\r
281 #define mbedtls_vsnprintf vsnprintf
\r
282 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
\r
283 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
\r
286 * The function pointers for exit
\r
288 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
\r
289 extern void (*mbedtls_exit)( int status );
\r
292 * \brief This function dynamically configures the exit
\r
293 * function that is called when the mbedtls_exit()
\r
294 * function is invoked by the library.
\r
296 * \param exit_func The \c exit function implementation.
\r
298 * \return \c 0 on success.
\r
300 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
\r
302 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
\r
303 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
\r
305 #define mbedtls_exit exit
\r
306 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
\r
307 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
\r
310 * The default exit values
\r
312 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
\r
313 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
\r
315 #define MBEDTLS_EXIT_SUCCESS 0
\r
317 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
\r
318 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
\r
320 #define MBEDTLS_EXIT_FAILURE 1
\r
324 * The function pointers for reading from and writing a seed file to
\r
325 * Non-Volatile storage (NV) in a platform-independent way
\r
327 * Only enabled when the NV seed entropy source is enabled
\r
329 #if defined(MBEDTLS_ENTROPY_NV_SEED)
\r
330 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
\r
331 /* Internal standard platform definitions */
\r
332 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
\r
333 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
\r
336 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
\r
337 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
\r
338 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
\r
341 * \brief This function allows configuring custom seed file writing and
\r
342 * reading functions.
\r
344 * \param nv_seed_read_func The seed reading function implementation.
\r
345 * \param nv_seed_write_func The seed writing function implementation.
\r
347 * \return \c 0 on success.
\r
349 int mbedtls_platform_set_nv_seed(
\r
350 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
\r
351 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
\r
354 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
\r
355 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
\r
356 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
\r
357 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
\r
359 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
\r
360 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
\r
362 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
\r
363 #endif /* MBEDTLS_ENTROPY_NV_SEED */
\r
365 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
\r
368 * \brief The platform context structure.
\r
370 * \note This structure may be used to assist platform-specific
\r
371 * setup or teardown operations.
\r
373 typedef struct mbedtls_platform_context
\r
375 char dummy; /**< A placeholder member, as empty structs are not portable. */
\r
377 mbedtls_platform_context;
\r
380 #include "platform_alt.h"
\r
381 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
\r
384 * \brief This function performs any platform-specific initialization
\r
387 * \note This function should be called before any other library functions.
\r
389 * Its implementation is platform-specific, and unless
\r
390 * platform-specific code is provided, it does nothing.
\r
392 * \note The usage and necessity of this function is dependent on the platform.
\r
394 * \param ctx The platform context.
\r
396 * \return \c 0 on success.
\r
398 int mbedtls_platform_setup( mbedtls_platform_context *ctx );
\r
400 * \brief This function performs any platform teardown operations.
\r
402 * \note This function should be called after every other Mbed TLS module
\r
403 * has been correctly freed using the appropriate free function.
\r
405 * Its implementation is platform-specific, and unless
\r
406 * platform-specific code is provided, it does nothing.
\r
408 * \note The usage and necessity of this function is dependent on the platform.
\r
410 * \param ctx The platform context.
\r
413 void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
\r
419 #endif /* platform.h */
\r