4 * \brief mbed TLS Platform abstraction layer
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * This file is part of mbed TLS (https://tls.mbed.org)
23 #ifndef MBEDTLS_PLATFORM_H
24 #define MBEDTLS_PLATFORM_H
26 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include MBEDTLS_CONFIG_FILE
37 * \name SECTION: Module settings
39 * The configuration options you can set for this module are in this section.
40 * Either change them in config.h or define them on the compiler command line.
44 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS)
48 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
50 #define MBEDTLS_PLATFORM_STD_SNPRINTF mbedtls_platform_win32_snprintf /**< Default snprintf to use */
52 #define MBEDTLS_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
55 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
56 #define MBEDTLS_PLATFORM_STD_PRINTF printf /**< Default printf to use */
58 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
59 #define MBEDTLS_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
61 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
62 #define MBEDTLS_PLATFORM_STD_CALLOC calloc /**< Default allocator to use */
64 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
65 #define MBEDTLS_PLATFORM_STD_FREE free /**< Default free to use */
67 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
68 #define MBEDTLS_PLATFORM_STD_EXIT exit /**< Default exit to use */
70 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
71 #define MBEDTLS_PLATFORM_STD_TIME time /**< Default time to use */
73 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
74 #define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS EXIT_SUCCESS /**< Default exit value to use */
76 #if !defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
77 #define MBEDTLS_PLATFORM_STD_EXIT_FAILURE EXIT_FAILURE /**< Default exit value to use */
79 #if defined(MBEDTLS_FS_IO)
80 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
81 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ mbedtls_platform_std_nv_seed_read
83 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
84 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE mbedtls_platform_std_nv_seed_write
86 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_FILE)
87 #define MBEDTLS_PLATFORM_STD_NV_SEED_FILE "seedfile"
89 #endif /* MBEDTLS_FS_IO */
90 #else /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
91 #if defined(MBEDTLS_PLATFORM_STD_MEM_HDR)
92 #include MBEDTLS_PLATFORM_STD_MEM_HDR
94 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
97 /* \} name SECTION: Module settings */
100 * The function pointers for calloc and free
102 #if defined(MBEDTLS_PLATFORM_MEMORY)
103 #if defined(MBEDTLS_PLATFORM_FREE_MACRO) && \
104 defined(MBEDTLS_PLATFORM_CALLOC_MACRO)
105 #define mbedtls_free MBEDTLS_PLATFORM_FREE_MACRO
106 #define mbedtls_calloc MBEDTLS_PLATFORM_CALLOC_MACRO
110 extern void * (*mbedtls_calloc
)( size_t n
, size_t size
);
111 extern void (*mbedtls_free
)( void *ptr
);
114 * \brief Set your own memory implementation function pointers
116 * \param calloc_func the calloc function implementation
117 * \param free_func the free function implementation
119 * \return 0 if successful
121 int mbedtls_platform_set_calloc_free( void * (*calloc_func
)( size_t, size_t ),
122 void (*free_func
)( void * ) );
123 #endif /* MBEDTLS_PLATFORM_FREE_MACRO && MBEDTLS_PLATFORM_CALLOC_MACRO */
124 #else /* !MBEDTLS_PLATFORM_MEMORY */
125 #define mbedtls_free free
126 #define mbedtls_calloc calloc
127 #endif /* MBEDTLS_PLATFORM_MEMORY && !MBEDTLS_PLATFORM_{FREE,CALLOC}_MACRO */
130 * The function pointers for fprintf
132 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
135 extern int (*mbedtls_fprintf
)( FILE *stream
, const char *format
, ... );
138 * \brief Set your own fprintf function pointer
140 * \param fprintf_func the fprintf function implementation
144 int mbedtls_platform_set_fprintf( int (*fprintf_func
)( FILE *stream
, const char *,
147 #if defined(MBEDTLS_PLATFORM_FPRINTF_MACRO)
148 #define mbedtls_fprintf MBEDTLS_PLATFORM_FPRINTF_MACRO
150 #define mbedtls_fprintf fprintf
151 #endif /* MBEDTLS_PLATFORM_FPRINTF_MACRO */
152 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
155 * The function pointers for printf
157 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
158 extern int (*mbedtls_printf
)( const char *format
, ... );
161 * \brief Set your own printf function pointer
163 * \param printf_func the printf function implementation
167 int mbedtls_platform_set_printf( int (*printf_func
)( const char *, ... ) );
168 #else /* !MBEDTLS_PLATFORM_PRINTF_ALT */
169 #if defined(MBEDTLS_PLATFORM_PRINTF_MACRO)
170 #define mbedtls_printf MBEDTLS_PLATFORM_PRINTF_MACRO
172 #define mbedtls_printf printf
173 #endif /* MBEDTLS_PLATFORM_PRINTF_MACRO */
174 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
177 * The function pointers for snprintf
179 * The snprintf implementation should conform to C99:
180 * - it *must* always correctly zero-terminate the buffer
181 * (except when n == 0, then it must leave the buffer untouched)
182 * - however it is acceptable to return -1 instead of the required length when
183 * the destination buffer is too short.
186 /* For Windows (inc. MSYS2), we provide our own fixed implementation */
187 int mbedtls_platform_win32_snprintf( char *s
, size_t n
, const char *fmt
, ... );
190 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
191 extern int (*mbedtls_snprintf
)( char * s
, size_t n
, const char * format
, ... );
194 * \brief Set your own snprintf function pointer
196 * \param snprintf_func the snprintf function implementation
200 int mbedtls_platform_set_snprintf( int (*snprintf_func
)( char * s
, size_t n
,
201 const char * format
, ... ) );
202 #else /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
203 #if defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO)
204 #define mbedtls_snprintf MBEDTLS_PLATFORM_SNPRINTF_MACRO
206 #define mbedtls_snprintf snprintf
207 #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */
208 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
211 * The function pointers for exit
213 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
214 extern void (*mbedtls_exit
)( int status
);
217 * \brief Set your own exit function pointer
219 * \param exit_func the exit function implementation
223 int mbedtls_platform_set_exit( void (*exit_func
)( int status
) );
225 #if defined(MBEDTLS_PLATFORM_EXIT_MACRO)
226 #define mbedtls_exit MBEDTLS_PLATFORM_EXIT_MACRO
228 #define mbedtls_exit exit
229 #endif /* MBEDTLS_PLATFORM_EXIT_MACRO */
230 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
233 * The default exit values
235 #if defined(MBEDTLS_PLATFORM_STD_EXIT_SUCCESS)
236 #define MBEDTLS_EXIT_SUCCESS MBEDTLS_PLATFORM_STD_EXIT_SUCCESS
238 #define MBEDTLS_EXIT_SUCCESS 0
240 #if defined(MBEDTLS_PLATFORM_STD_EXIT_FAILURE)
241 #define MBEDTLS_EXIT_FAILURE MBEDTLS_PLATFORM_STD_EXIT_FAILURE
243 #define MBEDTLS_EXIT_FAILURE 1
247 * The time_t datatype
249 #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO)
250 typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t
;
254 typedef time_t mbedtls_time_t
;
255 #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */
258 * The function pointers for time
260 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
261 extern mbedtls_time_t (*mbedtls_time
)( mbedtls_time_t
* time
);
264 * \brief Set your own time function pointer
266 * \param time_func the time function implementation
270 int mbedtls_platform_set_time( mbedtls_time_t (*time_func
)( mbedtls_time_t
* time
) );
272 #if defined(MBEDTLS_PLATFORM_TIME_MACRO)
273 #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO
275 #define mbedtls_time time
276 #endif /* MBEDTLS_PLATFORM_TIME_MACRO */
277 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
280 * The function pointers for reading from and writing a seed file to
281 * Non-Volatile storage (NV) in a platform-independent way
283 * Only enabled when the NV seed entropy source is enabled
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
);
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
);
297 * \brief Set your own seed file writing/reading functions
299 * \param nv_seed_read_func the seed reading function implementation
300 * \param nv_seed_write_func the seed writing function implementation
304 int mbedtls_platform_set_nv_seed(
305 int (*nv_seed_read_func
)( unsigned char *buf
, size_t buf_len
),
306 int (*nv_seed_write_func
)( unsigned char *buf
, size_t buf_len
)
309 #if defined(MBEDTLS_PLATFORM_NV_SEED_READ_MACRO) && \
310 defined(MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO)
311 #define mbedtls_nv_seed_read MBEDTLS_PLATFORM_NV_SEED_READ_MACRO
312 #define mbedtls_nv_seed_write MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO
314 #define mbedtls_nv_seed_read mbedtls_platform_std_nv_seed_read
315 #define mbedtls_nv_seed_write mbedtls_platform_std_nv_seed_write
317 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
318 #endif /* MBEDTLS_ENTROPY_NV_SEED */
324 #endif /* platform.h */