4 * \brief mbed TLS Platform abstraction layer
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: GPL-2.0
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * This file is part of mbed TLS (https://tls.mbed.org)
25 #ifndef MBEDTLS_PLATFORM_H
26 #define MBEDTLS_PLATFORM_H
28 #if !defined(MBEDTLS_CONFIG_FILE)
31 #include MBEDTLS_CONFIG_FILE
39 * \name SECTION: Module settings
41 * The configuration options you can set for this module are in this section.
42 * Either change them in config.h or define them on the compiler command line.
46 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
50 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
52 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
54 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
57 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
58 #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
60 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
61 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
63 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
64 #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
66 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
67 #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
69 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
70 #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
72 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
73 #define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
75 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
76 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
78 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
79 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
81 #if defined(MBEDTLS_FS_IO)
82 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
83 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
85 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
86 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
88 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
89 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
91 #endif /* MBEDTLS_FS_IO */
92 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
93 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
94 #include MBEDTLS_PLATFORM_STD_MEM_HDR
96 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
99 /* \} name SECTION: Module settings */
102 * The function pointers for calloc and free
104 #if defined(MBEDTLS_PLATFORM_MEMORY)
105 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
106 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
107 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
108 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
112 extern void * (*mbedtls_calloc
)( size_t n
, size_t size
);
113 extern void (*mbedtls_free
)( void *ptr
);
116 * \brief Set your own memory implementation function pointers
118 * \param calloc_func the calloc function implementation
119 * \param free_func the free function implementation
121 * \return 0 if successful
123 int mbedtls_platform_set_calloc_free( void * (*calloc_func
)( size_t, size_t ),
124 void (*free_func
)( void * ) );
125 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
126 #else /* !MBEDTLS_PLATFORM_MEMORY */
127 #define mbedtls_free free
128 #define mbedtls_calloc calloc
129 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
132 * The function pointers for fprintf
134 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
137 extern int (*mbedtls_fprintf
)( FILE *stream
, const char *format
, ... );
140 * \brief Set your own fprintf function pointer
142 * \param fprintf_func the fprintf function implementation
146 int mbedtls_platform_set_fprintf( int (*fprintf_func
)( FILE *stream
, const char *,
149 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
150 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
152 #define mbedtls_fprintf fprintf
153 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
154 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
157 * The function pointers for printf
159 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
160 extern int (*mbedtls_printf
)( const char *format
, ... );
163 * \brief Set your own printf function pointer
165 * \param printf_func the printf function implementation
169 int mbedtls_platform_set_printf( int (*printf_func
)( const char *, ... ) );
170 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
171 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
172 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
174 #define mbedtls_printf printf
175 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
176 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
179 * The function pointers for snprintf
181 * The snprintf implementation should conform to C99:
182 * - it *must* always correctly zero-terminate the buffer
183 * (except when n == 0, then it must leave the buffer untouched)
184 * - however it is acceptable to return -1 instead of the required length when
185 * the destination buffer is too short.
188 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
189 int mbedtls_platform_win32_snprintf( char *s
, size_t n
, const char *fmt
, ... );
192 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
193 extern int (*mbedtls_snprintf
)( char * s
, size_t n
, const char * format
, ... );
196 * \brief Set your own snprintf function pointer
198 * \param snprintf_func the snprintf function implementation
202 int mbedtls_platform_set_snprintf( int (*snprintf_func
)( char * s
, size_t n
,
203 const char * format
, ... ) );
204 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
205 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
206 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
208 #define mbedtls_snprintf snprintf
209 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
210 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
213 * The function pointers for exit
215 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
216 extern void (*mbedtls_exit
)( int status
);
219 * \brief Set your own exit function pointer
221 * \param exit_func the exit function implementation
225 int mbedtls_platform_set_exit( void (*exit_func
)( int status
) );
227 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
228 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
230 #define mbedtls_exit exit
231 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
232 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
235 * The default exit values
237 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
238 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
240 #define MBEDTLS_EXIT_SUCCESS 0
242 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
243 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
245 #define MBEDTLS_EXIT_FAILURE 1
249 * The time_t datatype
251 #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
252 typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t
;
256 typedef time_t mbedtls_time_t
;
257 #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
260 * The function pointers for time
262 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
263 extern mbedtls_time_t (*mbedtls_time
)( mbedtls_time_t
* time
);
266 * \brief Set your own time function pointer
268 * \param time_func the time function implementation
272 int mbedtls_platform_set_time( mbedtls_time_t (*time_func
)( mbedtls_time_t
* time
) );
274 #if defined(MBEDTLS_PLATFORM_TIME_MACRO)
275 #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO
277 #define mbedtls_time time
278 #endif /* MBEDTLS_PLATFORM_TIME_MACRO */
279 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
282 * The function pointers for reading from and writing a seed file to
283 * Non-Volatile storage (NV) in a platform-independent way
285 * Only enabled when the NV seed entropy source is enabled
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
);
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
);
299 * \brief Set your own seed file writing/reading functions
301 * \param nv_seed_read_func the seed reading function implementation
302 * \param nv_seed_write_func the seed writing function implementation
306 int mbedtls_platform_set_nv_seed(
307 int (*nv_seed_read_func
)( unsigned char *buf
, size_t buf_len
),
308 int (*nv_seed_write_func
)( unsigned char *buf
, size_t buf_len
)
311 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
312 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
313 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
314 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
316 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
317 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
319 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
320 #endif /* MBEDTLS_ENTROPY_NV_SEED */
326 #endif /* platform.h */