mbed TLS v2.16.7
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 OR GPL-2.0-or-later
18  *
19  * This file is provided under the Apache License 2.0, or the
20  * GNU General Public License v2.0 or later.
21  *
22  * **********
23  * Apache License 2.0:
24  *
25  * Licensed under the Apache License, Version 2.0 (the "License"); you may
26  * not use this file except in compliance with the License.
27  * You may obtain a copy of the License at
28  *
29  * http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34  * See the License for the specific language governing permissions and
35  * limitations under the License.
36  *
37  * **********
38  *
39  * **********
40  * GNU General Public License v2.0 or later:
41  *
42  * This program is free software; you can redistribute it and/or modify
43  * it under the terms of the GNU General Public License as published by
44  * the Free Software Foundation; either version 2 of the License, or
45  * (at your option) any later version.
46  *
47  * This program is distributed in the hope that it will be useful,
48  * but WITHOUT ANY WARRANTY; without even the implied warranty of
49  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50  * GNU General Public License for more details.
51  *
52  * You should have received a copy of the GNU General Public License along
53  * with this program; if not, write to the Free Software Foundation, Inc.,
54  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55  *
56  * **********
57  *
58  * This file is part of Mbed TLS (https://tls.mbed.org)
59  */
60 #ifndef MBEDTLS_PLATFORM_H
61 #define MBEDTLS_PLATFORM_H
62 
63 #if !defined(MBEDTLS_CONFIG_FILE)
64 #include "config.h"
65 #else
66 #include MBEDTLS_CONFIG_FILE
67 #endif
68 
69 #if defined(MBEDTLS_HAVE_TIME)
70 #include "platform_time.h"
71 #endif
72 
73 #define MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED -0x0070
74 #define MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED -0x0072
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
88 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <time.h>
92 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
93 #if defined(_WIN32)
94 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
95 #else
96 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
97 #endif
98 #endif
99 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
100 #define MBEDTLS_PLATFORM_STD_PRINTF printf
101 #endif
102 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
103 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
104 #endif
105 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
106 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
107 #endif
108 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
109 #define MBEDTLS_PLATFORM_STD_FREE free
110 #endif
111 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
112 #define MBEDTLS_PLATFORM_STD_EXIT exit
113 #endif
114 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
115 #define MBEDTLS_PLATFORM_STD_TIME time
116 #endif
117 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
118 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
119 #endif
120 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
121 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
122 #endif
123 #if defined(MBEDTLS_FS_IO)
124 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
125 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
126 #endif
127 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
128 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
129 #endif
130 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
131 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
132 #endif
133 #endif /* MBEDTLS_FS_IO */
134 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
135 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
136 #include MBEDTLS_PLATFORM_STD_MEM_HDR
137 #endif
138 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
139 
140 
141 /* \} name SECTION: Module settings */
142 
143 /*
144  * The function pointers for calloc and free.
145  */
146 #if defined(MBEDTLS_PLATFORM_MEMORY)
147 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
148  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
149 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
150 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
151 #else
152 /* For size_t */
153 #include <stddef.h>
154 extern void *mbedtls_calloc( size_t n, size_t size );
155 extern void mbedtls_free( void *ptr );
156 
166 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
167  void (*free_func)( void * ) );
168 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
169 #else /* !MBEDTLS_PLATFORM_MEMORY */
170 #define mbedtls_free free
171 #define mbedtls_calloc calloc
172 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
173 
174 /*
175  * The function pointers for fprintf
176  */
177 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
178 /* We need FILE * */
179 #include <stdio.h>
180 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
181 
191 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
192  ... ) );
193 #else
194 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
195 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
196 #else
197 #define mbedtls_fprintf fprintf
198 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
199 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
200 
201 /*
202  * The function pointers for printf
203  */
204 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
205 extern int (*mbedtls_printf)( const char *format, ... );
206 
216 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
217 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
218 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
219 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
220 #else
221 #define mbedtls_printf printf
222 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
223 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
224 
225 /*
226  * The function pointers for snprintf
227  *
228  * The snprintf implementation should conform to C99:
229  * - it *must* always correctly zero-terminate the buffer
230  * (except when n == 0, then it must leave the buffer untouched)
231  * - however it is acceptable to return -1 instead of the required length when
232  * the destination buffer is too short.
233  */
234 #if defined(_WIN32)
235 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
236 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
237 #endif
238 
239 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
240 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
241 
250 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
251  const char * format, ... ) );
252 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
253 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
254 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
255 #else
256 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
257 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
258 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
259 
260 /*
261  * The function pointers for exit
262  */
263 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
264 extern void (*mbedtls_exit)( int status );
265 
275 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
276 #else
277 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
278 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
279 #else
280 #define mbedtls_exit exit
281 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
282 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
283 
284 /*
285  * The default exit values
286  */
287 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
288 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
289 #else
290 #define MBEDTLS_EXIT_SUCCESS 0
291 #endif
292 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
293 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
294 #else
295 #define MBEDTLS_EXIT_FAILURE 1
296 #endif
297 
298 /*
299  * The function pointers for reading from and writing a seed file to
300  * Non-Volatile storage (NV) in a platform-independent way
301  *
302  * Only enabled when the NV seed entropy source is enabled
303  */
304 #if defined(MBEDTLS_ENTROPY_NV_SEED)
305 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
306 /* Internal standard platform definitions */
307 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
308 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
309 #endif
310 
311 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
312 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
313 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
314 
324 int mbedtls_platform_set_nv_seed(
325  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
326  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
327  );
328 #else
329 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
330  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
331 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
332 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
333 #else
334 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
335 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
336 #endif
337 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
338 #endif /* MBEDTLS_ENTROPY_NV_SEED */
339 
340 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
341 
349 {
350  char dummy;
351 }
353 
354 #else
355 #include "platform_alt.h"
356 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
357 
389 
390 #ifdef __cplusplus
391 }
392 #endif
393 
394 #endif /* platform.h */
mbedtls_platform_setup
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform-specific initialization operations.
mbedtls_printf
#define mbedtls_printf
Definition: platform.h:221
mbedtls_platform_teardown
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
mbedtls_exit
#define mbedtls_exit
Definition: platform.h:280
mbedtls_snprintf
#define mbedtls_snprintf
Definition: platform.h:256
mbedtls_platform_context
The platform context structure.
Definition: platform.h:349
mbedtls_platform_context::dummy
char dummy
Definition: platform.h:350
platform_time.h
mbed TLS Platform time abstraction
mbedtls_free
#define mbedtls_free
Definition: platform.h:170
mbedtls_calloc
#define mbedtls_calloc
Definition: platform.h:171
config.h
Configuration options (set of defines)
mbedtls_platform_context
struct mbedtls_platform_context mbedtls_platform_context
The platform context structure.
mbedtls_fprintf
#define mbedtls_fprintf
Definition: platform.h:197