blob: 81f981d259d52d64eda7b4aeced207cdc871d21c [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
Bence Szépkútic662b362021-05-27 11:25:03 +020035#include "mbedtls/build_info.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010036
Simon Butcherb5b6af22016-07-13 14:46:18 +010037#if defined(MBEDTLS_HAVE_TIME)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010039#endif
40
Paul Bakker747a83a2014-02-01 22:50:07 +010041#ifdef __cplusplus
42extern "C" {
43#endif
44
Paul Bakker088c5c52014-04-25 11:11:10 +020045/**
46 * \name SECTION: Module settings
47 *
48 * The configuration options you can set for this module are in this section.
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020049 * Either change them in mbedtls_config.h or define them on the compiler command line.
Paul Bakker088c5c52014-04-25 11:11:10 +020050 * \{
51 */
52
k-stachowiak723f8672018-07-16 14:27:07 +020053/* The older Microsoft Windows common runtime provides non-conforming
54 * implementations of some standard library functions, including snprintf
55 * and vsnprintf. This affects MSVC and MinGW builds.
56 */
Krzysztof Stachowiak2cdb6b42018-10-22 10:43:56 +020057#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
k-stachowiak723f8672018-07-16 14:27:07 +020058#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
59#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
60#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010064#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010065#include <time.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
k-stachowiak723f8672018-07-16 14:27:07 +020067#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Mateusz Starzyk16fec332021-07-22 16:43:35 +020068/** The default \c snprintf function to use. */
69#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020070#else
Mateusz Starzyk16fec332021-07-22 16:43:35 +020071/** The default \c snprintf function to use. */
72#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
Rich Evans46b0a8d2015-01-30 10:47:32 +000073#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020074#endif
k-stachowiak723f8672018-07-16 14:27:07 +020075#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
76#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Mateusz Starzyk16fec332021-07-22 16:43:35 +020077/** The default \c vsnprintf function to use. */
78#define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
k-stachowiak723f8672018-07-16 14:27:07 +020079#else
Mateusz Starzyk16fec332021-07-22 16:43:35 +020080/** The default \c vsnprintf function to use. */
81#define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
k-stachowiak723f8672018-07-16 14:27:07 +020082#endif
83#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Mateusz Starzyk16fec332021-07-22 16:43:35 +020085/** The default \c printf function to use. */
86#define MBEDTLS_PLATFORM_STD_PRINTF printf
Paul Bakker088c5c52014-04-25 11:11:10 +020087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Mateusz Starzyk16fec332021-07-22 16:43:35 +020089/** The default \c fprintf function to use. */
90#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
Paul Bakker088c5c52014-04-25 11:11:10 +020091#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020092#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Mateusz Starzyk16fec332021-07-22 16:43:35 +020093/** The default \c calloc function to use. */
94#define MBEDTLS_PLATFORM_STD_CALLOC calloc
Paul Bakker088c5c52014-04-25 11:11:10 +020095#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Mateusz Starzyk16fec332021-07-22 16:43:35 +020097/** The default \c free function to use. */
98#define MBEDTLS_PLATFORM_STD_FREE free
Paul Bakker088c5c52014-04-25 11:11:10 +020099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200101/** The default \c exit function to use. */
102#define MBEDTLS_PLATFORM_STD_EXIT exit
Janos Follath91947442016-03-18 13:49:27 +0000103#endif
SimonBd5800b72016-04-26 07:43:27 +0100104#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200105/** The default \c time function to use. */
106#define MBEDTLS_PLATFORM_STD_TIME time
SimonBd5800b72016-04-26 07:43:27 +0100107#endif
Janos Follath91947442016-03-18 13:49:27 +0000108#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200109/** The default exit value to use. */
110#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
Janos Follath91947442016-03-18 13:49:27 +0000111#endif
112#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Mateusz Starzyk16fec332021-07-22 16:43:35 +0200113/** The default exit value to use. */
114#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
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
Paul Bakker088c5c52014-04-25 11:11:10 +0200134/* \} 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.
Paul Bakker747a83a2014-02-01 22:50:07 +0100138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_PLATFORM_MEMORY)
140#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200141 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200143#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000144#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100145/* For size_t */
146#include <stddef.h>
Roberto Vargas7decfe82018-06-04 13:54:09 +0100147extern void *mbedtls_calloc( size_t n, size_t size );
148extern void mbedtls_free( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100149
150/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100151 * \brief This function dynamically sets the memory-management
152 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100153 *
Rose Zadik332658d2018-01-25 22:02:53 +0000154 * \param calloc_func The \c calloc function implementation.
155 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100156 *
Rose Zadik332658d2018-01-25 22:02:53 +0000157 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100158 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200159int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100160 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200161#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#else /* !MBEDTLS_PLATFORM_MEMORY */
163#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200164#define mbedtls_calloc calloc
165#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100166
167/*
168 * The function pointers for fprintf
169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100171/* We need FILE * */
172#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100174
Rich Evansc39cb492015-01-30 12:01:34 +0000175/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100176 * \brief This function dynamically configures the fprintf
177 * function that is called when the
178 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000179 *
Rose Zadik332658d2018-01-25 22:02:53 +0000180 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000181 *
Rose Zadik332658d2018-01-25 22:02:53 +0000182 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100185 ... ) );
186#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
188#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000189#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#define mbedtls_fprintf fprintf
191#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
192#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000193
194/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000195 * The function pointers for printf
196 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
198extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000199
200/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100201 * \brief This function dynamically configures the snprintf
202 * function that is called when the mbedtls_snprintf()
203 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000204 *
Rose Zadik332658d2018-01-25 22:02:53 +0000205 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000206 *
Rose Zadik332658d2018-01-25 22:02:53 +0000207 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
210#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
211#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
212#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000213#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#define mbedtls_printf printf
215#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
216#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000217
218/*
219 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200220 *
221 * The snprintf implementation should conform to C99:
222 * - it *must* always correctly zero-terminate the buffer
223 * (except when n == 0, then it must leave the buffer untouched)
224 * - however it is acceptable to return -1 instead of the required length when
225 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000226 */
k-stachowiak723f8672018-07-16 14:27:07 +0200227#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200228/* For Windows (inc. MSYS2), we provide our own fixed implementation */
229int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
230#endif
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
233extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000234
235/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100236 * \brief This function allows configuring a custom
237 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000238 *
Rose Zadik332658d2018-01-25 22:02:53 +0000239 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000240 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100241 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000244 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
246#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
247#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000248#else
Azim Khan8d54c062018-03-23 18:34:35 +0000249#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
251#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000252
253/*
k-stachowiak723f8672018-07-16 14:27:07 +0200254 * The function pointers for vsnprintf
255 *
256 * The vsnprintf implementation should conform to C99:
257 * - it *must* always correctly zero-terminate the buffer
258 * (except when n == 0, then it must leave the buffer untouched)
259 * - however it is acceptable to return -1 instead of the required length when
260 * the destination buffer is too short.
261 */
262#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
Jaeden Amero32eb58f2019-05-30 13:18:24 +0100263#include <stdarg.h>
k-stachowiak723f8672018-07-16 14:27:07 +0200264/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
265int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
266#endif
267
268#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
269#include <stdarg.h>
270extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
271
272/**
273 * \brief Set your own snprintf function pointer
274 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200275 * \param vsnprintf_func The \c vsnprintf function implementation
k-stachowiak723f8672018-07-16 14:27:07 +0200276 *
Krzysztof Stachowiak15557162018-09-24 14:15:46 +0200277 * \return \c 0
k-stachowiak723f8672018-07-16 14:27:07 +0200278 */
279int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
280 const char * format, va_list arg ) );
281#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
282#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
283#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
284#else
285#define mbedtls_vsnprintf vsnprintf
286#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
287#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
288
289/*
Rich Evansc39cb492015-01-30 12:01:34 +0000290 * The function pointers for exit
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
293extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000294
295/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100296 * \brief This function dynamically configures the exit
297 * function that is called when the mbedtls_exit()
298 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000299 *
Rose Zadik332658d2018-01-25 22:02:53 +0000300 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000301 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100302 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000305#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
307#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000308#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#define mbedtls_exit exit
310#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
311#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100312
Janos Follath91947442016-03-18 13:49:27 +0000313/*
314 * The default exit values
315 */
316#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
317#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
318#else
319#define MBEDTLS_EXIT_SUCCESS 0
320#endif
321#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
322#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
323#else
324#define MBEDTLS_EXIT_FAILURE 1
325#endif
326
SimonBd5800b72016-04-26 07:43:27 +0100327/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100328 * The function pointers for reading from and writing a seed file to
329 * Non-Volatile storage (NV) in a platform-independent way
330 *
331 * Only enabled when the NV seed entropy source is enabled
332 */
333#if defined(MBEDTLS_ENTROPY_NV_SEED)
334#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
335/* Internal standard platform definitions */
336int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
337int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
338#endif
339
340#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
341extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
342extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
343
344/**
Rose Zadik332658d2018-01-25 22:02:53 +0000345 * \brief This function allows configuring custom seed file writing and
346 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100347 *
Rose Zadik332658d2018-01-25 22:02:53 +0000348 * \param nv_seed_read_func The seed reading function implementation.
349 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100350 *
Rose Zadik332658d2018-01-25 22:02:53 +0000351 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100352 */
353int mbedtls_platform_set_nv_seed(
354 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
355 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
356 );
357#else
358#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
359 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
360#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
361#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
362#else
363#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
364#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
365#endif
366#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
367#endif /* MBEDTLS_ENTROPY_NV_SEED */
368
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100369#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100370
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100371/**
Rose Zadik332658d2018-01-25 22:02:53 +0000372 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100373 *
374 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000375 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100376 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200377typedef struct mbedtls_platform_context
378{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200379 char MBEDTLS_PRIVATE(dummy); /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100380}
381mbedtls_platform_context;
382
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100383#else
384#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100385#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100386
387/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100388 * \brief This function performs any platform-specific initialization
389 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100390 *
391 * \note This function should be called before any other library functions.
392 *
393 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000394 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100395 *
396 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100397 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100398 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100399 *
Rose Zadik332658d2018-01-25 22:02:53 +0000400 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100401 */
402int mbedtls_platform_setup( mbedtls_platform_context *ctx );
403/**
Rose Zadik332658d2018-01-25 22:02:53 +0000404 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100405 *
Rose Zadik332658d2018-01-25 22:02:53 +0000406 * \note This function should be called after every other Mbed TLS module
407 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100408 *
Rose Zadik332658d2018-01-25 22:02:53 +0000409 * Its implementation is platform-specific, and unless
410 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200411 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100412 * \note The usage and necessity of this function is dependent on the platform.
413 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100414 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100415 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100416 */
417void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
418
Paul Bakker747a83a2014-02-01 22:50:07 +0100419#ifdef __cplusplus
420}
421#endif
422
423#endif /* platform.h */