1 /*
2 * Platform abstraction layer
3 *
4 * Copyright The Mbed TLS Contributors
5 * 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:
12 *
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.
24 *
25 * **********
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 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54
55 #include "mbedtls/platform.h"
56
57 #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 */
mbedtls_zeroize(void * v,size_t n)60 static void mbedtls_zeroize( void *v, size_t n ) {
61 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
62 }
63 #endif
64
65 /* 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
73 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
platform_calloc_uninit(size_t n,size_t size)74 static void *platform_calloc_uninit( size_t n, size_t size )
75 {
76 ((void) n);
77 ((void) size);
78 return( NULL );
79 }
80
81 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
82 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
83
84 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
platform_free_uninit(void * ptr)85 static void platform_free_uninit( void *ptr )
86 {
87 ((void) ptr);
88 }
89
90 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
91 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
92
93 void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
94 void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
95
mbedtls_platform_set_calloc_free(void * (* calloc_func)(size_t,size_t),void (* free_func)(void *))96 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
97 void (*free_func)( void * ) )
98 {
99 mbedtls_calloc = calloc_func;
100 mbedtls_free = free_func;
101 return( 0 );
102 }
103 #endif /* MBEDTLS_PLATFORM_MEMORY &&
104 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
105 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
106
107 #if defined(_WIN32)
108 #include <stdarg.h>
mbedtls_platform_win32_snprintf(char * s,size_t n,const char * fmt,...)109 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
110 {
111 int ret;
112 va_list argp;
113
114 /* Avoid calling the invalid parameter handler by checking ourselves */
115 if( s == NULL || n == 0 || fmt == NULL )
116 return( -1 );
117
118 va_start( argp, fmt );
119 #if defined(_TRUNCATE) && !defined(__MINGW32__)
120 ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
121 #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
129 va_end( argp );
130
131 return( ret );
132 }
133 #endif
134
135 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
136 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
137 /*
138 * Make dummy function to prevent NULL pointer dereferences
139 */
platform_snprintf_uninit(char * s,size_t n,const char * format,...)140 static int platform_snprintf_uninit( char * s, size_t n,
141 const char * format, ... )
142 {
143 ((void) s);
144 ((void) n);
145 ((void) format);
146 return( 0 );
147 }
148
149 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
150 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
151
152 int (*mbedtls_snprintf)( char * s, size_t n,
153 const char * format,
154 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
155
mbedtls_platform_set_snprintf(int (* snprintf_func)(char * s,size_t n,const char * format,...))156 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
157 const char * format,
158 ... ) )
159 {
160 mbedtls_snprintf = snprintf_func;
161 return( 0 );
162 }
163 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
164
165 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
166 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
167 /*
168 * Make dummy function to prevent NULL pointer dereferences
169 */
platform_printf_uninit(const char * format,...)170 static int platform_printf_uninit( const char *format, ... )
171 {
172 ((void) format);
173 return( 0 );
174 }
175
176 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
177 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
178
179 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
180
mbedtls_platform_set_printf(int (* printf_func)(const char *,...))181 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
182 {
183 mbedtls_printf = printf_func;
184 return( 0 );
185 }
186 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
187
188 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
189 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
190 /*
191 * Make dummy function to prevent NULL pointer dereferences
192 */
platform_fprintf_uninit(FILE * stream,const char * format,...)193 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
194 {
195 ((void) stream);
196 ((void) format);
197 return( 0 );
198 }
199
200 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
201 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
202
203 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
204 MBEDTLS_PLATFORM_STD_FPRINTF;
205
mbedtls_platform_set_fprintf(int (* fprintf_func)(FILE *,const char *,...))206 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
207 {
208 mbedtls_fprintf = fprintf_func;
209 return( 0 );
210 }
211 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
212
213 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
214 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
215 /*
216 * Make dummy function to prevent NULL pointer dereferences
217 */
platform_exit_uninit(int status)218 static void platform_exit_uninit( int status )
219 {
220 ((void) status);
221 }
222
223 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
224 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
225
226 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
227
mbedtls_platform_set_exit(void (* exit_func)(int status))228 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
229 {
230 mbedtls_exit = exit_func;
231 return( 0 );
232 }
233 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
234
235 #if defined(MBEDTLS_HAVE_TIME)
236
237 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
238 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
239 /*
240 * Make dummy function to prevent NULL pointer dereferences
241 */
platform_time_uninit(mbedtls_time_t * timer)242 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
243 {
244 ((void) timer);
245 return( 0 );
246 }
247
248 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
249 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
250
251 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
252
mbedtls_platform_set_time(mbedtls_time_t (* time_func)(mbedtls_time_t * timer))253 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
254 {
255 mbedtls_time = time_func;
256 return( 0 );
257 }
258 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
259
260 #endif /* MBEDTLS_HAVE_TIME */
261
262 #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 */
mbedtls_platform_std_nv_seed_read(unsigned char * buf,size_t buf_len)267 int 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 )
273 return( -1 );
274
275 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
276 {
277 fclose( file );
278 mbedtls_zeroize( buf, buf_len );
279 return( -1 );
280 }
281
282 fclose( file );
283 return( (int)n );
284 }
285
mbedtls_platform_std_nv_seed_write(unsigned char * buf,size_t buf_len)286 int 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 );
301 return( (int)n );
302 }
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 */
platform_nv_seed_read_uninit(unsigned char * buf,size_t buf_len)310 static 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 */
platform_nv_seed_write_uninit(unsigned char * buf,size_t buf_len)324 static 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
334 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
335 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
336 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
337 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
338
mbedtls_platform_set_nv_seed(int (* nv_seed_read_func)(unsigned char * buf,size_t buf_len),int (* nv_seed_write_func)(unsigned char * buf,size_t buf_len))339 int 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
350 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
351 /*
352 * Placeholder platform setup that does nothing by default
353 */
mbedtls_platform_setup(mbedtls_platform_context * ctx)354 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
355 {
356 (void)ctx;
357
358 return( 0 );
359 }
360
361 /*
362 * Placeholder platform teardown that does nothing by default
363 */
mbedtls_platform_teardown(mbedtls_platform_context * ctx)364 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
365 {
366 (void)ctx;
367 }
368 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
369
370 #endif /* MBEDTLS_PLATFORM_C */
371