platform.h
mbedtls/config.h
mbedtls/platform_time.h
stdio.h
stdlib.h
time.h
mbedtls/check_config.h
mbedtls/config.h
platform.h
mbedtls/platform_time.h
limits.h
stdio.h
stdlib.h
time.h
mbedtls_platform_context
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.

MBEDTLS_PLATFORM_STD_SNPRINTF
snprintf

The default `snprintf` function to use.

MBEDTLS_PLATFORM_STD_VSNPRINTF
vsnprintf

The default `vsnprintf` function to use.

MBEDTLS_PLATFORM_STD_PRINTF
printf

The default `printf` function to use.

MBEDTLS_PLATFORM_STD_FPRINTF
fprintf

The default `fprintf` function to use.

MBEDTLS_PLATFORM_STD_CALLOC
calloc

Default allocator to use, can be undefined. It must initialize the allocated buffer memory to zeroes. The size of the buffer is the product of the two parameters. The calloc function returns either a null pointer or a pointer to the allocated space. If the product is 0, the function may either return NULL or a valid pointer to an array of size 0 which is a valid input to the deallocation function. An uninitialized MBEDTLS_PLATFORM_STD_CALLOC always fails, returning a null pointer. See the description of #MBEDTLS_PLATFORM_MEMORY for more details. The corresponding deallocation function is MBEDTLS_PLATFORM_STD_FREE.

The default `calloc` function to use.

MBEDTLS_PLATFORM_STD_FREE
free

Default free to use, can be undefined. NULL is a valid parameter, and the function must do nothing. A non-null parameter will always be a pointer previously returned by MBEDTLS_PLATFORM_STD_CALLOC and not yet freed. An uninitialized MBEDTLS_PLATFORM_STD_FREE does not do anything. See the description of #MBEDTLS_PLATFORM_MEMORY for more details (same principles as for MBEDTLS_PLATFORM_STD_CALLOC apply).

The default `free` function to use.

MBEDTLS_PLATFORM_STD_EXIT
exit

The default `exit` function to use.

MBEDTLS_PLATFORM_STD_TIME
time

The default `time` function to use.

MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
EXIT_SUCCESS

The default exit value to use.

MBEDTLS_PLATFORM_STD_EXIT_FAILURE
EXIT_FAILURE

The default exit value to use.

MBEDTLS_PLATFORM_STD_NV_SEED_READ
mbedtls_platform_std_nv_seed_read
MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
mbedtls_platform_std_nv_seed_write
MBEDTLS_PLATFORM_STD_NV_SEED_FILE
"seedfile"
MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
-0x0070

Hardware accelerator failed

MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED
-0x0072

The requested feature is not supported by the platform

mbedtls_free
free
mbedtls_calloc
calloc
mbedtls_fprintf
fprintf
mbedtls_printf
printf
mbedtls_snprintf
MBEDTLS_PLATFORM_STD_SNPRINTF
mbedtls_vsnprintf
vsnprintf
mbedtls_exit
exit
MBEDTLS_EXIT_SUCCESS
MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
MBEDTLS_EXIT_FAILURE
MBEDTLS_PLATFORM_STD_EXIT_FAILURE
struct mbedtls_platform_context typedef struct mbedtls_platform_context mbedtls_platform_context mbedtls_platform_context

The platform context structure.

This structure may be used to assist platform-specific setup or teardown operations.

int int mbedtls_platform_setup (mbedtls_platform_context *ctx) mbedtls_platform_setup mbedtls_platform_context * ctx

This function performs any platform-specific initialization operations.

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.

The usage and necessity of this function is dependent on the platform.

ctx

The platform context.

`0` on success.

void void mbedtls_platform_teardown (mbedtls_platform_context *ctx) mbedtls_platform_teardown mbedtls_platform_context * ctx

This function performs any platform teardown operations.

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.

The usage and necessity of this function is dependent on the platform.

ctx

The platform context.

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.

When all compilation options related to platform abstraction are disabled, this header just defines `mbedtls_xxx` function names as aliases to the standard `xxx` function.

Most modules in the library and example programs are expected to include this header.

    /*
    *CopyrightTheMbedTLSContributors
    *SPDX-License-Identifier:Apache-2.0ORGPL-2.0-or-later
    */
    #ifndefMBEDTLS_PLATFORM_H
    #defineMBEDTLS_PLATFORM_H

    #if!defined(MBEDTLS_CONFIG_FILE)
    #include"mbedtls/config.h"
    #else
    #includeMBEDTLS_CONFIG_FILE
    #endif

    #ifdefined(MBEDTLS_HAVE_TIME)
    #include"mbedtls/platform_time.h"
    #endif

    #defineMBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED-0x0070
    #defineMBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED-0x0072

    #ifdef__cplusplus
    extern"C"{
    #endif

    /*TheolderMicrosoftWindowscommonruntimeprovidesnon-conforming
    *implementationsofsomestandardlibraryfunctions,includingsnprintf
    *andvsnprintf.ThisaffectsMSVCandMinGWbuilds.
    */
    #ifdefined(__MINGW32__)||(defined(_MSC_VER)&&_MSC_VER<=1900)
    #defineMBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
    #defineMBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
    #endif

    #if!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
    #include<stdio.h>
    #include<stdlib.h>
    #ifdefined(MBEDTLS_HAVE_TIME)
    #include<time.h>
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
    #ifdefined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
    #defineMBEDTLS_PLATFORM_STD_SNPRINTFmbedtls_platform_win32_snprintf
    #else
    #defineMBEDTLS_PLATFORM_STD_SNPRINTFsnprintf
    #endif
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
    #ifdefined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
    #defineMBEDTLS_PLATFORM_STD_VSNPRINTFmbedtls_platform_win32_vsnprintf
    #else
    #defineMBEDTLS_PLATFORM_STD_VSNPRINTFvsnprintf
    #endif
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_PRINTF)
    #defineMBEDTLS_PLATFORM_STD_PRINTFprintf
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_FPRINTF)
    #defineMBEDTLS_PLATFORM_STD_FPRINTFfprintf
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_CALLOC)
    #defineMBEDTLS_PLATFORM_STD_CALLOCcalloc
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_FREE)
    #defineMBEDTLS_PLATFORM_STD_FREEfree
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_EXIT)
    #defineMBEDTLS_PLATFORM_STD_EXITexit
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_TIME)
    #defineMBEDTLS_PLATFORM_STD_TIMEtime
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
    #defineMBEDTLS_PLATFORM_STD_EXIT_SUCCESSEXIT_SUCCESS
    #endif
    #if!defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
    #defineMBEDTLS_PLATFORM_STD_EXIT_FAILUREEXIT_FAILURE
    #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*/

    /*Enablecertaindocumenteddefinesonlywhengeneratingdoxygentoavoid
    *an"unrecognizeddefine"error.*/
    #ifdefined(__DOXYGEN__)&&!defined(MBEDTLS_PLATFORM_STD_CALLOC)
    #defineMBEDTLS_PLATFORM_STD_CALLOC
    #endif

    #ifdefined(__DOXYGEN__)&&!defined(MBEDTLS_PLATFORM_STD_FREE)
    #defineMBEDTLS_PLATFORM_STD_FREE
    #endif

    /*
    *Thefunctionpointersforcallocandfree.
    *PleaseseeMBEDTLS_PLATFORM_STD_CALLOCandMBEDTLS_PLATFORM_STD_FREE
    *inmbedtls_config.hformoreinformationaboutbehaviourandrequirements.
    */
    #ifdefined(MBEDTLS_PLATFORM_MEMORY)
    #ifdefined(MBEDTLS_PLATFORM_FREE_MACRO)&&\
    defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
    #undefmbedtls_free
    #undefmbedtls_calloc
    #definembedtls_freeMBEDTLS_PLATFORM_FREE_MACRO
    #definembedtls_callocMBEDTLS_PLATFORM_CALLOC_MACRO
    #else
    /*Forsize_t*/
    #include<stddef.h>
    externvoid*mbedtls_calloc(size_tn,size_tsize);
    externvoidmbedtls_free(void*ptr);

    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*/
    #undefmbedtls_free
    #undefmbedtls_calloc
    #definembedtls_freefree
    #definembedtls_calloccalloc
    #endif/*MBEDTLS_PLATFORM_MEMORY&&!MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO*/

    /*
    *Thefunctionpointersforfprintf
    */
    #ifdefined(MBEDTLS_PLATFORM_FPRINTF_ALT)
    /*WeneedFILE**/
    #include<stdio.h>
    externint(*mbedtls_fprintf)(FILE*stream,constchar*format,...);

    intmbedtls_platform_set_fprintf(int(*fprintf_func)(FILE*stream,constchar*,
    ...));
    #else
    #undefmbedtls_fprintf
    #ifdefined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
    #definembedtls_fprintfMBEDTLS_PLATFORM_FPRINTF_MACRO
    #else
    #definembedtls_fprintffprintf
    #endif/*MBEDTLS_PLATFORM_FPRINTF_MACRO*/
    #endif/*MBEDTLS_PLATFORM_FPRINTF_ALT*/

    /*
    *Thefunctionpointersforprintf
    */
    #ifdefined(MBEDTLS_PLATFORM_PRINTF_ALT)
    externint(*mbedtls_printf)(constchar*format,...);

    intmbedtls_platform_set_printf(int(*printf_func)(constchar*,...));
    #else/*!MBEDTLS_PLATFORM_PRINTF_ALT*/
    #undefmbedtls_printf
    #ifdefined(MBEDTLS_PLATFORM_PRINTF_MACRO)
    #definembedtls_printfMBEDTLS_PLATFORM_PRINTF_MACRO
    #else
    #definembedtls_printfprintf
    #endif/*MBEDTLS_PLATFORM_PRINTF_MACRO*/
    #endif/*MBEDTLS_PLATFORM_PRINTF_ALT*/

    /*
    *Thefunctionpointersforsnprintf
    *
    *ThesnprintfimplementationshouldconformtoC99:
    *-it*must*alwayscorrectlyzero-terminatethebuffer
    *(exceptwhenn==0,thenitmustleavethebufferuntouched)
    *-howeveritisacceptabletoreturn-1insteadoftherequiredlengthwhen
    *thedestinationbufferistooshort.
    */
    #ifdefined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
    /*ForWindows(inc.MSYS2),weprovideourownfixedimplementation*/
    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,...);

    intmbedtls_platform_set_snprintf(int(*snprintf_func)(char*s,size_tn,
    constchar*format,...));
    #else/*MBEDTLS_PLATFORM_SNPRINTF_ALT*/
    #undefmbedtls_snprintf
    #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*/

    /*
    *Thefunctionpointersforvsnprintf
    *
    *ThevsnprintfimplementationshouldconformtoC99:
    *-it*must*alwayscorrectlyzero-terminatethebuffer
    *(exceptwhenn==0,thenitmustleavethebufferuntouched)
    *-howeveritisacceptabletoreturn-1insteadoftherequiredlengthwhen
    *thedestinationbufferistooshort.
    */
    #ifdefined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
    #include<stdarg.h>
    /*ForOlderWindows(inc.MSYS2),weprovideourownfixedimplementation*/
    intmbedtls_platform_win32_vsnprintf(char*s,size_tn,constchar*fmt,va_listarg);
    #endif

    #ifdefined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
    #include<stdarg.h>
    externint(*mbedtls_vsnprintf)(char*s,size_tn,constchar*format,va_listarg);

    intmbedtls_platform_set_vsnprintf(int(*vsnprintf_func)(char*s,size_tn,
    constchar*format,va_listarg));
    #else/*MBEDTLS_PLATFORM_VSNPRINTF_ALT*/
    #undefmbedtls_vsnprintf
    #ifdefined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
    #definembedtls_vsnprintfMBEDTLS_PLATFORM_VSNPRINTF_MACRO
    #else
    #definembedtls_vsnprintfvsnprintf
    #endif/*MBEDTLS_PLATFORM_VSNPRINTF_MACRO*/
    #endif/*MBEDTLS_PLATFORM_VSNPRINTF_ALT*/

    /*
    *Thefunctionpointersforexit
    */
    #ifdefined(MBEDTLS_PLATFORM_EXIT_ALT)
    externvoid(*mbedtls_exit)(intstatus);

    intmbedtls_platform_set_exit(void(*exit_func)(intstatus));
    #else
    #undefmbedtls_exit
    #ifdefined(MBEDTLS_PLATFORM_EXIT_MACRO)
    #definembedtls_exitMBEDTLS_PLATFORM_EXIT_MACRO
    #else
    #definembedtls_exitexit
    #endif/*MBEDTLS_PLATFORM_EXIT_MACRO*/
    #endif/*MBEDTLS_PLATFORM_EXIT_ALT*/

    /*
    *Thedefaultexitvalues
    */
    #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

    /*
    *Thefunctionpointersforreadingfromandwritingaseedfileto
    *Non-Volatilestorage(NV)inaplatform-independentway
    *
    *OnlyenabledwhentheNVseedentropysourceisenabled
    */
    #ifdefined(MBEDTLS_ENTROPY_NV_SEED)
    #if!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)&&defined(MBEDTLS_FS_IO)
    /*Internalstandardplatformdefinitions*/
    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);

    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
    #undefmbedtls_nv_seed_read
    #undefmbedtls_nv_seed_write
    #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)

    typedefstructmbedtls_platform_context{
    chardummy;
    }
    mbedtls_platform_context;

    #else
    #include"platform_alt.h"
    #endif/*!MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/

    intmbedtls_platform_setup(mbedtls_platform_context*ctx);
    voidmbedtls_platform_teardown(mbedtls_platform_context*ctx);

    #ifdef__cplusplus
    }
    #endif

    #endif/*platform.h*/