blob: a121654e2da94917d4ceb90528e7c9d93b3943e2 [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/*
Rose Zadik332658d2018-01-25 22:02:53 +000016 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 *
Rose Zadik332658d2018-01-25 22:02:53 +000031 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +010032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#ifndef MBEDTLS_PLATFORM_H
34#define MBEDTLS_PLATFORM_H
Paul Bakker747a83a2014-02-01 22:50:07 +010035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker747a83a2014-02-01 22:50:07 +010037#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020040#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010041
Simon Butcherb5b6af22016-07-13 14:46:18 +010042#if defined(MBEDTLS_HAVE_TIME)
43#include "mbedtls/platform_time.h"
44#endif
45
Paul Bakker747a83a2014-02-01 22:50:07 +010046#ifdef __cplusplus
47extern "C" {
48#endif
49
Paul Bakker088c5c52014-04-25 11:11:10 +020050/**
51 * \name SECTION: Module settings
52 *
53 * The configuration options you can set for this module are in this section.
54 * Either change them in config.h or define them on the compiler command line.
55 * \{
56 */
57
k-stachowiak723f8672018-07-16 14:27:07 +020058/* The older Microsoft Windows common runtime provides non-conforming
59 * implementations of some standard library functions, including snprintf
60 * and vsnprintf. This affects MSVC and MinGW builds.
61 */
62#if defined(_MSC_VER) && _MSC_VER <= 1900
63#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
64#define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
65#endif
66
Krzysztof Stachowiak643bcb32018-08-29 15:33:59 +020067#if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
68#define __USE_MINGW_ANSI_STDIO 1
69#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000072#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010073#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010074#include <time.h>
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +0000102#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +0000103#endif
SimonBd5800b72016-04-26 07:43:27 +0100104#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +0000105#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +0100106#endif
Janos Follath91947442016-03-18 13:49:27 +0000107#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +0000108#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000109#endif
110#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000111#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000112#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100113#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100114#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
115#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
116#endif
117#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
118#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
119#endif
120#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
121#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
122#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100123#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
125#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
126#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200127#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200129
Paul Bakkere021a4b2016-06-01 11:25:44 +0100130
Paul Bakker088c5c52014-04-25 11:11:10 +0200131/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100132
133/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100134 * The function pointers for calloc and free.
Paul Bakker747a83a2014-02-01 22:50:07 +0100135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_PLATFORM_MEMORY)
137#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200138 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200140#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000141#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100142/* For size_t */
143#include <stddef.h>
Roberto Vargas7decfe82018-06-04 13:54:09 +0100144extern void *mbedtls_calloc( size_t n, size_t size );
145extern void mbedtls_free( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100146
147/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100148 * \brief This function dynamically sets the memory-management
149 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100150 *
Rose Zadik332658d2018-01-25 22:02:53 +0000151 * \param calloc_func The \c calloc function implementation.
152 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100153 *
Rose Zadik332658d2018-01-25 22:02:53 +0000154 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100155 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200156int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100157 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200158#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#else /* !MBEDTLS_PLATFORM_MEMORY */
160#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200161#define mbedtls_calloc calloc
162#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100163
164/*
165 * The function pointers for fprintf
166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100168/* We need FILE * */
169#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100171
Rich Evansc39cb492015-01-30 12:01:34 +0000172/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100173 * \brief This function dynamically configures the fprintf
174 * function that is called when the
175 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000176 *
Rose Zadik332658d2018-01-25 22:02:53 +0000177 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000178 *
Rose Zadik332658d2018-01-25 22:02:53 +0000179 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100182 ... ) );
183#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
185#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000186#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#define mbedtls_fprintf fprintf
188#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
189#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000190
191/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000192 * The function pointers for printf
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
195extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000196
197/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100198 * \brief This function dynamically configures the snprintf
199 * function that is called when the mbedtls_snprintf()
200 * function is invoked by the library.
Rich Evans16f8cd82015-02-06 16:14:34 +0000201 *
Rose Zadik332658d2018-01-25 22:02:53 +0000202 * \param printf_func The \c printf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000203 *
Rose Zadik332658d2018-01-25 22:02:53 +0000204 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
207#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
208#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
209#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000210#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#define mbedtls_printf printf
212#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
213#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000214
215/*
216 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200217 *
218 * The snprintf implementation should conform to C99:
219 * - it *must* always correctly zero-terminate the buffer
220 * (except when n == 0, then it must leave the buffer untouched)
221 * - however it is acceptable to return -1 instead of the required length when
222 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000223 */
k-stachowiak723f8672018-07-16 14:27:07 +0200224#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200225/* For Windows (inc. MSYS2), we provide our own fixed implementation */
226int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
227#endif
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
230extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000231
232/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100233 * \brief This function allows configuring a custom
234 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000235 *
Rose Zadik332658d2018-01-25 22:02:53 +0000236 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000237 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100238 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000241 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
243#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
244#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000245#else
Azim Khan8d54c062018-03-23 18:34:35 +0000246#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
248#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000249
250/*
k-stachowiak723f8672018-07-16 14:27:07 +0200251 * The function pointers for vsnprintf
252 *
253 * The vsnprintf implementation should conform to C99:
254 * - it *must* always correctly zero-terminate the buffer
255 * (except when n == 0, then it must leave the buffer untouched)
256 * - however it is acceptable to return -1 instead of the required length when
257 * the destination buffer is too short.
258 */
259#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
260/* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
261int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
262#endif
263
264#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
265#include <stdarg.h>
266extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
267
268/**
269 * \brief Set your own snprintf function pointer
270 *
271 * \param vsnprintf_func the snprintf function implementation
272 *
273 * \return 0
274 */
275int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
276 const char * format, va_list arg ) );
277#else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
278#if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
279#define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
280#else
281#define mbedtls_vsnprintf vsnprintf
282#endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
283#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
284
285/*
Rich Evansc39cb492015-01-30 12:01:34 +0000286 * The function pointers for exit
287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
289extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000290
291/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100292 * \brief This function dynamically configures the exit
293 * function that is called when the mbedtls_exit()
294 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000295 *
Rose Zadik332658d2018-01-25 22:02:53 +0000296 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000297 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100298 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000301#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
303#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000304#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#define mbedtls_exit exit
306#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
307#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100308
Janos Follath91947442016-03-18 13:49:27 +0000309/*
310 * The default exit values
311 */
312#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
313#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
314#else
315#define MBEDTLS_EXIT_SUCCESS 0
316#endif
317#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
318#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
319#else
320#define MBEDTLS_EXIT_FAILURE 1
321#endif
322
SimonBd5800b72016-04-26 07:43:27 +0100323/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100324 * The function pointers for reading from and writing a seed file to
325 * Non-Volatile storage (NV) in a platform-independent way
326 *
327 * Only enabled when the NV seed entropy source is enabled
328 */
329#if defined(MBEDTLS_ENTROPY_NV_SEED)
330#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
331/* Internal standard platform definitions */
332int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
333int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
334#endif
335
336#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
337extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
338extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
339
340/**
Rose Zadik332658d2018-01-25 22:02:53 +0000341 * \brief This function allows configuring custom seed file writing and
342 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100343 *
Rose Zadik332658d2018-01-25 22:02:53 +0000344 * \param nv_seed_read_func The seed reading function implementation.
345 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100346 *
Rose Zadik332658d2018-01-25 22:02:53 +0000347 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100348 */
349int mbedtls_platform_set_nv_seed(
350 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
351 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
352 );
353#else
354#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
355 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
356#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
357#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
358#else
359#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
360#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
361#endif
362#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
363#endif /* MBEDTLS_ENTROPY_NV_SEED */
364
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100365#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100366
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100367/**
Rose Zadik332658d2018-01-25 22:02:53 +0000368 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100369 *
370 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000371 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100372 */
373typedef struct {
Rose Zadik4bca2b02018-03-27 13:12:52 +0100374 char dummy; /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100375}
376mbedtls_platform_context;
377
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100378#else
379#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100380#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100381
382/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100383 * \brief This function performs any platform-specific initialization
384 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100385 *
386 * \note This function should be called before any other library functions.
387 *
388 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000389 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100390 *
391 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100392 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100393 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100394 *
Rose Zadik332658d2018-01-25 22:02:53 +0000395 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100396 */
397int mbedtls_platform_setup( mbedtls_platform_context *ctx );
398/**
Rose Zadik332658d2018-01-25 22:02:53 +0000399 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100400 *
Rose Zadik332658d2018-01-25 22:02:53 +0000401 * \note This function should be called after every other Mbed TLS module
402 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100403 *
Rose Zadik332658d2018-01-25 22:02:53 +0000404 * Its implementation is platform-specific, and unless
405 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200406 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100407 * \note The usage and necessity of this function is dependent on the platform.
408 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100409 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100410 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100411 */
412void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
413
Paul Bakker747a83a2014-02-01 22:50:07 +0100414#ifdef __cplusplus
415}
416#endif
417
418#endif /* platform.h */