blob: 5dab974fdb7fd72a30c4d6ed754cd86165d4e72e [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker747a83a2014-02-01 22:50:07 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +01009 *
Paul Bakker747a83a2014-02-01 22:50:07 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
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
Paul Bakker747a83a2014-02-01 22:50:07 +010033#ifdef __cplusplus
34extern "C" {
35#endif
36
Paul Bakker088c5c52014-04-25 11:11:10 +020037/**
38 * \name SECTION: Module settings
39 *
40 * The configuration options you can set for this module are in this section.
41 * Either change them in config.h or define them on the compiler command line.
42 * \{
43 */
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <stdio.h>
Paul Bakkera9066cf2014-02-05 15:13:04 +010047#include <stdlib.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)
68#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default free to use */
Rich Evansc39cb492015-01-30 12:01:34 +000069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
71#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
72#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +020075
76/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +010077
78/*
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020079 * The function pointers for calloc and free
Paul Bakker747a83a2014-02-01 22:50:07 +010080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_PLATFORM_MEMORY)
82#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020083 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020085#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +000086#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +010087/* For size_t */
88#include <stddef.h>
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020089extern void * (*mbedtls_calloc)( size_t n, size_t size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090extern void (*mbedtls_free)( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010091
92/**
93 * \brief Set your own memory implementation function pointers
94 *
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020095 * \param calloc_func the calloc function implementation
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010096 * \param free_func the free function implementation
97 *
98 * \return 0 if successful
99 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200100int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100101 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200102#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#else /* !MBEDTLS_PLATFORM_MEMORY */
104#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200105#define mbedtls_calloc calloc
106#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100107
108/*
109 * The function pointers for fprintf
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100112/* We need FILE * */
113#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100115
Rich Evansc39cb492015-01-30 12:01:34 +0000116/**
117 * \brief Set your own fprintf function pointer
118 *
119 * \param fprintf_func the fprintf function implementation
120 *
121 * \return 0
122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100124 ... ) );
125#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
127#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000128#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#define mbedtls_fprintf fprintf
130#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
131#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000132
133/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000134 * The function pointers for printf
135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
137extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000138
139/**
140 * \brief Set your own printf function pointer
141 *
142 * \param printf_func the printf function implementation
143 *
144 * \return 0
145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
147#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
148#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
149#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000150#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#define mbedtls_printf printf
152#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
153#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000154
155/*
156 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200157 *
158 * The snprintf implementation should conform to C99:
159 * - it *must* always correctly zero-terminate the buffer
160 * (except when n == 0, then it must leave the buffer untouched)
161 * - however it is acceptable to return -1 instead of the required length when
162 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000163 */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200164#if defined(_WIN32)
165/* For Windows (inc. MSYS2), we provide our own fixed implementation */
166int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
167#endif
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
170extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000171
172/**
173 * \brief Set your own snprintf function pointer
174 *
175 * \param snprintf_func the snprintf function implementation
176 *
177 * \return 0
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000180 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
182#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
183#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000184#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#define mbedtls_snprintf snprintf
186#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
187#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000188
189/*
Rich Evansc39cb492015-01-30 12:01:34 +0000190 * The function pointers for exit
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
193extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000194
195/**
196 * \brief Set your own exit function pointer
197 *
198 * \param exit_func the exit function implementation
199 *
200 * \return 0
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000203#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
205#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000206#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#define mbedtls_exit exit
208#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
209#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100210
211#ifdef __cplusplus
212}
213#endif
214
215#endif /* platform.h */