Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / library / platform.c
1 /*
2  *  Platform abstraction layer
3  *
4  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
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
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29
30 #include "mbedtls/platform.h"
31 #include "mbedtls/platform_util.h"
32
33 /* The compile time configuration of memory allocation via the macros
34  * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
35  * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
36  * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
37 #if defined(MBEDTLS_PLATFORM_MEMORY) &&                 \
38     !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&        \
39        defined(MBEDTLS_PLATFORM_FREE_MACRO) )
40
41 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
42 static void *platform_calloc_uninit( size_t n, size_t size )
43 {
44     ((void) n);
45     ((void) size);
46     return( NULL );
47 }
48
49 #define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
50 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
51
52 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
53 static void platform_free_uninit( void *ptr )
54 {
55     ((void) ptr);
56 }
57
58 #define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
59 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
60
61 static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
62 static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
63
64 void * mbedtls_calloc( size_t nmemb, size_t size )
65 {
66     return (*mbedtls_calloc_func)( nmemb, size );
67 }
68
69 void mbedtls_free( void * ptr )
70 {
71     (*mbedtls_free_func)( ptr );
72 }
73
74 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
75                               void (*free_func)( void * ) )
76 {
77     mbedtls_calloc_func = calloc_func;
78     mbedtls_free_func = free_func;
79     return( 0 );
80 }
81 #endif /* MBEDTLS_PLATFORM_MEMORY &&
82           !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
83              defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
84
85 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
86 #include <stdarg.h>
87 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
88 {
89     int ret;
90     va_list argp;
91
92     va_start( argp, fmt );
93     ret = mbedtls_vsnprintf( s, n, fmt, argp );
94     va_end( argp );
95
96     return( ret );
97 }
98 #endif
99
100 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
101 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
102 /*
103  * Make dummy function to prevent NULL pointer dereferences
104  */
105 static int platform_snprintf_uninit( char * s, size_t n,
106                                      const char * format, ... )
107 {
108     ((void) s);
109     ((void) n);
110     ((void) format);
111     return( 0 );
112 }
113
114 #define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
115 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
116
117 int (*mbedtls_snprintf)( char * s, size_t n,
118                           const char * format,
119                           ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
120
121 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
122                                                  const char * format,
123                                                  ... ) )
124 {
125     mbedtls_snprintf = snprintf_func;
126     return( 0 );
127 }
128 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
129
130 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
131 #include <stdarg.h>
132 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
133 {
134     int ret;
135
136     /* Avoid calling the invalid parameter handler by checking ourselves */
137     if( s == NULL || n == 0 || fmt == NULL )
138         return( -1 );
139
140 #if defined(_TRUNCATE)
141     ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg );
142 #else
143     ret = vsnprintf( s, n, fmt, arg );
144     if( ret < 0 || (size_t) ret == n )
145     {
146         s[n-1] = '\0';
147         ret = -1;
148     }
149 #endif
150
151     return( ret );
152 }
153 #endif
154
155 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
156 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
157 /*
158  * Make dummy function to prevent NULL pointer dereferences
159  */
160 static int platform_vsnprintf_uninit( char * s, size_t n,
161                                      const char * format, va_list arg )
162 {
163     ((void) s);
164     ((void) n);
165     ((void) format);
166     ((void) arg);
167     return( -1 );
168 }
169
170 #define MBEDTLS_PLATFORM_STD_VSNPRINTF    platform_vsnprintf_uninit
171 #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
172
173 int (*mbedtls_vsnprintf)( char * s, size_t n,
174                           const char * format,
175                           va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
176
177 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
178                                                  const char * format,
179                                                  va_list arg ) )
180 {
181     mbedtls_vsnprintf = vsnprintf_func;
182     return( 0 );
183 }
184 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
185
186 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
187 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
188 /*
189  * Make dummy function to prevent NULL pointer dereferences
190  */
191 static int platform_printf_uninit( const char *format, ... )
192 {
193     ((void) format);
194     return( 0 );
195 }
196
197 #define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
198 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
199
200 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
201
202 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
203 {
204     mbedtls_printf = printf_func;
205     return( 0 );
206 }
207 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
208
209 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
210 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
211 /*
212  * Make dummy function to prevent NULL pointer dereferences
213  */
214 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
215 {
216     ((void) stream);
217     ((void) format);
218     return( 0 );
219 }
220
221 #define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
222 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
223
224 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
225                                         MBEDTLS_PLATFORM_STD_FPRINTF;
226
227 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
228 {
229     mbedtls_fprintf = fprintf_func;
230     return( 0 );
231 }
232 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
233
234 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
235 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
236 /*
237  * Make dummy function to prevent NULL pointer dereferences
238  */
239 static void platform_exit_uninit( int status )
240 {
241     ((void) status);
242 }
243
244 #define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
245 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
246
247 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
248
249 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
250 {
251     mbedtls_exit = exit_func;
252     return( 0 );
253 }
254 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
255
256 #if defined(MBEDTLS_HAVE_TIME)
257
258 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
259 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
260 /*
261  * Make dummy function to prevent NULL pointer dereferences
262  */
263 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
264 {
265     ((void) timer);
266     return( 0 );
267 }
268
269 #define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
270 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
271
272 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
273
274 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
275 {
276     mbedtls_time = time_func;
277     return( 0 );
278 }
279 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
280
281 #endif /* MBEDTLS_HAVE_TIME */
282
283 #if defined(MBEDTLS_ENTROPY_NV_SEED)
284 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
285 /* Default implementations for the platform independent seed functions use
286  * standard libc file functions to read from and write to a pre-defined filename
287  */
288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
289 {
290     FILE *file;
291     size_t n;
292
293     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
294         return( -1 );
295
296     if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
297     {
298         fclose( file );
299         mbedtls_platform_zeroize( buf, buf_len );
300         return( -1 );
301     }
302
303     fclose( file );
304     return( (int)n );
305 }
306
307 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
308 {
309     FILE *file;
310     size_t n;
311
312     if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
313         return -1;
314
315     if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
316     {
317         fclose( file );
318         return -1;
319     }
320
321     fclose( file );
322     return( (int)n );
323 }
324 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
325
326 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
327 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
328 /*
329  * Make dummy function to prevent NULL pointer dereferences
330  */
331 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
332 {
333     ((void) buf);
334     ((void) buf_len);
335     return( -1 );
336 }
337
338 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
339 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
340
341 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
342 /*
343  * Make dummy function to prevent NULL pointer dereferences
344  */
345 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
346 {
347     ((void) buf);
348     ((void) buf_len);
349     return( -1 );
350 }
351
352 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
353 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
354
355 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
356             MBEDTLS_PLATFORM_STD_NV_SEED_READ;
357 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
358             MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
359
360 int mbedtls_platform_set_nv_seed(
361         int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
362         int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
363 {
364     mbedtls_nv_seed_read = nv_seed_read_func;
365     mbedtls_nv_seed_write = nv_seed_write_func;
366     return( 0 );
367 }
368 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
369 #endif /* MBEDTLS_ENTROPY_NV_SEED */
370
371 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
372 /*
373  * Placeholder platform setup that does nothing by default
374  */
375 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
376 {
377     (void)ctx;
378
379     return( 0 );
380 }
381
382 /*
383  * Placeholder platform teardown that does nothing by default
384  */
385 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
386 {
387     (void)ctx;
388 }
389 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
390
391 #endif /* MBEDTLS_PLATFORM_C */