mbed TLS v2.7.6
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
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License"); you may
11  * not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * This file is part of Mbed TLS (https://tls.mbed.org)
23  */
24 #ifndef MBEDTLS_PLATFORM_H
25 #define MBEDTLS_PLATFORM_H
26 
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include MBEDTLS_CONFIG_FILE
31 #endif
32 
33 #if defined(MBEDTLS_HAVE_TIME)
34 #include "platform_time.h"
35 #endif
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
49 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <time.h>
53 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
54 #if defined(_WIN32)
55 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf
56 #else
57 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf
58 #endif
59 #endif
60 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
61 #define MBEDTLS_PLATFORM_STD_PRINTF printf
62 #endif
63 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
64 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf
65 #endif
66 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
67 #define MBEDTLS_PLATFORM_STD_CALLOC calloc
68 #endif
69 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
70 #define MBEDTLS_PLATFORM_STD_FREE free
71 #endif
72 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
73 #define MBEDTLS_PLATFORM_STD_EXIT exit
74 #endif
75 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
76 #define MBEDTLS_PLATFORM_STD_TIME time
77 #endif
78 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
79 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS
80 #endif
81 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
82 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE
83 #endif
84 #if defined(MBEDTLS_FS_IO)
85 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
86 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
87 #endif
88 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
89 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
90 #endif
91 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
92 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
93 #endif
94 #endif /* MBEDTLS_FS_IO */
95 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
96 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
97 #include MBEDTLS_PLATFORM_STD_MEM_HDR
98 #endif
99 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
100 
101 
102 /* \} name SECTION: Module settings */
103 
104 /*
105  * The function pointers for calloc and free
106  */
107 #if defined(MBEDTLS_PLATFORM_MEMORY)
108 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
109  defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
110 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
111 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
112 #else
113 /* For size_t */
114 #include <stddef.h>
115 extern void * (*mbedtls_calloc)( size_t n, size_t size );
116 extern void (*mbedtls_free)( void *ptr );
117 
126 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
127  void (*free_func)( void * ) );
128 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
129 #else /* !MBEDTLS_PLATFORM_MEMORY */
130 #define mbedtls_free free
131 #define mbedtls_calloc calloc
132 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
133 
134 /*
135  * The function pointers for fprintf
136  */
137 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
138 /* We need FILE * */
139 #include <stdio.h>
140 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
141 
149 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
150  ... ) );
151 #else
152 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
153 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
154 #else
155 #define mbedtls_fprintf fprintf
156 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
157 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
158 
159 /*
160  * The function pointers for printf
161  */
162 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
163 extern int (*mbedtls_printf)( const char *format, ... );
164 
173 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
174 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
175 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
176 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
177 #else
178 #define mbedtls_printf printf
179 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
180 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
181 
182 /*
183  * The function pointers for snprintf
184  *
185  * The snprintf implementation should conform to C99:
186  * - it *must* always correctly zero-terminate the buffer
187  * (except when n == 0, then it must leave the buffer untouched)
188  * - however it is acceptable to return -1 instead of the required length when
189  * the destination buffer is too short.
190  */
191 #if defined(_WIN32)
192 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
193 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
194 #endif
195 
196 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
197 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
198 
207 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
208  const char * format, ... ) );
209 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
210 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
211 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
212 #else
213 #define mbedtls_snprintf MBEDTLS_PLATFORM_STD_SNPRINTF
214 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
215 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
216 
217 /*
218  * The function pointers for exit
219  */
220 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
221 extern void (*mbedtls_exit)( int status );
222 
231 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
232 #else
233 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
234 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
235 #else
236 #define mbedtls_exit exit
237 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
238 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
239 
240 /*
241  * The default exit values
242  */
243 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
244 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
245 #else
246 #define MBEDTLS_EXIT_SUCCESS 0
247 #endif
248 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
249 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
250 #else
251 #define MBEDTLS_EXIT_FAILURE 1
252 #endif
253 
254 /*
255  * The function pointers for reading from and writing a seed file to
256  * Non-Volatile storage (NV) in a platform-independent way
257  *
258  * Only enabled when the NV seed entropy source is enabled
259  */
260 #if defined(MBEDTLS_ENTROPY_NV_SEED)
261 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
262 /* Internal standard platform definitions */
263 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
264 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
265 #endif
266 
267 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
268 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
269 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
270 
280 int mbedtls_platform_set_nv_seed(
281  int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
282  int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
283  );
284 #else
285 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
286  defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
287 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
288 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
289 #else
290 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
291 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
292 #endif
293 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
294 #endif /* MBEDTLS_ENTROPY_NV_SEED */
295 
296 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
297 
304 typedef struct {
305  char dummy;
306 }
308 
309 #else
310 #include "platform_alt.h"
311 #endif /* !MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
312 
343 
344 #ifdef __cplusplus
345 }
346 #endif
347 
348 #endif /* platform.h */
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
This function performs any platform teardown operations.
#define mbedtls_free
Definition: platform.h:130
Configuration options (set of defines)
#define mbedtls_fprintf
Definition: platform.h:155
The platform context structure.
Definition: platform.h:304
#define mbedtls_exit
Definition: platform.h:236
#define mbedtls_snprintf
Definition: platform.h:213
#define mbedtls_printf
Definition: platform.h:178
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
This function performs any platform initialization operations.
mbed TLS Platform time abstraction