xref: /reactos/dll/3rdparty/mbedtls/platform.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  *  Platform abstraction layer
3*c2c66affSColin Finck  *
4*c2c66affSColin Finck  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5*c2c66affSColin Finck  *  SPDX-License-Identifier: GPL-2.0
6*c2c66affSColin Finck  *
7*c2c66affSColin Finck  *  This program is free software; you can redistribute it and/or modify
8*c2c66affSColin Finck  *  it under the terms of the GNU General Public License as published by
9*c2c66affSColin Finck  *  the Free Software Foundation; either version 2 of the License, or
10*c2c66affSColin Finck  *  (at your option) any later version.
11*c2c66affSColin Finck  *
12*c2c66affSColin Finck  *  This program is distributed in the hope that it will be useful,
13*c2c66affSColin Finck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14*c2c66affSColin Finck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*c2c66affSColin Finck  *  GNU General Public License for more details.
16*c2c66affSColin Finck  *
17*c2c66affSColin Finck  *  You should have received a copy of the GNU General Public License along
18*c2c66affSColin Finck  *  with this program; if not, write to the Free Software Foundation, Inc.,
19*c2c66affSColin Finck  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20*c2c66affSColin Finck  *
21*c2c66affSColin Finck  *  This file is part of mbed TLS (https://tls.mbed.org)
22*c2c66affSColin Finck  */
23*c2c66affSColin Finck 
24*c2c66affSColin Finck #if !defined(MBEDTLS_CONFIG_FILE)
25*c2c66affSColin Finck #include "mbedtls/config.h"
26*c2c66affSColin Finck #else
27*c2c66affSColin Finck #include MBEDTLS_CONFIG_FILE
28*c2c66affSColin Finck #endif
29*c2c66affSColin Finck 
30*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_C)
31*c2c66affSColin Finck 
32*c2c66affSColin Finck #include "mbedtls/platform.h"
33*c2c66affSColin Finck 
34*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_MEMORY)
35*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
36*c2c66affSColin Finck static void *platform_calloc_uninit( size_t n, size_t size )
37*c2c66affSColin Finck {
38*c2c66affSColin Finck     ((void) n);
39*c2c66affSColin Finck     ((void) size);
40*c2c66affSColin Finck     return( NULL );
41*c2c66affSColin Finck }
42*c2c66affSColin Finck 
43*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
44*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
45*c2c66affSColin Finck 
46*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_FREE)
47*c2c66affSColin Finck static void platform_free_uninit( void *ptr )
48*c2c66affSColin Finck {
49*c2c66affSColin Finck     ((void) ptr);
50*c2c66affSColin Finck }
51*c2c66affSColin Finck 
52*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
53*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_FREE */
54*c2c66affSColin Finck 
55*c2c66affSColin Finck void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
56*c2c66affSColin Finck void (*mbedtls_free)( void * )     = MBEDTLS_PLATFORM_STD_FREE;
57*c2c66affSColin Finck 
58*c2c66affSColin Finck int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
59*c2c66affSColin Finck                               void (*free_func)( void * ) )
60*c2c66affSColin Finck {
61*c2c66affSColin Finck     mbedtls_calloc = calloc_func;
62*c2c66affSColin Finck     mbedtls_free = free_func;
63*c2c66affSColin Finck     return( 0 );
64*c2c66affSColin Finck }
65*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_MEMORY */
66*c2c66affSColin Finck 
67*c2c66affSColin Finck #if defined(_WIN32)
68*c2c66affSColin Finck #include <stdarg.h>
69*c2c66affSColin Finck int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
70*c2c66affSColin Finck {
71*c2c66affSColin Finck     int ret;
72*c2c66affSColin Finck     va_list argp;
73*c2c66affSColin Finck 
74*c2c66affSColin Finck     /* Avoid calling the invalid parameter handler by checking ourselves */
75*c2c66affSColin Finck     if( s == NULL || n == 0 || fmt == NULL )
76*c2c66affSColin Finck         return( -1 );
77*c2c66affSColin Finck 
78*c2c66affSColin Finck     va_start( argp, fmt );
79*c2c66affSColin Finck #if defined(_TRUNCATE)
80*c2c66affSColin Finck     ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
81*c2c66affSColin Finck #else
82*c2c66affSColin Finck     ret = _vsnprintf( s, n, fmt, argp );
83*c2c66affSColin Finck     if( ret < 0 || (size_t) ret == n )
84*c2c66affSColin Finck     {
85*c2c66affSColin Finck         s[n-1] = '\0';
86*c2c66affSColin Finck         ret = -1;
87*c2c66affSColin Finck     }
88*c2c66affSColin Finck #endif
89*c2c66affSColin Finck     va_end( argp );
90*c2c66affSColin Finck 
91*c2c66affSColin Finck     return( ret );
92*c2c66affSColin Finck }
93*c2c66affSColin Finck #endif
94*c2c66affSColin Finck 
95*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
96*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
97*c2c66affSColin Finck /*
98*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
99*c2c66affSColin Finck  */
100*c2c66affSColin Finck static int platform_snprintf_uninit( char * s, size_t n,
101*c2c66affSColin Finck                                      const char * format, ... )
102*c2c66affSColin Finck {
103*c2c66affSColin Finck     ((void) s);
104*c2c66affSColin Finck     ((void) n);
105*c2c66affSColin Finck     ((void) format);
106*c2c66affSColin Finck     return( 0 );
107*c2c66affSColin Finck }
108*c2c66affSColin Finck 
109*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
110*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
111*c2c66affSColin Finck 
112*c2c66affSColin Finck int (*mbedtls_snprintf)( char * s, size_t n,
113*c2c66affSColin Finck                           const char * format,
114*c2c66affSColin Finck                           ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
115*c2c66affSColin Finck 
116*c2c66affSColin Finck int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
117*c2c66affSColin Finck                                                  const char * format,
118*c2c66affSColin Finck                                                  ... ) )
119*c2c66affSColin Finck {
120*c2c66affSColin Finck     mbedtls_snprintf = snprintf_func;
121*c2c66affSColin Finck     return( 0 );
122*c2c66affSColin Finck }
123*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
124*c2c66affSColin Finck 
125*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
126*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
127*c2c66affSColin Finck /*
128*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
129*c2c66affSColin Finck  */
130*c2c66affSColin Finck static int platform_printf_uninit( const char *format, ... )
131*c2c66affSColin Finck {
132*c2c66affSColin Finck     ((void) format);
133*c2c66affSColin Finck     return( 0 );
134*c2c66affSColin Finck }
135*c2c66affSColin Finck 
136*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
137*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
138*c2c66affSColin Finck 
139*c2c66affSColin Finck int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
140*c2c66affSColin Finck 
141*c2c66affSColin Finck int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
142*c2c66affSColin Finck {
143*c2c66affSColin Finck     mbedtls_printf = printf_func;
144*c2c66affSColin Finck     return( 0 );
145*c2c66affSColin Finck }
146*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
147*c2c66affSColin Finck 
148*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
149*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
150*c2c66affSColin Finck /*
151*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
152*c2c66affSColin Finck  */
153*c2c66affSColin Finck static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
154*c2c66affSColin Finck {
155*c2c66affSColin Finck     ((void) stream);
156*c2c66affSColin Finck     ((void) format);
157*c2c66affSColin Finck     return( 0 );
158*c2c66affSColin Finck }
159*c2c66affSColin Finck 
160*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
161*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
162*c2c66affSColin Finck 
163*c2c66affSColin Finck int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
164*c2c66affSColin Finck                                         MBEDTLS_PLATFORM_STD_FPRINTF;
165*c2c66affSColin Finck 
166*c2c66affSColin Finck int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
167*c2c66affSColin Finck {
168*c2c66affSColin Finck     mbedtls_fprintf = fprintf_func;
169*c2c66affSColin Finck     return( 0 );
170*c2c66affSColin Finck }
171*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
172*c2c66affSColin Finck 
173*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
174*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
175*c2c66affSColin Finck /*
176*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
177*c2c66affSColin Finck  */
178*c2c66affSColin Finck static void platform_exit_uninit( int status )
179*c2c66affSColin Finck {
180*c2c66affSColin Finck     ((void) status);
181*c2c66affSColin Finck }
182*c2c66affSColin Finck 
183*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
184*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
185*c2c66affSColin Finck 
186*c2c66affSColin Finck void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
187*c2c66affSColin Finck 
188*c2c66affSColin Finck int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
189*c2c66affSColin Finck {
190*c2c66affSColin Finck     mbedtls_exit = exit_func;
191*c2c66affSColin Finck     return( 0 );
192*c2c66affSColin Finck }
193*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
194*c2c66affSColin Finck 
195*c2c66affSColin Finck #if defined(MBEDTLS_HAVE_TIME)
196*c2c66affSColin Finck 
197*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_TIME_ALT)
198*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_TIME)
199*c2c66affSColin Finck /*
200*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
201*c2c66affSColin Finck  */
202*c2c66affSColin Finck static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
203*c2c66affSColin Finck {
204*c2c66affSColin Finck     ((void) timer);
205*c2c66affSColin Finck     return( 0 );
206*c2c66affSColin Finck }
207*c2c66affSColin Finck 
208*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
209*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_TIME */
210*c2c66affSColin Finck 
211*c2c66affSColin Finck mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
212*c2c66affSColin Finck 
213*c2c66affSColin Finck int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
214*c2c66affSColin Finck {
215*c2c66affSColin Finck     mbedtls_time = time_func;
216*c2c66affSColin Finck     return( 0 );
217*c2c66affSColin Finck }
218*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_TIME_ALT */
219*c2c66affSColin Finck 
220*c2c66affSColin Finck #endif /* MBEDTLS_HAVE_TIME */
221*c2c66affSColin Finck 
222*c2c66affSColin Finck #if defined(MBEDTLS_ENTROPY_NV_SEED)
223*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
224*c2c66affSColin Finck /* Default implementations for the platform independent seed functions use
225*c2c66affSColin Finck  * standard libc file functions to read from and write to a pre-defined filename
226*c2c66affSColin Finck  */
227*c2c66affSColin Finck int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
228*c2c66affSColin Finck {
229*c2c66affSColin Finck     FILE *file;
230*c2c66affSColin Finck     size_t n;
231*c2c66affSColin Finck 
232*c2c66affSColin Finck     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
233*c2c66affSColin Finck         return -1;
234*c2c66affSColin Finck 
235*c2c66affSColin Finck     if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
236*c2c66affSColin Finck     {
237*c2c66affSColin Finck         fclose( file );
238*c2c66affSColin Finck         return -1;
239*c2c66affSColin Finck     }
240*c2c66affSColin Finck 
241*c2c66affSColin Finck     fclose( file );
242*c2c66affSColin Finck     return( (int)n );
243*c2c66affSColin Finck }
244*c2c66affSColin Finck 
245*c2c66affSColin Finck int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
246*c2c66affSColin Finck {
247*c2c66affSColin Finck     FILE *file;
248*c2c66affSColin Finck     size_t n;
249*c2c66affSColin Finck 
250*c2c66affSColin Finck     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
251*c2c66affSColin Finck         return -1;
252*c2c66affSColin Finck 
253*c2c66affSColin Finck     if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
254*c2c66affSColin Finck     {
255*c2c66affSColin Finck         fclose( file );
256*c2c66affSColin Finck         return -1;
257*c2c66affSColin Finck     }
258*c2c66affSColin Finck 
259*c2c66affSColin Finck     fclose( file );
260*c2c66affSColin Finck     return( (int)n );
261*c2c66affSColin Finck }
262*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
263*c2c66affSColin Finck 
264*c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
265*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
266*c2c66affSColin Finck /*
267*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
268*c2c66affSColin Finck  */
269*c2c66affSColin Finck static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
270*c2c66affSColin Finck {
271*c2c66affSColin Finck     ((void) buf);
272*c2c66affSColin Finck     ((void) buf_len);
273*c2c66affSColin Finck     return( -1 );
274*c2c66affSColin Finck }
275*c2c66affSColin Finck 
276*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
277*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
278*c2c66affSColin Finck 
279*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
280*c2c66affSColin Finck /*
281*c2c66affSColin Finck  * Make dummy function to prevent NULL pointer dereferences
282*c2c66affSColin Finck  */
283*c2c66affSColin Finck static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
284*c2c66affSColin Finck {
285*c2c66affSColin Finck     ((void) buf);
286*c2c66affSColin Finck     ((void) buf_len);
287*c2c66affSColin Finck     return( -1 );
288*c2c66affSColin Finck }
289*c2c66affSColin Finck 
290*c2c66affSColin Finck #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
291*c2c66affSColin Finck #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
292*c2c66affSColin Finck 
293*c2c66affSColin Finck int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
294*c2c66affSColin Finck             MBEDTLS_PLATFORM_STD_NV_SEED_READ;
295*c2c66affSColin Finck int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
296*c2c66affSColin Finck             MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
297*c2c66affSColin Finck 
298*c2c66affSColin Finck int mbedtls_platform_set_nv_seed(
299*c2c66affSColin Finck         int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
300*c2c66affSColin Finck         int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
301*c2c66affSColin Finck {
302*c2c66affSColin Finck     mbedtls_nv_seed_read = nv_seed_read_func;
303*c2c66affSColin Finck     mbedtls_nv_seed_write = nv_seed_write_func;
304*c2c66affSColin Finck     return( 0 );
305*c2c66affSColin Finck }
306*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
307*c2c66affSColin Finck #endif /* MBEDTLS_ENTROPY_NV_SEED */
308*c2c66affSColin Finck 
309*c2c66affSColin Finck #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
310*c2c66affSColin Finck /*
311*c2c66affSColin Finck  * Placeholder platform setup that does nothing by default
312*c2c66affSColin Finck  */
313*c2c66affSColin Finck int mbedtls_platform_setup( mbedtls_platform_context *ctx )
314*c2c66affSColin Finck {
315*c2c66affSColin Finck     (void)ctx;
316*c2c66affSColin Finck 
317*c2c66affSColin Finck     return( 0 );
318*c2c66affSColin Finck }
319*c2c66affSColin Finck 
320*c2c66affSColin Finck /*
321*c2c66affSColin Finck  * Placeholder platform teardown that does nothing by default
322*c2c66affSColin Finck  */
323*c2c66affSColin Finck void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
324*c2c66affSColin Finck {
325*c2c66affSColin Finck     (void)ctx;
326*c2c66affSColin Finck }
327*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
328*c2c66affSColin Finck 
329*c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_C */
330