2014-02-01 22:50:07 +01:00
|
|
|
/**
|
|
|
|
* \file platform.h
|
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \brief This file contains the definitions and functions of the
|
|
|
|
* Mbed TLS platform abstraction layer.
|
2018-04-16 16:28:35 +02:00
|
|
|
*
|
|
|
|
* 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
|
2018-03-13 16:22:58 +01:00
|
|
|
* their platform, which can be statically linked to the library or
|
2018-04-16 16:28:35 +02:00
|
|
|
* dynamically configured at runtime.
|
2022-09-15 20:19:01 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2018-01-05 16:33:17 +01:00
|
|
|
*/
|
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2014-02-01 22:50:07 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#ifndef MBEDTLS_PLATFORM_H
|
|
|
|
#define MBEDTLS_PLATFORM_H
|
2021-05-19 19:44:07 +02:00
|
|
|
#include "mbedtls/private_access.h"
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2016-07-13 15:46:18 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2019-07-04 21:01:14 +02:00
|
|
|
#include "mbedtls/platform_time.h"
|
2016-07-13 15:46:18 +02:00
|
|
|
#endif
|
|
|
|
|
2014-02-01 22:50:07 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-04-25 11:11:10 +02:00
|
|
|
/**
|
|
|
|
* \name SECTION: Module settings
|
|
|
|
*
|
|
|
|
* The configuration options you can set for this module are in this section.
|
2021-05-28 09:42:25 +02:00
|
|
|
* Either change them in mbedtls_config.h or define them on the compiler command line.
|
2014-04-25 11:11:10 +02:00
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
2018-07-16 14:27:07 +02:00
|
|
|
/* The older Microsoft Windows common runtime provides non-conforming
|
|
|
|
* implementations of some standard library functions, including snprintf
|
|
|
|
* and vsnprintf. This affects MSVC and MinGW builds.
|
|
|
|
*/
|
2018-10-22 10:43:56 +02:00
|
|
|
#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
|
2018-07-16 14:27:07 +02:00
|
|
|
#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
|
|
|
|
#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2014-02-05 15:13:04 +01:00
|
|
|
#include <stdlib.h>
|
2020-05-28 03:43:41 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2016-04-26 20:51:29 +02:00
|
|
|
#include <time.h>
|
2020-05-28 03:43:41 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
|
2018-07-16 14:27:07 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */
|
2015-06-22 10:23:34 +02:00
|
|
|
#else
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
|
2015-01-30 11:47:32 +01:00
|
|
|
#endif
|
2015-06-22 10:23:34 +02:00
|
|
|
#endif
|
2018-07-16 14:27:07 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
|
|
|
|
#endif
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
|
2014-04-25 11:11:10 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
|
2014-04-25 11:11:10 +02:00
|
|
|
#endif
|
2015-05-26 16:15:20 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
|
2014-04-25 11:11:10 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
|
2014-04-25 11:11:10 +02:00
|
|
|
#endif
|
2022-06-30 17:01:40 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_SETBUF setbuf /**< The default \c setbuf function to use. */
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
|
2016-03-18 14:49:27 +01:00
|
|
|
#endif
|
2016-04-26 08:43:27 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_TIME)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
|
2016-04-26 08:43:27 +02:00
|
|
|
#endif
|
2016-03-18 14:49:27 +01:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
|
2016-03-18 14:49:27 +01:00
|
|
|
#endif
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
|
2018-01-25 23:02:53 +01:00
|
|
|
#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
|
2015-01-30 13:01:34 +01:00
|
|
|
#endif
|
2016-06-01 12:44:12 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2016-06-01 12:25:44 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
|
|
|
|
#endif
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
|
|
|
|
#endif
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
|
|
|
|
#endif
|
2016-06-01 12:44:12 +02:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2015-04-08 12:49:31 +02:00
|
|
|
#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
|
|
|
|
#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
|
|
|
|
#include MBEDTLS_PLATFORM_STD_MEM_HDR
|
2014-04-02 13:43:48 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
|
2014-04-25 11:11:10 +02:00
|
|
|
|
2023-07-03 12:52:37 +02:00
|
|
|
/* Enable certain documented defines only when generating doxygen to avoid
|
|
|
|
* an "unrecognized define" error. */
|
|
|
|
#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_CALLOC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
|
|
|
|
#define MBEDTLS_PLATFORM_STD_FREE
|
|
|
|
#endif
|
2016-06-01 12:25:44 +02:00
|
|
|
|
2021-12-28 16:22:52 +01:00
|
|
|
/** \} name SECTION: Module settings */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
|
|
|
/*
|
2018-03-27 14:12:52 +02:00
|
|
|
* The function pointers for calloc and free.
|
2023-05-04 23:07:57 +02:00
|
|
|
* Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
|
2023-04-27 15:19:35 +02:00
|
|
|
* in mbedtls_config.h for more information about behaviour and requirements.
|
2014-02-01 22:50:07 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_MEMORY)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
|
2015-05-26 16:15:20 +02:00
|
|
|
defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_free
|
|
|
|
#undef mbedtls_calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
|
2015-05-26 16:15:20 +02:00
|
|
|
#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
|
2015-02-10 13:28:15 +01:00
|
|
|
#else
|
2015-06-03 11:33:55 +02:00
|
|
|
/* For size_t */
|
|
|
|
#include <stddef.h>
|
2023-01-11 14:50:10 +01:00
|
|
|
extern void *mbedtls_calloc(size_t n, size_t size);
|
|
|
|
extern void mbedtls_free(void *ptr);
|
2014-02-04 17:30:24 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-16 16:28:35 +02:00
|
|
|
* \brief This function dynamically sets the memory-management
|
|
|
|
* functions used by the library, during runtime.
|
2014-02-04 17:30:24 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \param calloc_func The \c calloc function implementation.
|
|
|
|
* \param free_func The \c free function implementation.
|
2014-02-04 17:30:24 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \return \c 0.
|
2014-02-04 17:30:24 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
|
|
|
|
void (*free_func)(void *));
|
2015-05-26 16:15:20 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
|
2015-04-08 12:49:31 +02:00
|
|
|
#else /* !MBEDTLS_PLATFORM_MEMORY */
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_free
|
|
|
|
#undef mbedtls_calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2015-05-26 16:15:20 +02:00
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2015-01-30 11:47:32 +01:00
|
|
|
/*
|
2015-02-06 17:14:34 +01:00
|
|
|
* The function pointers for fprintf
|
2015-01-30 11:47:32 +01:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
|
2015-06-03 11:33:55 +02:00
|
|
|
/* We need FILE * */
|
|
|
|
#include <stdio.h>
|
2023-01-11 14:50:10 +01:00
|
|
|
extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
|
2015-01-30 11:47:32 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-16 16:28:35 +02:00
|
|
|
* \brief This function dynamically configures the fprintf
|
|
|
|
* function that is called when the
|
|
|
|
* mbedtls_fprintf() function is invoked by the library.
|
2015-01-30 11:47:32 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \param fprintf_func The \c fprintf function implementation.
|
2015-01-30 11:47:32 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \return \c 0.
|
2015-01-30 11:47:32 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
|
|
|
|
...));
|
2015-02-06 17:14:34 +01:00
|
|
|
#else
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_fprintf
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
|
|
|
|
#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
|
2015-02-06 17:14:34 +01:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_fprintf fprintf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
|
|
|
|
#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
|
2015-01-30 11:47:32 +01:00
|
|
|
|
2014-02-01 22:50:07 +01:00
|
|
|
/*
|
|
|
|
* The function pointers for printf
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
|
2023-01-11 14:50:10 +01:00
|
|
|
extern int (*mbedtls_printf)(const char *format, ...);
|
2014-02-01 22:50:07 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-16 16:28:35 +02:00
|
|
|
* \brief This function dynamically configures the snprintf
|
|
|
|
* function that is called when the mbedtls_snprintf()
|
|
|
|
* function is invoked by the library.
|
2014-02-01 22:50:07 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \param printf_func The \c printf function implementation.
|
2014-02-01 22:50:07 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \return \c 0 on success.
|
2014-02-01 22:50:07 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
|
2015-04-08 12:49:31 +02:00
|
|
|
#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_printf
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
|
|
|
|
#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
|
2015-02-06 17:14:34 +01:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
|
|
|
|
#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
|
|
|
/*
|
2015-02-06 17:14:34 +01:00
|
|
|
* The function pointers for snprintf
|
2015-06-22 10:23:34 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2014-02-01 22:50:07 +01:00
|
|
|
*/
|
2018-07-16 14:27:07 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
|
2015-06-22 10:23:34 +02:00
|
|
|
/* For Windows (inc. MSYS2), we provide our own fixed implementation */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
|
2015-06-22 10:23:34 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
|
2023-01-11 14:50:10 +01:00
|
|
|
extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2015-01-30 13:01:34 +01:00
|
|
|
/**
|
2018-03-27 14:12:52 +02:00
|
|
|
* \brief This function allows configuring a custom
|
|
|
|
* \c snprintf function pointer.
|
2015-01-30 13:01:34 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \param snprintf_func The \c snprintf function implementation.
|
2015-01-30 13:01:34 +01:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \return \c 0 on success.
|
2015-01-30 13:01:34 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
|
|
|
|
const char *format, ...));
|
2015-04-08 12:49:31 +02:00
|
|
|
#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_snprintf
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
|
|
|
|
#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
|
2014-02-01 22:50:07 +01:00
|
|
|
#else
|
2018-03-23 19:34:35 +01:00
|
|
|
#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
|
|
|
|
#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
|
2015-01-30 13:01:34 +01:00
|
|
|
|
2018-07-16 14:27:07 +02:00
|
|
|
/*
|
|
|
|
* The function pointers for vsnprintf
|
|
|
|
*
|
|
|
|
* The vsnprintf 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.
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
|
2019-05-30 14:18:24 +02:00
|
|
|
#include <stdarg.h>
|
2018-07-16 14:27:07 +02:00
|
|
|
/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
|
2018-07-16 14:27:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
|
|
|
|
#include <stdarg.h>
|
2023-01-11 14:50:10 +01:00
|
|
|
extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
|
2018-07-16 14:27:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set your own snprintf function pointer
|
|
|
|
*
|
2018-09-24 14:15:46 +02:00
|
|
|
* \param vsnprintf_func The \c vsnprintf function implementation
|
2018-07-16 14:27:07 +02:00
|
|
|
*
|
2018-09-24 14:15:46 +02:00
|
|
|
* \return \c 0
|
2018-07-16 14:27:07 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
|
|
|
|
const char *format, va_list arg));
|
2018-07-16 14:27:07 +02:00
|
|
|
#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_vsnprintf
|
2018-07-16 14:27:07 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
|
|
|
|
#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
|
|
|
|
#else
|
|
|
|
#define mbedtls_vsnprintf vsnprintf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
|
|
|
|
#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
|
|
|
|
|
2022-06-30 17:01:40 +02:00
|
|
|
/*
|
|
|
|
* The function pointers for setbuf
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
|
|
|
|
#include <stdio.h>
|
|
|
|
/**
|
|
|
|
* \brief Function pointer to call for `setbuf()` functionality
|
|
|
|
* (changing the internal buffering on stdio calls).
|
|
|
|
*
|
|
|
|
* \note The library calls this function to disable
|
|
|
|
* buffering when reading or writing sensitive data,
|
|
|
|
* to avoid having extra copies of sensitive data
|
|
|
|
* remaining in stdio buffers after the file is
|
|
|
|
* closed. If this is not a concern, for example if
|
|
|
|
* your platform's stdio doesn't have any buffering,
|
|
|
|
* you can set mbedtls_setbuf to a function that
|
|
|
|
* does nothing.
|
|
|
|
*
|
2022-06-30 19:32:02 +02:00
|
|
|
* The library always calls this function with
|
|
|
|
* `buf` equal to `NULL`.
|
2022-06-30 17:01:40 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
extern void (*mbedtls_setbuf)(FILE *stream, char *buf);
|
2022-06-30 17:01:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Dynamically configure the function that is called
|
|
|
|
* when the mbedtls_setbuf() function is called by the
|
|
|
|
* library.
|
|
|
|
*
|
|
|
|
* \param setbuf_func The \c setbuf function implementation
|
|
|
|
*
|
|
|
|
* \return \c 0
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_setbuf(void (*setbuf_func)(
|
|
|
|
FILE *stream, char *buf));
|
2023-01-17 19:26:35 +01:00
|
|
|
#else
|
|
|
|
#undef mbedtls_setbuf
|
|
|
|
#if defined(MBEDTLS_PLATFORM_SETBUF_MACRO)
|
2022-06-30 17:01:40 +02:00
|
|
|
/**
|
|
|
|
* \brief Macro defining the function for the library to
|
|
|
|
* call for `setbuf` functionality (changing the
|
|
|
|
* internal buffering on stdio calls).
|
|
|
|
*
|
|
|
|
* \note See extra comments on the mbedtls_setbuf() function
|
|
|
|
* pointer above.
|
|
|
|
*
|
|
|
|
* \return \c 0 on success, negative on error.
|
|
|
|
*/
|
|
|
|
#define mbedtls_setbuf MBEDTLS_PLATFORM_SETBUF_MACRO
|
|
|
|
#else
|
|
|
|
#define mbedtls_setbuf setbuf
|
2023-01-17 19:26:35 +01:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SETBUF_MACRO */
|
|
|
|
#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
|
2022-06-30 17:01:40 +02:00
|
|
|
|
2015-01-30 13:01:34 +01:00
|
|
|
/*
|
|
|
|
* The function pointers for exit
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
|
2023-01-11 14:50:10 +01:00
|
|
|
extern void (*mbedtls_exit)(int status);
|
2015-01-30 13:01:34 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-16 16:28:35 +02:00
|
|
|
* \brief This function dynamically configures the exit
|
|
|
|
* function that is called when the mbedtls_exit()
|
|
|
|
* function is invoked by the library.
|
2015-01-30 13:01:34 +01:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \param exit_func The \c exit function implementation.
|
2015-01-30 13:01:34 +01:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \return \c 0 on success.
|
2015-01-30 13:01:34 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_exit(void (*exit_func)(int status));
|
2015-01-30 13:01:34 +01:00
|
|
|
#else
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_exit
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
|
|
|
|
#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
|
2015-02-06 17:14:34 +01:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_exit exit
|
|
|
|
#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
|
|
|
|
#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2016-03-18 14:49:27 +01:00
|
|
|
/*
|
|
|
|
* The default exit values
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
|
|
|
|
#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_EXIT_SUCCESS 0
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
|
|
|
|
#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_EXIT_FAILURE 1
|
|
|
|
#endif
|
|
|
|
|
2016-06-01 12:25:44 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
|
|
|
|
/* Internal standard platform definitions */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
|
|
|
|
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
|
2016-06-01 12:25:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
|
2023-01-11 14:50:10 +01:00
|
|
|
extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
|
|
|
|
extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
|
2016-06-01 12:25:44 +02:00
|
|
|
|
|
|
|
/**
|
2018-01-25 23:02:53 +01:00
|
|
|
* \brief This function allows configuring custom seed file writing and
|
|
|
|
* reading functions.
|
2016-06-01 12:25:44 +02:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \param nv_seed_read_func The seed reading function implementation.
|
|
|
|
* \param nv_seed_write_func The seed writing function implementation.
|
2016-06-01 12:25:44 +02:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \return \c 0 on success.
|
2016-06-01 12:25:44 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_platform_set_nv_seed(
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
|
|
|
|
int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
|
|
|
|
);
|
2016-06-01 12:25:44 +02:00
|
|
|
#else
|
2023-01-17 19:26:35 +01:00
|
|
|
#undef mbedtls_nv_seed_read
|
|
|
|
#undef mbedtls_nv_seed_write
|
2016-06-01 12:25:44 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
|
|
|
|
defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
|
|
|
|
#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
|
|
|
|
#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
|
|
|
|
#else
|
|
|
|
#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
|
|
|
|
#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
|
|
|
|
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
|
|
|
|
2017-07-18 11:23:04 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
2017-07-12 12:32:40 +02:00
|
|
|
|
|
|
|
/**
|
2018-01-25 23:02:53 +01:00
|
|
|
* \brief The platform context structure.
|
2017-07-12 12:32:40 +02:00
|
|
|
*
|
|
|
|
* \note This structure may be used to assist platform-specific
|
2018-01-25 23:02:53 +01:00
|
|
|
* setup or teardown operations.
|
2017-07-12 12:32:40 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct mbedtls_platform_context {
|
2021-05-19 19:44:07 +02:00
|
|
|
char MBEDTLS_PRIVATE(dummy); /**< A placeholder member, as empty structs are not portable. */
|
2017-07-12 12:32:40 +02:00
|
|
|
}
|
|
|
|
mbedtls_platform_context;
|
2017-07-12 12:25:17 +02:00
|
|
|
|
2017-07-07 14:03:23 +02:00
|
|
|
#else
|
|
|
|
#include "platform_alt.h"
|
2017-07-18 11:23:04 +02:00
|
|
|
#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
|
2017-07-07 14:03:23 +02:00
|
|
|
|
|
|
|
/**
|
2018-04-16 16:28:35 +02:00
|
|
|
* \brief This function performs any platform-specific initialization
|
|
|
|
* operations.
|
2017-07-07 14:03:23 +02:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \note This function should be called before any other library functions.
|
2017-07-07 14:03:23 +02:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* Its implementation is platform-specific, and unless
|
2018-03-13 16:22:58 +01:00
|
|
|
* platform-specific code is provided, it does nothing.
|
2017-07-07 14:03:23 +02:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \note The usage and necessity of this function is dependent on the platform.
|
|
|
|
*
|
2018-04-16 16:28:35 +02:00
|
|
|
* \param ctx The platform context.
|
2017-07-21 02:08:00 +02:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \return \c 0 on success.
|
2017-07-07 14:03:23 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_setup(mbedtls_platform_context *ctx);
|
2017-07-07 14:03:23 +02:00
|
|
|
/**
|
2018-01-25 23:02:53 +01:00
|
|
|
* \brief This function performs any platform teardown operations.
|
2017-07-07 14:03:23 +02:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* \note This function should be called after every other Mbed TLS module
|
|
|
|
* has been correctly freed using the appropriate free function.
|
2018-03-27 14:12:52 +02:00
|
|
|
*
|
2018-01-25 23:02:53 +01:00
|
|
|
* Its implementation is platform-specific, and unless
|
|
|
|
* platform-specific code is provided, it does nothing.
|
2017-07-21 02:08:00 +02:00
|
|
|
*
|
2018-03-27 14:12:52 +02:00
|
|
|
* \note The usage and necessity of this function is dependent on the platform.
|
|
|
|
*
|
2018-04-16 16:28:35 +02:00
|
|
|
* \param ctx The platform context.
|
2018-03-27 14:12:52 +02:00
|
|
|
*
|
2017-07-07 14:03:23 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_platform_teardown(mbedtls_platform_context *ctx);
|
2017-07-07 14:03:23 +02:00
|
|
|
|
2014-02-01 22:50:07 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* platform.h */
|