blob: d6faa7eda644e9fd68a37290f5cb85616593d720 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020024 * SPDX-License-Identifier: Apache-2.0
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License"); you may
27 * not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#ifndef MBEDTLS_PLATFORM_H
39#define MBEDTLS_PLATFORM_H
Paul Bakker747a83a2014-02-01 22:50:07 +010040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010042#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020045#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010046
Simon Butcherb5b6af22016-07-13 14:46:18 +010047#if defined(MBEDTLS_HAVE_TIME)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010048#include "mbedtls/platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010049#endif
50
Gilles Peskinea3974432021-07-26 18:48:10 +020051/** Hardware accelerator failed */
52#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
53/** The requested feature is not supported by the platform */
54#define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
Ron Eldor0ff4e0b2018-08-29 18:53:20 +030055
Paul Bakker747a83a2014-02-01 22:50:07 +010056#ifdef __cplusplus
57extern "C" {
58#endif
59
Paul Bakker088c5c52014-04-25 11:11:10 +020060/**
61 * \name SECTION: Module settings
62 *
63 * The configuration options you can set for this module are in this section.
64 * Either change them in config.h or define them on the compiler command line.
65 * \{
66 */
67
k-stachowiak723f8672018-07-16 14:27:07 +020068/* The older Microsoft Windows common runtime provides non-conforming
69 * implementations of some standard library functions, including snprintf
70 * and vsnprintf. This affects MSVC and MinGW builds.
71 */
Krzysztof Stachowiak2cdb6b42018-10-22 10:43:56 +020072#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
k-stachowiak723f8672018-07-16 14:27:07 +020073#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
74#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
75#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000078#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010079#include <stdlib.h>
Daniel Axtens301db662020-05-28 11:43:41 +100080#if defined(MBEDTLS_HAVE_TIME)
Simon Butcher3fe6cd32016-04-26 19:51:29 +010081#include <time.h>
Daniel Axtens301db662020-05-28 11:43:41 +100082#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
k-stachowiak723f8672018-07-16 14:27:07 +020084#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000085#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 +020086#else
Rose Zadik332658d2018-01-25 22:02:53 +000087#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
Rich Evans46b0a8d2015-01-30 10:47:32 +000088#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020089#endif
k-stachowiak723f8672018-07-16 14:27:07 +020090#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
91#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
92#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
93#else
94#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
95#endif
96#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000098#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +0000101#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200102#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200103#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Rose Zadik332658d2018-01-25 22:02:53 +0000104#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Rose Zadik332658d2018-01-25 22:02:53 +0000107#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200108#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +0000110#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +0000111#endif
SimonBd5800b72016-04-26 07:43:27 +0100112#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +0000113#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +0100114#endif
Janos Follath91947442016-03-18 13:49:27 +0000115#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +0000116#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000117#endif
118#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000119#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000120#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100121#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100122#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
123#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
124#endif
125#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
126#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
127#endif
128#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
129#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
130#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100131#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
133#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
134#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200137
Paul Bakkere021a4b2016-06-01 11:25:44 +0100138
Andrzej Kurek73afe272022-01-24 10:31:06 -0500139/** \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100140
141/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100142 * The function pointers for calloc and free.
Paul Bakker747a83a2014-02-01 22:50:07 +0100143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_PLATFORM_MEMORY)
145#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200146 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500147#undef mbedtls_free
148#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200150#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000151#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100152/* For size_t */
153#include <stddef.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154extern void *mbedtls_calloc(size_t n, size_t size);
155extern void mbedtls_free(void *ptr);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100156
157/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100158 * \brief This function dynamically sets the memory-management
159 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100160 *
Rose Zadik332658d2018-01-25 22:02:53 +0000161 * \param calloc_func The \c calloc function implementation.
162 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100163 *
Rose Zadik332658d2018-01-25 22:02:53 +0000164 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
167 void (*free_func)(void *));
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200168#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#else /* !MBEDTLS_PLATFORM_MEMORY */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500170#undef mbedtls_free
171#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200173#define mbedtls_calloc calloc
174#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100175
176/*
177 * The function pointers for fprintf
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100180/* We need FILE * */
181#include <stdio.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
Paul Bakker747a83a2014-02-01 22:50:07 +0100183
Rich Evansc39cb492015-01-30 12:01:34 +0000184/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100185 * \brief This function dynamically configures the fprintf
186 * function that is called when the
187 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000188 *
Rose Zadik332658d2018-01-25 22:02:53 +0000189 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000190 *
Rose Zadik332658d2018-01-25 22:02:53 +0000191 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
194 ...));
Paul Bakker747a83a2014-02-01 22:50:07 +0100195#else
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500196#undef mbedtls_fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
198#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000199#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#define mbedtls_fprintf fprintf
201#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
202#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000203
204/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000205 * The function pointers for printf
206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208extern int (*mbedtls_printf)(const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000209
210/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100211 * \brief This function dynamically configures the snprintf
212 * function that is called when the mbedtls_snprintf()
213 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000214 *
Rose Zadik332658d2018-01-25 22:02:53 +0000215 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000216 *
Rose Zadik332658d2018-01-25 22:02:53 +0000217 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000218 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500221#undef mbedtls_printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
223#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000224#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#define mbedtls_printf printf
226#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
227#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000228
229/*
230 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200231 *
232 * The snprintf implementation should conform to C99:
233 * - it *must* always correctly zero-terminate the buffer
234 * (except when n == 0, then it must leave the buffer untouched)
235 * - however it is acceptable to return -1 instead of the required length when
236 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000237 */
k-stachowiak723f8672018-07-16 14:27:07 +0200238#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200239/* For Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200241#endif
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000245
246/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100247 * \brief This function allows configuring a custom
248 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000249 *
Rose Zadik332658d2018-01-25 22:02:53 +0000250 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000251 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100252 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000253 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
255 const char *format, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500257#undef mbedtls_snprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
259#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000260#else
Azim Khan8d54c062018-03-23 18:34:35 +0000261#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
263#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000264
265/*
k-stachowiak723f8672018-07-16 14:27:07 +0200266 * The function pointers for vsnprintf
267 *
268 * The vsnprintf implementation should conform to C99:
269 * - it *must* always correctly zero-terminate the buffer
270 * (except when n == 0, then it must leave the buffer untouched)
271 * - however it is acceptable to return -1 instead of the required length when
272 * the destination buffer is too short.
273 */
274#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100275#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200276/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200278#endif
279
280#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
281#include <stdarg.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200283
284/**
285 * \brief Set your own snprintf function pointer
286 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200287 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200288 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200289 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
292 const char *format, va_list arg));
k-stachowiak723f8672018-07-16 14:27:07 +0200293#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500294#undef mbedtls_vsnprintf
k-stachowiak723f8672018-07-16 14:27:07 +0200295#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
296#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
297#else
298#define mbedtls_vsnprintf vsnprintf
299#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
300#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
301
302/*
Rich Evansc39cb492015-01-30 12:01:34 +0000303 * The function pointers for exit
304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306extern void (*mbedtls_exit)(int status);
Rich Evansc39cb492015-01-30 12:01:34 +0000307
308/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100309 * \brief This function dynamically configures the exit
310 * function that is called when the mbedtls_exit()
311 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000312 *
Rose Zadik332658d2018-01-25 22:02:53 +0000313 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000314 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100315 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000316 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317int mbedtls_platform_set_exit(void (*exit_func)(int status));
Rich Evansc39cb492015-01-30 12:01:34 +0000318#else
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500319#undef mbedtls_exit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
321#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000322#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#define mbedtls_exit exit
324#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
325#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100326
Janos Follath91947442016-03-18 13:49:27 +0000327/*
328 * The default exit values
329 */
330#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
331#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
332#else
333#define MBEDTLS_EXIT_SUCCESS 0
334#endif
335#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
336#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
337#else
338#define MBEDTLS_EXIT_FAILURE 1
339#endif
340
SimonBd5800b72016-04-26 07:43:27 +0100341/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100342 * The function pointers for reading from and writing a seed file to
343 * Non-Volatile storage (NV) in a platform-independent way
344 *
345 * Only enabled when the NV seed entropy source is enabled
346 */
347#if defined(MBEDTLS_ENTROPY_NV_SEED)
348#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
349/* Internal standard platform definitions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
351int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100352#endif
353
354#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
356extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100357
358/**
Rose Zadik332658d2018-01-25 22:02:53 +0000359 * \brief This function allows configuring custom seed file writing and
360 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100361 *
Rose Zadik332658d2018-01-25 22:02:53 +0000362 * \param nv_seed_read_func The seed reading function implementation.
363 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100364 *
Rose Zadik332658d2018-01-25 22:02:53 +0000365 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100366 */
367int mbedtls_platform_set_nv_seed(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
369 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
370 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100371#else
Aaron M. Ucko82e06cb2023-01-17 13:26:35 -0500372#undef mbedtls_nv_seed_read
373#undef mbedtls_nv_seed_write
Paul Bakkere021a4b2016-06-01 11:25:44 +0100374#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
375 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
376#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
377#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
378#else
379#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
380#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
381#endif
382#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
383#endif /* MBEDTLS_ENTROPY_NV_SEED */
384
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100385#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100386
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100387/**
Rose Zadik332658d2018-01-25 22:02:53 +0000388 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100389 *
390 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000391 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100392 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393typedef struct mbedtls_platform_context {
Rose Zadik4bca2b02018-03-27 13:12:52 +0100394 char dummy; /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100395}
396mbedtls_platform_context;
397
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100398#else
399#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100400#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100401
402/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100403 * \brief This function performs any platform-specific initialization
404 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100405 *
406 * \note This function should be called before any other library functions.
407 *
408 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000409 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100410 *
411 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100412 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100413 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100414 *
Rose Zadik332658d2018-01-25 22:02:53 +0000415 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100416 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417int mbedtls_platform_setup(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100418/**
Rose Zadik332658d2018-01-25 22:02:53 +0000419 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100420 *
Rose Zadik332658d2018-01-25 22:02:53 +0000421 * \note This function should be called after every other Mbed TLS module
422 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100423 *
Rose Zadik332658d2018-01-25 22:02:53 +0000424 * Its implementation is platform-specific, and unless
425 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200426 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100427 * \note The usage and necessity of this function is dependent on the platform.
428 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100429 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100430 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432void mbedtls_platform_teardown(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100433
Paul Bakker747a83a2014-02-01 22:50:07 +0100434#ifdef __cplusplus
435}
436#endif
437
438#endif /* platform.h */