2 * Platform abstraction layer
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
30 #include "mbedtls/platform.h"
31 #include "mbedtls/platform_util.h"
33 /* The compile time configuration of memory allocation via the macros
34 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
35 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
36 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
37 #if defined(MBEDTLS_PLATFORM_MEMORY) && \
38 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
39 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
41 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
42 static void *platform_calloc_uninit( size_t n, size_t size )
49 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
50 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
52 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
53 static void platform_free_uninit( void *ptr )
58 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
59 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
61 static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
62 static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
64 void * mbedtls_calloc( size_t nmemb, size_t size )
66 return (*mbedtls_calloc_func)( nmemb, size );
69 void mbedtls_free( void * ptr )
71 (*mbedtls_free_func)( ptr );
74 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
75 void (*free_func)( void * ) )
77 mbedtls_calloc_func = calloc_func;
78 mbedtls_free_func = free_func;
81 #endif /* MBEDTLS_PLATFORM_MEMORY &&
82 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
83 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
85 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
87 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
92 va_start( argp, fmt );
93 ret = mbedtls_vsnprintf( s, n, fmt, argp );
100 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
101 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
103 * Make dummy function to prevent NULL pointer dereferences
105 static int platform_snprintf_uninit( char * s, size_t n,
106 const char * format, ... )
114 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
115 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
117 int (*mbedtls_snprintf)( char * s, size_t n,
119 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
121 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
125 mbedtls_snprintf = snprintf_func;
128 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
130 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
132 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
136 /* Avoid calling the invalid parameter handler by checking ourselves */
137 if( s == NULL || n == 0 || fmt == NULL )
140 #if defined(_TRUNCATE)
141 ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg );
143 ret = vsnprintf( s, n, fmt, arg );
144 if( ret < 0 || (size_t) ret == n )
155 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
156 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
158 * Make dummy function to prevent NULL pointer dereferences
160 static int platform_vsnprintf_uninit( char * s, size_t n,
161 const char * format, va_list arg )
170 #define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
171 #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
173 int (*mbedtls_vsnprintf)( char * s, size_t n,
175 va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
177 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
181 mbedtls_vsnprintf = vsnprintf_func;
184 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
186 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
187 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
189 * Make dummy function to prevent NULL pointer dereferences
191 static int platform_printf_uninit( const char *format, ... )
197 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
198 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
200 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
202 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
204 mbedtls_printf = printf_func;
207 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
209 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
210 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
212 * Make dummy function to prevent NULL pointer dereferences
214 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
221 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
222 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
224 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
225 MBEDTLS_PLATFORM_STD_FPRINTF;
227 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
229 mbedtls_fprintf = fprintf_func;
232 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
234 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
235 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
237 * Make dummy function to prevent NULL pointer dereferences
239 static void platform_exit_uninit( int status )
244 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
245 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
247 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
249 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
251 mbedtls_exit = exit_func;
254 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
256 #if defined(MBEDTLS_HAVE_TIME)
258 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
259 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
261 * Make dummy function to prevent NULL pointer dereferences
263 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
269 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
270 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
272 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
274 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
276 mbedtls_time = time_func;
279 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
281 #endif /* MBEDTLS_HAVE_TIME */
283 #if defined(MBEDTLS_ENTROPY_NV_SEED)
284 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
285 /* Default implementations for the platform independent seed functions use
286 * standard libc file functions to read from and write to a pre-defined filename
288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
293 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
296 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
299 mbedtls_platform_zeroize( buf, buf_len );
307 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
312 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
315 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
324 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
326 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
327 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
329 * Make dummy function to prevent NULL pointer dereferences
331 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
338 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
339 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
341 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
343 * Make dummy function to prevent NULL pointer dereferences
345 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
352 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
353 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
355 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
356 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
357 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
358 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
360 int mbedtls_platform_set_nv_seed(
361 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
362 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
364 mbedtls_nv_seed_read = nv_seed_read_func;
365 mbedtls_nv_seed_write = nv_seed_write_func;
368 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
369 #endif /* MBEDTLS_ENTROPY_NV_SEED */
371 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
373 * Placeholder platform setup that does nothing by default
375 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
383 * Placeholder platform teardown that does nothing by default
385 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
389 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
391 #endif /* MBEDTLS_PLATFORM_C */