blob: 3161a4c0079f1b65efb6ae889fd4678b95e16f34 [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 );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020078#if defined(_TRUNCATE)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020079 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020080#else
81 ret = _vsnprintf( s, n, fmt, argp );
82 if( ret < 0 || (size_t) ret == n )
83 {
84 s[n-1] = '\0';
85 ret = -1;
86 }
87#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020088 va_end( argp );
89
90 return( ret );
91}
92#endif
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
95#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000096/*
97 * Make dummy function to prevent NULL pointer dereferences
98 */
99static int platform_snprintf_uninit( char * s, size_t n,
100 const char * format, ... )
101{
102 ((void) s);
103 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100104 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000105 return( 0 );
106}
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
109#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000112 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000116 const char * format,
117 ... ) )
118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000120 return( 0 );
121}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
125#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100126/*
127 * Make dummy function to prevent NULL pointer dereferences
128 */
129static int platform_printf_uninit( const char *format, ... )
130{
131 ((void) format);
132 return( 0 );
133}
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
136#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100143 return( 0 );
144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
148#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100149/*
150 * Make dummy function to prevent NULL pointer dereferences
151 */
152static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
153{
154 ((void) stream);
155 ((void) format);
156 return( 0 );
157}
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
160#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
163 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100166{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100168 return( 0 );
169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
173#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000174/*
175 * Make dummy function to prevent NULL pointer dereferences
176 */
177static void platform_exit_uninit( int status )
178{
179 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000180}
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
183#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000184
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100185void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000190 return( 0 );
191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* MBEDTLS_PLATFORM_C */