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"
33 #include "mbedtls/platform_util.h"
35 #if defined(MBEDTLS_PLATFORM_MEMORY)
36 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
37 static void *platform_calloc_uninit( size_t n
, size_t size
)
44 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
45 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
47 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
48 static void platform_free_uninit( void *ptr
)
53 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
54 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
56 static void * (*mbedtls_calloc_func
)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC
;
57 static void (*mbedtls_free_func
)( void * ) = MBEDTLS_PLATFORM_STD_FREE
;
59 void * mbedtls_calloc( size_t nmemb
, size_t size
)
61 return (*mbedtls_calloc_func
)( nmemb
, size
);
64 void mbedtls_free( void * ptr
)
66 (*mbedtls_free_func
)( ptr
);
69 int mbedtls_platform_set_calloc_free( void * (*calloc_func
)( size_t, size_t ),
70 void (*free_func
)( void * ) )
72 mbedtls_calloc_func
= calloc_func
;
73 mbedtls_free_func
= free_func
;
76 #endif /* MBEDTLS_PLATFORM_MEMORY */
80 int mbedtls_platform_win32_snprintf( char *s
, size_t n
, const char *fmt
, ... )
85 /* Avoid calling the invalid parameter handler by checking ourselves */
86 if( s
== NULL
|| n
== 0 || fmt
== NULL
)
89 va_start( argp
, fmt
);
90 #if defined(_TRUNCATE) && !defined(__MINGW32__)
91 ret
= _vsnprintf_s( s
, n
, _TRUNCATE
, fmt
, argp
);
93 ret
= _vsnprintf( s
, n
, fmt
, argp
);
94 if( ret
< 0 || (size_t) ret
== n
)
106 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
107 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
109 * Make dummy function to prevent NULL pointer dereferences
111 static int platform_snprintf_uninit( char * s
, size_t n
,
112 const char * format
, ... )
120 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
121 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
123 int (*mbedtls_snprintf
)( char * s
, size_t n
,
125 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF
;
127 int mbedtls_platform_set_snprintf( int (*snprintf_func
)( char * s
, size_t n
,
131 mbedtls_snprintf
= snprintf_func
;
134 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
136 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
137 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
139 * Make dummy function to prevent NULL pointer dereferences
141 static int platform_printf_uninit( const char *format
, ... )
147 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
148 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
150 int (*mbedtls_printf
)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF
;
152 int mbedtls_platform_set_printf( int (*printf_func
)( const char *, ... ) )
154 mbedtls_printf
= printf_func
;
157 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
159 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
160 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
162 * Make dummy function to prevent NULL pointer dereferences
164 static int platform_fprintf_uninit( FILE *stream
, const char *format
, ... )
171 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
172 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
174 int (*mbedtls_fprintf
)( FILE *, const char *, ... ) =
175 MBEDTLS_PLATFORM_STD_FPRINTF
;
177 int mbedtls_platform_set_fprintf( int (*fprintf_func
)( FILE *, const char *, ... ) )
179 mbedtls_fprintf
= fprintf_func
;
182 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
184 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
185 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
187 * Make dummy function to prevent NULL pointer dereferences
189 static void platform_exit_uninit( int status
)
194 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
195 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
197 void (*mbedtls_exit
)( int status
) = MBEDTLS_PLATFORM_STD_EXIT
;
199 int mbedtls_platform_set_exit( void (*exit_func
)( int status
) )
201 mbedtls_exit
= exit_func
;
204 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
206 #if defined(MBEDTLS_HAVE_TIME)
208 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
209 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
211 * Make dummy function to prevent NULL pointer dereferences
213 static mbedtls_time_t
platform_time_uninit( mbedtls_time_t
* timer
)
219 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
220 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
222 mbedtls_time_t (*mbedtls_time
)( mbedtls_time_t
* timer
) = MBEDTLS_PLATFORM_STD_TIME
;
224 int mbedtls_platform_set_time( mbedtls_time_t (*time_func
)( mbedtls_time_t
* timer
) )
226 mbedtls_time
= time_func
;
229 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
231 #endif /* MBEDTLS_HAVE_TIME */
233 #if defined(MBEDTLS_ENTROPY_NV_SEED)
234 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
235 /* Default implementations for the platform independent seed functions use
236 * standard libc file functions to read from and write to a pre-defined filename
238 int mbedtls_platform_std_nv_seed_read( unsigned char *buf
, size_t buf_len
)
243 if( ( file
= fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "rb" ) ) == NULL
)
246 if( ( n
= fread( buf
, 1, buf_len
, file
) ) != buf_len
)
249 mbedtls_platform_zeroize( buf
, buf_len
);
257 int mbedtls_platform_std_nv_seed_write( unsigned char *buf
, size_t buf_len
)
262 if( ( file
= fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "w" ) ) == NULL
)
265 if( ( n
= fwrite( buf
, 1, buf_len
, file
) ) != buf_len
)
274 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
276 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
277 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
279 * Make dummy function to prevent NULL pointer dereferences
281 static int platform_nv_seed_read_uninit( unsigned char *buf
, size_t buf_len
)
288 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
289 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
291 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
293 * Make dummy function to prevent NULL pointer dereferences
295 static int platform_nv_seed_write_uninit( unsigned char *buf
, size_t buf_len
)
302 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
303 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
305 int (*mbedtls_nv_seed_read
)( unsigned char *buf
, size_t buf_len
) =
306 MBEDTLS_PLATFORM_STD_NV_SEED_READ
;
307 int (*mbedtls_nv_seed_write
)( unsigned char *buf
, size_t buf_len
) =
308 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
;
310 int mbedtls_platform_set_nv_seed(
311 int (*nv_seed_read_func
)( unsigned char *buf
, size_t buf_len
),
312 int (*nv_seed_write_func
)( unsigned char *buf
, size_t buf_len
) )
314 mbedtls_nv_seed_read
= nv_seed_read_func
;
315 mbedtls_nv_seed_write
= nv_seed_write_func
;
318 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
319 #endif /* MBEDTLS_ENTROPY_NV_SEED */
321 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
323 * Placeholder platform setup that does nothing by default
325 int mbedtls_platform_setup( mbedtls_platform_context
*ctx
)
333 * Placeholder platform teardown that does nothing by default
335 void mbedtls_platform_teardown( mbedtls_platform_context
*ctx
)
339 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
341 #endif /* MBEDTLS_PLATFORM_C */