blob: 5d4e69eeb5786b7a4e6e1af83ce5606e7d2f977d [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
Andrzej Kurek33b12222023-07-14 10:14:29 -0400138/* Enable certain documented defines only when generating doxygen to avoid
139 * an "unrecognized define" error. */
140#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_CALLOC)
141#define MBEDTLS_PLATFORM_STD_CALLOC
142#endif
143
144#if defined(__DOXYGEN__) && !defined(MBEDTLS_PLATFORM_STD_FREE)
145#define MBEDTLS_PLATFORM_STD_FREE
146#endif
Paul Bakkere021a4b2016-06-01 11:25:44 +0100147
Andrzej Kurek73afe272022-01-24 10:31:06 -0500148/** \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100149
150/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100151 * The function pointers for calloc and free.
Andrzej Kurekba168592023-07-14 09:56:02 -0400152 * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
Andrzej Kurekdc11cd12023-07-14 09:47:05 -0400153 * in mbedtls_config.h for more information about behaviour and requirements.
Paul Bakker747a83a2014-02-01 22:50:07 +0100154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_PLATFORM_MEMORY)
156#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200157 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200159#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000160#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100161/* For size_t */
162#include <stddef.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163extern void *mbedtls_calloc(size_t n, size_t size);
164extern void mbedtls_free(void *ptr);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100165
166/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100167 * \brief This function dynamically sets the memory-management
168 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100169 *
Rose Zadik332658d2018-01-25 22:02:53 +0000170 * \param calloc_func The \c calloc function implementation.
171 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100172 *
Rose Zadik332658d2018-01-25 22:02:53 +0000173 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100174 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
176 void (*free_func)(void *));
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200177#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#else /* !MBEDTLS_PLATFORM_MEMORY */
179#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200180#define mbedtls_calloc calloc
181#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100182
183/*
184 * The function pointers for fprintf
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100187/* We need FILE * */
188#include <stdio.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189extern int (*mbedtls_fprintf)(FILE *stream, const char *format, ...);
Paul Bakker747a83a2014-02-01 22:50:07 +0100190
Rich Evansc39cb492015-01-30 12:01:34 +0000191/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100192 * \brief This function dynamically configures the fprintf
193 * function that is called when the
194 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000195 *
Rose Zadik332658d2018-01-25 22:02:53 +0000196 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000197 *
Rose Zadik332658d2018-01-25 22:02:53 +0000198 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000199 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
201 ...));
Paul Bakker747a83a2014-02-01 22:50:07 +0100202#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
204#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000205#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#define mbedtls_fprintf fprintf
207#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
208#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000209
210/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000211 * The function pointers for printf
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214extern int (*mbedtls_printf)(const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000215
216/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100217 * \brief This function dynamically configures the snprintf
218 * function that is called when the mbedtls_snprintf()
219 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000220 *
Rose Zadik332658d2018-01-25 22:02:53 +0000221 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000222 *
Rose Zadik332658d2018-01-25 22:02:53 +0000223 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000224 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
227#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
228#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000229#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#define mbedtls_printf printf
231#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
232#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000233
234/*
235 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200236 *
237 * The snprintf implementation should conform to C99:
238 * - it *must* always correctly zero-terminate the buffer
239 * (except when n == 0, then it must leave the buffer untouched)
240 * - however it is acceptable to return -1 instead of the required length when
241 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000242 */
k-stachowiak723f8672018-07-16 14:27:07 +0200243#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200244/* For Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200246#endif
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000250
251/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100252 * \brief This function allows configuring a custom
253 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000254 *
Rose Zadik332658d2018-01-25 22:02:53 +0000255 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000256 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100257 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000258 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
260 const char *format, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
262#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
263#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000264#else
Azim Khan8d54c062018-03-23 18:34:35 +0000265#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
267#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000268
269/*
k-stachowiak723f8672018-07-16 14:27:07 +0200270 * The function pointers for vsnprintf
271 *
272 * The vsnprintf implementation should conform to C99:
273 * - it *must* always correctly zero-terminate the buffer
274 * (except when n == 0, then it must leave the buffer untouched)
275 * - however it is acceptable to return -1 instead of the required length when
276 * the destination buffer is too short.
277 */
278#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100279#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200280/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200282#endif
283
284#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
285#include <stdarg.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200287
288/**
289 * \brief Set your own snprintf function pointer
290 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200291 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200292 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200293 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200294 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
296 const char *format, va_list arg));
k-stachowiak723f8672018-07-16 14:27:07 +0200297#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
298#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
299#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
300#else
301#define mbedtls_vsnprintf vsnprintf
302#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
303#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
304
305/*
Rich Evansc39cb492015-01-30 12:01:34 +0000306 * The function pointers for exit
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309extern void (*mbedtls_exit)(int status);
Rich Evansc39cb492015-01-30 12:01:34 +0000310
311/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100312 * \brief This function dynamically configures the exit
313 * function that is called when the mbedtls_exit()
314 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000315 *
Rose Zadik332658d2018-01-25 22:02:53 +0000316 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000317 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100318 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000319 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320int mbedtls_platform_set_exit(void (*exit_func)(int status));
Rich Evansc39cb492015-01-30 12:01:34 +0000321#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
323#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000324#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#define mbedtls_exit exit
326#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
327#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100328
Janos Follath91947442016-03-18 13:49:27 +0000329/*
330 * The default exit values
331 */
332#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
333#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
334#else
335#define MBEDTLS_EXIT_SUCCESS 0
336#endif
337#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
338#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
339#else
340#define MBEDTLS_EXIT_FAILURE 1
341#endif
342
SimonBd5800b72016-04-26 07:43:27 +0100343/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100344 * The function pointers for reading from and writing a seed file to
345 * Non-Volatile storage (NV) in a platform-independent way
346 *
347 * Only enabled when the NV seed entropy source is enabled
348 */
349#if defined(MBEDTLS_ENTROPY_NV_SEED)
350#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
351/* Internal standard platform definitions */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
353int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100354#endif
355
356#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
358extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100359
360/**
Rose Zadik332658d2018-01-25 22:02:53 +0000361 * \brief This function allows configuring custom seed file writing and
362 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100363 *
Rose Zadik332658d2018-01-25 22:02:53 +0000364 * \param nv_seed_read_func The seed reading function implementation.
365 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100366 *
Rose Zadik332658d2018-01-25 22:02:53 +0000367 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100368 */
369int mbedtls_platform_set_nv_seed(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
371 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
372 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100373#else
374#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 */