mbed TLS v2.16.1
platform.h
Go to the documentation of this file.
1 
15 /*
16  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
17  * SPDX-License-Identifier: GPL-2.0
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License along
30  * with this program; if not, write to the Free Software Foundation, Inc.,
31  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32  *
33  * This file is part of Mbed TLS (https://tls.mbed.org)
34  */
35 #ifndef MBEDTLS_PLATFORM_H
36 #define MBEDTLS_PLATFORM_H
37 
38 #if !defined(MBEDTLS_CONFIG_FILE)
39 #include "config.h"
40 #else
41 #include MBEDTLS_CONFIG_FILE
42 #endif
43 
44 #if defined(MBEDTLS_HAVE_TIME)
45 #include "platform_time.h"
46 #endif
47 
48 #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
49 #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
63 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <time.h>
67 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
68 #if defined(_WIN32)
69 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
70 #else
71 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
72 #endif
73 #endif
74 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
75 #define MBEDTLS_PLATFORM_STD_PRINTF printf
76 #endif
77 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
78 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
79 #endif
80 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
81 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
82 #endif
83 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
84 #define MBEDTLS_PLATFORM_STD_FREE free
85 #endif
86 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
87 #define MBEDTLS_PLATFORM_STD_EXIT exit
88 #endif
89 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
90 #define MBEDTLS_PLATFORM_STD_TIME time
91 #endif
92 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
93 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
94 #endif
95 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
96 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
97 #endif
98 #if defined(MBEDTLS_FS_IO)
99 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
100 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
101 #endif
102 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
103 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
104 #endif
105 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
106 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
107 #endif
108 #endif /* MBEDTLS_FS_IO */
109 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
110 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
111 #include MBEDTLS_PLATFORM_STD_MEM_HDR
112 #endif
113 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
114 
115 
116 /* \} name SECTION: Module settings */
117 
118 /*
119  * The function pointers for calloc and free.
120  */
121 #if defined(MBEDTLS_PLATFORM_MEMORY)
122 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
123  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
124 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
125 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
126 #else
127 /* For size_t */
128 #include <stddef.h>
129 extern void *mbedtls_calloc( size_t n, size_t size );
130 extern void mbedtls_free( void *ptr );
131 
141 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
142  void (*free_func)( void * ) );
143 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
144 #else /* !MBEDTLS_PLATFORM_MEMORY */
145 #define mbedtls_free free
146 #define mbedtls_calloc calloc
147 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
148 
149 /*
150  * The function pointers for fprintf
151  */
152 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
153 /* We need FILE * */
154 #include <stdio.h>
155 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
156 
166 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
167  ... ) );
168 #else
169 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
170 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
171 #else
172 #define mbedtls_fprintf fprintf
173 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
174 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
175 
176 /*
177  * The function pointers for printf
178  */
179 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
180 extern int (*mbedtls_printf)( const char *format, ... );
181 
191 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
192 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
193 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
194 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
195 #else
196 #define mbedtls_printf printf
197 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
198 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
199 
200 /*
201  * The function pointers for snprintf
202  *
203  * The snprintf implementation should conform to C99:
204  * - it *must* always correctly zero-terminate the buffer
205  * (except when n == 0, then it must leave the buffer untouched)
206  * - however it is acceptable to return -1 instead of the required length when
207  * the destination buffer is too short.
208  */
209 #if defined(_WIN32)
210 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
211 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
212 #endif
213 
214 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
215 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
216 
225 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
226  const char * format, ... ) );
227 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
228 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
229 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
230 #else
231 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
232 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
233 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
234 
235 /*
236  * The function pointers for exit
237  */
238 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
239 extern void (*mbedtls_exit)( int status );
240 
250 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
251 #else
252 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
253 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
254 #else
255 #define mbedtls_exit exit
256 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
257 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
258 
259 /*
260  * The default exit values
261  */
262 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
263 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
264 #else
265 #define MBEDTLS_EXIT_SUCCESS 0
266 #endif
267 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
268 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
269 #else
270 #define MBEDTLS_EXIT_FAILURE 1
271 #endif
272 
273 /*
274  * The function pointers for reading from and writing a seed file to
275  * Non-Volatile storage (NV) in a platform-independent way
276  *
277  * Only enabled when the NV seed entropy source is enabled
278  */
279 #if defined(MBEDTLS_ENTROPY_NV_SEED)
280 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
281 /* Internal standard platform definitions */
282 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
283 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
284 #endif
285 
286 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
287 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
288 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
289 
299 int mbedtls_platform_set_nv_seed(
300  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
301  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
302  );
303 #else
304 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
305  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
306 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
307 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
308 #else
309 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
310 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
311 #endif
312 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
313 #endif /* MBEDTLS_ENTROPY_NV_SEED */
314 
315 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
316 
324 {
325  char dummy;
326 }
328 
329 #else
330 #include "platform_alt.h"
331 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
332 
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 
369 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_calloc
Definition: platform.h:146
#define mbedtls_free
Definition: platform.h:145
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:172
The platform context structure.
Definition: platform.h:323
#define mbedtls_exit
Definition: platform.h:255
#define mbedtls_snprintf
Definition: platform.h:231
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_printf
Definition: platform.h:196
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbed TLS Platform time abstraction