blob: c4c3fd332d6371b570a79710a486a7df24f2a031 [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050056#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010057
Hanno Beckercfa2e332018-10-11 10:26:55 +010058/* The compile time configuration of memory allocation via the macros
59 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
60 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
61 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
62#if defined(MBEDTLS_PLATFORM_MEMORY) && \
63 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
64 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
65
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020066#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
67static void *platform_calloc_uninit( size_t n, size_t size )
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010068{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020069 ((void) n);
70 ((void) size);
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010071 return( NULL );
72}
73
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020074#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
75#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010078static void platform_free_uninit( void *ptr )
79{
80 ((void) ptr);
81}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
84#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010085
Roberto Vargas7decfe82018-06-04 13:54:09 +010086static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
87static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
88
89void * mbedtls_calloc( size_t nmemb, size_t size )
90{
91 return (*mbedtls_calloc_func)( nmemb, size );
92}
93
94void mbedtls_free( void * ptr )
95{
96 (*mbedtls_free_func)( ptr );
97}
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010098
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020099int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100100 void (*free_func)( void * ) )
101{
Roberto Vargas7decfe82018-06-04 13:54:09 +0100102 mbedtls_calloc_func = calloc_func;
103 mbedtls_free_func = free_func;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100104 return( 0 );
105}
Hanno Beckercfa2e332018-10-11 10:26:55 +0100106#endif /* MBEDTLS_PLATFORM_MEMORY &&
107 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
108 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +0100109
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200110#if defined(_WIN32)
111#include <stdarg.h>
112int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
113{
114 int ret;
115 va_list argp;
116
Manuel Pégourié-Gonnardf659d2c2015-06-26 17:45:00 +0200117 /* Avoid calling the invalid parameter handler by checking ourselves */
118 if( s == NULL || n == 0 || fmt == NULL )
119 return( -1 );
120
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200121 va_start( argp, fmt );
Ron Eldorbc18eb32017-09-06 17:49:10 +0300122#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200123 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +0200124#else
125 ret = _vsnprintf( s, n, fmt, argp );
126 if( ret < 0 || (size_t) ret == n )
127 {
128 s[n-1] = '\0';
129 ret = -1;
130 }
131#endif
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +0200132 va_end( argp );
133
134 return( ret );
135}
136#endif
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
139#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000140/*
141 * Make dummy function to prevent NULL pointer dereferences
142 */
143static int platform_snprintf_uninit( char * s, size_t n,
144 const char * format, ... )
145{
146 ((void) s);
147 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100148 ((void) format);
Rich Evans46b0a8d2015-01-30 10:47:32 +0000149 return( 0 );
150}
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
153#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int (*mbedtls_snprintf)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000156 const char * format,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
Rich Evans46b0a8d2015-01-30 10:47:32 +0000160 const char * format,
161 ... ) )
162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_snprintf = snprintf_func;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000164 return( 0 );
165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
169#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100170/*
171 * Make dummy function to prevent NULL pointer dereferences
172 */
173static int platform_printf_uninit( const char *format, ... )
174{
175 ((void) format);
176 return( 0 );
177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
180#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_printf = printf_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_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
192#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100193/*
194 * Make dummy function to prevent NULL pointer dereferences
195 */
196static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
197{
198 ((void) stream);
199 ((void) format);
200 return( 0 );
201}
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
204#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
207 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
Paul Bakker747a83a2014-02-01 22:50:07 +0100210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_fprintf = fprintf_func;
Paul Bakker747a83a2014-02-01 22:50:07 +0100212 return( 0 );
213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
217#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000218/*
219 * Make dummy function to prevent NULL pointer dereferences
220 */
221static void platform_exit_uninit( int status )
222{
223 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000224}
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
227#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000228
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100229void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
Rich Evansc39cb492015-01-30 12:01:34 +0000232{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_exit = exit_func;
Rich Evansc39cb492015-01-30 12:01:34 +0000234 return( 0 );
235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000237
Simon Butcher23e97782016-07-13 13:31:08 +0100238#if defined(MBEDTLS_HAVE_TIME)
239
SimonBd5800b72016-04-26 07:43:27 +0100240#if defined(MBEDTLS_PLATFORM_TIME_ALT)
241#if !defined(MBEDTLS_PLATFORM_STD_TIME)
242/*
243 * Make dummy function to prevent NULL pointer dereferences
244 */
245static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
246{
247 ((void) timer);
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100248 return( 0 );
SimonBd5800b72016-04-26 07:43:27 +0100249}
250
251#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
252#endif /* !MBEDTLS_PLATFORM_STD_TIME */
253
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100254mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100255
Simon Butcher3fe6cd32016-04-26 19:51:29 +0100256int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
SimonBd5800b72016-04-26 07:43:27 +0100257{
258 mbedtls_time = time_func;
259 return( 0 );
260}
261#endif /* MBEDTLS_PLATFORM_TIME_ALT */
262
Simon Butcher23e97782016-07-13 13:31:08 +0100263#endif /* MBEDTLS_HAVE_TIME */
264
Paul Bakkere021a4b2016-06-01 11:25:44 +0100265#if defined(MBEDTLS_ENTROPY_NV_SEED)
266#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
267/* Default implementations for the platform independent seed functions use
268 * standard libc file functions to read from and write to a pre-defined filename
269 */
270int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
271{
272 FILE *file;
273 size_t n;
274
275 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100276 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100277
278 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
279 {
280 fclose( file );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500281 mbedtls_platform_zeroize( buf, buf_len );
Andres Amaya Garcia79a2e7e2017-06-26 11:10:22 +0100282 return( -1 );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100283 }
284
285 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000286 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100287}
288
289int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
290{
291 FILE *file;
292 size_t n;
293
294 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
295 return -1;
296
297 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
298 {
299 fclose( file );
300 return -1;
301 }
302
303 fclose( file );
Simon B3249cb72016-11-03 01:11:37 +0000304 return( (int)n );
Paul Bakkere021a4b2016-06-01 11:25:44 +0100305}
306#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
307
308#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
309#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
310/*
311 * Make dummy function to prevent NULL pointer dereferences
312 */
313static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
314{
315 ((void) buf);
316 ((void) buf_len);
317 return( -1 );
318}
319
320#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
321#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
322
323#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
324/*
325 * Make dummy function to prevent NULL pointer dereferences
326 */
327static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
328{
329 ((void) buf);
330 ((void) buf_len);
331 return( -1 );
332}
333
334#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
335#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
336
337int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
338 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
339int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
340 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
341
342int mbedtls_platform_set_nv_seed(
343 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
344 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
345{
346 mbedtls_nv_seed_read = nv_seed_read_func;
347 mbedtls_nv_seed_write = nv_seed_write_func;
348 return( 0 );
349}
350#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
351#endif /* MBEDTLS_ENTROPY_NV_SEED */
352
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100353#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100354/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100355 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100356 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100357int mbedtls_platform_setup( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100358{
359 (void)ctx;
360
361 return( 0 );
362}
363
364/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100365 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100366 */
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100367void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100368{
369 (void)ctx;
370}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100371#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#endif /* MBEDTLS_PLATFORM_C */