blob: c50d7d211de13d55b94d63195db6fd52157244bc [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.
Darryl Greena40a1012018-01-05 15:33:17 +000014 */
15/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020016 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#ifndef MBEDTLS_PLATFORM_H
32#define MBEDTLS_PLATFORM_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020033#include "mbedtls/private_access.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010040
Simon Butcherb5b6af22016-07-13 14:46:18 +010041#if defined(MBEDTLS_HAVE_TIME)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010042#include "mbedtls/platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010043#endif
44
Paul Bakker747a83a2014-02-01 22:50:07 +010045#ifdef __cplusplus
46extern "C" {
47#endif
48
Paul Bakker088c5c52014-04-25 11:11:10 +020049/**
50 * \name SECTION: Module settings
51 *
52 * The configuration options you can set for this module are in this section.
53 * Either change them in config.h or define them on the compiler command line.
54 * \{
55 */
56
k-stachowiak723f8672018-07-16 14:27:07 +020057/* The older Microsoft Windows common runtime provides non-conforming
58 * implementations of some standard library functions, including snprintf
59 * and vsnprintf. This affects MSVC and MinGW builds.
60 */
Krzysztof Stachowiak2cdb6b42018-10-22 10:43:56 +020061#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
k-stachowiak723f8672018-07-16 14:27:07 +020062#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
63#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
64#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000067#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010068#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010069#include <time.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
k-stachowiak723f8672018-07-16 14:27:07 +020071#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000072#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 +020073#else
Rose Zadik332658d2018-01-25 22:02:53 +000074#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
Rich Evans46b0a8d2015-01-30 10:47:32 +000075#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020076#endif
k-stachowiak723f8672018-07-16 14:27:07 +020077#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
78#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
79#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use. */
80#else
81#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf /**< The default \c vsnprintf function to use. */
82#endif
83#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000085#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020086#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +000088#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020089#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020090#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Rose Zadik332658d2018-01-25 22:02:53 +000091#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020092#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Rose Zadik332658d2018-01-25 22:02:53 +000094#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +020095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +000097#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +000098#endif
SimonBd5800b72016-04-26 07:43:27 +010099#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +0000100#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +0100101#endif
Janos Follath91947442016-03-18 13:49:27 +0000102#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +0000103#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000104#endif
105#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000106#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000107#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100108#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100109#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
110#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
111#endif
112#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
113#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
114#endif
115#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
116#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
117#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100118#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
120#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
121#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200122#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200124
Paul Bakkere021a4b2016-06-01 11:25:44 +0100125
Paul Bakker088c5c52014-04-25 11:11:10 +0200126/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100127
128/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100129 * The function pointers for calloc and free.
Paul Bakker747a83a2014-02-01 22:50:07 +0100130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_PLATFORM_MEMORY)
132#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200133 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200135#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000136#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100137/* For size_t */
138#include <stddef.h>
Roberto Vargas7decfe82018-06-04 13:54:09 +0100139extern void *mbedtls_calloc( size_t n, size_t size );
140extern void mbedtls_free( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100141
142/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100143 * \brief This function dynamically sets the memory-management
144 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100145 *
Rose Zadik332658d2018-01-25 22:02:53 +0000146 * \param calloc_func The \c calloc function implementation.
147 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100148 *
Rose Zadik332658d2018-01-25 22:02:53 +0000149 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100150 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200151int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100152 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200153#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#else /* !MBEDTLS_PLATFORM_MEMORY */
155#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200156#define mbedtls_calloc calloc
157#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100158
159/*
160 * The function pointers for fprintf
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100163/* We need FILE * */
164#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100166
Rich Evansc39cb492015-01-30 12:01:34 +0000167/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100168 * \brief This function dynamically configures the fprintf
169 * function that is called when the
170 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000171 *
Rose Zadik332658d2018-01-25 22:02:53 +0000172 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000173 *
Rose Zadik332658d2018-01-25 22:02:53 +0000174 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100177 ... ) );
178#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
180#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000181#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#define mbedtls_fprintf fprintf
183#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
184#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000185
186/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000187 * The function pointers for printf
188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
190extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000191
192/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100193 * \brief This function dynamically configures the snprintf
194 * function that is called when the mbedtls_snprintf()
195 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000196 *
Rose Zadik332658d2018-01-25 22:02:53 +0000197 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000198 *
Rose Zadik332658d2018-01-25 22:02:53 +0000199 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
202#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
203#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
204#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000205#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#define mbedtls_printf printf
207#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
208#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000209
210/*
211 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200212 *
213 * The snprintf implementation should conform to C99:
214 * - it *must* always correctly zero-terminate the buffer
215 * (except when n == 0, then it must leave the buffer untouched)
216 * - however it is acceptable to return -1 instead of the required length when
217 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000218 */
k-stachowiak723f8672018-07-16 14:27:07 +0200219#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200220/* For Windows (inc. MSYS2), we provide our own fixed implementation */
221int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
222#endif
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
225extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000226
227/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100228 * \brief This function allows configuring a custom
229 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000230 *
Rose Zadik332658d2018-01-25 22:02:53 +0000231 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000232 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100233 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000236 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
238#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
239#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000240#else
Azim Khan8d54c062018-03-23 18:34:35 +0000241#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
243#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000244
245/*
k-stachowiak723f8672018-07-16 14:27:07 +0200246 * The function pointers for vsnprintf
247 *
248 * The vsnprintf implementation should conform to C99:
249 * - it *must* always correctly zero-terminate the buffer
250 * (except when n == 0, then it must leave the buffer untouched)
251 * - however it is acceptable to return -1 instead of the required length when
252 * the destination buffer is too short.
253 */
254#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100255#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200256/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
257int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
258#endif
259
260#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
261#include <stdarg.h>
262extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
263
264/**
265 * \brief Set your own snprintf function pointer
266 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200267 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200268 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200269 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200270 */
271int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
272 const char * format, va_list arg ) );
273#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
274#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
275#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
276#else
277#define mbedtls_vsnprintf vsnprintf
278#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
279#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
280
281/*
Rich Evansc39cb492015-01-30 12:01:34 +0000282 * The function pointers for exit
283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
285extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000286
287/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100288 * \brief This function dynamically configures the exit
289 * function that is called when the mbedtls_exit()
290 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000291 *
Rose Zadik332658d2018-01-25 22:02:53 +0000292 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000293 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100294 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000297#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
299#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000300#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#define mbedtls_exit exit
302#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
303#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100304
Janos Follath91947442016-03-18 13:49:27 +0000305/*
306 * The default exit values
307 */
308#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
309#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
310#else
311#define MBEDTLS_EXIT_SUCCESS 0
312#endif
313#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
314#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
315#else
316#define MBEDTLS_EXIT_FAILURE 1
317#endif
318
SimonBd5800b72016-04-26 07:43:27 +0100319/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100320 * The function pointers for reading from and writing a seed file to
321 * Non-Volatile storage (NV) in a platform-independent way
322 *
323 * Only enabled when the NV seed entropy source is enabled
324 */
325#if defined(MBEDTLS_ENTROPY_NV_SEED)
326#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
327/* Internal standard platform definitions */
328int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
329int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
330#endif
331
332#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
333extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
334extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
335
336/**
Rose Zadik332658d2018-01-25 22:02:53 +0000337 * \brief This function allows configuring custom seed file writing and
338 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100339 *
Rose Zadik332658d2018-01-25 22:02:53 +0000340 * \param nv_seed_read_func The seed reading function implementation.
341 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100342 *
Rose Zadik332658d2018-01-25 22:02:53 +0000343 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100344 */
345int mbedtls_platform_set_nv_seed(
346 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
347 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
348 );
349#else
350#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
351 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
352#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
353#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
354#else
355#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
356#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
357#endif
358#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
359#endif /* MBEDTLS_ENTROPY_NV_SEED */
360
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100361#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100362
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100363/**
Rose Zadik332658d2018-01-25 22:02:53 +0000364 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100365 *
366 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000367 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100368 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200369typedef struct mbedtls_platform_context
370{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200371 char MBEDTLS_PRIVATE(dummy); /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100372}
373mbedtls_platform_context;
374
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100375#else
376#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100377#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100378
379/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100380 * \brief This function performs any platform-specific initialization
381 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100382 *
383 * \note This function should be called before any other library functions.
384 *
385 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000386 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100387 *
388 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100389 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100390 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100391 *
Rose Zadik332658d2018-01-25 22:02:53 +0000392 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100393 */
394int mbedtls_platform_setup( mbedtls_platform_context *ctx );
395/**
Rose Zadik332658d2018-01-25 22:02:53 +0000396 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100397 *
Rose Zadik332658d2018-01-25 22:02:53 +0000398 * \note This function should be called after every other Mbed TLS module
399 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100400 *
Rose Zadik332658d2018-01-25 22:02:53 +0000401 * Its implementation is platform-specific, and unless
402 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200403 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100404 * \note The usage and necessity of this function is dependent on the platform.
405 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100406 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100407 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100408 */
409void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
410
Paul Bakker747a83a2014-02-01 22:50:07 +0100411#ifdef __cplusplus
412}
413#endif
414
415#endif /* platform.h */