blob: 17639542b679710ce77419769b02f6a60f67bb44 [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/**
2 * \file platform.h
3 *
Rose Zadik4bca2b02018-03-27 13:12:52 +01004 * \brief This file contains the definitions and functions of the
5 * Mbed TLS platform abstraction layer.
Rose Zadik9464d7b2018-04-16 15:28:35 +01006 *
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
Darryl Green11999bb2018-03-13 15:22:58 +000012 * their platform, which can be statically linked to the library or
Rose Zadik9464d7b2018-04-16 15:28:35 +010013 * dynamically configured at runtime.
Gilles Peskine445aa5e2022-09-15 20:19:01 +020014 *
15 * When all compilation options related to platform abstraction are
16 * disabled, this header just defines `mbedtls_xxx` function names
17 * as aliases to the standard `xxx` function.
18 *
19 * Most modules in the library and example programs are expected to
20 * include this header.
Darryl Greena40a1012018-01-05 15:33:17 +000021 */
22/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020023 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000024 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker747a83a2014-02-01 22:50:07 +010025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_PLATFORM_H
27#define MBEDTLS_PLATFORM_H
Paul Bakker747a83a2014-02-01 22:50:07 +010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010034
Simon Butcherb5b6af22016-07-13 14:46:18 +010035#if defined(MBEDTLS_HAVE_TIME)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010037#endif
38
Gilles Peskinea3974432021-07-26 18:48:10 +020039/** Hardware accelerator failed */
40#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
41/** The requested feature is not supported by the platform */
42#define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
Ron Eldor0ff4e0b2018-08-29 18:53:20 +030043
Paul Bakker747a83a2014-02-01 22:50:07 +010044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker088c5c52014-04-25 11:11:10 +020048/**
49 * \name SECTION: Module settings
50 *
51 * The configuration options you can set for this module are in this section.
52 * Either change them in config.h or define them on the compiler command line.
53 * \{
54 */
55
k-stachowiak723f8672018-07-16 14:27:07 +020056/* The older Microsoft Windows common runtime provides non-conforming
57 * implementations of some standard library functions, including snprintf
58 * and vsnprintf. This affects MSVC and MinGW builds.
59 */
Krzysztof Stachowiak2cdb6b42018-10-22 10:43:56 +020060#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
k-stachowiak723f8672018-07-16 14:27:07 +020061#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
62#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
63#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000066#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010067#include <stdlib.h>
Daniel Axtens301db662020-05-28 11:43:41 +100068#if defined(MBEDTLS_HAVE_TIME)
Simon Butcher3fe6cd32016-04-26 19:51:29 +010069#include <time.h>
Daniel Axtens301db662020-05-28 11:43:41 +100070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
k-stachowiak723f8672018-07-16 14:27:07 +020072#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000073#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use. */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020074#else
Rose Zadik332658d2018-01-25 22:02:53 +000075#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
Rich Evans46b0a8d2015-01-30 10:47:32 +000076#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020077#endif
k-stachowiak723f8672018-07-16 14:27:07 +020078#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
79#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
80#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
81#else
82#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
83#endif
84#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000086#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000089#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020090#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020091#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Rose Zadik332658d2018-01-25 22:02:53 +000092#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Rose Zadik332658d2018-01-25 22:02:53 +000095#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +000098#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +000099#endif
SimonBd5800b72016-04-26 07:43:27 +0100100#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +0000101#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +0100102#endif
Janos Follath91947442016-03-18 13:49:27 +0000103#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +0000104#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000105#endif
106#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000107#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000108#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100109#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100110#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
111#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
112#endif
113#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
114#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
115#endif
116#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
117#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
118#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100119#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
121#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
122#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200125
Andrzej Kurek33b12222023-07-14 10:14:29 -0400126/* Enable certain documented defines only when generating doxygen to avoid
127 * an "unrecognized define" error. */
128#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
129#define MBEDTLS_PLATFORM_STD_CALLOC
130#endif
131
132#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
133#define MBEDTLS_PLATFORM_STD_FREE
134#endif
Paul Bakkere021a4b2016-06-01 11:25:44 +0100135
Andrzej Kurek73afe272022-01-24 10:31:06 -0500136/** \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100137
138/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100139 * The function pointers for calloc and free.
Andrzej Kurekba168592023-07-14 09:56:02 -0400140 * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
Andrzej Kurekdc11cd12023-07-14 09:47:05 -0400141 * in mbedtls_config.h for more information about behaviour and requirements.
Paul Bakker747a83a2014-02-01 22:50:07 +0100142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_PLATFORM_MEMORY)
144#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200145 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500146#undef mbedtls_free
147#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200149#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000150#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100151/* For size_t */
152#include <stddef.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153extern void *mbedtls_calloc(size_t n, size_t size);
154extern void mbedtls_free(void *ptr);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100155
156/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100157 * \brief This function dynamically sets the memory-management
158 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100159 *
Rose Zadik332658d2018-01-25 22:02:53 +0000160 * \param calloc_func The \c calloc function implementation.
161 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100162 *
Rose Zadik332658d2018-01-25 22:02:53 +0000163 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100164 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
166 void (*free_func)(void *));
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200167#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#else /* !MBEDTLS_PLATFORM_MEMORY */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500169#undef mbedtls_free
170#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200172#define mbedtls_calloc calloc
173#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100174
175/*
176 * The function pointers for fprintf
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100179/* We need FILE * */
180#include <stdio.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
Paul Bakker747a83a2014-02-01 22:50:07 +0100182
Rich Evansc39cb492015-01-30 12:01:34 +0000183/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100184 * \brief This function dynamically configures the fprintf
185 * function that is called when the
186 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000187 *
Rose Zadik332658d2018-01-25 22:02:53 +0000188 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000189 *
Rose Zadik332658d2018-01-25 22:02:53 +0000190 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000191 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
193 ...));
Paul Bakker747a83a2014-02-01 22:50:07 +0100194#else
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500195#undef mbedtls_fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
197#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000198#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#define mbedtls_fprintf fprintf
200#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
201#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000202
203/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000204 * The function pointers for printf
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207extern int (*mbedtls_printf)(const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000208
209/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100210 * \brief This function dynamically configures the snprintf
211 * function that is called when the mbedtls_snprintf()
212 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000213 *
Rose Zadik332658d2018-01-25 22:02:53 +0000214 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000215 *
Rose Zadik332658d2018-01-25 22:02:53 +0000216 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000217 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500220#undef mbedtls_printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
222#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000223#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#define mbedtls_printf printf
225#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
226#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000227
228/*
229 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200230 *
231 * The snprintf implementation should conform to C99:
232 * - it *must* always correctly zero-terminate the buffer
233 * (except when n == 0, then it must leave the buffer untouched)
234 * - however it is acceptable to return -1 instead of the required length when
235 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000236 */
k-stachowiak723f8672018-07-16 14:27:07 +0200237#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200238/* For Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200240#endif
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000244
245/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100246 * \brief This function allows configuring a custom
247 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000248 *
Rose Zadik332658d2018-01-25 22:02:53 +0000249 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000250 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100251 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000252 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
254 const char *format, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500256#undef mbedtls_snprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
258#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000259#else
Azim Khan8d54c062018-03-23 18:34:35 +0000260#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
262#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000263
264/*
k-stachowiak723f8672018-07-16 14:27:07 +0200265 * The function pointers for vsnprintf
266 *
267 * The vsnprintf implementation should conform to C99:
268 * - it *must* always correctly zero-terminate the buffer
269 * (except when n == 0, then it must leave the buffer untouched)
270 * - however it is acceptable to return -1 instead of the required length when
271 * the destination buffer is too short.
272 */
273#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100274#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200275/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200277#endif
278
279#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
280#include <stdarg.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200282
283/**
284 * \brief Set your own snprintf function pointer
285 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200286 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200287 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200288 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200289 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
291 const char *format, va_list arg));
k-stachowiak723f8672018-07-16 14:27:07 +0200292#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500293#undef mbedtls_vsnprintf
k-stachowiak723f8672018-07-16 14:27:07 +0200294#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
295#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
296#else
297#define mbedtls_vsnprintf vsnprintf
298#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
299#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
300
301/*
Rich Evansc39cb492015-01-30 12:01:34 +0000302 * The function pointers for exit
303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305extern void (*mbedtls_exit)(int status);
Rich Evansc39cb492015-01-30 12:01:34 +0000306
307/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100308 * \brief This function dynamically configures the exit
309 * function that is called when the mbedtls_exit()
310 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000311 *
Rose Zadik332658d2018-01-25 22:02:53 +0000312 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000313 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100314 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000315 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316int mbedtls_platform_set_exit(void (*exit_func)(int status));
Rich Evansc39cb492015-01-30 12:01:34 +0000317#else
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500318#undef mbedtls_exit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
320#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000321#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#define mbedtls_exit exit
323#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
324#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100325
Janos Follath91947442016-03-18 13:49:27 +0000326/*
327 * The default exit values
328 */
329#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
330#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
331#else
332#define MBEDTLS_EXIT_SUCCESS 0
333#endif
334#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
335#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
336#else
337#define MBEDTLS_EXIT_FAILURE 1
338#endif
339
SimonBd5800b72016-04-26 07:43:27 +0100340/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100341 * The function pointers for reading from and writing a seed file to
342 * Non-Volatile storage (NV) in a platform-independent way
343 *
344 * Only enabled when the NV seed entropy source is enabled
345 */
346#if defined(MBEDTLS_ENTROPY_NV_SEED)
347#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
348/* Internal standard platform definitions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
350int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100351#endif
352
353#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
355extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100356
357/**
Rose Zadik332658d2018-01-25 22:02:53 +0000358 * \brief This function allows configuring custom seed file writing and
359 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100360 *
Rose Zadik332658d2018-01-25 22:02:53 +0000361 * \param nv_seed_read_func The seed reading function implementation.
362 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100363 *
Rose Zadik332658d2018-01-25 22:02:53 +0000364 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100365 */
366int mbedtls_platform_set_nv_seed(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
368 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
369 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100370#else
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500371#undef mbedtls_nv_seed_read
372#undef mbedtls_nv_seed_write
Paul Bakkere021a4b2016-06-01 11:25:44 +0100373#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
374 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
375#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
376#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
377#else
378#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
379#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
380#endif
381#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
382#endif /* MBEDTLS_ENTROPY_NV_SEED */
383
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100384#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100385
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100386/**
Rose Zadik332658d2018-01-25 22:02:53 +0000387 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100388 *
389 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000390 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100391 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392typedef struct mbedtls_platform_context {
Rose Zadik4bca2b02018-03-27 13:12:52 +0100393 char dummy; /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100394}
395mbedtls_platform_context;
396
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100397#else
398#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100399#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100400
401/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100402 * \brief This function performs any platform-specific initialization
403 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100404 *
405 * \note This function should be called before any other library functions.
406 *
407 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000408 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100409 *
410 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100411 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100412 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100413 *
Rose Zadik332658d2018-01-25 22:02:53 +0000414 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416int mbedtls_platform_setup(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100417/**
Rose Zadik332658d2018-01-25 22:02:53 +0000418 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100419 *
Rose Zadik332658d2018-01-25 22:02:53 +0000420 * \note This function should be called after every other Mbed TLS module
421 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100422 *
Rose Zadik332658d2018-01-25 22:02:53 +0000423 * Its implementation is platform-specific, and unless
424 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200425 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100426 * \note The usage and necessity of this function is dependent on the platform.
427 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100428 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100429 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100430 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431void mbedtls_platform_teardown(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100432
Paul Bakker747a83a2014-02-01 22:50:07 +0100433#ifdef __cplusplus
434}
435#endif
436
437#endif /* platform.h */