blob: 73a6db9ebe2a4ff8d2b9c99196fe183424a6b45b [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Paul Bakkere021a4b2016-06-01 11:25:44 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker747a83a2014-02-01 22:50:07 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +010020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050031#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010032
Hanno Beckercfa2e332018-10-11 10:26:55 +010033/* 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
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020041#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
42static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010043{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020044 ((void) n);
45 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010046 return( NULL );
47}
48
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020049#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
50#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010053static void platform_free_uninit( void *ptr )
54{
55 ((void) ptr);
56}
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
59#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010060
Roberto Vargas7decfe82018-06-04 13:54:09 +010061static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
62static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
63
64void * mbedtls_calloc( size_t nmemb, size_t size )
65{
66 return (*mbedtls_calloc_func)( nmemb, size );
67}
68
69void mbedtls_free( void * ptr )
70{
71 (*mbedtls_free_func)( ptr );
72}
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010073
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020074int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010075 void (*free_func)( void * ) )
76{
Roberto Vargas7decfe82018-06-04 13:54:09 +010077 mbedtls_calloc_func = calloc_func;
78 mbedtls_free_func = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010079 return( 0 );
80}
Hanno Beckercfa2e332018-10-11 10:26:55 +010081#endif /* MBEDTLS_PLATFORM_MEMORY &&
82 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
83 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010084
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020085#if defined(_WIN32)
86#include <stdarg.h>
87int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
88{
89 int ret;
90 va_list argp;
91
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +020092 /* Avoid calling the invalid parameter handler by checking ourselves */
93 if( s == NULL || n == 0 || fmt == NULL )
94 return( -1 );
95
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020096 va_start( argp, fmt );
Ron Eldorbc18eb32017-09-06 17:49:10 +030097#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020098 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020099#else
100 ret = _vsnprintf( s, n, fmt, argp );
101 if( ret < 0 || (size_t) ret == n )
102 {
103 s[n-1] = '\0';
104 ret = -1;
105 }
106#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200107 va_end( argp );
108
109 return( ret );
110}
111#endif
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
114#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000115/*
116 * Make dummy function to prevent NULL pointer dereferences
117 */
118static int platform_snprintf_uninit( char * s, size_t n,
119 const char * format, ... )
120{
121 ((void) s);
122 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100123 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000124 return( 0 );
125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
128#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000131 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000135 const char * format,
136 ... ) )
137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000139 return( 0 );
140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
144#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100145/*
146 * Make dummy function to prevent NULL pointer dereferences
147 */
148static int platform_printf_uninit( const char *format, ... )
149{
150 ((void) format);
151 return( 0 );
152}
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
155#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100162 return( 0 );
163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
167#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100168/*
169 * Make dummy function to prevent NULL pointer dereferences
170 */
171static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
172{
173 ((void) stream);
174 ((void) format);
175 return( 0 );
176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
179#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
182 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100187 return( 0 );
188}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
192#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000193/*
194 * Make dummy function to prevent NULL pointer dereferences
195 */
196static void platform_exit_uninit( int status )
197{
198 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000199}
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
202#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000203
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100204void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000209 return( 0 );
210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000212
Simon Butcher23e97782016-07-13 13:31:08 +0100213#if defined(MBEDTLS_HAVE_TIME)
214
SimonBd5800b72016-04-26 07:43:27 +0100215#if defined(MBEDTLS_PLATFORM_TIME_ALT)
216#if !defined(MBEDTLS_PLATFORM_STD_TIME)
217/*
218 * Make dummy function to prevent NULL pointer dereferences
219 */
220static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
221{
222 ((void) timer);
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100223 return( 0 );
SimonBd5800b72016-04-26 07:43:27 +0100224}
225
226#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
227#endif /* !MBEDTLS_PLATFORM_STD_TIME */
228
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100229mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100230
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100231int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
SimonBd5800b72016-04-26 07:43:27 +0100232{
233 mbedtls_time = time_func;
234 return( 0 );
235}
236#endif /* MBEDTLS_PLATFORM_TIME_ALT */
237
Simon Butcher23e97782016-07-13 13:31:08 +0100238#endif /* MBEDTLS_HAVE_TIME */
239
Paul Bakkere021a4b2016-06-01 11:25:44 +0100240#if defined(MBEDTLS_ENTROPY_NV_SEED)
241#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
242/* Default implementations for the platform independent seed functions use
243 * standard libc file functions to read from and write to a pre-defined filename
244 */
245int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
246{
247 FILE *file;
248 size_t n;
249
250 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100251 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100252
253 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
254 {
255 fclose( file );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500256 mbedtls_platform_zeroize( buf, buf_len );
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100257 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100258 }
259
260 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000261 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100262}
263
264int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
265{
266 FILE *file;
267 size_t n;
268
269 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
270 return -1;
271
272 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
273 {
274 fclose( file );
275 return -1;
276 }
277
278 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000279 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100280}
281#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
282
283#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
284#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
285/*
286 * Make dummy function to prevent NULL pointer dereferences
287 */
288static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
289{
290 ((void) buf);
291 ((void) buf_len);
292 return( -1 );
293}
294
295#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
296#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
297
298#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
299/*
300 * Make dummy function to prevent NULL pointer dereferences
301 */
302static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
303{
304 ((void) buf);
305 ((void) buf_len);
306 return( -1 );
307}
308
309#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
310#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
311
312int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
313 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
314int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
315 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
316
317int mbedtls_platform_set_nv_seed(
318 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
319 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
320{
321 mbedtls_nv_seed_read = nv_seed_read_func;
322 mbedtls_nv_seed_write = nv_seed_write_func;
323 return( 0 );
324}
325#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
326#endif /* MBEDTLS_ENTROPY_NV_SEED */
327
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100328#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100329/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100330 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100331 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100332int mbedtls_platform_setup( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100333{
334 (void)ctx;
335
336 return( 0 );
337}
338
339/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100340 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100341 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100342void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100343{
344 (void)ctx;
345}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100346#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_PLATFORM_C */