blob: a9267b16f8405079d86efa9696c4610bd40b07b4 [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker747a83a2014-02-01 22:50:07 +010045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker747a83a2014-02-01 22:50:07 +010052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010056
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010057#if defined(MBEDTLS_ENTROPY_NV_SEED) && \
58 !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
59/* Implementation that should never be optimized out by the compiler */
60static void mbedtls_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
62}
63#endif
64
Hanno Becker643f3112018-10-11 10:26:55 +010065/* The compile time configuration of memory allocation via the macros
66 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
67 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
68 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
69#if defined(MBEDTLS_PLATFORM_MEMORY) && \
70 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
71 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
72
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020073#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
74static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010075{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020076 ((void) n);
77 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010078 return( NULL );
79}
80
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020081#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
82#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010085static void platform_free_uninit( void *ptr )
86{
87 ((void) ptr);
88}
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
91#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010092
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020093void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010095
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020096int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010097 void (*free_func)( void * ) )
98{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020099 mbedtls_calloc = calloc_func;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_free = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100101 return( 0 );
102}
Hanno Becker643f3112018-10-11 10:26:55 +0100103#endif /* MBEDTLS_PLATFORM_MEMORY &&
104 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
105 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100106
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200107#if defined(_WIN32)
108#include <stdarg.h>
109int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
110{
111 int ret;
112 va_list argp;
113
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +0200114 /* Avoid calling the invalid parameter handler by checking ourselves */
115 if( s == NULL || n == 0 || fmt == NULL )
116 return( -1 );
117
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200118 va_start( argp, fmt );
Ron Eldorbc18eb32017-09-06 17:49:10 +0300119#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200120 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +0200121#else
122 ret = _vsnprintf( s, n, fmt, argp );
123 if( ret < 0 || (size_t) ret == n )
124 {
125 s[n-1] = '\0';
126 ret = -1;
127 }
128#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200129 va_end( argp );
130
131 return( ret );
132}
133#endif
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
136#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000137/*
138 * Make dummy function to prevent NULL pointer dereferences
139 */
140static int platform_snprintf_uninit( char * s, size_t n,
141 const char * format, ... )
142{
143 ((void) s);
144 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100145 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000146 return( 0 );
147}
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
150#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000153 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000157 const char * format,
158 ... ) )
159{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000161 return( 0 );
162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
166#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100167/*
168 * Make dummy function to prevent NULL pointer dereferences
169 */
170static int platform_printf_uninit( const char *format, ... )
171{
172 ((void) format);
173 return( 0 );
174}
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
177#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf = printf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100184 return( 0 );
185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
189#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100190/*
191 * Make dummy function to prevent NULL pointer dereferences
192 */
193static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
194{
195 ((void) stream);
196 ((void) format);
197 return( 0 );
198}
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
201#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
204 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100209 return( 0 );
210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
214#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000215/*
216 * Make dummy function to prevent NULL pointer dereferences
217 */
218static void platform_exit_uninit( int status )
219{
220 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000221}
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
224#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000225
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100226void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000231 return( 0 );
232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000234
Simon Butcher23e97782016-07-13 13:31:08 +0100235#if defined(MBEDTLS_HAVE_TIME)
236
SimonBd5800b72016-04-26 07:43:27 +0100237#if defined(MBEDTLS_PLATFORM_TIME_ALT)
238#if !defined(MBEDTLS_PLATFORM_STD_TIME)
239/*
240 * Make dummy function to prevent NULL pointer dereferences
241 */
242static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
243{
244 ((void) timer);
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100245 return( 0 );
SimonBd5800b72016-04-26 07:43:27 +0100246}
247
248#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
249#endif /* !MBEDTLS_PLATFORM_STD_TIME */
250
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100251mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100252
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100253int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
SimonBd5800b72016-04-26 07:43:27 +0100254{
255 mbedtls_time = time_func;
256 return( 0 );
257}
258#endif /* MBEDTLS_PLATFORM_TIME_ALT */
259
Simon Butcher23e97782016-07-13 13:31:08 +0100260#endif /* MBEDTLS_HAVE_TIME */
261
Paul Bakkere021a4b2016-06-01 11:25:44 +0100262#if defined(MBEDTLS_ENTROPY_NV_SEED)
263#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
264/* Default implementations for the platform independent seed functions use
265 * standard libc file functions to read from and write to a pre-defined filename
266 */
267int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
268{
269 FILE *file;
270 size_t n;
271
272 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100273 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100274
275 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
276 {
277 fclose( file );
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100278 mbedtls_zeroize( buf, buf_len );
279 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100280 }
281
282 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000283 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100284}
285
286int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
287{
288 FILE *file;
289 size_t n;
290
291 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
292 return -1;
293
294 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
295 {
296 fclose( file );
297 return -1;
298 }
299
300 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000301 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100302}
303#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
304
305#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
306#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
307/*
308 * Make dummy function to prevent NULL pointer dereferences
309 */
310static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
311{
312 ((void) buf);
313 ((void) buf_len);
314 return( -1 );
315}
316
317#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
318#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
319
320#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
321/*
322 * Make dummy function to prevent NULL pointer dereferences
323 */
324static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
325{
326 ((void) buf);
327 ((void) buf_len);
328 return( -1 );
329}
330
331#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
332#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
333
334int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
335 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
336int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
337 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
338
339int mbedtls_platform_set_nv_seed(
340 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
341 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
342{
343 mbedtls_nv_seed_read = nv_seed_read_func;
344 mbedtls_nv_seed_write = nv_seed_write_func;
345 return( 0 );
346}
347#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
348#endif /* MBEDTLS_ENTROPY_NV_SEED */
349
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100350#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100351/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100352 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100353 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100354int mbedtls_platform_setup( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100355{
356 (void)ctx;
357
358 return( 0 );
359}
360
361/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100362 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100363 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100364void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100365{
366 (void)ctx;
367}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100368#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#endif /* MBEDTLS_PLATFORM_C */