blob: 0606214b3bbf22a268851aa6bd7dda73e638fe5b [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é-Gonnard6c0c8e02015-06-22 10:23:34 +020066#if defined(_WIN32)
67#include <stdarg.h>
68int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
69{
70 int ret;
71 va_list argp;
72
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +020073 /* Avoid calling the invalid parameter handler by checking ourselves */
74 if( s == NULL || n == 0 || fmt == NULL )
75 return( -1 );
76
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020077 va_start( argp, fmt );
78 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
79 va_end( argp );
80
81 return( ret );
82}
83#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
86#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000087/*
88 * Make dummy function to prevent NULL pointer dereferences
89 */
90static int platform_snprintf_uninit( char * s, size_t n,
91 const char * format, ... )
92{
93 ((void) s);
94 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +010095 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +000096 return( 0 );
97}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
100#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000103 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000107 const char * format,
108 ... ) )
109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000111 return( 0 );
112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
116#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100117/*
118 * Make dummy function to prevent NULL pointer dereferences
119 */
120static int platform_printf_uninit( const char *format, ... )
121{
122 ((void) format);
123 return( 0 );
124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
127#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100134 return( 0 );
135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
139#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100140/*
141 * Make dummy function to prevent NULL pointer dereferences
142 */
143static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
144{
145 ((void) stream);
146 ((void) format);
147 return( 0 );
148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
151#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
154 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100159 return( 0 );
160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
164#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000165/*
166 * Make dummy function to prevent NULL pointer dereferences
167 */
168static void platform_exit_uninit( int status )
169{
170 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000171}
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
174#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000175
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100176void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000181 return( 0 );
182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* MBEDTLS_PLATFORM_C */