mbed TLS v2.9.0
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: 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  * This file is part of Mbed TLS (https://tls.mbed.org)
32  */
33 #ifndef MBEDTLS_PLATFORM_H
34 #define MBEDTLS_PLATFORM_H
35 
36 #if !defined(MBEDTLS_CONFIG_FILE)
37 #include "config.h"
38 #else
39 #include MBEDTLS_CONFIG_FILE
40 #endif
41 
42 #if defined(MBEDTLS_HAVE_TIME)
43 #include "mbedtls/platform_time.h"
44 #endif
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
58 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <time.h>
62 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
63 #if defined(_WIN32)
64 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
65 #else
66 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
67 #endif
68 #endif
69 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
70 #define MBEDTLS_PLATFORM_STD_PRINTF printf
71 #endif
72 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
73 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
74 #endif
75 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
76 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
77 #endif
78 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
79 #define MBEDTLS_PLATFORM_STD_FREE free
80 #endif
81 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
82 #define MBEDTLS_PLATFORM_STD_EXIT exit
83 #endif
84 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
85 #define MBEDTLS_PLATFORM_STD_TIME time
86 #endif
87 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
88 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
89 #endif
90 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
91 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
92 #endif
93 #if defined(MBEDTLS_FS_IO)
94 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
95 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
96 #endif
97 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
98 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
99 #endif
100 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
101 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
102 #endif
103 #endif /* MBEDTLS_FS_IO */
104 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
105 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
106 #include MBEDTLS_PLATFORM_STD_MEM_HDR
107 #endif
108 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
109 
110 
111 /* \} name SECTION: Module settings */
112 
113 /*
114  * The function pointers for calloc and free.
115  */
116 #if defined(MBEDTLS_PLATFORM_MEMORY)
117 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
118  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
119 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
120 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
121 #else
122 /* For size_t */
123 #include <stddef.h>
124 extern void * (*mbedtls_calloc)( size_t n, size_t size );
125 extern void (*mbedtls_free)( void *ptr );
126 
136 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
137  void (*free_func)( void * ) );
138 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
139 #else /* !MBEDTLS_PLATFORM_MEMORY */
140 #define mbedtls_free free
141 #define mbedtls_calloc calloc
142 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
143 
144 /*
145  * The function pointers for fprintf
146  */
147 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
148 /* We need FILE * */
149 #include <stdio.h>
150 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
151 
161 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
162  ... ) );
163 #else
164 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
165 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
166 #else
167 #define mbedtls_fprintf fprintf
168 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
169 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
170 
171 /*
172  * The function pointers for printf
173  */
174 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
175 extern int (*mbedtls_printf)( const char *format, ... );
176 
186 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
187 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
188 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
189 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
190 #else
191 #define mbedtls_printf printf
192 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
193 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
194 
195 /*
196  * The function pointers for snprintf
197  *
198  * The snprintf implementation should conform to C99:
199  * - it *must* always correctly zero-terminate the buffer
200  * (except when n == 0, then it must leave the buffer untouched)
201  * - however it is acceptable to return -1 instead of the required length when
202  * the destination buffer is too short.
203  */
204 #if defined(_WIN32)
205 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
206 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
207 #endif
208 
209 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
210 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
211 
220 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
221  const char * format, ... ) );
222 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
223 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
224 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
225 #else
226 #define mbedtls_snprintf snprintf
227 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
228 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
229 
230 /*
231  * The function pointers for exit
232  */
233 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
234 extern void (*mbedtls_exit)( int status );
235 
245 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
246 #else
247 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
248 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
249 #else
250 #define mbedtls_exit exit
251 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
252 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
253 
254 /*
255  * The default exit values
256  */
257 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
258 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
259 #else
260 #define MBEDTLS_EXIT_SUCCESS 0
261 #endif
262 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
263 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
264 #else
265 #define MBEDTLS_EXIT_FAILURE 1
266 #endif
267 
268 /*
269  * The function pointers for reading from and writing a seed file to
270  * Non-Volatile storage (NV) in a platform-independent way
271  *
272  * Only enabled when the NV seed entropy source is enabled
273  */
274 #if defined(MBEDTLS_ENTROPY_NV_SEED)
275 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
276 /* Internal standard platform definitions */
277 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
278 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
279 #endif
280 
281 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
282 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
283 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
284 
294 int mbedtls_platform_set_nv_seed(
295  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
296  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
297  );
298 #else
299 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
300  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
301 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
302 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
303 #else
304 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
305 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
306 #endif
307 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
308 #endif /* MBEDTLS_ENTROPY_NV_SEED */
309 
310 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
311 
318 typedef struct {
319  char dummy;
320 }
322 
323 #else
324 #include "platform_alt.h"
325 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
326 
358 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:140
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:167
The platform context structure.
Definition: platform.h:318
#define mbedtls_exit
Definition: platform.h:250
#define mbedtls_snprintf
Definition: platform.h:226
#define mbedtls_printf
Definition: platform.h:191
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbed TLS Platform time abstraction