mbed TLS v2.7.17
platform.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright The Mbed TLS Contributors
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 #ifndef MBEDTLS_PLATFORM_H
50 #define MBEDTLS_PLATFORM_H
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #if defined(MBEDTLS_HAVE_TIME)
59 #include "platform_time.h"
60 #endif
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
74 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <time.h>
78 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
79 #if defined(_WIN32)
80 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
81 #else
82 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
83 #endif
84 #endif
85 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
86 #define MBEDTLS_PLATFORM_STD_PRINTF printf
87 #endif
88 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
89 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
90 #endif
91 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
92 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
93 #endif
94 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
95 #define MBEDTLS_PLATFORM_STD_FREE free
96 #endif
97 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
98 #define MBEDTLS_PLATFORM_STD_EXIT exit
99 #endif
100 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
101 #define MBEDTLS_PLATFORM_STD_TIME time
102 #endif
103 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
104 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
105 #endif
106 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
107 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
108 #endif
109 #if defined(MBEDTLS_FS_IO)
110 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
111 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
112 #endif
113 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
114 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
115 #endif
116 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
117 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
118 #endif
119 #endif /* MBEDTLS_FS_IO */
120 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
121 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
122 #include MBEDTLS_PLATFORM_STD_MEM_HDR
123 #endif
124 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
125 
126 
127 /* \} name SECTION: Module settings */
128 
129 /*
130  * The function pointers for calloc and free
131  */
132 #if defined(MBEDTLS_PLATFORM_MEMORY)
133 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
134  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
135 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
136 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
137 #else
138 /* For size_t */
139 #include <stddef.h>
140 extern void * (*mbedtls_calloc)( size_t n, size_t size );
141 extern void (*mbedtls_free)( void *ptr );
142 
151 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
152  void (*free_func)( void * ) );
153 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
154 #else /* !MBEDTLS_PLATFORM_MEMORY */
155 #define mbedtls_free free
156 #define mbedtls_calloc calloc
157 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
158 
159 /*
160  * The function pointers for fprintf
161  */
162 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
163 /* We need FILE * */
164 #include <stdio.h>
165 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
166 
174 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
175  ... ) );
176 #else
177 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
178 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
179 #else
180 #define mbedtls_fprintf fprintf
181 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
182 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
183 
184 /*
185  * The function pointers for printf
186  */
187 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
188 extern int (*mbedtls_printf)( const char *format, ... );
189 
198 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
199 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
200 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
201 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
202 #else
203 #define mbedtls_printf printf
204 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
205 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
206 
207 /*
208  * The function pointers for snprintf
209  *
210  * The snprintf implementation should conform to C99:
211  * - it *must* always correctly zero-terminate the buffer
212  * (except when n == 0, then it must leave the buffer untouched)
213  * - however it is acceptable to return -1 instead of the required length when
214  * the destination buffer is too short.
215  */
216 #if defined(_WIN32)
217 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
218 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
219 #endif
220 
221 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
222 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
223 
232 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
233  const char * format, ... ) );
234 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
235 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
236 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
237 #else
238 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
239 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
240 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
241 
242 /*
243  * The function pointers for exit
244  */
245 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
246 extern void (*mbedtls_exit)( int status );
247 
256 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
257 #else
258 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
259 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
260 #else
261 #define mbedtls_exit exit
262 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
263 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
264 
265 /*
266  * The default exit values
267  */
268 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
269 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
270 #else
271 #define MBEDTLS_EXIT_SUCCESS 0
272 #endif
273 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
274 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
275 #else
276 #define MBEDTLS_EXIT_FAILURE 1
277 #endif
278 
279 /*
280  * The function pointers for reading from and writing a seed file to
281  * Non-Volatile storage (NV) in a platform-independent way
282  *
283  * Only enabled when the NV seed entropy source is enabled
284  */
285 #if defined(MBEDTLS_ENTROPY_NV_SEED)
286 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
287 /* Internal standard platform definitions */
288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
289 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
290 #endif
291 
292 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
293 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
294 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
295 
305 int mbedtls_platform_set_nv_seed(
306  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
307  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
308  );
309 #else
310 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
311  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
312 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
313 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
314 #else
315 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
316 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
317 #endif
318 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
319 #endif /* MBEDTLS_ENTROPY_NV_SEED */
320 
321 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
322 
329 typedef struct {
330  char dummy;
331 }
333 
334 #else
335 #include "platform_alt.h"
336 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
337 
368 
369 #ifdef __cplusplus
370 }
371 #endif
372 
373 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:155
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:180
The platform context structure.
Definition: platform.h:329
#define mbedtls_exit
Definition: platform.h:261
#define mbedtls_snprintf
Definition: platform.h:238
#define mbedtls_printf
Definition: platform.h:203
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform initialization operations.
mbed TLS Platform time abstraction