/** * \file platform.h * * \brief This file contains the definitions and functions of the * Mbed TLS platform abstraction layer. * * The platform abstraction layer removes the need for the library * to directly link to standard C library functions or operating * system services, making the library easier to port and embed. * Application developers and users of the library can provide their own * implementations of these functions, or implementations specific to * their platform, which can be statically linked to the library or * dynamically configured at runtime. *//* ... *//* * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This file is part of Mbed TLS (https://tls.mbed.org) *//* ... */#ifndefMBEDTLS_PLATFORM_H#defineMBEDTLS_PLATFORM_H#if!defined(MBEDTLS_CONFIG_FILE)#include"config.h"#else#includeMBEDTLS_CONFIG_FILE#endif#ifdefined(MBEDTLS_HAVE_TIME)#include"platform_time.h"#endif#defineMBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED-0x0070/**< Hardware accelerator failed */#defineMBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED-0x0072/**< The requested feature is not supported by the platform */#ifdef__cplusplusextern"C"{#endif/** * \name SECTION: Module settings * * The configuration options you can set for this module are in this section. * Either change them in config.h or define them on the compiler command line. * \{ *//* ... */#if!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)#include<stdio.h>#include<stdlib.h>#include<time.h>#if!defined(MBEDTLS_PLATFORM_STD_SNPRINTF)#ifdefined(_WIN32)#defineMBEDTLS_PLATFORM_STD_SNPRINTFmbedtls_platform_win32_snprintf/**< The default \c snprintf function to use. */#else#defineMBEDTLS_PLATFORM_STD_SNPRINTFsnprintf/**< The default \c snprintf function to use. */#endif/* ... */#endif#if!defined(MBEDTLS_PLATFORM_STD_PRINTF)#defineMBEDTLS_PLATFORM_STD_PRINTFprintf/**< The default \c printf function to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_FPRINTF)#defineMBEDTLS_PLATFORM_STD_FPRINTFfprintf/**< The default \c fprintf function to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_CALLOC)#defineMBEDTLS_PLATFORM_STD_CALLOCcalloc/**< The default \c calloc function to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_FREE)#defineMBEDTLS_PLATFORM_STD_FREEfree/**< The default \c free function to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_EXIT)#defineMBEDTLS_PLATFORM_STD_EXITexit/**< The default \c exit function to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_TIME)#defineMBEDTLS_PLATFORM_STD_TIMEtime/**< The default \c time function to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)#defineMBEDTLS_PLATFORM_STD_EXIT_SUCCESSEXIT_SUCCESS/**< The default exit value to use. */#endif#if!defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)#defineMBEDTLS_PLATFORM_STD_EXIT_FAILUREEXIT_FAILURE/**< The default exit value to use. */#endif#ifdefined(MBEDTLS_FS_IO)#if!defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)#defineMBEDTLS_PLATFORM_STD_NV_SEED_READmbedtls_platform_std_nv_seed_read#endif#if!defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)#defineMBEDTLS_PLATFORM_STD_NV_SEED_WRITEmbedtls_platform_std_nv_seed_write#endif#if!defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)#defineMBEDTLS_PLATFORM_STD_NV_SEED_FILE"seedfile"#endif/* ... */#endif/* MBEDTLS_FS_IO *//* ... */#else/* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */#ifdefined(MBEDTLS_PLATFORM_STD_MEM_HDR)#includeMBEDTLS_PLATFORM_STD_MEM_HDR#endif/* ... */#endif/* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS *//* \} name SECTION: Module settings *//* * The function pointers for calloc and free. *//* ... */#ifdefined(MBEDTLS_PLATFORM_MEMORY)#ifdefined(MBEDTLS_PLATFORM_FREE_MACRO)&&\defined(MBEDTLS_PLATFORM_CALLOC_MACRO)#definembedtls_freeMBEDTLS_PLATFORM_FREE_MACRO#definembedtls_callocMBEDTLS_PLATFORM_CALLOC_MACRO/* ... */#else/* For size_t */#include<stddef.h>externvoid*mbedtls_calloc(size_tn,size_tsize);externvoidmbedtls_free(void*ptr);/** * \brief This function dynamically sets the memory-management * functions used by the library, during runtime. * * \param calloc_func The \c calloc function implementation. * \param free_func The \c free function implementation. * * \return \c 0. *//* ... */intmbedtls_platform_set_calloc_free(void*(*calloc_func)(size_t,size_t),void(*free_func)(void*));/* ... */#endif/* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO *//* ... */#else/* !MBEDTLS_PLATFORM_MEMORY */#definembedtls_freefree#definembedtls_calloccalloc/* ... */#endif/* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO *//* * The function pointers for fprintf *//* ... */#ifdefined(MBEDTLS_PLATFORM_FPRINTF_ALT)/* We need FILE * */#include<stdio.h>externint(*mbedtls_fprintf)(FILE*stream,constchar*format,...);/** * \brief This function dynamically configures the fprintf * function that is called when the * mbedtls_fprintf() function is invoked by the library. * * \param fprintf_func The \c fprintf function implementation. * * \return \c 0. *//* ... */intmbedtls_platform_set_fprintf(int(*fprintf_func)(FILE*stream,constchar*,...));/* ... */#else#ifdefined(MBEDTLS_PLATFORM_FPRINTF_MACRO)#definembedtls_fprintfMBEDTLS_PLATFORM_FPRINTF_MACRO#else#definembedtls_fprintffprintf#endif/* MBEDTLS_PLATFORM_FPRINTF_MACRO *//* ... */#endif/* MBEDTLS_PLATFORM_FPRINTF_ALT *//* * The function pointers for printf *//* ... */#ifdefined(MBEDTLS_PLATFORM_PRINTF_ALT)externint(*mbedtls_printf)(constchar*format,...);/** * \brief This function dynamically configures the snprintf * function that is called when the mbedtls_snprintf() * function is invoked by the library. * * \param printf_func The \c printf function implementation. * * \return \c 0 on success. *//* ... */intmbedtls_platform_set_printf(int(*printf_func)(constchar*,...));/* ... */#else/* !MBEDTLS_PLATFORM_PRINTF_ALT */#ifdefined(MBEDTLS_PLATFORM_PRINTF_MACRO)#definembedtls_printfMBEDTLS_PLATFORM_PRINTF_MACRO#else#definembedtls_printfprintf#endif/* MBEDTLS_PLATFORM_PRINTF_MACRO *//* ... */#endif/* MBEDTLS_PLATFORM_PRINTF_ALT *//* * The function pointers for snprintf * * The snprintf implementation should conform to C99: * - it *must* always correctly zero-terminate the buffer * (except when n == 0, then it must leave the buffer untouched) * - however it is acceptable to return -1 instead of the required length when * the destination buffer is too short. *//* ... */#ifdefined(_WIN32)/* For Windows (inc. MSYS2), we provide our own fixed implementation */intmbedtls_platform_win32_snprintf(char*s,size_tn,constchar*fmt,...);/* ... */#endif#ifdefined(MBEDTLS_PLATFORM_SNPRINTF_ALT)externint(*mbedtls_snprintf)(char*s,size_tn,constchar*format,...);/** * \brief This function allows configuring a custom * \c snprintf function pointer. * * \param snprintf_func The \c snprintf function implementation. * * \return \c 0 on success. *//* ... */intmbedtls_platform_set_snprintf(int(*snprintf_func)(char*s,size_tn,constchar*format,...));/* ... */#else/* MBEDTLS_PLATFORM_SNPRINTF_ALT */#ifdefined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)#definembedtls_snprintfMBEDTLS_PLATFORM_SNPRINTF_MACRO#else#definembedtls_snprintfMBEDTLS_PLATFORM_STD_SNPRINTF#endif/* MBEDTLS_PLATFORM_SNPRINTF_MACRO *//* ... */#endif/* MBEDTLS_PLATFORM_SNPRINTF_ALT *//* * The function pointers for exit *//* ... */#ifdefined(MBEDTLS_PLATFORM_EXIT_ALT)externvoid(*mbedtls_exit)(intstatus);/** * \brief This function dynamically configures the exit * function that is called when the mbedtls_exit() * function is invoked by the library. * * \param exit_func The \c exit function implementation. * * \return \c 0 on success. *//* ... */intmbedtls_platform_set_exit(void(*exit_func)(intstatus));/* ... */#else#ifdefined(MBEDTLS_PLATFORM_EXIT_MACRO)#definembedtls_exitMBEDTLS_PLATFORM_EXIT_MACRO#else#definembedtls_exitexit#endif/* MBEDTLS_PLATFORM_EXIT_MACRO *//* ... */#endif/* MBEDTLS_PLATFORM_EXIT_ALT *//* * The default exit values *//* ... */#ifdefined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)#defineMBEDTLS_EXIT_SUCCESSMBEDTLS_PLATFORM_STD_EXIT_SUCCESS#else#defineMBEDTLS_EXIT_SUCCESS0#endif#ifdefined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)#defineMBEDTLS_EXIT_FAILUREMBEDTLS_PLATFORM_STD_EXIT_FAILURE#else#defineMBEDTLS_EXIT_FAILURE1#endif/* * The function pointers for reading from and writing a seed file to * Non-Volatile storage (NV) in a platform-independent way * * Only enabled when the NV seed entropy source is enabled *//* ... */#ifdefined(MBEDTLS_ENTROPY_NV_SEED)#if!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)&&defined(MBEDTLS_FS_IO)/* Internal standard platform definitions */intmbedtls_platform_std_nv_seed_read(unsignedchar*buf,size_tbuf_len);intmbedtls_platform_std_nv_seed_write(unsignedchar*buf,size_tbuf_len);/* ... */#endif#ifdefined(MBEDTLS_PLATFORM_NV_SEED_ALT)externint(*mbedtls_nv_seed_read)(unsignedchar*buf,size_tbuf_len);externint(*mbedtls_nv_seed_write)(unsignedchar*buf,size_tbuf_len);/** * \brief This function allows configuring custom seed file writing and * reading functions. * * \param nv_seed_read_func The seed reading function implementation. * \param nv_seed_write_func The seed writing function implementation. * * \return \c 0 on success. *//* ... */intmbedtls_platform_set_nv_seed(int(*nv_seed_read_func)(unsignedchar*buf,size_tbuf_len),int(*nv_seed_write_func)(unsignedchar*buf,size_tbuf_len));/* ... */#else#ifdefined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO)&&\defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)#definembedtls_nv_seed_readMBEDTLS_PLATFORM_NV_SEED_READ_MACRO#definembedtls_nv_seed_writeMBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO/* ... */#else#definembedtls_nv_seed_readmbedtls_platform_std_nv_seed_read#definembedtls_nv_seed_writembedtls_platform_std_nv_seed_write/* ... */#endif/* ... */#endif/* MBEDTLS_PLATFORM_NV_SEED_ALT *//* ... */#endif/* MBEDTLS_ENTROPY_NV_SEED */#if!defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)/** * \brief The platform context structure. * * \note This structure may be used to assist platform-specific * setup or teardown operations. *//* ... */typedefstructmbedtls_platform_context{chardummy;/**< A placeholder member, as empty structs are not portable. */...}mbedtls_platform_context;/* ... */#else#include"platform_alt.h"#endif/* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT *//** * \brief This function performs any platform-specific initialization * operations. * * \note This function should be called before any other library functions. * * Its implementation is platform-specific, and unless * platform-specific code is provided, it does nothing. * * \note The usage and necessity of this function is dependent on the platform. * * \param ctx The platform context. * * \return \c 0 on success. *//* ... */intmbedtls_platform_setup(mbedtls_platform_context*ctx);/** * \brief This function performs any platform teardown operations. * * \note This function should be called after every other Mbed TLS module * has been correctly freed using the appropriate free function. * * Its implementation is platform-specific, and unless * platform-specific code is provided, it does nothing. * * \note The usage and necessity of this function is dependent on the platform. * * \param ctx The platform context. * *//* ... */voidmbedtls_platform_teardown(mbedtls_platform_context*ctx);#ifdef__cplusplus}extern "C" { ... }#endif/* ... */#endif/* platform.h */
Details
Show: from
Types: Columns:
All items filtered out
All items filtered out
This file uses the notable symbols shown below. Click anywhere in the file to view more details.