blob: b24b2fa65218107706f5b2c2ee3b58ae2da10d4d [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_PLATFORM_MEMORY)
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020034#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
35static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010036{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020037 ((void) n);
38 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010039 return( NULL );
40}
41
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020042#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
43#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010046static void platform_free_uninit( void *ptr )
47{
48 ((void) ptr);
49}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
52#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010053
Roberto Vargas7decfe82018-06-04 13:54:09 +010054static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
55static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
56
57void * mbedtls_calloc( size_t nmemb, size_t size )
58{
59 return (*mbedtls_calloc_func)( nmemb, size );
60}
61
62void mbedtls_free( void * ptr )
63{
64 (*mbedtls_free_func)( ptr );
65}
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010066
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020067int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010068 void (*free_func)( void * ) )
69{
Roberto Vargas7decfe82018-06-04 13:54:09 +010070 mbedtls_calloc_func = calloc_func;
71 mbedtls_free_func = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010072 return( 0 );
73}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#endif /* MBEDTLS_PLATFORM_MEMORY */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010075
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020076#if defined(_WIN32)
77#include <stdarg.h>
78int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
79{
80 int ret;
81 va_list argp;
82
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +020083 /* Avoid calling the invalid parameter handler by checking ourselves */
84 if( s == NULL || n == 0 || fmt == NULL )
85 return( -1 );
86
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020087 va_start( argp, fmt );
Ron Eldorbc18eb32017-09-06 17:49:10 +030088#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020089 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +020090#else
91 ret = _vsnprintf( s, n, fmt, argp );
92 if( ret < 0 || (size_t) ret == n )
93 {
94 s[n-1] = '\0';
95 ret = -1;
96 }
97#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020098 va_end( argp );
99
100 return( ret );
101}
102#endif
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
105#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000106/*
107 * Make dummy function to prevent NULL pointer dereferences
108 */
109static int platform_snprintf_uninit( char * s, size_t n,
110 const char * format, ... )
111{
112 ((void) s);
113 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100114 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000115 return( 0 );
116}
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
119#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000122 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000126 const char * format,
127 ... ) )
128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000130 return( 0 );
131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
135#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100136/*
137 * Make dummy function to prevent NULL pointer dereferences
138 */
139static int platform_printf_uninit( const char *format, ... )
140{
141 ((void) format);
142 return( 0 );
143}
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
146#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100153 return( 0 );
154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
158#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100159/*
160 * Make dummy function to prevent NULL pointer dereferences
161 */
162static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
163{
164 ((void) stream);
165 ((void) format);
166 return( 0 );
167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
170#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
173 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100176{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100178 return( 0 );
179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
183#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000184/*
185 * Make dummy function to prevent NULL pointer dereferences
186 */
187static void platform_exit_uninit( int status )
188{
189 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
193#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000194
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100195void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000200 return( 0 );
201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000203
Simon Butcher23e97782016-07-13 13:31:08 +0100204#if defined(MBEDTLS_HAVE_TIME)
205
SimonBd5800b72016-04-26 07:43:27 +0100206#if defined(MBEDTLS_PLATFORM_TIME_ALT)
207#if !defined(MBEDTLS_PLATFORM_STD_TIME)
208/*
209 * Make dummy function to prevent NULL pointer dereferences
210 */
211static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
212{
213 ((void) timer);
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100214 return( 0 );
SimonBd5800b72016-04-26 07:43:27 +0100215}
216
217#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
218#endif /* !MBEDTLS_PLATFORM_STD_TIME */
219
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100220mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100221
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100222int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
SimonBd5800b72016-04-26 07:43:27 +0100223{
224 mbedtls_time = time_func;
225 return( 0 );
226}
227#endif /* MBEDTLS_PLATFORM_TIME_ALT */
228
Simon Butcher23e97782016-07-13 13:31:08 +0100229#endif /* MBEDTLS_HAVE_TIME */
230
Paul Bakkere021a4b2016-06-01 11:25:44 +0100231#if defined(MBEDTLS_ENTROPY_NV_SEED)
232#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
233/* Default implementations for the platform independent seed functions use
234 * standard libc file functions to read from and write to a pre-defined filename
235 */
236int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
237{
238 FILE *file;
239 size_t n;
240
241 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100242 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100243
244 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
245 {
246 fclose( file );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500247 mbedtls_platform_zeroize( buf, buf_len );
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100248 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100249 }
250
251 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000252 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100253}
254
255int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
256{
257 FILE *file;
258 size_t n;
259
260 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
261 return -1;
262
263 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
264 {
265 fclose( file );
266 return -1;
267 }
268
269 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000270 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100271}
272#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
273
274#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
275#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
276/*
277 * Make dummy function to prevent NULL pointer dereferences
278 */
279static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
280{
281 ((void) buf);
282 ((void) buf_len);
283 return( -1 );
284}
285
286#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
287#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
288
289#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
290/*
291 * Make dummy function to prevent NULL pointer dereferences
292 */
293static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
294{
295 ((void) buf);
296 ((void) buf_len);
297 return( -1 );
298}
299
300#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
301#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
302
303int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
304 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
305int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
306 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
307
308int mbedtls_platform_set_nv_seed(
309 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
310 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
311{
312 mbedtls_nv_seed_read = nv_seed_read_func;
313 mbedtls_nv_seed_write = nv_seed_write_func;
314 return( 0 );
315}
316#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
317#endif /* MBEDTLS_ENTROPY_NV_SEED */
318
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100319#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100320/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100321 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100322 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100323int mbedtls_platform_setup( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100324{
325 (void)ctx;
326
327 return( 0 );
328}
329
330/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100331 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100332 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100333void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100334{
335 (void)ctx;
336}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100337#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_PLATFORM_C */