blob: d634c6277e1ae0588ce63e2dd06dd748e3576c67 [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +010020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_PLATFORM_MEMORY)
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020033#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
34static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010035{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020036 ((void) n);
37 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010038 return( NULL );
39}
40
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020041#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
42#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
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é-Gonnardb9ef1182015-05-26 16:15:20 +020053void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010055
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020056int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010057 void (*free_func)( void * ) )
58{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020059 mbedtls_calloc = calloc_func;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 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é-Gonnard6c0c8e02015-06-22 10:23:34 +020065#if defined(_WIN32)
66#include <stdarg.h>
67int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
68{
69 int ret;
70 va_list argp;
71
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +020072 /* Avoid calling the invalid parameter handler by checking ourselves */
73 if( s == NULL || n == 0 || fmt == NULL )
74 return( -1 );
75
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020076 va_start( argp, fmt );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020077#if defined(_TRUNCATE)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020078 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020079#else
80 ret = _vsnprintf( s, n, fmt, argp );
81 if( ret < 0 || (size_t) ret == n )
82 {
83 s[n-1] = '\0';
84 ret = -1;
85 }
86#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020087 va_end( argp );
88
89 return( ret );
90}
91#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
94#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000095/*
96 * Make dummy function to prevent NULL pointer dereferences
97 */
98static int platform_snprintf_uninit( char * s, size_t n,
99 const char * format, ... )
100{
101 ((void) s);
102 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100103 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000104 return( 0 );
105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
108#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000111 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000115 const char * format,
116 ... ) )
117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000119 return( 0 );
120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
124#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100125/*
126 * Make dummy function to prevent NULL pointer dereferences
127 */
128static int platform_printf_uninit( const char *format, ... )
129{
130 ((void) format);
131 return( 0 );
132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
135#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100142 return( 0 );
143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
147#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100148/*
149 * Make dummy function to prevent NULL pointer dereferences
150 */
151static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
152{
153 ((void) stream);
154 ((void) format);
155 return( 0 );
156}
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
159#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
162 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100167 return( 0 );
168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
172#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000173/*
174 * Make dummy function to prevent NULL pointer dereferences
175 */
176static void platform_exit_uninit( int status )
177{
178 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
182#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000183
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100184void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000187{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000189 return( 0 );
190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_PLATFORM_C */