blob: a8b30f0bdd5d6ddcea2270bb9e98ccc0bd956d09 [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker747a83a2014-02-01 22:50:07 +010024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker747a83a2014-02-01 22:50:07 +010047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010058
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010059#if defined(MBEDTLS_ENTROPY_NV_SEED) && \
60 !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
61/* Implementation that should never be optimized out by the compiler */
62static void mbedtls_zeroize( void *v, size_t n ) {
63 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
64}
65#endif
66
Hanno Becker643f3112018-10-11 10:26:55 +010067/* The compile time configuration of memory allocation via the macros
68 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
69 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
70 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
71#if defined(MBEDTLS_PLATFORM_MEMORY) && \
72 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
73 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
74
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020075#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
76static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010077{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020078 ((void) n);
79 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010080 return( NULL );
81}
82
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020083#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
84#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010087static void platform_free_uninit( void *ptr )
88{
89 ((void) ptr);
90}
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
93#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010094
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020095void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010097
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020098int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010099 void (*free_func)( void * ) )
100{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +0200101 mbedtls_calloc = calloc_func;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_free = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100103 return( 0 );
104}
Hanno Becker643f3112018-10-11 10:26:55 +0100105#endif /* MBEDTLS_PLATFORM_MEMORY &&
106 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
107 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100108
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200109#if defined(_WIN32)
110#include <stdarg.h>
111int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
112{
113 int ret;
114 va_list argp;
115
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +0200116 /* Avoid calling the invalid parameter handler by checking ourselves */
117 if( s == NULL || n == 0 || fmt == NULL )
118 return( -1 );
119
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200120 va_start( argp, fmt );
Ron Eldorbc18eb32017-09-06 17:49:10 +0300121#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200122 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +0200123#else
124 ret = _vsnprintf( s, n, fmt, argp );
125 if( ret < 0 || (size_t) ret == n )
126 {
127 s[n-1] = '\0';
128 ret = -1;
129 }
130#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200131 va_end( argp );
132
133 return( ret );
134}
135#endif
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
138#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000139/*
140 * Make dummy function to prevent NULL pointer dereferences
141 */
142static int platform_snprintf_uninit( char * s, size_t n,
143 const char * format, ... )
144{
145 ((void) s);
146 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100147 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000148 return( 0 );
149}
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
152#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000155 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000159 const char * format,
160 ... ) )
161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000163 return( 0 );
164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
168#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100169/*
170 * Make dummy function to prevent NULL pointer dereferences
171 */
172static int platform_printf_uninit( const char *format, ... )
173{
174 ((void) format);
175 return( 0 );
176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
179#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100186 return( 0 );
187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
191#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100192/*
193 * Make dummy function to prevent NULL pointer dereferences
194 */
195static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
196{
197 ((void) stream);
198 ((void) format);
199 return( 0 );
200}
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
203#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
206 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100211 return( 0 );
212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
216#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000217/*
218 * Make dummy function to prevent NULL pointer dereferences
219 */
220static void platform_exit_uninit( int status )
221{
222 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
226#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000227
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100228void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000233 return( 0 );
234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000236
Simon Butcher23e97782016-07-13 13:31:08 +0100237#if defined(MBEDTLS_HAVE_TIME)
238
SimonBd5800b72016-04-26 07:43:27 +0100239#if defined(MBEDTLS_PLATFORM_TIME_ALT)
240#if !defined(MBEDTLS_PLATFORM_STD_TIME)
241/*
242 * Make dummy function to prevent NULL pointer dereferences
243 */
244static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
245{
246 ((void) timer);
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100247 return( 0 );
SimonBd5800b72016-04-26 07:43:27 +0100248}
249
250#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
251#endif /* !MBEDTLS_PLATFORM_STD_TIME */
252
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100253mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100254
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100255int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
SimonBd5800b72016-04-26 07:43:27 +0100256{
257 mbedtls_time = time_func;
258 return( 0 );
259}
260#endif /* MBEDTLS_PLATFORM_TIME_ALT */
261
Simon Butcher23e97782016-07-13 13:31:08 +0100262#endif /* MBEDTLS_HAVE_TIME */
263
Paul Bakkere021a4b2016-06-01 11:25:44 +0100264#if defined(MBEDTLS_ENTROPY_NV_SEED)
265#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
266/* Default implementations for the platform independent seed functions use
267 * standard libc file functions to read from and write to a pre-defined filename
268 */
269int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
270{
271 FILE *file;
272 size_t n;
273
274 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100275 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100276
277 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
278 {
279 fclose( file );
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100280 mbedtls_zeroize( buf, buf_len );
281 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100282 }
283
284 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000285 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100286}
287
288int mbedtls_platform_std_nv_seed_write( 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, "w" ) ) == NULL )
294 return -1;
295
296 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
297 {
298 fclose( file );
299 return -1;
300 }
301
302 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000303 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100304}
305#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
306
307#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
308#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
309/*
310 * Make dummy function to prevent NULL pointer dereferences
311 */
312static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
313{
314 ((void) buf);
315 ((void) buf_len);
316 return( -1 );
317}
318
319#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
320#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
321
322#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
323/*
324 * Make dummy function to prevent NULL pointer dereferences
325 */
326static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
327{
328 ((void) buf);
329 ((void) buf_len);
330 return( -1 );
331}
332
333#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
334#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
335
336int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
337 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
338int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
339 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
340
341int mbedtls_platform_set_nv_seed(
342 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
343 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
344{
345 mbedtls_nv_seed_read = nv_seed_read_func;
346 mbedtls_nv_seed_write = nv_seed_write_func;
347 return( 0 );
348}
349#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
350#endif /* MBEDTLS_ENTROPY_NV_SEED */
351
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100352#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100353/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100354 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100355 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100356int mbedtls_platform_setup( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100357{
358 (void)ctx;
359
360 return( 0 );
361}
362
363/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100364 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100365 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100366void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100367{
368 (void)ctx;
369}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100370#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_PLATFORM_C */