mbed TLS v2.7.16
platform.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
8  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  *
10  * This file is provided under the Apache License 2.0, or the
11  * GNU General Public License v2.0 or later.
12  *
13  * **********
14  * Apache License 2.0:
15  *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  *
28  * **********
29  *
30  * **********
31  * GNU General Public License v2.0 or later:
32  *
33  * This program is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation; either version 2 of the License, or
36  * (at your option) any later version.
37  *
38  * This program is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41  * GNU General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License along
44  * with this program; if not, write to the Free Software Foundation, Inc.,
45  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46  *
47  * **********
48  *
49  * This file is part of Mbed TLS (https://tls.mbed.org)
50  */
51 #ifndef MBEDTLS_PLATFORM_H
52 #define MBEDTLS_PLATFORM_H
53 
54 #if !defined(MBEDTLS_CONFIG_FILE)
55 #include "config.h"
56 #else
57 #include MBEDTLS_CONFIG_FILE
58 #endif
59 
60 #if defined(MBEDTLS_HAVE_TIME)
61 #include "platform_time.h"
62 #endif
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
76 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <time.h>
80 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
81 #if defined(_WIN32)
82 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
83 #else
84 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
85 #endif
86 #endif
87 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
88 #define MBEDTLS_PLATFORM_STD_PRINTF printf
89 #endif
90 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
91 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
92 #endif
93 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
94 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
95 #endif
96 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
97 #define MBEDTLS_PLATFORM_STD_FREE free
98 #endif
99 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
100 #define MBEDTLS_PLATFORM_STD_EXIT exit
101 #endif
102 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
103 #define MBEDTLS_PLATFORM_STD_TIME time
104 #endif
105 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
106 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
107 #endif
108 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
109 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
110 #endif
111 #if defined(MBEDTLS_FS_IO)
112 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
113 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
114 #endif
115 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
116 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
117 #endif
118 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
119 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
120 #endif
121 #endif /* MBEDTLS_FS_IO */
122 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
123 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
124 #include MBEDTLS_PLATFORM_STD_MEM_HDR
125 #endif
126 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
127 
128 
129 /* \} name SECTION: Module settings */
130 
131 /*
132  * The function pointers for calloc and free
133  */
134 #if defined(MBEDTLS_PLATFORM_MEMORY)
135 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
136  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
137 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
138 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
139 #else
140 /* For size_t */
141 #include <stddef.h>
142 extern void * (*mbedtls_calloc)( size_t n, size_t size );
143 extern void (*mbedtls_free)( void *ptr );
144 
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 
176 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
177  ... ) );
178 #else
179 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
180 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
181 #else
182 #define mbedtls_fprintf fprintf
183 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
184 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
185 
186 /*
187  * The function pointers for printf
188  */
189 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
190 extern int (*mbedtls_printf)( const char *format, ... );
191 
200 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
201 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
202 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
203 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
204 #else
205 #define mbedtls_printf printf
206 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
207 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
208 
209 /*
210  * The function pointers for snprintf
211  *
212  * The snprintf implementation should conform to C99:
213  * - it *must* always correctly zero-terminate the buffer
214  * (except when n == 0, then it must leave the buffer untouched)
215  * - however it is acceptable to return -1 instead of the required length when
216  * the destination buffer is too short.
217  */
218 #if defined(_WIN32)
219 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
220 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
221 #endif
222 
223 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
224 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
225 
234 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
235  const char * format, ... ) );
236 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
237 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
238 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
239 #else
240 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
241 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
242 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
243 
244 /*
245  * The function pointers for exit
246  */
247 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
248 extern void (*mbedtls_exit)( int status );
249 
258 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
259 #else
260 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
261 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
262 #else
263 #define mbedtls_exit exit
264 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
265 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
266 
267 /*
268  * The default exit values
269  */
270 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
271 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
272 #else
273 #define MBEDTLS_EXIT_SUCCESS 0
274 #endif
275 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
276 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
277 #else
278 #define MBEDTLS_EXIT_FAILURE 1
279 #endif
280 
281 /*
282  * The function pointers for reading from and writing a seed file to
283  * Non-Volatile storage (NV) in a platform-independent way
284  *
285  * Only enabled when the NV seed entropy source is enabled
286  */
287 #if defined(MBEDTLS_ENTROPY_NV_SEED)
288 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
289 /* Internal standard platform definitions */
290 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
291 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
292 #endif
293 
294 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
295 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
296 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
297 
307 int mbedtls_platform_set_nv_seed(
308  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
309  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
310  );
311 #else
312 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
313  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
314 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
315 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
316 #else
317 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
318 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
319 #endif
320 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
321 #endif /* MBEDTLS_ENTROPY_NV_SEED */
322 
323 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
324 
331 typedef struct {
332  char dummy;
333 }
335 
336 #else
337 #include "platform_alt.h"
338 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
339 
370 
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:157
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:182
The platform context structure.
Definition: platform.h:331
#define mbedtls_exit
Definition: platform.h:263
#define mbedtls_snprintf
Definition: platform.h:240
#define mbedtls_printf
Definition: platform.h:205
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform initialization operations.
mbed TLS Platform time abstraction