blob: caf8f25271aa31ea05c8712f6f4be6c5e67e5d06 [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
Paul Bakker747a83a2014-02-01 22:50:07 +010032#ifdef __cplusplus
33extern "C" {
34#endif
35
Paul Bakker088c5c52014-04-25 11:11:10 +020036/**
37 * \name SECTION: Module settings
38 *
39 * The configuration options you can set for this module are in this section.
40 * Either change them in config.h or define them on the compiler command line.
41 * \{
42 */
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010046#include <stdlib.h>
Simon Butcher3fe6cd32016-04-26 19:51:29 +010047#include <time.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020049#if defined(_WIN32)
50#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
51#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
Rich Evans46b0a8d2015-01-30 10:47:32 +000053#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020054#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
56#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020057#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
59#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020060#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020061#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
62#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_PLATFORM_STD_FREE)
65#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Janos Follath91947442016-03-18 13:49:27 +000068#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
69#endif
SimonBd5800b72016-04-26 07:43:27 +010070#if !defined(MBEDTLS_PLATFORM_STD_TIME)
71#define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
72#endif
Janos Follath91947442016-03-18 13:49:27 +000073#if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
74#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
75#endif
76#if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
77#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
Rich Evansc39cb492015-01-30 12:01:34 +000078#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +010079#if defined(MBEDTLS_FS_IO)
Paul Bakkere021a4b2016-06-01 11:25:44 +010080#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
81#define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
82#endif
83#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
84#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
85#endif
86#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
87#define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
88#endif
Paul Bakkera9c321c2016-06-01 11:44:12 +010089#endif /* MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
91#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
92#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +020095
Paul Bakkere021a4b2016-06-01 11:25:44 +010096
Paul Bakker088c5c52014-04-25 11:11:10 +020097/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +010098
99/*
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200100 * The function pointers for calloc and free
Paul Bakker747a83a2014-02-01 22:50:07 +0100101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_PLATFORM_MEMORY)
103#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200104 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200106#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +0000107#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100108/* For size_t */
109#include <stddef.h>
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200110extern void * (*mbedtls_calloc)( size_t n, size_t size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111extern void (*mbedtls_free)( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100112
113/**
114 * \brief Set your own memory implementation function pointers
115 *
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200116 * \param calloc_func the calloc function implementation
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100117 * \param free_func the free function implementation
118 *
119 * \return 0 if successful
120 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200121int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100122 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200123#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#else /* !MBEDTLS_PLATFORM_MEMORY */
125#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200126#define mbedtls_calloc calloc
127#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100128
129/*
130 * The function pointers for fprintf
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100133/* We need FILE * */
134#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100136
Rich Evansc39cb492015-01-30 12:01:34 +0000137/**
138 * \brief Set your own fprintf function pointer
139 *
140 * \param fprintf_func the fprintf function implementation
141 *
142 * \return 0
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100145 ... ) );
146#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
148#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000149#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#define mbedtls_fprintf fprintf
151#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
152#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000153
154/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000155 * The function pointers for printf
156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
158extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000159
160/**
161 * \brief Set your own printf function pointer
162 *
163 * \param printf_func the printf function implementation
164 *
165 * \return 0
166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
168#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
169#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
170#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000171#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#define mbedtls_printf printf
173#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
174#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000175
176/*
177 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200178 *
179 * The snprintf implementation should conform to C99:
180 * - it *must* always correctly zero-terminate the buffer
181 * (except when n == 0, then it must leave the buffer untouched)
182 * - however it is acceptable to return -1 instead of the required length when
183 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000184 */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200185#if defined(_WIN32)
186/* For Windows (inc. MSYS2), we provide our own fixed implementation */
187int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
188#endif
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
191extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000192
193/**
194 * \brief Set your own snprintf function pointer
195 *
196 * \param snprintf_func the snprintf function implementation
197 *
198 * \return 0
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000201 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
203#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
204#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000205#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#define mbedtls_snprintf snprintf
207#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
208#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000209
210/*
Rich Evansc39cb492015-01-30 12:01:34 +0000211 * The function pointers for exit
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
214extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000215
216/**
217 * \brief Set your own exit function pointer
218 *
219 * \param exit_func the exit function implementation
220 *
221 * \return 0
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000224#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
226#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000227#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#define mbedtls_exit exit
229#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
230#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100231
Janos Follath91947442016-03-18 13:49:27 +0000232/*
233 * The default exit values
234 */
235#if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
236#define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
237#else
238#define MBEDTLS_EXIT_SUCCESS 0
239#endif
240#if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
241#define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
242#else
243#define MBEDTLS_EXIT_FAILURE 1
244#endif
245
SimonBd5800b72016-04-26 07:43:27 +0100246/*
247 * The time_t datatype
248 */
Simon Butcher80aea302016-04-27 00:28:14 +0100249#if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
250typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t;
SimonBd5800b72016-04-26 07:43:27 +0100251#else
Simon Butcherdb0feca2016-05-17 00:03:14 +0100252/* For time_t */
253#include <time.h>
Simon Butcher80aea302016-04-27 00:28:14 +0100254typedef time_t mbedtls_time_t;
255#endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
SimonBd5800b72016-04-26 07:43:27 +0100256
257/*
258 * The function pointers for time
259 */
260#if defined(MBEDTLS_PLATFORM_TIME_ALT)
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100261extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time );
SimonBd5800b72016-04-26 07:43:27 +0100262
263/**
264 * \brief Set your own time function pointer
265 *
266 * \param time_func the time function implementation
267 *
268 * \return 0
269 */
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100270int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) );
SimonBd5800b72016-04-26 07:43:27 +0100271#else
272#if defined(MBEDTLS_PLATFORM_TIME_MACRO)
273#define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO
274#else
275#define mbedtls_time time
276#endif /* MBEDTLS_PLATFORM_TIME_MACRO */
277#endif /* MBEDTLS_PLATFORM_TIME_ALT */
278
Paul Bakkere021a4b2016-06-01 11:25:44 +0100279/*
280 * The function pointers for reading from and writing a seed file to
281 * Non-Volatile storage (NV) in a platform-independent way
282 *
283 * Only enabled when the NV seed entropy source is enabled
284 */
285#if defined(MBEDTLS_ENTROPY_NV_SEED)
286#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
287/* Internal standard platform definitions */
288int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
289int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
290#endif
291
292#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
293extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
294extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
295
296/**
297 * \brief Set your own seed file writing/reading functions
298 *
299 * \param nv_seed_read_func the seed reading function implementation
300 * \param nv_seed_write_func the seed writing function implementation
301 *
302 * \return 0
303 */
304int mbedtls_platform_set_nv_seed(
305 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
306 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
307 );
308#else
309#if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
310 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
311#define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
312#define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
313#else
314#define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
315#define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
316#endif
317#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
318#endif /* MBEDTLS_ENTROPY_NV_SEED */
319
Paul Bakker747a83a2014-02-01 22:50:07 +0100320#ifdef __cplusplus
321}
322#endif
323
324#endif /* platform.h */