blob: e051751189b9d87724a27664dc1fc34ac8cf981e [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/**
2 * \file platform.h
3 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief mbed TLS Platform abstraction layer
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Paul Bakkere021a4b2016-06-01 11:25:44 +01007 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +010023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_PLATFORM_H
25#define MBEDTLS_PLATFORM_H
Paul Bakker747a83a2014-02-01 22:50:07 +010026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker747a83a2014-02-01 22:50:07 +010028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010032
Simon Butcherb5b6af22016-07-13 14:46:18 +010033#if defined(MBEDTLS_HAVE_TIME)
34#include "mbedtls/platform_time.h"
35#endif
36
Paul Bakker747a83a2014-02-01 22:50:07 +010037#ifdef __cplusplus
38extern "C" {
39#endif
40
Paul Bakker088c5c52014-04-25 11:11:10 +020041/**
42 * \name SECTION: Module settings
43 *
44 * The configuration options you can set for this module are in this section.
45 * Either change them in config.h or define them on the compiler command line.
46 * \{
47 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010051#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010052#include <time.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020054#if defined(_WIN32)
55#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
56#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
Rich Evans46b0a8d2015-01-30 10:47:32 +000058#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020059#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
61#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020062#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
64#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020065#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020066#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
67#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_PLATFORM_STD_FREE)
70#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Janos Follath91947442016-03-18 13:49:27 +000073#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
74#endif
SimonBd5800b72016-04-26 07:43:27 +010075#if !defined(MBEDTLS_PLATFORM_STD_TIME)
76#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
77#endif
Janos Follath91947442016-03-18 13:49:27 +000078#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
79#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
80#endif
81#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
82#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
Rich Evansc39cb492015-01-30 12:01:34 +000083#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +010084#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +010085#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
86#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
87#endif
88#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
89#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
90#endif
91#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
92#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
93#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +010094#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
96#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
97#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +0200100
Paul Bakkere021a4b2016-06-01 11:25:44 +0100101
Paul Bakker088c5c52014-04-25 11:11:10 +0200102/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100103
104/*
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200105 * The function pointers for calloc and free
Paul Bakker747a83a2014-02-01 22:50:07 +0100106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_PLATFORM_MEMORY)
108#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200109 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200111#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000112#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100113/* For size_t */
114#include <stddef.h>
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200115extern void * (*mbedtls_calloc)( size_t n, size_t size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116extern void (*mbedtls_free)( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100117
118/**
119 * \brief Set your own memory implementation function pointers
120 *
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200121 * \param calloc_func the calloc function implementation
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100122 * \param free_func the free function implementation
123 *
124 * \return 0 if successful
125 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200126int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100127 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200128#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#else /* !MBEDTLS_PLATFORM_MEMORY */
130#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200131#define mbedtls_calloc calloc
132#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100133
134/*
135 * The function pointers for fprintf
136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100138/* We need FILE * */
139#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100141
Rich Evansc39cb492015-01-30 12:01:34 +0000142/**
143 * \brief Set your own fprintf function pointer
144 *
145 * \param fprintf_func the fprintf function implementation
146 *
147 * \return 0
148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100150 ... ) );
151#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
153#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000154#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#define mbedtls_fprintf fprintf
156#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
157#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000158
159/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000160 * The function pointers for printf
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
163extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000164
165/**
166 * \brief Set your own printf function pointer
167 *
168 * \param printf_func the printf function implementation
169 *
170 * \return 0
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
173#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
174#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
175#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000176#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#define mbedtls_printf printf
178#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
179#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000180
181/*
182 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200183 *
184 * The snprintf implementation should conform to C99:
185 * - it *must* always correctly zero-terminate the buffer
186 * (except when n == 0, then it must leave the buffer untouched)
187 * - however it is acceptable to return -1 instead of the required length when
188 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000189 */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200190#if defined(_WIN32)
191/* For Windows (inc. MSYS2), we provide our own fixed implementation */
192int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
193#endif
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
196extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000197
198/**
199 * \brief Set your own snprintf function pointer
200 *
201 * \param snprintf_func the snprintf function implementation
202 *
203 * \return 0
204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000206 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
208#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
209#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000210#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#define mbedtls_snprintf snprintf
212#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
213#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000214
215/*
Rich Evansc39cb492015-01-30 12:01:34 +0000216 * The function pointers for exit
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
219extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000220
221/**
222 * \brief Set your own exit function pointer
223 *
224 * \param exit_func the exit function implementation
225 *
226 * \return 0
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000229#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
231#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000232#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#define mbedtls_exit exit
234#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
235#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100236
Janos Follath91947442016-03-18 13:49:27 +0000237/*
238 * The default exit values
239 */
240#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
241#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
242#else
243#define MBEDTLS_EXIT_SUCCESS 0
244#endif
245#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
246#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
247#else
248#define MBEDTLS_EXIT_FAILURE 1
249#endif
250
SimonBd5800b72016-04-26 07:43:27 +0100251/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100252 * The function pointers for reading from and writing a seed file to
253 * Non-Volatile storage (NV) in a platform-independent way
254 *
255 * Only enabled when the NV seed entropy source is enabled
256 */
257#if defined(MBEDTLS_ENTROPY_NV_SEED)
258#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
259/* Internal standard platform definitions */
260int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
261int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
262#endif
263
264#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
265extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
266extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
267
268/**
269 * \brief Set your own seed file writing/reading functions
270 *
271 * \param nv_seed_read_func the seed reading function implementation
272 * \param nv_seed_write_func the seed writing function implementation
273 *
274 * \return 0
275 */
276int mbedtls_platform_set_nv_seed(
277 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
278 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
279 );
280#else
281#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
282 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
283#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
284#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
285#else
286#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
287#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
288#endif
289#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
290#endif /* MBEDTLS_ENTROPY_NV_SEED */
291
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100292#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100293
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100294/**
295 * \brief Platform context structure
296 *
297 * \note This structure may be used to assist platform-specific
298 * setup/teardown operations.
299 */
300typedef struct {
301 char dummy; /**< Placeholder member as empty structs are not portable */
302}
303mbedtls_platform_context;
304
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100305#else
306#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100307#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100308
309/**
310 * \brief Perform any platform initialisation operations
311 *
312 * \param ctx mbed TLS context
313 *
314 * \return 0 if successful
315 *
Simon Butcherd3be27a2017-07-21 02:08:00 +0200316 * \note This function is intended to allow platform specific initialisation,
317 * and should be called before any other library functions. Its
318 * implementation is platform specific, and by default, unless platform
319 * specific code is provided, it does nothing.
320 *
321 * Its use and whether its necessary to be called is dependent on the
322 * platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100323 */
324int mbedtls_platform_setup( mbedtls_platform_context *ctx );
325/**
326 * \brief Perform any platform teardown operations
327 *
328 * \param ctx mbed TLS context
329 *
Simon Butcherd3be27a2017-07-21 02:08:00 +0200330 * \note This function should be called after every other mbed TLS module has
331 * been correctly freed using the appropriate free function.
332 * Its implementation is platform specific, and by default, unless
333 * platform specific code is provided, it does nothing.
334 *
335 * Its use and whether its necessary to be called is dependent on the
336 * platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100337 */
338void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
339
Paul Bakker747a83a2014-02-01 22:50:07 +0100340#ifdef __cplusplus
341}
342#endif
343
344#endif /* platform.h */