]> git.zerfleddert.de Git - proxmark3-svn/blame - common/mbedtls/platform.c
Bugfixes for LF FDX
[proxmark3-svn] / common / mbedtls / platform.c
CommitLineData
700d8687
OM
1/*
2 * Platform abstraction layer
3 *
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
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.
11 *
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.
16 *
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.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24#if !defined(MBEDTLS_CONFIG_FILE)
25#include "mbedtls/config.h"
26#else
27#include MBEDTLS_CONFIG_FILE
28#endif
29
30#if defined(MBEDTLS_PLATFORM_C)
31
32#include "mbedtls/platform.h"
33#include "mbedtls/platform_util.h"
34
35#if defined(MBEDTLS_PLATFORM_MEMORY)
36#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
37static void *platform_calloc_uninit( size_t n, size_t size )
38{
39 ((void) n);
40 ((void) size);
41 return( NULL );
42}
43
44#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
45#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
46
47#if !defined(MBEDTLS_PLATFORM_STD_FREE)
48static void platform_free_uninit( void *ptr )
49{
50 ((void) ptr);
51}
52
53#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
54#endif /* !MBEDTLS_PLATFORM_STD_FREE */
55
56static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
57static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
58
59void * mbedtls_calloc( size_t nmemb, size_t size )
60{
61 return (*mbedtls_calloc_func)( nmemb, size );
62}
63
64void mbedtls_free( void * ptr )
65{
66 (*mbedtls_free_func)( ptr );
67}
68
69int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
70 void (*free_func)( void * ) )
71{
72 mbedtls_calloc_func = calloc_func;
73 mbedtls_free_func = free_func;
74 return( 0 );
75}
76#endif /* MBEDTLS_PLATFORM_MEMORY */
77
78#if defined(_WIN32)
79#include <stdarg.h>
80int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
81{
82 int ret;
83 va_list argp;
84
85 /* Avoid calling the invalid parameter handler by checking ourselves */
86 if( s == NULL || n == 0 || fmt == NULL )
87 return( -1 );
88
89 va_start( argp, fmt );
90#if defined(_TRUNCATE) && !defined(__MINGW32__)
91 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
92#else
93 ret = _vsnprintf( s, n, fmt, argp );
94 if( ret < 0 || (size_t) ret == n )
95 {
96 s[n-1] = '\0';
97 ret = -1;
98 }
99#endif
100 va_end( argp );
101
102 return( ret );
103}
104#endif
105
106#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
107#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
108/*
109 * Make dummy function to prevent NULL pointer dereferences
110 */
111static int platform_snprintf_uninit( char * s, size_t n,
112 const char * format, ... )
113{
114 ((void) s);
115 ((void) n);
116 ((void) format);
117 return( 0 );
118}
119
120#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
121#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
122
123int (*mbedtls_snprintf)( char * s, size_t n,
124 const char * format,
125 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
126
127int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
128 const char * format,
129 ... ) )
130{
131 mbedtls_snprintf = snprintf_func;
132 return( 0 );
133}
134#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
135
136#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
137#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
138/*
139 * Make dummy function to prevent NULL pointer dereferences
140 */
141static int platform_printf_uninit( const char *format, ... )
142{
143 ((void) format);
144 return( 0 );
145}
146
147#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
148#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
149
150int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
151
152int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
153{
154 mbedtls_printf = printf_func;
155 return( 0 );
156}
157#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
158
159#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
160#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
161/*
162 * Make dummy function to prevent NULL pointer dereferences
163 */
164static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
165{
166 ((void) stream);
167 ((void) format);
168 return( 0 );
169}
170
171#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
172#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
173
174int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
175 MBEDTLS_PLATFORM_STD_FPRINTF;
176
177int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
178{
179 mbedtls_fprintf = fprintf_func;
180 return( 0 );
181}
182#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
183
184#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
185#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
186/*
187 * Make dummy function to prevent NULL pointer dereferences
188 */
189static void platform_exit_uninit( int status )
190{
191 ((void) status);
192}
193
194#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
195#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
196
197void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
198
199int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
200{
201 mbedtls_exit = exit_func;
202 return( 0 );
203}
204#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
205
206#if defined(MBEDTLS_HAVE_TIME)
207
208#if defined(MBEDTLS_PLATFORM_TIME_ALT)
209#if !defined(MBEDTLS_PLATFORM_STD_TIME)
210/*
211 * Make dummy function to prevent NULL pointer dereferences
212 */
213static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
214{
215 ((void) timer);
216 return( 0 );
217}
218
219#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
220#endif /* !MBEDTLS_PLATFORM_STD_TIME */
221
222mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
223
224int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
225{
226 mbedtls_time = time_func;
227 return( 0 );
228}
229#endif /* MBEDTLS_PLATFORM_TIME_ALT */
230
231#endif /* MBEDTLS_HAVE_TIME */
232
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
237 */
238int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
239{
240 FILE *file;
241 size_t n;
242
243 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
244 return( -1 );
245
246 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
247 {
248 fclose( file );
249 mbedtls_platform_zeroize( buf, buf_len );
250 return( -1 );
251 }
252
253 fclose( file );
254 return( (int)n );
255}
256
257int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
258{
259 FILE *file;
260 size_t n;
261
262 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
263 return -1;
264
265 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
266 {
267 fclose( file );
268 return -1;
269 }
270
271 fclose( file );
272 return( (int)n );
273}
274#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
275
276#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
277#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
278/*
279 * Make dummy function to prevent NULL pointer dereferences
280 */
281static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
282{
283 ((void) buf);
284 ((void) buf_len);
285 return( -1 );
286}
287
288#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
289#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
290
291#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
292/*
293 * Make dummy function to prevent NULL pointer dereferences
294 */
295static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
296{
297 ((void) buf);
298 ((void) buf_len);
299 return( -1 );
300}
301
302#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
303#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
304
305int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
306 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
307int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
308 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
309
310int 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 ) )
313{
314 mbedtls_nv_seed_read = nv_seed_read_func;
315 mbedtls_nv_seed_write = nv_seed_write_func;
316 return( 0 );
317}
318#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
319#endif /* MBEDTLS_ENTROPY_NV_SEED */
320
321#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
322/*
323 * Placeholder platform setup that does nothing by default
324 */
325int mbedtls_platform_setup( mbedtls_platform_context *ctx )
326{
327 (void)ctx;
328
329 return( 0 );
330}
331
332/*
333 * Placeholder platform teardown that does nothing by default
334 */
335void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
336{
337 (void)ctx;
338}
339#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
340
341#endif /* MBEDTLS_PLATFORM_C */
Impressum, Datenschutz