mbed TLS v2.26.0
platform.h
Go to the documentation of this file.
1 
15 /*
16  * Copyright The Mbed TLS Contributors
17  * SPDX-License-Identifier: Apache-2.0
18  *
19  * Licensed under the Apache License, Version 2.0 (the "License"); you may
20  * not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  *
23  * http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31 #ifndef MBEDTLS_PLATFORM_H
32 #define MBEDTLS_PLATFORM_H
33 
34 #if !defined(MBEDTLS_CONFIG_FILE)
35 #include "mbedtls/config.h"
36 #else
37 #include MBEDTLS_CONFIG_FILE
38 #endif
39 
40 #if defined(MBEDTLS_HAVE_TIME)
41 #include "mbedtls/platform_time.h"
42 #endif
43 
44 #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
45 #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
59 /* The older Microsoft Windows common runtime provides non-conforming
60  * implementations of some standard library functions, including snprintf
61  * and vsnprintf. This affects MSVC and MinGW builds.
62  */
63 #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900)
64 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF
65 #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF
66 #endif
67 
68 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <time.h>
72 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
73 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
74 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
75 #else
76 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
77 #endif
78 #endif
79 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
80 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
81 #define MBEDTLS_PLATFORM_STD_VSNPRINTF mbedtls_platform_win32_vsnprintf
82 #else
83 #define MBEDTLS_PLATFORM_STD_VSNPRINTF vsnprintf
84 #endif
85 #endif
86 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
87 #define MBEDTLS_PLATFORM_STD_PRINTF printf
88 #endif
89 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
90 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
91 #endif
92 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
93 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
94 #endif
95 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
96 #define MBEDTLS_PLATFORM_STD_FREE free
97 #endif
98 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
99 #define MBEDTLS_PLATFORM_STD_EXIT exit
100 #endif
101 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
102 #define MBEDTLS_PLATFORM_STD_TIME time
103 #endif
104 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
105 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
106 #endif
107 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
108 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
109 #endif
110 #if defined(MBEDTLS_FS_IO)
111 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
112 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
113 #endif
114 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
115 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
116 #endif
117 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
118 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
119 #endif
120 #endif /* MBEDTLS_FS_IO */
121 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
122 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
123 #include MBEDTLS_PLATFORM_STD_MEM_HDR
124 #endif
125 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
126 
127 
128 /* \} name SECTION: Module settings */
129 
130 /*
131  * The function pointers for calloc and free.
132  */
133 #if defined(MBEDTLS_PLATFORM_MEMORY)
134 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
135  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
136 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
137 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
138 #else
139 /* For size_t */
140 #include <stddef.h>
141 extern void *mbedtls_calloc( size_t n, size_t size );
142 extern void mbedtls_free( void *ptr );
143 
153 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
154  void (*free_func)( void * ) );
155 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
156 #else /* !MBEDTLS_PLATFORM_MEMORY */
157 #define mbedtls_free free
158 #define mbedtls_calloc calloc
159 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
160 
161 /*
162  * The function pointers for fprintf
163  */
164 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
165 /* We need FILE * */
166 #include <stdio.h>
167 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
168 
178 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
179  ... ) );
180 #else
181 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
182 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
183 #else
184 #define mbedtls_fprintf fprintf
185 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
186 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
187 
188 /*
189  * The function pointers for printf
190  */
191 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
192 extern int (*mbedtls_printf)( const char *format, ... );
193 
203 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
204 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
205 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
206 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
207 #else
208 #define mbedtls_printf printf
209 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
210 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
211 
212 /*
213  * The function pointers for snprintf
214  *
215  * The snprintf implementation should conform to C99:
216  * - it *must* always correctly zero-terminate the buffer
217  * (except when n == 0, then it must leave the buffer untouched)
218  * - however it is acceptable to return -1 instead of the required length when
219  * the destination buffer is too short.
220  */
221 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
222 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
223 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
224 #endif
225 
226 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
227 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
228 
237 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
238  const char * format, ... ) );
239 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
240 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
241 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
242 #else
243 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
244 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
245 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
246 
247 /*
248  * The function pointers for vsnprintf
249  *
250  * The vsnprintf implementation should conform to C99:
251  * - it *must* always correctly zero-terminate the buffer
252  * (except when n == 0, then it must leave the buffer untouched)
253  * - however it is acceptable to return -1 instead of the required length when
254  * the destination buffer is too short.
255  */
256 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
257 #include <stdarg.h>
258 /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */
259 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg );
260 #endif
261 
262 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
263 #include <stdarg.h>
264 extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg );
265 
273 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
274  const char * format, va_list arg ) );
275 #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
276 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO)
277 #define mbedtls_vsnprintf MBEDTLS_PLATFORM_VSNPRINTF_MACRO
278 #else
279 #define mbedtls_vsnprintf vsnprintf
280 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */
281 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
282 
283 /*
284  * The function pointers for exit
285  */
286 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
287 extern void (*mbedtls_exit)( int status );
288 
298 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
299 #else
300 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
301 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
302 #else
303 #define mbedtls_exit exit
304 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
305 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
306 
307 /*
308  * The default exit values
309  */
310 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
311 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
312 #else
313 #define MBEDTLS_EXIT_SUCCESS 0
314 #endif
315 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
316 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
317 #else
318 #define MBEDTLS_EXIT_FAILURE 1
319 #endif
320 
321 /*
322  * The function pointers for reading from and writing a seed file to
323  * Non-Volatile storage (NV) in a platform-independent way
324  *
325  * Only enabled when the NV seed entropy source is enabled
326  */
327 #if defined(MBEDTLS_ENTROPY_NV_SEED)
328 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
329 /* Internal standard platform definitions */
330 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
331 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
332 #endif
333 
334 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
335 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
336 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
337 
347 int mbedtls_platform_set_nv_seed(
348  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
349  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
350  );
351 #else
352 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
353  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
354 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
355 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
356 #else
357 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
358 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
359 #endif
360 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
361 #endif /* MBEDTLS_ENTROPY_NV_SEED */
362 
363 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
364 
372 {
373  char dummy;
374 }
376 
377 #else
378 #include "platform_alt.h"
379 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
380 
412 
413 #ifdef __cplusplus
414 }
415 #endif
416 
417 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_calloc
Definition: platform.h:158
#define mbedtls_free
Definition: platform.h:157
#define mbedtls_vsnprintf
Definition: platform.h:279
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:184
The platform context structure.
Definition: platform.h:371
#define mbedtls_exit
Definition: platform.h:303
#define mbedtls_snprintf
Definition: platform.h:243
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
#define mbedtls_printf
Definition: platform.h:208
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbed TLS Platform time abstraction