blob: 87e880fbb8fb6d7f73f55a90a62da8033af76ab4 [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
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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020040#include "mbedtls/private_access.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010041
Bence Szépkútic662b362021-05-27 11:25:03 +020042#include "mbedtls/build_info.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010043
Simon Butcherb5b6af22016-07-13 14:46:18 +010044#if defined(MBEDTLS_HAVE_TIME)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010045#include "mbedtls/platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010046#endif
47
Paul Bakker747a83a2014-02-01 22:50:07 +010048#ifdef __cplusplus
49extern "C" {
50#endif
51
Paul Bakker088c5c52014-04-25 11:11:10 +020052/**
53 * \name SECTION: Module settings
54 *
55 * The configuration options you can set for this module are in this section.
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020056 * Either change them in mbedtls_config.h or define them on the compiler command line.
Paul Bakker088c5c52014-04-25 11:11:10 +020057 * \{
58 */
59
k-stachowiak723f8672018-07-16 14:27:07 +020060/* The older Microsoft Windows common runtime provides non-conforming
61 * implementations of some standard library functions, including snprintf
62 * and vsnprintf. This affects MSVC and MinGW builds.
63 */
Krzysztof Stachowiak2cdb6b42018-10-22 10:43:56 +020064#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
k-stachowiak723f8672018-07-16 14:27:07 +020065#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
66#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000070#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010071#include <stdlib.h>
Daniel Axtensf0710242020-05-28 11:43:41 +100072#if defined(MBEDTLS_HAVE_TIME)
Simon Butcher3fe6cd32016-04-26 19:51:29 +010073#include <time.h>
Daniel Axtensf0710242020-05-28 11:43:41 +100074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
k-stachowiak723f8672018-07-16 14:27:07 +020076#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000077#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 +020078#else
Rose Zadik332658d2018-01-25 22:02:53 +000079#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
Rich Evans46b0a8d2015-01-30 10:47:32 +000080#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020081#endif
k-stachowiak723f8672018-07-16 14:27:07 +020082#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
83#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
84#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
85#else
86#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
87#endif
88#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000090#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020091#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000093#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020094#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020095#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Rose Zadik332658d2018-01-25 22:02:53 +000096#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Rose Zadik332658d2018-01-25 22:02:53 +000099#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200100#endif
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200101#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
102#define MBEDTLS_PLATFORM_STD_SETBUF setbuf /**< The default \c setbuf function to use. */
103#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +0000105#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +0000106#endif
SimonBd5800b72016-04-26 07:43:27 +0100107#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +0000108#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +0100109#endif
Janos Follath91947442016-03-18 13:49:27 +0000110#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +0000111#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000112#endif
113#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000114#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000115#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100116#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100117#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
118#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
119#endif
120#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
121#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
122#endif
123#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
124#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
125#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100126#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
128#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
129#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200132
Paul Bakkere021a4b2016-06-01 11:25:44 +0100133
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +0100134/** \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100135
136/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100137 * The function pointers for calloc and free.
Andrzej Kurekecaf6fb2023-05-04 17:07:57 -0400138 * Please see MBEDTLS_PLATFORM_STD_CALLOC and MBEDTLS_PLATFORM_STD_FREE
Andrzej Kurek2d981f02023-04-27 09:19:35 -0400139 * in mbedtls_config.h for more information about behaviour and requirements.
Paul Bakker747a83a2014-02-01 22:50:07 +0100140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_PLATFORM_MEMORY)
142#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200143 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500144#undef mbedtls_free
145#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200147#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000148#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100149/* For size_t */
150#include <stddef.h>
Gilles Peskine449bd832023-01-11 14:50:10 +0100151extern void *mbedtls_calloc(size_t n, size_t size);
152extern void mbedtls_free(void *ptr);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100153
154/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100155 * \brief This function dynamically sets the memory-management
156 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100157 *
Rose Zadik332658d2018-01-25 22:02:53 +0000158 * \param calloc_func The \c calloc function implementation.
159 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100160 *
Rose Zadik332658d2018-01-25 22:02:53 +0000161 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100162 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
164 void (*free_func)(void *));
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200165#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#else /* !MBEDTLS_PLATFORM_MEMORY */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500167#undef mbedtls_free
168#undef mbedtls_calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100190int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *stream, const char *,
191 ...));
Paul Bakker747a83a2014-02-01 22:50:07 +0100192#else
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500193#undef mbedtls_fprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
195#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000196#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#define mbedtls_fprintf fprintf
198#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
199#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000200
201/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000202 * The function pointers for printf
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100205extern int (*mbedtls_printf)(const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000206
207/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100208 * \brief This function dynamically configures the snprintf
209 * function that is called when the mbedtls_snprintf()
210 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000211 *
Rose Zadik332658d2018-01-25 22:02:53 +0000212 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000213 *
Rose Zadik332658d2018-01-25 22:02:53 +0000214 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500218#undef mbedtls_printf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
220#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000221#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#define mbedtls_printf printf
223#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
224#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000225
226/*
227 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200228 *
229 * The snprintf implementation should conform to C99:
230 * - it *must* always correctly zero-terminate the buffer
231 * (except when n == 0, then it must leave the buffer untouched)
232 * - however it is acceptable to return -1 instead of the required length when
233 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000234 */
k-stachowiak723f8672018-07-16 14:27:07 +0200235#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200236/* For Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200238#endif
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100241extern int (*mbedtls_snprintf)(char *s, size_t n, const char *format, ...);
Rich Evans16f8cd82015-02-06 16:14:34 +0000242
243/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100244 * \brief This function allows configuring a custom
245 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000246 *
Rose Zadik332658d2018-01-25 22:02:53 +0000247 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000248 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100249 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
252 const char *format, ...));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500254#undef mbedtls_snprintf
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
256#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000257#else
Azim Khan8d54c062018-03-23 18:34:35 +0000258#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
260#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000261
262/*
k-stachowiak723f8672018-07-16 14:27:07 +0200263 * The function pointers for vsnprintf
264 *
265 * The vsnprintf implementation should conform to C99:
266 * - it *must* always correctly zero-terminate the buffer
267 * (except when n == 0, then it must leave the buffer untouched)
268 * - however it is acceptable to return -1 instead of the required length when
269 * the destination buffer is too short.
270 */
271#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100272#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200273/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200275#endif
276
277#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
278#include <stdarg.h>
Gilles Peskine449bd832023-01-11 14:50:10 +0100279extern int (*mbedtls_vsnprintf)(char *s, size_t n, const char *format, va_list arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200280
281/**
282 * \brief Set your own snprintf function pointer
283 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200284 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200285 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200286 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
289 const char *format, va_list arg));
k-stachowiak723f8672018-07-16 14:27:07 +0200290#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500291#undef mbedtls_vsnprintf
k-stachowiak723f8672018-07-16 14:27:07 +0200292#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
293#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
294#else
295#define mbedtls_vsnprintf vsnprintf
296#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
297#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
298
299/*
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200300 * The function pointers for setbuf
301 */
302#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
303#include <stdio.h>
304/**
305 * \brief Function pointer to call for `setbuf()` functionality
306 * (changing the internal buffering on stdio calls).
307 *
308 * \note The library calls this function to disable
309 * buffering when reading or writing sensitive data,
310 * to avoid having extra copies of sensitive data
311 * remaining in stdio buffers after the file is
312 * closed. If this is not a concern, for example if
313 * your platform's stdio doesn't have any buffering,
314 * you can set mbedtls_setbuf to a function that
315 * does nothing.
316 *
Gilles Peskine0bd76ee2022-06-30 19:32:02 +0200317 * The library always calls this function with
318 * `buf` equal to `NULL`.
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200319 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320extern void (*mbedtls_setbuf)(FILE *stream, char *buf);
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200321
322/**
323 * \brief Dynamically configure the function that is called
324 * when the mbedtls_setbuf() function is called by the
325 * library.
326 *
327 * \param setbuf_func The \c setbuf function implementation
328 *
329 * \return \c 0
330 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331int mbedtls_platform_set_setbuf(void (*setbuf_func)(
332 FILE *stream, char *buf));
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500333#else
334#undef mbedtls_setbuf
335#if defined(MBEDTLS_PLATFORM_SETBUF_MACRO)
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200336/**
337 * \brief Macro defining the function for the library to
338 * call for `setbuf` functionality (changing the
339 * internal buffering on stdio calls).
340 *
341 * \note See extra comments on the mbedtls_setbuf() function
342 * pointer above.
343 *
344 * \return \c 0 on success, negative on error.
345 */
346#define mbedtls_setbuf MBEDTLS_PLATFORM_SETBUF_MACRO
347#else
348#define mbedtls_setbuf setbuf
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500349#endif /* MBEDTLS_PLATFORM_SETBUF_MACRO */
350#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200351
352/*
Rich Evansc39cb492015-01-30 12:01:34 +0000353 * The function pointers for exit
354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100356extern void (*mbedtls_exit)(int status);
Rich Evansc39cb492015-01-30 12:01:34 +0000357
358/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100359 * \brief This function dynamically configures the exit
360 * function that is called when the mbedtls_exit()
361 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000362 *
Rose Zadik332658d2018-01-25 22:02:53 +0000363 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000364 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100365 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367int mbedtls_platform_set_exit(void (*exit_func)(int status));
Rich Evansc39cb492015-01-30 12:01:34 +0000368#else
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500369#undef mbedtls_exit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
371#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000372#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#define mbedtls_exit exit
374#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
375#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100376
Janos Follath91947442016-03-18 13:49:27 +0000377/*
378 * The default exit values
379 */
380#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
381#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
382#else
383#define MBEDTLS_EXIT_SUCCESS 0
384#endif
385#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
386#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
387#else
388#define MBEDTLS_EXIT_FAILURE 1
389#endif
390
SimonBd5800b72016-04-26 07:43:27 +0100391/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100392 * The function pointers for reading from and writing a seed file to
393 * Non-Volatile storage (NV) in a platform-independent way
394 *
395 * Only enabled when the NV seed entropy source is enabled
396 */
397#if defined(MBEDTLS_ENTROPY_NV_SEED)
398#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
399/* Internal standard platform definitions */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len);
401int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100402#endif
403
404#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100405extern int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len);
406extern int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len);
Paul Bakkere021a4b2016-06-01 11:25:44 +0100407
408/**
Rose Zadik332658d2018-01-25 22:02:53 +0000409 * \brief This function allows configuring custom seed file writing and
410 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100411 *
Rose Zadik332658d2018-01-25 22:02:53 +0000412 * \param nv_seed_read_func The seed reading function implementation.
413 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100414 *
Rose Zadik332658d2018-01-25 22:02:53 +0000415 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100416 */
417int mbedtls_platform_set_nv_seed(
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
419 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)
420 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100421#else
Aaron M. Ucko5e4a01b2023-01-17 13:26:35 -0500422#undef mbedtls_nv_seed_read
423#undef mbedtls_nv_seed_write
Paul Bakkere021a4b2016-06-01 11:25:44 +0100424#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
425 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
426#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
427#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
428#else
429#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
430#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
431#endif
432#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
433#endif /* MBEDTLS_ENTROPY_NV_SEED */
434
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100435#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100436
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100437/**
Rose Zadik332658d2018-01-25 22:02:53 +0000438 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100439 *
440 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000441 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443typedef struct mbedtls_platform_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200444 char MBEDTLS_PRIVATE(dummy); /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100445}
446mbedtls_platform_context;
447
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100448#else
449#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100450#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100451
452/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100453 * \brief This function performs any platform-specific initialization
454 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100455 *
456 * \note This function should be called before any other library functions.
457 *
458 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000459 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100460 *
461 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100462 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100463 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100464 *
Rose Zadik332658d2018-01-25 22:02:53 +0000465 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100466 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467int mbedtls_platform_setup(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100468/**
Rose Zadik332658d2018-01-25 22:02:53 +0000469 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100470 *
Rose Zadik332658d2018-01-25 22:02:53 +0000471 * \note This function should be called after every other Mbed TLS module
472 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100473 *
Rose Zadik332658d2018-01-25 22:02:53 +0000474 * Its implementation is platform-specific, and unless
475 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200476 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100477 * \note The usage and necessity of this function is dependent on the platform.
478 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100479 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100480 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482void mbedtls_platform_teardown(mbedtls_platform_context *ctx);
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100483
Paul Bakker747a83a2014-02-01 22:50:07 +0100484#ifdef __cplusplus
485}
486#endif
487
488#endif /* platform.h */