blob: 2cdfe70df10d4f32f0d61a6afec6d68dfc519fd5 [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
Hanno Beckerbbd51312018-10-11 10:26:55 +010032/* The compile time configuration of memory allocation via the macros
33 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
34 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
35 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
36#if defined(MBEDTLS_PLATFORM_MEMORY) && \
37 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
38 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
39
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020040#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
41static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010042{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020043 ((void) n);
44 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010045 return( NULL );
46}
47
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020048#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
49#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010052static void platform_free_uninit( void *ptr )
53{
54 ((void) ptr);
55}
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
58#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010059
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020060void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010062
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020063int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010064 void (*free_func)( void * ) )
65{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020066 mbedtls_calloc = calloc_func;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_free = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010068 return( 0 );
69}
Hanno Beckerbbd51312018-10-11 10:26:55 +010070#endif /* MBEDTLS_PLATFORM_MEMORY &&
71 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
72 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010073
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020074#if defined(_WIN32)
75#include <stdarg.h>
76int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
77{
78 int ret;
79 va_list argp;
80
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +020081 /* Avoid calling the invalid parameter handler by checking ourselves */
82 if( s == NULL || n == 0 || fmt == NULL )
83 return( -1 );
84
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020085 va_start( argp, fmt );
Ron Eldora1413e02017-09-06 17:49:10 +030086#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020087 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020088#else
89 ret = _vsnprintf( s, n, fmt, argp );
90 if( ret < 0 || (size_t) ret == n )
91 {
92 s[n-1] = '\0';
93 ret = -1;
94 }
95#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020096 va_end( argp );
97
98 return( ret );
99}
100#endif
101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
103#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000104/*
105 * Make dummy function to prevent NULL pointer dereferences
106 */
107static int platform_snprintf_uninit( char * s, size_t n,
108 const char * format, ... )
109{
110 ((void) s);
111 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100112 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000113 return( 0 );
114}
115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
117#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000120 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000124 const char * format,
125 ... ) )
126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000128 return( 0 );
129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
133#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100134/*
135 * Make dummy function to prevent NULL pointer dereferences
136 */
137static int platform_printf_uninit( const char *format, ... )
138{
139 ((void) format);
140 return( 0 );
141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
144#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100149{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100151 return( 0 );
152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
156#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100157/*
158 * Make dummy function to prevent NULL pointer dereferences
159 */
160static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
161{
162 ((void) stream);
163 ((void) format);
164 return( 0 );
165}
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
168#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
171 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100174{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100176 return( 0 );
177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
181#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000182/*
183 * Make dummy function to prevent NULL pointer dereferences
184 */
185static void platform_exit_uninit( int status )
186{
187 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000188}
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
191#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000192
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100193void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000198 return( 0 );
199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_PLATFORM_C */