blob: e9ab3024ea1525eb393b250dd4bbb7e69f37d645 [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker747a83a2014-02-01 22:50:07 +01005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +01007 *
Paul Bakker747a83a2014-02-01 22:50:07 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_PLATFORM_MEMORY)
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020034#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
35static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010036{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020037 ((void) n);
38 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010039 return( NULL );
40}
41
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020042#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
43#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010046static void platform_free_uninit( void *ptr )
47{
48 ((void) ptr);
49}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
52#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010053
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020054void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010056
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020057int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010058 void (*free_func)( void * ) )
59{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020060 mbedtls_calloc = calloc_func;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_free = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010062 return( 0 );
63}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#endif /* MBEDTLS_PLATFORM_MEMORY */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
67#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000068/*
69 * Make dummy function to prevent NULL pointer dereferences
70 */
71static int platform_snprintf_uninit( char * s, size_t n,
72 const char * format, ... )
73{
74 ((void) s);
75 ((void) n);
76 ((void) format)
77 return( 0 );
78}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
81#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +000084 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +000088 const char * format,
89 ... ) )
90{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +000092 return( 0 );
93}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
97#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +010098/*
99 * Make dummy function to prevent NULL pointer dereferences
100 */
101static int platform_printf_uninit( const char *format, ... )
102{
103 ((void) format);
104 return( 0 );
105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
108#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100115 return( 0 );
116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
120#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100121/*
122 * Make dummy function to prevent NULL pointer dereferences
123 */
124static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
125{
126 ((void) stream);
127 ((void) format);
128 return( 0 );
129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
132#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
135 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100140 return( 0 );
141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
145#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000146/*
147 * Make dummy function to prevent NULL pointer dereferences
148 */
149static void platform_exit_uninit( int status )
150{
151 ((void) status);
152 return( 0 );
153}
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
156#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000163 return( 0 );
164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /* MBEDTLS_PLATFORM_C */