2 * Platform abstraction layer
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
27 #include MBEDTLS_CONFIG_FILE
30 #if defined(MBEDTLS_PLATFORM_C)
32 #include "mbedtls/platform.h"
34 #if defined(MBEDTLS_PLATFORM_MEMORY)
35 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
36 static void *platform_calloc_uninit( size_t n
, size_t size
)
43 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
44 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
46 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
47 static void platform_free_uninit( void *ptr
)
52 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
53 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
55 void * (*mbedtls_calloc
)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC
;
56 void (*mbedtls_free
)( void * ) = MBEDTLS_PLATFORM_STD_FREE
;
58 int mbedtls_platform_set_calloc_free( void * (*calloc_func
)( size_t, size_t ),
59 void (*free_func
)( void * ) )
61 mbedtls_calloc
= calloc_func
;
62 mbedtls_free
= free_func
;
65 #endif /* MBEDTLS_PLATFORM_MEMORY */
69 int mbedtls_platform_win32_snprintf( char *s
, size_t n
, const char *fmt
, ... )
74 /* Avoid calling the invalid parameter handler by checking ourselves */
75 if( s
== NULL
|| n
== 0 || fmt
== NULL
)
78 va_start( argp
, fmt
);
79 #if defined(_TRUNCATE)
80 ret
= _vsnprintf_s( s
, n
, _TRUNCATE
, fmt
, argp
);
82 ret
= _vsnprintf( s
, n
, fmt
, argp
);
83 if( ret
< 0 || (size_t) ret
== n
)
95 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
96 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
98 * Make dummy function to prevent NULL pointer dereferences
100 static int platform_snprintf_uninit( char * s
, size_t n
,
101 const char * format
, ... )
109 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
110 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
112 int (*mbedtls_snprintf
)( char * s
, size_t n
,
114 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF
;
116 int mbedtls_platform_set_snprintf( int (*snprintf_func
)( char * s
, size_t n
,
120 mbedtls_snprintf
= snprintf_func
;
123 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
125 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
126 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
128 * Make dummy function to prevent NULL pointer dereferences
130 static int platform_printf_uninit( const char *format
, ... )
136 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
137 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
139 int (*mbedtls_printf
)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF
;
141 int mbedtls_platform_set_printf( int (*printf_func
)( const char *, ... ) )
143 mbedtls_printf
= printf_func
;
146 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
148 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
149 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
151 * Make dummy function to prevent NULL pointer dereferences
153 static int platform_fprintf_uninit( FILE *stream
, const char *format
, ... )
160 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
161 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
163 int (*mbedtls_fprintf
)( FILE *, const char *, ... ) =
164 MBEDTLS_PLATFORM_STD_FPRINTF
;
166 int mbedtls_platform_set_fprintf( int (*fprintf_func
)( FILE *, const char *, ... ) )
168 mbedtls_fprintf
= fprintf_func
;
171 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
173 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
174 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
176 * Make dummy function to prevent NULL pointer dereferences
178 static void platform_exit_uninit( int status
)
183 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
184 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
186 void (*mbedtls_exit
)( int status
) = MBEDTLS_PLATFORM_STD_EXIT
;
188 int mbedtls_platform_set_exit( void (*exit_func
)( int status
) )
190 mbedtls_exit
= exit_func
;
193 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
195 #if defined(MBEDTLS_HAVE_TIME)
197 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
198 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
200 * Make dummy function to prevent NULL pointer dereferences
202 static mbedtls_time_t
platform_time_uninit( mbedtls_time_t
* timer
)
208 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
209 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
211 mbedtls_time_t (*mbedtls_time
)( mbedtls_time_t
* timer
) = MBEDTLS_PLATFORM_STD_TIME
;
213 int mbedtls_platform_set_time( mbedtls_time_t (*time_func
)( mbedtls_time_t
* timer
) )
215 mbedtls_time
= time_func
;
218 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
220 #endif /* MBEDTLS_HAVE_TIME */
222 #if defined(MBEDTLS_ENTROPY_NV_SEED)
223 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
224 /* Default implementations for the platform independent seed functions use
225 * standard libc file functions to read from and write to a pre-defined filename
227 int mbedtls_platform_std_nv_seed_read( unsigned char *buf
, size_t buf_len
)
232 if( ( file
= fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "rb" ) ) == NULL
)
235 if( ( n
= fread( buf
, 1, buf_len
, file
) ) != buf_len
)
245 int mbedtls_platform_std_nv_seed_write( unsigned char *buf
, size_t buf_len
)
250 if( ( file
= fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "w" ) ) == NULL
)
253 if( ( n
= fwrite( buf
, 1, buf_len
, file
) ) != buf_len
)
262 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
264 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
265 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
267 * Make dummy function to prevent NULL pointer dereferences
269 static int platform_nv_seed_read_uninit( unsigned char *buf
, size_t buf_len
)
276 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
277 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
279 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
281 * Make dummy function to prevent NULL pointer dereferences
283 static int platform_nv_seed_write_uninit( unsigned char *buf
, size_t buf_len
)
290 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
291 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
293 int (*mbedtls_nv_seed_read
)( unsigned char *buf
, size_t buf_len
) =
294 MBEDTLS_PLATFORM_STD_NV_SEED_READ
;
295 int (*mbedtls_nv_seed_write
)( unsigned char *buf
, size_t buf_len
) =
296 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
;
298 int mbedtls_platform_set_nv_seed(
299 int (*nv_seed_read_func
)( unsigned char *buf
, size_t buf_len
),
300 int (*nv_seed_write_func
)( unsigned char *buf
, size_t buf_len
) )
302 mbedtls_nv_seed_read
= nv_seed_read_func
;
303 mbedtls_nv_seed_write
= nv_seed_write_func
;
306 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
307 #endif /* MBEDTLS_ENTROPY_NV_SEED */
309 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
311 * Placeholder platform setup that does nothing by default
313 int mbedtls_platform_setup( mbedtls_platform_context
*ctx
)
321 * Placeholder platform teardown that does nothing by default
323 void mbedtls_platform_teardown( mbedtls_platform_context
*ctx
)
327 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
329 #endif /* MBEDTLS_PLATFORM_C */