blob: f71f1b6494ef4ce52bcb7583f6640f51e024962d [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é-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, 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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020048#if defined(_WIN32)
49#define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
50#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
Rich Evans46b0a8d2015-01-30 10:47:32 +000052#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
55#define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
58#define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020059#endif
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020060#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
61#define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator 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_FREE)
64#define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
Paul Bakker088c5c52014-04-25 11:11:10 +020065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
67#define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default free to use */
Rich Evansc39cb492015-01-30 12:01:34 +000068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
70#if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
71#include MBEDTLS_PLATFORM_STD_MEM_HDR
Manuel Pégourié-Gonnardeb82a742014-04-02 13:43:48 +020072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
Paul Bakker088c5c52014-04-25 11:11:10 +020074
75/* \} name SECTION: Module settings */
Paul Bakker747a83a2014-02-01 22:50:07 +010076
77/*
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020078 * The function pointers for calloc and free
Paul Bakker747a83a2014-02-01 22:50:07 +010079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_PLATFORM_MEMORY)
81#if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020082 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020084#define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
Rich Evans401bb902015-02-10 12:28:15 +000085#else
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +010086/* For size_t */
87#include <stddef.h>
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020088extern void * (*mbedtls_calloc)( size_t n, size_t size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089extern void (*mbedtls_free)( void *ptr );
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010090
91/**
92 * \brief Set your own memory implementation function pointers
93 *
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020094 * \param calloc_func the calloc function implementation
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010095 * \param free_func the free function implementation
96 *
97 * \return 0 if successful
98 */
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020099int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100100 void (*free_func)( void * ) );
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200101#endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#else /* !MBEDTLS_PLATFORM_MEMORY */
103#define mbedtls_free free
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200104#define mbedtls_calloc calloc
105#endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
Paul Bakker747a83a2014-02-01 22:50:07 +0100106
107/*
108 * The function pointers for fprintf
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100111/* We need FILE * */
112#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
Paul Bakker747a83a2014-02-01 22:50:07 +0100114
Rich Evansc39cb492015-01-30 12:01:34 +0000115/**
116 * \brief Set your own fprintf function pointer
117 *
118 * \param fprintf_func the fprintf function implementation
119 *
120 * \return 0
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
Paul Bakker747a83a2014-02-01 22:50:07 +0100123 ... ) );
124#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
126#define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000127#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#define mbedtls_fprintf fprintf
129#endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
130#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000131
132/*
Rich Evans16f8cd82015-02-06 16:14:34 +0000133 * The function pointers for printf
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
136extern int (*mbedtls_printf)( const char *format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000137
138/**
139 * \brief Set your own printf function pointer
140 *
141 * \param printf_func the printf function implementation
142 *
143 * \return 0
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
146#else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
147#if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
148#define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000149#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#define mbedtls_printf printf
151#endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
152#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000153
154/*
155 * The function pointers for snprintf
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200156 *
157 * The snprintf implementation should conform to C99:
158 * - it *must* always correctly zero-terminate the buffer
159 * (except when n == 0, then it must leave the buffer untouched)
160 * - however it is acceptable to return -1 instead of the required length when
161 * the destination buffer is too short.
Rich Evans16f8cd82015-02-06 16:14:34 +0000162 */
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200163#if defined(_WIN32)
164/* For Windows (inc. MSYS2), we provide our own fixed implementation */
165int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
166#endif
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
169extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
Rich Evans16f8cd82015-02-06 16:14:34 +0000170
171/**
172 * \brief Set your own snprintf function pointer
173 *
174 * \param snprintf_func the snprintf function implementation
175 *
176 * \return 0
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans16f8cd82015-02-06 16:14:34 +0000179 const char * format, ... ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
181#if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
182#define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000183#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#define mbedtls_snprintf snprintf
185#endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
186#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans16f8cd82015-02-06 16:14:34 +0000187
188/*
Rich Evansc39cb492015-01-30 12:01:34 +0000189 * The function pointers for exit
190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
192extern void (*mbedtls_exit)( int status );
Rich Evansc39cb492015-01-30 12:01:34 +0000193
194/**
195 * \brief Set your own exit function pointer
196 *
197 * \param exit_func the exit function implementation
198 *
199 * \return 0
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
Rich Evansc39cb492015-01-30 12:01:34 +0000202#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
204#define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
Rich Evans16f8cd82015-02-06 16:14:34 +0000205#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#define mbedtls_exit exit
207#endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
208#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100209
210#ifdef __cplusplus
211}
212#endif
213
214#endif /* platform.h */