2 * Platform abstraction layer
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #if defined(MBEDTLS_PLATFORM_C)
24 #include "mbedtls/platform.h"
25 #include "mbedtls/platform_util.h"
26 #include "mbedtls/error.h"
28 /* The compile time configuration of memory allocation via the macros
29 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
30 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
31 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
32 #if defined(MBEDTLS_PLATFORM_MEMORY) && \
33 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
34 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
36 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
37 static void *platform_calloc_uninit(size_t n
, size_t size
) {
43 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
44 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
46 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
47 static void platform_free_uninit(void *ptr
) {
51 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
52 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
54 static void *(*mbedtls_calloc_func
)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC
;
55 static void (*mbedtls_free_func
)(void *) = MBEDTLS_PLATFORM_STD_FREE
;
57 void *mbedtls_calloc(size_t nmemb
, size_t size
) {
58 return (*mbedtls_calloc_func
)(nmemb
, size
);
61 void mbedtls_free(void *ptr
) {
62 (*mbedtls_free_func
)(ptr
);
65 int mbedtls_platform_set_calloc_free(void *(*calloc_func
)(size_t, size_t),
66 void (*free_func
)(void *)) {
67 mbedtls_calloc_func
= calloc_func
;
68 mbedtls_free_func
= free_func
;
71 #endif /* MBEDTLS_PLATFORM_MEMORY &&
72 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
73 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
75 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
77 int mbedtls_platform_win32_snprintf(char *s
, size_t n
, const char *fmt
, ...) {
78 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
82 ret
= mbedtls_vsnprintf(s
, n
, fmt
, argp
);
89 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
90 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
92 * Make dummy function to prevent NULL pointer dereferences
94 static int platform_snprintf_uninit(char *s
, size_t n
,
95 const char *format
, ...) {
102 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
103 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
105 int (*mbedtls_snprintf
)(char *s
, size_t n
,
107 ...) = MBEDTLS_PLATFORM_STD_SNPRINTF
;
109 int mbedtls_platform_set_snprintf(int (*snprintf_func
)(char *s
, size_t n
,
112 mbedtls_snprintf
= snprintf_func
;
115 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
117 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
119 int mbedtls_platform_win32_vsnprintf(char *s
, size_t n
, const char *fmt
, va_list arg
) {
120 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
122 /* Avoid calling the invalid parameter handler by checking ourselves */
123 if (s
== NULL
|| n
== 0 || fmt
== NULL
)
126 #if defined(_TRUNCATE)
127 ret
= vsnprintf_s(s
, n
, _TRUNCATE
, fmt
, arg
);
129 ret
= vsnprintf(s
, n
, fmt
, arg
);
130 if (ret
< 0 || (size_t) ret
== n
) {
140 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
141 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
143 * Make dummy function to prevent NULL pointer dereferences
145 static int platform_vsnprintf_uninit(char *s
, size_t n
,
146 const char *format
, va_list arg
) {
154 #define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
155 #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
157 int (*mbedtls_vsnprintf
)(char *s
, size_t n
,
159 va_list arg
) = MBEDTLS_PLATFORM_STD_VSNPRINTF
;
161 int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func
)(char *s
, size_t n
,
164 mbedtls_vsnprintf
= vsnprintf_func
;
167 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
169 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
170 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
172 * Make dummy function to prevent NULL pointer dereferences
174 static int platform_printf_uninit(const char *format
, ...) {
179 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
180 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
182 int (*mbedtls_printf
)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF
;
184 int mbedtls_platform_set_printf(int (*printf_func
)(const char *, ...)) {
185 mbedtls_printf
= printf_func
;
188 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
190 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
191 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
193 * Make dummy function to prevent NULL pointer dereferences
195 static int platform_fprintf_uninit(FILE *stream
, const char *format
, ...) {
201 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
202 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
204 int (*mbedtls_fprintf
)(FILE *, const char *, ...) =
205 MBEDTLS_PLATFORM_STD_FPRINTF
;
207 int mbedtls_platform_set_fprintf(int (*fprintf_func
)(FILE *, const char *, ...)) {
208 mbedtls_fprintf
= fprintf_func
;
211 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
213 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
214 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
216 * Make dummy function to prevent NULL pointer dereferences
218 static void platform_exit_uninit(int status
) {
222 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
223 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
225 void (*mbedtls_exit
)(int status
) = MBEDTLS_PLATFORM_STD_EXIT
;
227 int mbedtls_platform_set_exit(void (*exit_func
)(int status
)) {
228 mbedtls_exit
= exit_func
;
231 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
233 #if defined(MBEDTLS_HAVE_TIME)
235 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
236 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
238 * Make dummy function to prevent NULL pointer dereferences
240 static mbedtls_time_t
platform_time_uninit(mbedtls_time_t
*timer
) {
245 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
246 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
248 mbedtls_time_t (*mbedtls_time
)(mbedtls_time_t
*timer
) = MBEDTLS_PLATFORM_STD_TIME
;
250 int mbedtls_platform_set_time(mbedtls_time_t (*time_func
)(mbedtls_time_t
*timer
)) {
251 mbedtls_time
= time_func
;
254 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
256 #endif /* MBEDTLS_HAVE_TIME */
258 #if defined(MBEDTLS_ENTROPY_NV_SEED)
259 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
260 /* Default implementations for the platform independent seed functions use
261 * standard libc file functions to read from and write to a pre-defined filename
263 int mbedtls_platform_std_nv_seed_read(unsigned char *buf
, size_t buf_len
) {
267 if ((file
= fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "rb")) == NULL
)
270 if ((n
= fread(buf
, 1, buf_len
, file
)) != buf_len
) {
272 mbedtls_platform_zeroize(buf
, buf_len
);
280 int mbedtls_platform_std_nv_seed_write(unsigned char *buf
, size_t buf_len
) {
284 if ((file
= fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE
, "w")) == NULL
)
287 if ((n
= fwrite(buf
, 1, buf_len
, file
)) != buf_len
) {
295 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
297 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
298 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
300 * Make dummy function to prevent NULL pointer dereferences
302 static int platform_nv_seed_read_uninit(unsigned char *buf
, size_t buf_len
) {
308 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
309 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
311 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
313 * Make dummy function to prevent NULL pointer dereferences
315 static int platform_nv_seed_write_uninit(unsigned char *buf
, size_t buf_len
) {
321 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
322 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
324 int (*mbedtls_nv_seed_read
)(unsigned char *buf
, size_t buf_len
) =
325 MBEDTLS_PLATFORM_STD_NV_SEED_READ
;
326 int (*mbedtls_nv_seed_write
)(unsigned char *buf
, size_t buf_len
) =
327 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE
;
329 int mbedtls_platform_set_nv_seed(
330 int (*nv_seed_read_func
)(unsigned char *buf
, size_t buf_len
),
331 int (*nv_seed_write_func
)(unsigned char *buf
, size_t buf_len
)) {
332 mbedtls_nv_seed_read
= nv_seed_read_func
;
333 mbedtls_nv_seed_write
= nv_seed_write_func
;
336 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
337 #endif /* MBEDTLS_ENTROPY_NV_SEED */
339 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
341 * Placeholder platform setup that does nothing by default
343 int mbedtls_platform_setup(mbedtls_platform_context
*ctx
) {
350 * Placeholder platform teardown that does nothing by default
352 void mbedtls_platform_teardown(mbedtls_platform_context
*ctx
) {
355 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
357 #endif /* MBEDTLS_PLATFORM_C */