1 /**
2  * \file platform.h
3  *
4  * \brief mbed TLS Platform abstraction layer
5  *
6  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7  *  SPDX-License-Identifier: Apache-2.0
8  *
9  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
10  *  not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *  http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_PLATFORM_H
24 #define MBEDTLS_PLATFORM_H
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #if defined(MBEDTLS_HAVE_TIME)
33 #include "mbedtls/platform_time.h"
34 #endif
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * \name SECTION: Module settings
42  *
43  * The configuration options you can set for this module are in this section.
44  * Either change them in config.h or define them on the compiler command line.
45  * \{
46  */
47 
48 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <time.h>
52 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
53 #if defined(_WIN32)
54 #define MBEDTLS_PLATFORM_STD_SNPRINTF   mbedtls_platform_win32_snprintf /**< Default snprintf to use  */
55 #else
56 #define MBEDTLS_PLATFORM_STD_SNPRINTF   snprintf /**< Default snprintf to use  */
57 #endif
58 #endif
59 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
60 #define MBEDTLS_PLATFORM_STD_PRINTF   printf /**< Default printf to use  */
61 #endif
62 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
63 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
64 #endif
65 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
66 #define MBEDTLS_PLATFORM_STD_CALLOC   calloc /**< Default allocator to use */
67 #endif
68 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
69 #define MBEDTLS_PLATFORM_STD_FREE       free /**< Default free to use */
70 #endif
71 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
72 #define MBEDTLS_PLATFORM_STD_EXIT      exit /**< Default exit to use */
73 #endif
74 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
75 #define MBEDTLS_PLATFORM_STD_TIME       time    /**< Default time to use */
76 #endif
77 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
78 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS /**< Default exit value to use */
79 #endif
80 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
81 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE /**< Default exit value to use */
82 #endif
83 #if defined(MBEDTLS_FS_IO)
84 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
85 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ   mbedtls_platform_std_nv_seed_read
86 #endif
87 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
88 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE  mbedtls_platform_std_nv_seed_write
89 #endif
90 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
91 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE   "seedfile"
92 #endif
93 #endif /* MBEDTLS_FS_IO */
94 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
95 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
96 #include MBEDTLS_PLATFORM_STD_MEM_HDR
97 #endif
98 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
99 
100 
101 /* \} name SECTION: Module settings */
102 
103 /*
104  * The function pointers for calloc and free
105  */
106 #if defined(MBEDTLS_PLATFORM_MEMORY)
107 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
108     defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
109 #define mbedtls_free       MBEDTLS_PLATFORM_FREE_MACRO
110 #define mbedtls_calloc     MBEDTLS_PLATFORM_CALLOC_MACRO
111 #else
112 /* For size_t */
113 #include <stddef.h>
114 extern void * (*mbedtls_calloc)( size_t n, size_t size );
115 extern void (*mbedtls_free)( void *ptr );
116 
117 /**
118  * \brief   Set your own memory implementation function pointers
119  *
120  * \param calloc_func   the calloc function implementation
121  * \param free_func     the free function implementation
122  *
123  * \return              0 if successful
124  */
125 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
126                               void (*free_func)( void * ) );
127 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
128 #else /* !MBEDTLS_PLATFORM_MEMORY */
129 #define mbedtls_free       free
130 #define mbedtls_calloc     calloc
131 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
132 
133 /*
134  * The function pointers for fprintf
135  */
136 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
137 /* We need FILE * */
138 #include <stdio.h>
139 extern int (*mbedtls_fprintf)( FILE *stream, const char *format, ... );
140 
141 /**
142  * \brief   Set your own fprintf function pointer
143  *
144  * \param fprintf_func   the fprintf function implementation
145  *
146  * \return              0
147  */
148 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
149                                                ... ) );
150 #else
151 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
152 #define mbedtls_fprintf    MBEDTLS_PLATFORM_FPRINTF_MACRO
153 #else
154 #define mbedtls_fprintf    fprintf
155 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
156 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
157 
158 /*
159  * The function pointers for printf
160  */
161 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
162 extern int (*mbedtls_printf)( const char *format, ... );
163 
164 /**
165  * \brief   Set your own printf function pointer
166  *
167  * \param printf_func   the printf function implementation
168  *
169  * \return              0
170  */
171 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) );
172 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
173 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
174 #define mbedtls_printf     MBEDTLS_PLATFORM_PRINTF_MACRO
175 #else
176 #define mbedtls_printf     printf
177 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
178 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
179 
180 /*
181  * The function pointers for snprintf
182  *
183  * The snprintf implementation should conform to C99:
184  * - it *must* always correctly zero-terminate the buffer
185  *   (except when n == 0, then it must leave the buffer untouched)
186  * - however it is acceptable to return -1 instead of the required length when
187  *   the destination buffer is too short.
188  */
189 #if defined(_WIN32)
190 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
191 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... );
192 #endif
193 
194 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
195 extern int (*mbedtls_snprintf)( char * s, size_t n, const char * format, ... );
196 
197 /**
198  * \brief   Set your own snprintf function pointer
199  *
200  * \param snprintf_func   the snprintf function implementation
201  *
202  * \return              0
203  */
204 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
205                                                  const char * format, ... ) );
206 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
207 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
208 #define mbedtls_snprintf   MBEDTLS_PLATFORM_SNPRINTF_MACRO
209 #else
210 #define mbedtls_snprintf   snprintf
211 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
212 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
213 
214 /*
215  * The function pointers for exit
216  */
217 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
218 extern void (*mbedtls_exit)( int status );
219 
220 /**
221  * \brief   Set your own exit function pointer
222  *
223  * \param exit_func   the exit function implementation
224  *
225  * \return              0
226  */
227 int mbedtls_platform_set_exit( void (*exit_func)( int status ) );
228 #else
229 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
230 #define mbedtls_exit   MBEDTLS_PLATFORM_EXIT_MACRO
231 #else
232 #define mbedtls_exit   exit
233 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
234 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
235 
236 /*
237  * The default exit values
238  */
239 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
240 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
241 #else
242 #define MBEDTLS_EXIT_SUCCESS 0
243 #endif
244 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
245 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
246 #else
247 #define MBEDTLS_EXIT_FAILURE 1
248 #endif
249 
250 /*
251  * The function pointers for reading from and writing a seed file to
252  * Non-Volatile storage (NV) in a platform-independent way
253  *
254  * Only enabled when the NV seed entropy source is enabled
255  */
256 #if defined(MBEDTLS_ENTROPY_NV_SEED)
257 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
258 /* Internal standard platform definitions */
259 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len );
260 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len );
261 #endif
262 
263 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
264 extern int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len );
265 extern int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len );
266 
267 /**
268  * \brief   Set your own seed file writing/reading functions
269  *
270  * \param   nv_seed_read_func   the seed reading function implementation
271  * \param   nv_seed_write_func  the seed writing function implementation
272  *
273  * \return              0
274  */
275 int mbedtls_platform_set_nv_seed(
276             int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
277             int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len )
278             );
279 #else
280 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
281     defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
282 #define mbedtls_nv_seed_read    MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
283 #define mbedtls_nv_seed_write   MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
284 #else
285 #define mbedtls_nv_seed_read    mbedtls_platform_std_nv_seed_read
286 #define mbedtls_nv_seed_write   mbedtls_platform_std_nv_seed_write
287 #endif
288 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
289 #endif /* MBEDTLS_ENTROPY_NV_SEED */
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 #endif /* platform.h */
296