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"
32 #if defined(MBEDTLS_PLATFORM_MEMORY)
33 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
34 static void *platform_calloc_uninit( size_t n
, size_t size
)
41 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
42 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
44 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
45 static void platform_free_uninit( void *ptr
)
50 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
51 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
53 void * (*mbedtls_calloc
)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC
;
54 void (*mbedtls_free
)( void * ) = MBEDTLS_PLATFORM_STD_FREE
;
56 int mbedtls_platform_set_calloc_free( void * (*calloc_func
)( size_t, size_t ),
57 void (*free_func
)( void * ) )
59 mbedtls_calloc
= calloc_func
;
60 mbedtls_free
= free_func
;
63 #endif /* MBEDTLS_PLATFORM_MEMORY */
67 int mbedtls_platform_win32_snprintf( char *s
, size_t n
, const char *fmt
, ... )
72 /* Avoid calling the invalid parameter handler by checking ourselves */
73 if( s
== NULL
|| n
== 0 || fmt
== NULL
)
76 va_start( argp
, fmt
);
77 #if defined(_TRUNCATE)
78 ret
= _vsnprintf_s( s
, n
, _TRUNCATE
, fmt
, argp
);
80 ret
= _vsnprintf( s
, n
, fmt
, argp
);
81 if( ret
< 0 || (size_t) ret
== n
)
93 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
94 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
96 * Make dummy function to prevent NULL pointer dereferences
98 static int platform_snprintf_uninit( char * s
, size_t n
,
99 const char * format
, ... )
107 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
108 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
110 int (*mbedtls_snprintf
)( char * s
, size_t n
,
112 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF
;
114 int mbedtls_platform_set_snprintf( int (*snprintf_func
)( char * s
, size_t n
,
118 mbedtls_snprintf
= snprintf_func
;
121 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
123 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
124 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
126 * Make dummy function to prevent NULL pointer dereferences
128 static int platform_printf_uninit( const char *format
, ... )
134 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
135 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
137 int (*mbedtls_printf
)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF
;
139 int mbedtls_platform_set_printf( int (*printf_func
)( const char *, ... ) )
141 mbedtls_printf
= printf_func
;
144 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
146 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
147 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
149 * Make dummy function to prevent NULL pointer dereferences
151 static int platform_fprintf_uninit( FILE *stream
, const char *format
, ... )
158 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
159 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
161 int (*mbedtls_fprintf
)( FILE *, const char *, ... ) =
162 MBEDTLS_PLATFORM_STD_FPRINTF
;
164 int mbedtls_platform_set_fprintf( int (*fprintf_func
)( FILE *, const char *, ... ) )
166 mbedtls_fprintf
= fprintf_func
;
169 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
171 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
172 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
174 * Make dummy function to prevent NULL pointer dereferences
176 static void platform_exit_uninit( int status
)
181 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
182 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
184 void (*mbedtls_exit
)( int status
) = MBEDTLS_PLATFORM_STD_EXIT
;
186 int mbedtls_platform_set_exit( void (*exit_func
)( int status
) )
188 mbedtls_exit
= exit_func
;
191 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
193 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
194 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
196 * Make dummy function to prevent NULL pointer dereferences
198 static mbedtls_time_t
platform_time_uninit( mbedtls_time_t
* timer
)
204 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
205 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
207 mbedtls_time_t (*mbedtls_time
)( mbedtls_time_t
* timer
) = MBEDTLS_PLATFORM_STD_TIME
;
209 int mbedtls_platform_set_time( mbedtls_time_t (*time_func
)( mbedtls_time_t
* timer
) )
211 mbedtls_time
= time_func
;
214 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
216 #if defined(MBEDTLS_ENTROPY_NV_SEED)
217 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
218 /* Default implementations for the platform independent seed functions use
219 * standard libc file functions to read from and write to a pre-defined filename
221 int mbedtls_platform_std_nv_seed_read( unsigned char *buf
, size_t buf_len
)
226 if( ( file
= fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "rb" ) ) == NULL
)
229 if( ( n
= fread( buf
, 1, buf_len
, file
) ) != buf_len
)
239 int mbedtls_platform_std_nv_seed_write( unsigned char *buf
, size_t buf_len
)
244 if( ( file
= fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "w" ) ) == NULL
)
247 if( ( n
= fwrite( buf
, 1, buf_len
, file
) ) != buf_len
)
256 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
258 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
259 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
261 * Make dummy function to prevent NULL pointer dereferences
263 static int platform_nv_seed_read_uninit( unsigned char *buf
, size_t buf_len
)
270 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
271 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
273 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
275 * Make dummy function to prevent NULL pointer dereferences
277 static int platform_nv_seed_write_uninit( unsigned char *buf
, size_t buf_len
)
284 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
285 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
287 int (*mbedtls_nv_seed_read
)( unsigned char *buf
, size_t buf_len
) =
288 MBEDTLS_PLATFORM_STD_NV_SEED_READ
;
289 int (*mbedtls_nv_seed_write
)( unsigned char *buf
, size_t buf_len
) =
290 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
;
292 int mbedtls_platform_set_nv_seed(
293 int (*nv_seed_read_func
)( unsigned char *buf
, size_t buf_len
),
294 int (*nv_seed_write_func
)( unsigned char *buf
, size_t buf_len
) )
296 mbedtls_nv_seed_read
= nv_seed_read_func
;
297 mbedtls_nv_seed_write
= nv_seed_write_func
;
300 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
301 #endif /* MBEDTLS_ENTROPY_NV_SEED */
303 #endif /* MBEDTLS_PLATFORM_C */