blob: cd0e85bbec674cdd959a998e9820f17f99063a21 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010029
Hanno Beckercfa2e332018-10-11 10:26:55 +010030/* The compile time configuration of memory allocation via the macros
31 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
32 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
33 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
34#if defined(MBEDTLS_PLATFORM_MEMORY) && \
35 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
36 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
37
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020038#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
39static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010040{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020041 ((void) n);
42 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010043 return( NULL );
44}
45
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020046#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
47#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010050static void platform_free_uninit( void *ptr )
51{
52 ((void) ptr);
53}
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
56#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010057
Roberto Vargas7decfe82018-06-04 13:54:09 +010058static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
59static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
60
61void * mbedtls_calloc( size_t nmemb, size_t size )
62{
63 return (*mbedtls_calloc_func)( nmemb, size );
64}
65
66void mbedtls_free( void * ptr )
67{
68 (*mbedtls_free_func)( ptr );
69}
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010070
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020071int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010072 void (*free_func)( void * ) )
73{
Roberto Vargas7decfe82018-06-04 13:54:09 +010074 mbedtls_calloc_func = calloc_func;
75 mbedtls_free_func = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010076 return( 0 );
77}
Hanno Beckercfa2e332018-10-11 10:26:55 +010078#endif /* MBEDTLS_PLATFORM_MEMORY &&
79 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
80 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010081
k-stachowiak723f8672018-07-16 14:27:07 +020082#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020083#include <stdarg.h>
84int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
85{
Janos Follath24eed8d2019-11-22 13:21:35 +000086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020087 va_list argp;
88
89 va_start( argp, fmt );
k-stachowiak723f8672018-07-16 14:27:07 +020090 ret = mbedtls_vsnprintf( s, n, fmt, argp );
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020091 va_end( argp );
92
93 return( ret );
94}
95#endif
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
98#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000099/*
100 * Make dummy function to prevent NULL pointer dereferences
101 */
102static int platform_snprintf_uninit( char * s, size_t n,
103 const char * format, ... )
104{
105 ((void) s);
106 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100107 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000108 return( 0 );
109}
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
112#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000115 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000119 const char * format,
120 ... ) )
121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000123 return( 0 );
124}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000126
k-stachowiak723f8672018-07-16 14:27:07 +0200127#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
128#include <stdarg.h>
129int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
130{
Janos Follath24eed8d2019-11-22 13:21:35 +0000131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
k-stachowiak723f8672018-07-16 14:27:07 +0200132
133 /* Avoid calling the invalid parameter handler by checking ourselves */
134 if( s == NULL || n == 0 || fmt == NULL )
135 return( -1 );
136
137#if defined(_TRUNCATE)
138 ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg );
139#else
140 ret = vsnprintf( s, n, fmt, arg );
141 if( ret < 0 || (size_t) ret == n )
142 {
143 s[n-1] = '\0';
144 ret = -1;
145 }
146#endif
147
148 return( ret );
149}
150#endif
151
152#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
153#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
154/*
155 * Make dummy function to prevent NULL pointer dereferences
156 */
157static int platform_vsnprintf_uninit( char * s, size_t n,
158 const char * format, va_list arg )
159{
160 ((void) s);
161 ((void) n);
162 ((void) format);
163 ((void) arg);
Krzysztof Stachowiak41734a12018-09-13 15:04:31 +0200164 return( -1 );
k-stachowiak723f8672018-07-16 14:27:07 +0200165}
166
167#define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
168#endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
169
170int (*mbedtls_vsnprintf)( char * s, size_t n,
171 const char * format,
172 va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
173
174int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
175 const char * format,
176 va_list arg ) )
177{
178 mbedtls_vsnprintf = vsnprintf_func;
179 return( 0 );
180}
181#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
184#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100185/*
186 * Make dummy function to prevent NULL pointer dereferences
187 */
188static int platform_printf_uninit( const char *format, ... )
189{
190 ((void) format);
191 return( 0 );
192}
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
195#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100202 return( 0 );
203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
207#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100208/*
209 * Make dummy function to prevent NULL pointer dereferences
210 */
211static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
212{
213 ((void) stream);
214 ((void) format);
215 return( 0 );
216}
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
219#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
222 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100227 return( 0 );
228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
232#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000233/*
234 * Make dummy function to prevent NULL pointer dereferences
235 */
236static void platform_exit_uninit( int status )
237{
238 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
242#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000243
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100244void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000249 return( 0 );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000252
Simon Butcher23e97782016-07-13 13:31:08 +0100253#if defined(MBEDTLS_HAVE_TIME)
254
SimonBd5800b72016-04-26 07:43:27 +0100255#if defined(MBEDTLS_PLATFORM_TIME_ALT)
256#if !defined(MBEDTLS_PLATFORM_STD_TIME)
257/*
258 * Make dummy function to prevent NULL pointer dereferences
259 */
260static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
261{
262 ((void) timer);
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100263 return( 0 );
SimonBd5800b72016-04-26 07:43:27 +0100264}
265
266#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
267#endif /* !MBEDTLS_PLATFORM_STD_TIME */
268
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100269mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100270
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100271int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
SimonBd5800b72016-04-26 07:43:27 +0100272{
273 mbedtls_time = time_func;
274 return( 0 );
275}
276#endif /* MBEDTLS_PLATFORM_TIME_ALT */
277
Simon Butcher23e97782016-07-13 13:31:08 +0100278#endif /* MBEDTLS_HAVE_TIME */
279
Paul Bakkere021a4b2016-06-01 11:25:44 +0100280#if defined(MBEDTLS_ENTROPY_NV_SEED)
281#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
282/* Default implementations for the platform independent seed functions use
283 * standard libc file functions to read from and write to a pre-defined filename
284 */
285int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
286{
287 FILE *file;
288 size_t n;
289
290 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100291 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100292
293 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
294 {
295 fclose( file );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500296 mbedtls_platform_zeroize( buf, buf_len );
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100297 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100298 }
299
300 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000301 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100302}
303
304int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
305{
306 FILE *file;
307 size_t n;
308
309 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
310 return -1;
311
312 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
313 {
314 fclose( file );
315 return -1;
316 }
317
318 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000319 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100320}
321#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
322
323#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
324#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
325/*
326 * Make dummy function to prevent NULL pointer dereferences
327 */
328static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
329{
330 ((void) buf);
331 ((void) buf_len);
332 return( -1 );
333}
334
335#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
336#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
337
338#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
339/*
340 * Make dummy function to prevent NULL pointer dereferences
341 */
342static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
343{
344 ((void) buf);
345 ((void) buf_len);
346 return( -1 );
347}
348
349#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
350#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
351
352int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
353 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
354int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
355 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
356
357int mbedtls_platform_set_nv_seed(
358 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
359 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
360{
361 mbedtls_nv_seed_read = nv_seed_read_func;
362 mbedtls_nv_seed_write = nv_seed_write_func;
363 return( 0 );
364}
365#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
366#endif /* MBEDTLS_ENTROPY_NV_SEED */
367
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100368#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100369/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100370 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100371 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100372int mbedtls_platform_setup( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100373{
374 (void)ctx;
375
376 return( 0 );
377}
378
379/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100380 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100381 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100382void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100383{
384 (void)ctx;
385}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100386#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#endif /* MBEDTLS_PLATFORM_C */