blob: 121ba3ad73ca964e6909ec75adf6e3479bccb9e7 [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.
Andrzej Kureka242c752023-04-25 04:48:15 -0400143 * mbedtls_calloc will allocate and zeroize the buffer.
Paul Bakker747a83a2014-02-01 22:50:07 +0100144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_PLATFORM_MEMORY)
146#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200147 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
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 */
169#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200170#define mbedtls_calloc calloc
171#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100172
173/*
174 * The function pointers for fprintf
175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100177/* We need FILE * */
178#include <stdio.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
Paul Bakker747a83a2014-02-01 22:50:07 +0100180
Rich Evansc39cb492015-01-30 12:01:34 +0000181/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100182 * \brief This function dynamically configures the fprintf
183 * function that is called when the
184 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000185 *
Rose Zadik332658d2018-01-25 22:02:53 +0000186 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000187 *
Rose Zadik332658d2018-01-25 22:02:53 +0000188 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
191 ...));
Paul Bakker747a83a2014-02-01 22:50:07 +0100192#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
194#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000195#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#define mbedtls_fprintf fprintf
197#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
198#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000199
200/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000201 * The function pointers for printf
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204extern int (*mbedtls_printf)(const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000205
206/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100207 * \brief This function dynamically configures the snprintf
208 * function that is called when the mbedtls_snprintf()
209 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000210 *
Rose Zadik332658d2018-01-25 22:02:53 +0000211 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000212 *
Rose Zadik332658d2018-01-25 22:02:53 +0000213 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000214 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
217#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
218#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000219#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#define mbedtls_printf printf
221#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
222#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000223
224/*
225 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200226 *
227 * The snprintf implementation should conform to C99:
228 * - it *must* always correctly zero-terminate the buffer
229 * (except when n == 0, then it must leave the buffer untouched)
230 * - however it is acceptable to return -1 instead of the required length when
231 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000232 */
k-stachowiak723f8672018-07-16 14:27:07 +0200233#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200234/* For Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200236#endif
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000240
241/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100242 * \brief This function allows configuring a custom
243 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000244 *
Rose Zadik332658d2018-01-25 22:02:53 +0000245 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000246 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100247 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000248 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
250 const char *format, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
252#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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 */
288#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
289#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
290#else
291#define mbedtls_vsnprintf vsnprintf
292#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
293#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
294
295/*
Rich Evansc39cb492015-01-30 12:01:34 +0000296 * The function pointers for exit
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299extern void (*mbedtls_exit)(int status);
Rich Evansc39cb492015-01-30 12:01:34 +0000300
301/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100302 * \brief This function dynamically configures the exit
303 * function that is called when the mbedtls_exit()
304 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000305 *
Rose Zadik332658d2018-01-25 22:02:53 +0000306 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000307 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100308 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000309 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310int mbedtls_platform_set_exit(void (*exit_func)(int status));
Rich Evansc39cb492015-01-30 12:01:34 +0000311#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
313#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000314#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#define mbedtls_exit exit
316#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
317#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100318
Janos Follath91947442016-03-18 13:49:27 +0000319/*
320 * The default exit values
321 */
322#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
323#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
324#else
325#define MBEDTLS_EXIT_SUCCESS 0
326#endif
327#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
328#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
329#else
330#define MBEDTLS_EXIT_FAILURE 1
331#endif
332
SimonBd5800b72016-04-26 07:43:27 +0100333/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100334 * The function pointers for reading from and writing a seed file to
335 * Non-Volatile storage (NV) in a platform-independent way
336 *
337 * Only enabled when the NV seed entropy source is enabled
338 */
339#if defined(MBEDTLS_ENTROPY_NV_SEED)
340#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
341/* Internal standard platform definitions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
343int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100344#endif
345
346#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
348extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100349
350/**
Rose Zadik332658d2018-01-25 22:02:53 +0000351 * \brief This function allows configuring custom seed file writing and
352 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100353 *
Rose Zadik332658d2018-01-25 22:02:53 +0000354 * \param nv_seed_read_func The seed reading function implementation.
355 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100356 *
Rose Zadik332658d2018-01-25 22:02:53 +0000357 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100358 */
359int mbedtls_platform_set_nv_seed(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
361 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
362 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100363#else
364#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
365 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
366#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
367#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
368#else
369#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
370#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
371#endif
372#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
373#endif /* MBEDTLS_ENTROPY_NV_SEED */
374
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100375#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100376
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100377/**
Rose Zadik332658d2018-01-25 22:02:53 +0000378 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100379 *
380 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000381 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383typedef struct mbedtls_platform_context {
Rose Zadik4bca2b02018-03-27 13:12:52 +0100384 char dummy; /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100385}
386mbedtls_platform_context;
387
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100388#else
389#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100390#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100391
392/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100393 * \brief This function performs any platform-specific initialization
394 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100395 *
396 * \note This function should be called before any other library functions.
397 *
398 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000399 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100400 *
401 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100402 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100403 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100404 *
Rose Zadik332658d2018-01-25 22:02:53 +0000405 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100406 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407int mbedtls_platform_setup(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100408/**
Rose Zadik332658d2018-01-25 22:02:53 +0000409 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100410 *
Rose Zadik332658d2018-01-25 22:02:53 +0000411 * \note This function should be called after every other Mbed TLS module
412 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100413 *
Rose Zadik332658d2018-01-25 22:02:53 +0000414 * Its implementation is platform-specific, and unless
415 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200416 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100417 * \note The usage and necessity of this function is dependent on the platform.
418 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100419 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100420 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100421 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422void mbedtls_platform_teardown(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100423
Paul Bakker747a83a2014-02-01 22:50:07 +0100424#ifdef __cplusplus
425}
426#endif
427
428#endif /* platform.h */