blob: f6ccd1cd82f6a500affd133376d1c15dbdf7596a [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útia2947ac2020-08-19 16:37:36 +020016 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
18 *
19 * This file is provided under the Apache License 2.0, or the
20 * GNU General Public License v2.0 or later.
21 *
22 * **********
23 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020024 *
25 * Licensed under the Apache License, Version 2.0 (the "License"); you may
26 * not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010036 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020037 * **********
38 *
39 * **********
40 * GNU General Public License v2.0 or later:
41 *
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License as published by
44 * the Free Software Foundation; either version 2 of the License, or
45 * (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License along
53 * with this program; if not, write to the Free Software Foundation, Inc.,
54 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55 *
56 * **********
Paul Bakker747a83a2014-02-01 22:50:07 +010057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#ifndef MBEDTLS_PLATFORM_H
59#define MBEDTLS_PLATFORM_H
Paul Bakker747a83a2014-02-01 22:50:07 +010060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker747a83a2014-02-01 22:50:07 +010062#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020063#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020065#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010066
Simon Butcherb5b6af22016-07-13 14:46:18 +010067#if defined(MBEDTLS_HAVE_TIME)
Ron Eldor6fd941f2017-05-14 16:17:33 +030068#include "platform_time.h"
Simon Butcherb5b6af22016-07-13 14:46:18 +010069#endif
70
Gilles Peskine1990fab2021-07-26 18:48:10 +020071/** Hardware accelerator failed */
72#define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
73/** The requested feature is not supported by the platform */
74#define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
Ron Eldor0ff4e0b2018-08-29 18:53:20 +030075
Paul Bakker747a83a2014-02-01 22:50:07 +010076#ifdef __cplusplus
77extern "C" {
78#endif
79
Paul Bakker088c5c52014-04-25 11:11:10 +020080/**
81 * \name SECTION: Module settings
82 *
83 * The configuration options you can set for this module are in this section.
84 * Either change them in config.h or define them on the compiler command line.
85 * \{
86 */
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000089#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010090#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010091#include <time.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020093#if defined(_WIN32)
Rose Zadik332658d2018-01-25 22:02:53 +000094#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 +020095#else
Rose Zadik332658d2018-01-25 22:02:53 +000096#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< The default \c snprintf function to use. */
Rich Evans46b0a8d2015-01-30 10:47:32 +000097#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +0000100#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< The default \c printf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200101#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Rose Zadik332658d2018-01-25 22:02:53 +0000103#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< The default \c fprintf function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200104#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200105#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Rose Zadik332658d2018-01-25 22:02:53 +0000106#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< The default \c calloc function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200107#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Rose Zadik332658d2018-01-25 22:02:53 +0000109#define MBEDTLS_PLATFORM_STD_FREE free /**< The default \c free function to use. */
Paul Bakker088c5c52014-04-25 11:11:10 +0200110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rose Zadik332658d2018-01-25 22:02:53 +0000112#define MBEDTLS_PLATFORM_STD_EXIT exit /**< The default \c exit function to use. */
Janos Follath91947442016-03-18 13:49:27 +0000113#endif
SimonBd5800b72016-04-26 07:43:27 +0100114#if !defined(MBEDTLS_PLATFORM_STD_TIME)
Rose Zadik332658d2018-01-25 22:02:53 +0000115#define MBEDTLS_PLATFORM_STD_TIME time /**< The default \c time function to use. */
SimonBd5800b72016-04-26 07:43:27 +0100116#endif
Janos Follath91947442016-03-18 13:49:27 +0000117#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
Rose Zadik332658d2018-01-25 22:02:53 +0000118#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< The default exit value to use. */
Janos Follath91947442016-03-18 13:49:27 +0000119#endif
120#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
Rose Zadik332658d2018-01-25 22:02:53 +0000121#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< The default exit value to use. */
Rich Evansc39cb492015-01-30 12:01:34 +0000122#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100123#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100124#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
125#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
126#endif
127#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
128#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
129#endif
130#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
131#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
132#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +0100133#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
135#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
136#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +0200137#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200139
Paul Bakkere021a4b2016-06-01 11:25:44 +0100140
Paul Bakker088c5c52014-04-25 11:11:10 +0200141/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100142
143/*
Rose Zadik4bca2b02018-03-27 13:12:52 +0100144 * The function pointers for calloc and free.
Paul Bakker747a83a2014-02-01 22:50:07 +0100145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_PLATFORM_MEMORY)
147#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200148 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200150#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000151#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100152/* For size_t */
153#include <stddef.h>
Roberto Vargas7decfe82018-06-04 13:54:09 +0100154extern void *mbedtls_calloc( size_t n, size_t size );
155extern void mbedtls_free( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100156
157/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100158 * \brief This function dynamically sets the memory-management
159 * functions used by the library, during runtime.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100160 *
Rose Zadik332658d2018-01-25 22:02:53 +0000161 * \param calloc_func The \c calloc function implementation.
162 * \param free_func The \c free function implementation.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100163 *
Rose Zadik332658d2018-01-25 22:02:53 +0000164 * \return \c 0.
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100165 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200166int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100167 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200168#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#else /* !MBEDTLS_PLATFORM_MEMORY */
170#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200171#define mbedtls_calloc calloc
172#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100173
174/*
175 * The function pointers for fprintf
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100178/* We need FILE * */
179#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100181
Rich Evansc39cb492015-01-30 12:01:34 +0000182/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100183 * \brief This function dynamically configures the fprintf
184 * function that is called when the
185 * mbedtls_fprintf() function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000186 *
Rose Zadik332658d2018-01-25 22:02:53 +0000187 * \param fprintf_func The \c fprintf function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000188 *
Rose Zadik332658d2018-01-25 22:02:53 +0000189 * \return \c 0.
Rich Evansc39cb492015-01-30 12:01:34 +0000190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100192 ... ) );
193#else
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)
205extern 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
217#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
218#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
219#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000220#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#define mbedtls_printf printf
222#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
223#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000224
225/*
226 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200227 *
228 * The snprintf implementation should conform to C99:
229 * - it *must* always correctly zero-terminate the buffer
230 * (except when n == 0, then it must leave the buffer untouched)
231 * - however it is acceptable to return -1 instead of the required length when
232 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000233 */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200234#if defined(_WIN32)
235/* For Windows (inc. MSYS2), we provide our own fixed implementation */
236int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
237#endif
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
240extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000241
242/**
Rose Zadik4bca2b02018-03-27 13:12:52 +0100243 * \brief This function allows configuring a custom
244 * \c snprintf function pointer.
Rich Evans16f8cd82015-02-06 16:14:34 +0000245 *
Rose Zadik332658d2018-01-25 22:02:53 +0000246 * \param snprintf_func The \c snprintf function implementation.
Rich Evans16f8cd82015-02-06 16:14:34 +0000247 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100248 * \return \c 0 on success.
Rich Evans16f8cd82015-02-06 16:14:34 +0000249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000251 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
253#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
254#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000255#else
Azim Khan8d54c062018-03-23 18:34:35 +0000256#define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
258#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000259
260/*
Rich Evansc39cb492015-01-30 12:01:34 +0000261 * The function pointers for exit
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
264extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000265
266/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100267 * \brief This function dynamically configures the exit
268 * function that is called when the mbedtls_exit()
269 * function is invoked by the library.
Rich Evansc39cb492015-01-30 12:01:34 +0000270 *
Rose Zadik332658d2018-01-25 22:02:53 +0000271 * \param exit_func The \c exit function implementation.
Rich Evansc39cb492015-01-30 12:01:34 +0000272 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100273 * \return \c 0 on success.
Rich Evansc39cb492015-01-30 12:01:34 +0000274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000276#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
278#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000279#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#define mbedtls_exit exit
281#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
282#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100283
Janos Follath91947442016-03-18 13:49:27 +0000284/*
285 * The default exit values
286 */
287#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
288#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
289#else
290#define MBEDTLS_EXIT_SUCCESS 0
291#endif
292#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
293#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
294#else
295#define MBEDTLS_EXIT_FAILURE 1
296#endif
297
SimonBd5800b72016-04-26 07:43:27 +0100298/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100299 * The function pointers for reading from and writing a seed file to
300 * Non-Volatile storage (NV) in a platform-independent way
301 *
302 * Only enabled when the NV seed entropy source is enabled
303 */
304#if defined(MBEDTLS_ENTROPY_NV_SEED)
305#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
306/* Internal standard platform definitions */
307int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
308int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
309#endif
310
311#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
312extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
313extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
314
315/**
Rose Zadik332658d2018-01-25 22:02:53 +0000316 * \brief This function allows configuring custom seed file writing and
317 * reading functions.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100318 *
Rose Zadik332658d2018-01-25 22:02:53 +0000319 * \param nv_seed_read_func The seed reading function implementation.
320 * \param nv_seed_write_func The seed writing function implementation.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100321 *
Rose Zadik332658d2018-01-25 22:02:53 +0000322 * \return \c 0 on success.
Paul Bakkere021a4b2016-06-01 11:25:44 +0100323 */
324int mbedtls_platform_set_nv_seed(
325 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
326 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
327 );
328#else
329#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
330 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
331#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
332#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
333#else
334#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
335#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
336#endif
337#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
338#endif /* MBEDTLS_ENTROPY_NV_SEED */
339
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100340#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100341
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100342/**
Rose Zadik332658d2018-01-25 22:02:53 +0000343 * \brief The platform context structure.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100344 *
345 * \note This structure may be used to assist platform-specific
Rose Zadik332658d2018-01-25 22:02:53 +0000346 * setup or teardown operations.
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100347 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200348typedef struct mbedtls_platform_context
349{
Rose Zadik4bca2b02018-03-27 13:12:52 +0100350 char dummy; /**< A placeholder member, as empty structs are not portable. */
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100351}
352mbedtls_platform_context;
353
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100354#else
355#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100356#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100357
358/**
Rose Zadik9464d7b2018-04-16 15:28:35 +0100359 * \brief This function performs any platform-specific initialization
360 * operations.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100361 *
362 * \note This function should be called before any other library functions.
363 *
364 * Its implementation is platform-specific, and unless
Darryl Green11999bb2018-03-13 15:22:58 +0000365 * platform-specific code is provided, it does nothing.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100366 *
367 * \note The usage and necessity of this function is dependent on the platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100368 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100369 * \param ctx The platform context.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100370 *
Rose Zadik332658d2018-01-25 22:02:53 +0000371 * \return \c 0 on success.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100372 */
373int mbedtls_platform_setup( mbedtls_platform_context *ctx );
374/**
Rose Zadik332658d2018-01-25 22:02:53 +0000375 * \brief This function performs any platform teardown operations.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100376 *
Rose Zadik332658d2018-01-25 22:02:53 +0000377 * \note This function should be called after every other Mbed TLS module
378 * has been correctly freed using the appropriate free function.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100379 *
Rose Zadik332658d2018-01-25 22:02:53 +0000380 * Its implementation is platform-specific, and unless
381 * platform-specific code is provided, it does nothing.
Simon Butcherd3be27a2017-07-21 02:08:00 +0200382 *
Rose Zadik4bca2b02018-03-27 13:12:52 +0100383 * \note The usage and necessity of this function is dependent on the platform.
384 *
Rose Zadik9464d7b2018-04-16 15:28:35 +0100385 * \param ctx The platform context.
Rose Zadik4bca2b02018-03-27 13:12:52 +0100386 *
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100387 */
388void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
389
Paul Bakker747a83a2014-02-01 22:50:07 +0100390#ifdef __cplusplus
391}
392#endif
393
394#endif /* platform.h */