blob: 25b5d2129334d36157a6b0d62f2555842e2d4a6e [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
Paul Bakker747a83a2014-02-01 22:50:07 +01005 *
Paul Bakkere021a4b2016-06-01 11:25:44 +01006 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_PLATFORM_H
24#define MBEDTLS_PLATFORM_H
Paul Bakker747a83a2014-02-01 22:50:07 +010025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker747a83a2014-02-01 22:50:07 +010027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010031
Simon Butcherb5b6af22016-07-13 14:46:18 +010032#if defined(MBEDTLS_HAVE_TIME)
33#include "mbedtls/platform_time.h"
34#endif
35
Paul Bakker747a83a2014-02-01 22:50:07 +010036#ifdef __cplusplus
37extern "C" {
38#endif
39
Paul Bakker088c5c52014-04-25 11:11:10 +020040/**
41 * \name SECTION: Module settings
42 *
43 * The configuration options you can set for this module are in this section.
44 * Either change them in config.h or define them on the compiler command line.
45 * \{
46 */
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000049#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010050#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010051#include <time.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020053#if defined(_WIN32)
54#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
55#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
Rich Evans46b0a8d2015-01-30 10:47:32 +000057#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
60#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
63#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020064#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020065#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
66#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_PLATFORM_STD_FREE)
69#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Janos Follath91947442016-03-18 13:49:27 +000072#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
73#endif
SimonBd5800b72016-04-26 07:43:27 +010074#if !defined(MBEDTLS_PLATFORM_STD_TIME)
75#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
76#endif
Janos Follath91947442016-03-18 13:49:27 +000077#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
78#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
79#endif
80#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
81#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
Rich Evansc39cb492015-01-30 12:01:34 +000082#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +010083#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +010084#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
85#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
86#endif
87#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
88#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
89#endif
90#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
91#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
92#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +010093#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
95#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
96#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020097#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +020099
Paul Bakkere021a4b2016-06-01 11:25:44 +0100100
Paul Bakker088c5c52014-04-25 11:11:10 +0200101/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +0100102
103/*
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200104 * The function pointers for calloc and free
Paul Bakker747a83a2014-02-01 22:50:07 +0100105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_PLATFORM_MEMORY)
107#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200108 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200110#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000111#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100112/* For size_t */
113#include <stddef.h>
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200114extern void * (*mbedtls_calloc)( size_t n, size_t size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115extern void (*mbedtls_free)( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100116
117/**
118 * \brief Set your own memory implementation function pointers
119 *
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200120 * \param calloc_func the calloc function implementation
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100121 * \param free_func the free function implementation
122 *
123 * \return 0 if successful
124 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200125int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100126 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200127#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#else /* !MBEDTLS_PLATFORM_MEMORY */
129#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200130#define mbedtls_calloc calloc
131#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100132
133/*
134 * The function pointers for fprintf
135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100137/* We need FILE * */
138#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100140
Rich Evansc39cb492015-01-30 12:01:34 +0000141/**
142 * \brief Set your own fprintf function pointer
143 *
144 * \param fprintf_func the fprintf function implementation
145 *
146 * \return 0
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100149 ... ) );
150#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
152#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000153#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define mbedtls_fprintf fprintf
155#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
156#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000157
158/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000159 * The function pointers for printf
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
162extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000163
164/**
165 * \brief Set your own printf function pointer
166 *
167 * \param printf_func the printf function implementation
168 *
169 * \return 0
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
172#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
173#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
174#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000175#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#define mbedtls_printf printf
177#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
178#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000179
180/*
181 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200182 *
183 * The snprintf implementation should conform to C99:
184 * - it *must* always correctly zero-terminate the buffer
185 * (except when n == 0, then it must leave the buffer untouched)
186 * - however it is acceptable to return -1 instead of the required length when
187 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000188 */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200189#if defined(_WIN32)
190/* For Windows (inc. MSYS2), we provide our own fixed implementation */
191int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
192#endif
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
195extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000196
197/**
198 * \brief Set your own snprintf function pointer
199 *
200 * \param snprintf_func the snprintf function implementation
201 *
202 * \return 0
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000205 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
207#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
208#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000209#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#define mbedtls_snprintf snprintf
211#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
212#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000213
214/*
Rich Evansc39cb492015-01-30 12:01:34 +0000215 * The function pointers for exit
216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
218extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000219
220/**
221 * \brief Set your own exit function pointer
222 *
223 * \param exit_func the exit function implementation
224 *
225 * \return 0
226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000228#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
230#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000231#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#define mbedtls_exit exit
233#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
234#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100235
Janos Follath91947442016-03-18 13:49:27 +0000236/*
237 * The default exit values
238 */
239#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
240#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
241#else
242#define MBEDTLS_EXIT_SUCCESS 0
243#endif
244#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
245#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
246#else
247#define MBEDTLS_EXIT_FAILURE 1
248#endif
249
SimonBd5800b72016-04-26 07:43:27 +0100250/*
Paul Bakkere021a4b2016-06-01 11:25:44 +0100251 * The function pointers for reading from and writing a seed file to
252 * Non-Volatile storage (NV) in a platform-independent way
253 *
254 * Only enabled when the NV seed entropy source is enabled
255 */
256#if defined(MBEDTLS_ENTROPY_NV_SEED)
257#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
258/* Internal standard platform definitions */
259int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
260int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
261#endif
262
263#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
264extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
265extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
266
267/**
268 * \brief Set your own seed file writing/reading functions
269 *
270 * \param nv_seed_read_func the seed reading function implementation
271 * \param nv_seed_write_func the seed writing function implementation
272 *
273 * \return 0
274 */
275int mbedtls_platform_set_nv_seed(
276 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
277 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
278 );
279#else
280#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
281 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
282#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
283#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
284#else
285#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
286#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
287#endif
288#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
289#endif /* MBEDTLS_ENTROPY_NV_SEED */
290
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100291#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100292
Andres Amaya Garcia64b02cd2017-07-12 11:32:40 +0100293/**
294 * \brief Platform context structure
295 *
296 * \note This structure may be used to assist platform-specific
297 * setup/teardown operations.
298 */
299typedef struct {
300 char dummy; /**< Placeholder member as empty structs are not portable */
301}
302mbedtls_platform_context;
303
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100304#else
305#include "platform_alt.h"
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100306#endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100307
308/**
309 * \brief Perform any platform initialisation operations
310 *
311 * \param ctx mbed TLS context
312 *
313 * \return 0 if successful
314 *
Simon Butcherd3be27a2017-07-21 02:08:00 +0200315 * \note This function is intended to allow platform specific initialisation,
316 * and should be called before any other library functions. Its
317 * implementation is platform specific, and by default, unless platform
318 * specific code is provided, it does nothing.
319 *
320 * Its use and whether its necessary to be called is dependent on the
321 * platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100322 */
323int mbedtls_platform_setup( mbedtls_platform_context *ctx );
324/**
325 * \brief Perform any platform teardown operations
326 *
327 * \param ctx mbed TLS context
328 *
329 * \return 0 if successful
330 *
Simon Butcherd3be27a2017-07-21 02:08:00 +0200331 * \note This function should be called after every other mbed TLS module has
332 * been correctly freed using the appropriate free function.
333 * Its implementation is platform specific, and by default, unless
334 * platform specific code is provided, it does nothing.
335 *
336 * Its use and whether its necessary to be called is dependent on the
337 * platform.
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100338 */
339void mbedtls_platform_teardown( mbedtls_platform_context *ctx );
340
Paul Bakker747a83a2014-02-01 22:50:07 +0100341#ifdef __cplusplus
342}
343#endif
344
345#endif /* platform.h */