blob: 788cd1f704398a1c9f2ab6ce4c39ad8204161882 [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)
34#if !defined(MBEDTLS_PLATFORM_STD_MALLOC)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010035static void *platform_malloc_uninit( size_t len )
36{
37 ((void) len);
38 return( NULL );
39}
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_PLATFORM_STD_MALLOC platform_malloc_uninit
42#endif /* !MBEDTLS_PLATFORM_STD_MALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010045static void platform_free_uninit( void *ptr )
46{
47 ((void) ptr);
48}
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
51#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053void * (*mbedtls_malloc)( size_t ) = MBEDTLS_PLATFORM_STD_MALLOC;
54void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056int mbedtls_platform_set_malloc_free( void * (*malloc_func)( size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010057 void (*free_func)( void * ) )
58{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_malloc = malloc_func;
60 mbedtls_free = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010061 return( 0 );
62}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#endif /* MBEDTLS_PLATFORM_MEMORY */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
66#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000067/*
68 * Make dummy function to prevent NULL pointer dereferences
69 */
70static int platform_snprintf_uninit( char * s, size_t n,
71 const char * format, ... )
72{
73 ((void) s);
74 ((void) n);
75 ((void) format)
76 return( 0 );
77}
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
80#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +000083 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +000087 const char * format,
88 ... ) )
89{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +000091 return( 0 );
92}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +000094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
96#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +010097/*
98 * Make dummy function to prevent NULL pointer dereferences
99 */
100static int platform_printf_uninit( const char *format, ... )
101{
102 ((void) format);
103 return( 0 );
104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
107#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100114 return( 0 );
115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
119#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100120/*
121 * Make dummy function to prevent NULL pointer dereferences
122 */
123static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
124{
125 ((void) stream);
126 ((void) format);
127 return( 0 );
128}
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
131#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
134 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100139 return( 0 );
140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
144#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000145/*
146 * Make dummy function to prevent NULL pointer dereferences
147 */
148static void platform_exit_uninit( int status )
149{
150 ((void) status);
151 return( 0 );
152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
155#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000162 return( 0 );
163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_PLATFORM_C */