blob: de3d71d9dc5be5748276604adf58f972cda401ca [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 Peskine04e4c192022-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 Rodgman16799db2023-11-02 19:47:20 +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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010029
Bence Szépkútic662b362021-05-27 11:25:03 +020030#include "mbedtls/build_info.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010031
Simon Butcherb5b6af22016-07-13 14:46:18 +010032#if defined(MBEDTLS_HAVE_TIME)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010033#include "mbedtls/platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010034#endif
35
Paul Bakker747a83a2014-02-01 22:50:07 +010036#ifdef __cplusplus
37extern "C" {
38#endif
39
Paul Bakker088c5c52014-04-25 11:11:10 +020040/**
41 * \name SECTION: Module settings
42 *
43 * The configuration options you can set for this module are in this section.
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020044 * Either change them in mbedtls_config.h or define them on the compiler command line.
Paul Bakker088c5c52014-04-25 11:11:10 +020045 * \{
46 */
47
k-stachowiak723f8672018-07-16 14:27:07 +020048/* The older Microsoft Windows common runtime provides non-conforming
49 * implementations of some standard library functions, including snprintf
50 * and vsnprintf. This affects MSVC and MinGW builds.
51 */
Krzysztof Stachowiak2cdb6b42018-10-22 10:43:56 +020052#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
k-stachowiak723f8672018-07-16 14:27:07 +020053#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
54#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
55#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010059#include <stdlib.h>
Daniel Axtensf0710242020-05-28 11:43:41 +100060#if defined(MBEDTLS_HAVE_TIME)
Simon Butcher3fe6cd32016-04-26 19:51:29 +010061#include <time.h>
Daniel Axtensf0710242020-05-28 11:43:41 +100062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
k-stachowiak723f8672018-07-16 14:27:07 +020064#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000065#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 +020066#else
Rose Zadik332658d2018-01-25 22:02:53 +000067#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
Rich Evans46b0a8d2015-01-30 10:47:32 +000068#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020069#endif
k-stachowiak723f8672018-07-16 14:27:07 +020070#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
71#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
72#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
73#else
74#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
75#endif
76#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000078#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000081#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020082#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020083#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Rose Zadik332658d2018-01-25 22:02:53 +000084#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020085#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Rose Zadik332658d2018-01-25 22:02:53 +000087#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020088#endif
Gilles Peskine6497b5a2022-06-30 17:01:40 +020089#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
90#define MBEDTLS_PLATFORM_STD_SETBUF setbuf /**< The default \c setbuf function to use. */
91#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +000093#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +000094#endif
SimonBd5800b72016-04-26 07:43:27 +010095#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +000096#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +010097#endif
Janos Follath91947442016-03-18 13:49:27 +000098#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +000099#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000100#endif
101#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000102#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000103#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100104#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100105#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
106#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
107#endif
108#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
109#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
110#endif
111#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
112#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
113#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100114#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
116#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
117#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200118#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200120
Andrzej Kurek2b3c06e2023-07-03 06:52:37 -0400121/* Enable certain documented defines only when generating doxygen to avoid
122 * an "unrecognized define" error. */
123#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
124#define MBEDTLS_PLATFORM_STD_CALLOC
125#endif
126
127#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
128#define MBEDTLS_PLATFORM_STD_FREE
129#endif
Paul Bakkere021a4b2016-06-01 11:25:44 +0100130
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100131/** \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100132
133/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100134 * The function pointers for calloc and free.
Andrzej Kurekecaf6fb2023-05-04 17:07:57 -0400135 * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
Andrzej Kurek2d981f02023-04-27 09:19:35 -0400136 * in mbedtls_config.h for more information about behaviour and requirements.
Paul Bakker747a83a2014-02-01 22:50:07 +0100137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_PLATFORM_MEMORY)
139#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200140 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500141#undef mbedtls_free
142#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200144#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000145#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100146/* For size_t */
147#include <stddef.h>
Gilles Peskine449bd832023-01-11 14:50:10 +0100148extern void *mbedtls_calloc(size_t n, size_t size);
149extern void mbedtls_free(void *ptr);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100150
151/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100152 * \brief This function dynamically sets the memory-management
153 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100154 *
Rose Zadik332658d2018-01-25 22:02:53 +0000155 * \param calloc_func The \c calloc function implementation.
156 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100157 *
Rose Zadik332658d2018-01-25 22:02:53 +0000158 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
161 void (*free_func)(void *));
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200162#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#else /* !MBEDTLS_PLATFORM_MEMORY */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500164#undef mbedtls_free
165#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200167#define mbedtls_calloc calloc
168#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100169
170/*
171 * The function pointers for fprintf
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100174/* We need FILE * */
175#include <stdio.h>
Gilles Peskine449bd832023-01-11 14:50:10 +0100176extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
Paul Bakker747a83a2014-02-01 22:50:07 +0100177
Rich Evansc39cb492015-01-30 12:01:34 +0000178/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100179 * \brief This function dynamically configures the fprintf
180 * function that is called when the
181 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000182 *
Rose Zadik332658d2018-01-25 22:02:53 +0000183 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000184 *
Rose Zadik332658d2018-01-25 22:02:53 +0000185 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
188 ...));
Paul Bakker747a83a2014-02-01 22:50:07 +0100189#else
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500190#undef mbedtls_fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
192#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000193#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#define mbedtls_fprintf fprintf
195#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
196#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000197
198/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000199 * The function pointers for printf
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100202extern int (*mbedtls_printf)(const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000203
204/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100205 * \brief This function dynamically configures the snprintf
206 * function that is called when the mbedtls_snprintf()
207 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000208 *
Rose Zadik332658d2018-01-25 22:02:53 +0000209 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000210 *
Rose Zadik332658d2018-01-25 22:02:53 +0000211 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000212 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500215#undef mbedtls_printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
217#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000218#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#define mbedtls_printf printf
220#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
221#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000222
223/*
224 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200225 *
226 * The snprintf implementation should conform to C99:
227 * - it *must* always correctly zero-terminate the buffer
228 * (except when n == 0, then it must leave the buffer untouched)
229 * - however it is acceptable to return -1 instead of the required length when
230 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000231 */
k-stachowiak723f8672018-07-16 14:27:07 +0200232#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200233/* For Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200235#endif
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100238extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000239
240/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100241 * \brief This function allows configuring a custom
242 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000243 *
Rose Zadik332658d2018-01-25 22:02:53 +0000244 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000245 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100246 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
249 const char *format, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500251#undef mbedtls_snprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
253#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000254#else
Azim Khan8d54c062018-03-23 18:34:35 +0000255#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
257#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000258
259/*
k-stachowiak723f8672018-07-16 14:27:07 +0200260 * The function pointers for vsnprintf
261 *
262 * The vsnprintf implementation should conform to C99:
263 * - it *must* always correctly zero-terminate the buffer
264 * (except when n == 0, then it must leave the buffer untouched)
265 * - however it is acceptable to return -1 instead of the required length when
266 * the destination buffer is too short.
267 */
268#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100269#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200270/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200272#endif
273
274#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
275#include <stdarg.h>
Gilles Peskine449bd832023-01-11 14:50:10 +0100276extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200277
278/**
279 * \brief Set your own snprintf function pointer
280 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200281 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200282 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200283 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
286 const char *format, va_list arg));
k-stachowiak723f8672018-07-16 14:27:07 +0200287#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500288#undef mbedtls_vsnprintf
k-stachowiak723f8672018-07-16 14:27:07 +0200289#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
290#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
291#else
292#define mbedtls_vsnprintf vsnprintf
293#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
294#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
295
296/*
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200297 * The function pointers for setbuf
298 */
299#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
300#include <stdio.h>
301/**
302 * \brief Function pointer to call for `setbuf()` functionality
303 * (changing the internal buffering on stdio calls).
304 *
305 * \note The library calls this function to disable
306 * buffering when reading or writing sensitive data,
307 * to avoid having extra copies of sensitive data
308 * remaining in stdio buffers after the file is
309 * closed. If this is not a concern, for example if
310 * your platform's stdio doesn't have any buffering,
311 * you can set mbedtls_setbuf to a function that
312 * does nothing.
313 *
Gilles Peskine0bd76ee2022-06-30 19:32:02 +0200314 * The library always calls this function with
315 * `buf` equal to `NULL`.
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200316 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317extern void (*mbedtls_setbuf)(FILE *stream, char *buf);
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200318
319/**
320 * \brief Dynamically configure the function that is called
321 * when the mbedtls_setbuf() function is called by the
322 * library.
323 *
324 * \param setbuf_func The \c setbuf function implementation
325 *
326 * \return \c 0
327 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328int mbedtls_platform_set_setbuf(void (*setbuf_func)(
329 FILE *stream, char *buf));
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500330#else
331#undef mbedtls_setbuf
332#if defined(MBEDTLS_PLATFORM_SETBUF_MACRO)
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200333/**
334 * \brief Macro defining the function for the library to
335 * call for `setbuf` functionality (changing the
336 * internal buffering on stdio calls).
337 *
338 * \note See extra comments on the mbedtls_setbuf() function
339 * pointer above.
340 *
341 * \return \c 0 on success, negative on error.
342 */
343#define mbedtls_setbuf MBEDTLS_PLATFORM_SETBUF_MACRO
344#else
345#define mbedtls_setbuf setbuf
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500346#endif /* MBEDTLS_PLATFORM_SETBUF_MACRO */
347#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200348
349/*
Rich Evansc39cb492015-01-30 12:01:34 +0000350 * The function pointers for exit
351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100353extern void (*mbedtls_exit)(int status);
Rich Evansc39cb492015-01-30 12:01:34 +0000354
355/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100356 * \brief This function dynamically configures the exit
357 * function that is called when the mbedtls_exit()
358 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000359 *
Rose Zadik332658d2018-01-25 22:02:53 +0000360 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000361 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100362 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364int mbedtls_platform_set_exit(void (*exit_func)(int status));
Rich Evansc39cb492015-01-30 12:01:34 +0000365#else
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500366#undef mbedtls_exit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
368#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000369#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#define mbedtls_exit exit
371#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
372#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100373
Janos Follath91947442016-03-18 13:49:27 +0000374/*
375 * The default exit values
376 */
377#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
378#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
379#else
380#define MBEDTLS_EXIT_SUCCESS 0
381#endif
382#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
383#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
384#else
385#define MBEDTLS_EXIT_FAILURE 1
386#endif
387
SimonBd5800b72016-04-26 07:43:27 +0100388/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100389 * The function pointers for reading from and writing a seed file to
390 * Non-Volatile storage (NV) in a platform-independent way
391 *
392 * Only enabled when the NV seed entropy source is enabled
393 */
394#if defined(MBEDTLS_ENTROPY_NV_SEED)
395#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
396/* Internal standard platform definitions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
398int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100399#endif
400
401#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100402extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
403extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100404
405/**
Rose Zadik332658d2018-01-25 22:02:53 +0000406 * \brief This function allows configuring custom seed file writing and
407 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100408 *
Rose Zadik332658d2018-01-25 22:02:53 +0000409 * \param nv_seed_read_func The seed reading function implementation.
410 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100411 *
Rose Zadik332658d2018-01-25 22:02:53 +0000412 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100413 */
414int mbedtls_platform_set_nv_seed(
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
416 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
417 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100418#else
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500419#undef mbedtls_nv_seed_read
420#undef mbedtls_nv_seed_write
Paul Bakkere021a4b2016-06-01 11:25:44 +0100421#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
422 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
423#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
424#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
425#else
426#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
427#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
428#endif
429#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
430#endif /* MBEDTLS_ENTROPY_NV_SEED */
431
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100432#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100433
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100434/**
Rose Zadik332658d2018-01-25 22:02:53 +0000435 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100436 *
437 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000438 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440typedef struct mbedtls_platform_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200441 char MBEDTLS_PRIVATE(dummy); /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100442}
443mbedtls_platform_context;
444
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100445#else
446#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100447#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100448
449/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100450 * \brief This function performs any platform-specific initialization
451 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100452 *
453 * \note This function should be called before any other library functions.
454 *
455 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000456 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100457 *
458 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100459 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100460 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100461 *
Rose Zadik332658d2018-01-25 22:02:53 +0000462 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100463 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464int mbedtls_platform_setup(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100465/**
Rose Zadik332658d2018-01-25 22:02:53 +0000466 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100467 *
Rose Zadik332658d2018-01-25 22:02:53 +0000468 * \note This function should be called after every other Mbed TLS module
469 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100470 *
Rose Zadik332658d2018-01-25 22:02:53 +0000471 * Its implementation is platform-specific, and unless
472 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200473 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100474 * \note The usage and necessity of this function is dependent on the platform.
475 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100476 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100477 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479void mbedtls_platform_teardown(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100480
Paul Bakker747a83a2014-02-01 22:50:07 +0100481#ifdef __cplusplus
482}
483#endif
484
485#endif /* platform.h */