1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
37
38
39
40
41
42
51
52
53
60
61
62
63
64
68
69
73
74
81
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
141
142
143
144
165
166
167
168
180
181
182
183
184
189
190
191
192
212
213
214
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
259
260
261
262
263
264
265
266
267
268
269
270
271
272
277
278
279
280
281
282
283
284
285
297
298
299
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
338
339
342
346
347
348
/* ... */
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#include "mbedtls/platform_util.h"
/* ... */
#if defined(MBEDTLS_PLATFORM_MEMORY) && \
!( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
defined(MBEDTLS_PLATFORM_FREE_MACRO) )
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
static void *platform_calloc_uninit( size_t n, size_t size )
{
((void) n);
((void) size);
return( NULL );
}platform_calloc_uninit (size_t n, size_t size) { ... }
#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
/* ... */#endif
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
static void platform_free_uninit( void *ptr )
{
((void) ptr);
}platform_free_uninit (void *ptr) { ... }
#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
/* ... */#endif
static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
void * mbedtls_calloc( size_t nmemb, size_t size )
{
return (*mbedtls_calloc_func)( nmemb, size );
}mbedtls_calloc (size_t nmemb, size_t size) { ... }
void mbedtls_free( void * ptr )
{
(*mbedtls_free_func)( ptr );
}mbedtls_free (void * ptr) { ... }
int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
void (*free_func)( void * ) )
{
mbedtls_calloc_func = calloc_func;
mbedtls_free_func = free_func;
return( 0 );
}mbedtls_platform_set_calloc_free (void * (*calloc_func)( size_t, size_t ), void (*free_func)( void * )) { ... }
/* ... */#endif
/* ... */
#if defined(_WIN32)
#include <stdarg.h>
int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
{
int ret;
va_list argp;
if( s == NULL || n == 0 || fmt == NULL )
return( -1 );
va_start( argp, fmt );
#if defined(_TRUNCATE) && !defined(__MINGW32__)
ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
#else
ret = _vsnprintf( s, n, fmt, argp );
if( ret < 0 || (size_t) ret == n )
{
s[n-1] = '\0';
ret = -1;
}if (ret < 0 || (size_t) ret == n) { ... }
/* ... */#endif
va_end( argp );
return( ret );
}mbedtls_platform_win32_snprintf (char *s, size_t n, const char *fmt, ...) { ... }
/* ... */#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
/* ... */
static int platform_snprintf_uninit( char * s, size_t n,
const char * format, ... )
{
((void) s);
((void) n);
((void) format);
return( 0 );
}platform_snprintf_uninit (char * s, size_t n, const char * format, ...) { ... }
#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
/* ... */#endif
int (*mbedtls_snprintf)( char * s, size_t n,
const char * format,
... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
const char * format,
... ) )
{
mbedtls_snprintf = snprintf_func;
return( 0 );
}mbedtls_platform_set_snprintf (int (*snprintf_func)( char * s, size_t n, const char * format, ... )) { ... }
/* ... */#endif
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
/* ... */
static int platform_printf_uninit( const char *format, ... )
{
((void) format);
return( 0 );
}platform_printf_uninit (const char *format, ...) { ... }
#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
/* ... */#endif
int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
{
mbedtls_printf = printf_func;
return( 0 );
}mbedtls_platform_set_printf (int (*printf_func)( const char *, ... )) { ... }
/* ... */#endif
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
/* ... */
static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
{
((void) stream);
((void) format);
return( 0 );
}platform_fprintf_uninit (FILE *stream, const char *format, ...) { ... }
#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
/* ... */#endif
int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
MBEDTLS_PLATFORM_STD_FPRINTF;
int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
{
mbedtls_fprintf = fprintf_func;
return( 0 );
}mbedtls_platform_set_fprintf (int (*fprintf_func)( FILE *, const char *, ... )) { ... }
/* ... */#endif
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
/* ... */
static void platform_exit_uninit( int status )
{
((void) status);
}platform_exit_uninit (int status) { ... }
#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
/* ... */#endif
void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
{
mbedtls_exit = exit_func;
return( 0 );
}mbedtls_platform_set_exit (void (*exit_func)( int status )) { ... }
/* ... */#endif
#if defined(MBEDTLS_HAVE_TIME)
#if defined(MBEDTLS_PLATFORM_TIME_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_TIME)
/* ... */
static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
{
((void) timer);
return( 0 );
}platform_time_uninit (mbedtls_time_t* timer) { ... }
#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
/* ... */#endif
mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
{
mbedtls_time = time_func;
return( 0 );
}mbedtls_platform_set_time (mbedtls_time_t (*time_func)( mbedtls_time_t* timer )) { ... }
/* ... */#endif
/* ... */
#endif
#if defined(MBEDTLS_ENTROPY_NV_SEED)
#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
/* ... */
int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
{
FILE *file;
size_t n;
if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
return( -1 );
if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
{
fclose( file );
mbedtls_platform_zeroize( buf, buf_len );
return( -1 );
}if (( n = fread( buf, 1, buf_len, file ) ) != buf_len) { ... }
fclose( file );
return( (int)n );
}mbedtls_platform_std_nv_seed_read (unsigned char *buf, size_t buf_len) { ... }
int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
{
FILE *file;
size_t n;
if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
return -1;
if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
{
fclose( file );
return -1;
}if (( n = fwrite( buf, 1, buf_len, file ) ) != buf_len) { ... }
fclose( file );
return( (int)n );
}mbedtls_platform_std_nv_seed_write (unsigned char *buf, size_t buf_len) { ... }
/* ... */#endif
#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
/* ... */
static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
{
((void) buf);
((void) buf_len);
return( -1 );
}platform_nv_seed_read_uninit (unsigned char *buf, size_t buf_len) { ... }
#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
/* ... */#endif
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
/* ... */
static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
{
((void) buf);
((void) buf_len);
return( -1 );
}platform_nv_seed_write_uninit (unsigned char *buf, size_t buf_len) { ... }
#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
/* ... */#endif
int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
MBEDTLS_PLATFORM_STD_NV_SEED_READ;
int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
int mbedtls_platform_set_nv_seed(
int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
{
mbedtls_nv_seed_read = nv_seed_read_func;
mbedtls_nv_seed_write = nv_seed_write_func;
return( 0 );
}mbedtls_platform_set_nv_seed (int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )) { ... }
/* ... */#endif /* ... */
#endif
#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
/* ... */
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
{
(void)ctx;
return( 0 );
}{ ... }
/* ... */
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
{
(void)ctx;
}{ ... }
#endif/* ... */
/* ... */
#endif