1a8e1175bSopenharmony_ci/*
2a8e1175bSopenharmony_ci *  Platform abstraction layer
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
5a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6a8e1175bSopenharmony_ci */
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ci#include <stdio.h>
9a8e1175bSopenharmony_ci#include <stdlib.h>
10a8e1175bSopenharmony_ci
11a8e1175bSopenharmony_ci#include "common.h"
12a8e1175bSopenharmony_ci
13a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_C)
14a8e1175bSopenharmony_ci
15a8e1175bSopenharmony_ci#include "mbedtls/platform.h"
16a8e1175bSopenharmony_ci#include "mbedtls/platform_util.h"
17a8e1175bSopenharmony_ci#include "mbedtls/error.h"
18a8e1175bSopenharmony_ci
19a8e1175bSopenharmony_ci/* The compile time configuration of memory allocation via the macros
20a8e1175bSopenharmony_ci * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
21a8e1175bSopenharmony_ci * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
22a8e1175bSopenharmony_ci * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
23a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_MEMORY) &&                 \
24a8e1175bSopenharmony_ci    !(defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&        \
25a8e1175bSopenharmony_ci    defined(MBEDTLS_PLATFORM_FREE_MACRO))
26a8e1175bSopenharmony_ci
27a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
28a8e1175bSopenharmony_cistatic void *platform_calloc_uninit(size_t n, size_t size)
29a8e1175bSopenharmony_ci{
30a8e1175bSopenharmony_ci    ((void) n);
31a8e1175bSopenharmony_ci    ((void) size);
32a8e1175bSopenharmony_ci    return NULL;
33a8e1175bSopenharmony_ci}
34a8e1175bSopenharmony_ci
35a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_CALLOC   platform_calloc_uninit
36a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_FREE)
39a8e1175bSopenharmony_cistatic void platform_free_uninit(void *ptr)
40a8e1175bSopenharmony_ci{
41a8e1175bSopenharmony_ci    ((void) ptr);
42a8e1175bSopenharmony_ci}
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_FREE     platform_free_uninit
45a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_FREE */
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_cistatic void * (*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
48a8e1175bSopenharmony_cistatic void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_civoid *mbedtls_calloc(size_t nmemb, size_t size)
51a8e1175bSopenharmony_ci{
52a8e1175bSopenharmony_ci    return (*mbedtls_calloc_func)(nmemb, size);
53a8e1175bSopenharmony_ci}
54a8e1175bSopenharmony_ci
55a8e1175bSopenharmony_civoid mbedtls_free(void *ptr)
56a8e1175bSopenharmony_ci{
57a8e1175bSopenharmony_ci    (*mbedtls_free_func)(ptr);
58a8e1175bSopenharmony_ci}
59a8e1175bSopenharmony_ci
60a8e1175bSopenharmony_ciint mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
61a8e1175bSopenharmony_ci                                     void (*free_func)(void *))
62a8e1175bSopenharmony_ci{
63a8e1175bSopenharmony_ci    mbedtls_calloc_func = calloc_func;
64a8e1175bSopenharmony_ci    mbedtls_free_func = free_func;
65a8e1175bSopenharmony_ci    return 0;
66a8e1175bSopenharmony_ci}
67a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_MEMORY &&
68a8e1175bSopenharmony_ci          !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
69a8e1175bSopenharmony_ci             defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
72a8e1175bSopenharmony_ci#include <stdarg.h>
73a8e1175bSopenharmony_ciint mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
74a8e1175bSopenharmony_ci{
75a8e1175bSopenharmony_ci    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
76a8e1175bSopenharmony_ci    va_list argp;
77a8e1175bSopenharmony_ci
78a8e1175bSopenharmony_ci    va_start(argp, fmt);
79a8e1175bSopenharmony_ci    ret = mbedtls_vsnprintf(s, n, fmt, argp);
80a8e1175bSopenharmony_ci    va_end(argp);
81a8e1175bSopenharmony_ci
82a8e1175bSopenharmony_ci    return ret;
83a8e1175bSopenharmony_ci}
84a8e1175bSopenharmony_ci#endif
85a8e1175bSopenharmony_ci
86a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
87a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
88a8e1175bSopenharmony_ci/*
89a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
90a8e1175bSopenharmony_ci */
91a8e1175bSopenharmony_cistatic int platform_snprintf_uninit(char *s, size_t n,
92a8e1175bSopenharmony_ci                                    const char *format, ...)
93a8e1175bSopenharmony_ci{
94a8e1175bSopenharmony_ci    ((void) s);
95a8e1175bSopenharmony_ci    ((void) n);
96a8e1175bSopenharmony_ci    ((void) format);
97a8e1175bSopenharmony_ci    return 0;
98a8e1175bSopenharmony_ci}
99a8e1175bSopenharmony_ci
100a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_SNPRINTF    platform_snprintf_uninit
101a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ciint (*mbedtls_snprintf)(char *s, size_t n,
104a8e1175bSopenharmony_ci                        const char *format,
105a8e1175bSopenharmony_ci                        ...) = MBEDTLS_PLATFORM_STD_SNPRINTF;
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ciint mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
108a8e1175bSopenharmony_ci                                                       const char *format,
109a8e1175bSopenharmony_ci                                                       ...))
110a8e1175bSopenharmony_ci{
111a8e1175bSopenharmony_ci    mbedtls_snprintf = snprintf_func;
112a8e1175bSopenharmony_ci    return 0;
113a8e1175bSopenharmony_ci}
114a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
115a8e1175bSopenharmony_ci
116a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
117a8e1175bSopenharmony_ci#include <stdarg.h>
118a8e1175bSopenharmony_ciint mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg)
119a8e1175bSopenharmony_ci{
120a8e1175bSopenharmony_ci    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
121a8e1175bSopenharmony_ci
122a8e1175bSopenharmony_ci    /* Avoid calling the invalid parameter handler by checking ourselves */
123a8e1175bSopenharmony_ci    if (s == NULL || n == 0 || fmt == NULL) {
124a8e1175bSopenharmony_ci        return -1;
125a8e1175bSopenharmony_ci    }
126a8e1175bSopenharmony_ci
127a8e1175bSopenharmony_ci#if defined(_TRUNCATE)
128a8e1175bSopenharmony_ci    ret = vsnprintf_s(s, n, _TRUNCATE, fmt, arg);
129a8e1175bSopenharmony_ci#else
130a8e1175bSopenharmony_ci    ret = vsnprintf(s, n, fmt, arg);
131a8e1175bSopenharmony_ci    if (ret < 0 || (size_t) ret == n) {
132a8e1175bSopenharmony_ci        s[n-1] = '\0';
133a8e1175bSopenharmony_ci        ret = -1;
134a8e1175bSopenharmony_ci    }
135a8e1175bSopenharmony_ci#endif
136a8e1175bSopenharmony_ci
137a8e1175bSopenharmony_ci    return ret;
138a8e1175bSopenharmony_ci}
139a8e1175bSopenharmony_ci#endif
140a8e1175bSopenharmony_ci
141a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
142a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
143a8e1175bSopenharmony_ci/*
144a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
145a8e1175bSopenharmony_ci */
146a8e1175bSopenharmony_cistatic int platform_vsnprintf_uninit(char *s, size_t n,
147a8e1175bSopenharmony_ci                                     const char *format, va_list arg)
148a8e1175bSopenharmony_ci{
149a8e1175bSopenharmony_ci    ((void) s);
150a8e1175bSopenharmony_ci    ((void) n);
151a8e1175bSopenharmony_ci    ((void) format);
152a8e1175bSopenharmony_ci    ((void) arg);
153a8e1175bSopenharmony_ci    return -1;
154a8e1175bSopenharmony_ci}
155a8e1175bSopenharmony_ci
156a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_VSNPRINTF    platform_vsnprintf_uninit
157a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
158a8e1175bSopenharmony_ci
159a8e1175bSopenharmony_ciint (*mbedtls_vsnprintf)(char *s, size_t n,
160a8e1175bSopenharmony_ci                         const char *format,
161a8e1175bSopenharmony_ci                         va_list arg) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
162a8e1175bSopenharmony_ci
163a8e1175bSopenharmony_ciint mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
164a8e1175bSopenharmony_ci                                                         const char *format,
165a8e1175bSopenharmony_ci                                                         va_list arg))
166a8e1175bSopenharmony_ci{
167a8e1175bSopenharmony_ci    mbedtls_vsnprintf = vsnprintf_func;
168a8e1175bSopenharmony_ci    return 0;
169a8e1175bSopenharmony_ci}
170a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
171a8e1175bSopenharmony_ci
172a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
173a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
174a8e1175bSopenharmony_ci/*
175a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
176a8e1175bSopenharmony_ci */
177a8e1175bSopenharmony_cistatic int platform_printf_uninit(const char *format, ...)
178a8e1175bSopenharmony_ci{
179a8e1175bSopenharmony_ci    ((void) format);
180a8e1175bSopenharmony_ci    return 0;
181a8e1175bSopenharmony_ci}
182a8e1175bSopenharmony_ci
183a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_PRINTF    platform_printf_uninit
184a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
185a8e1175bSopenharmony_ci
186a8e1175bSopenharmony_ciint (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
187a8e1175bSopenharmony_ci
188a8e1175bSopenharmony_ciint mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
189a8e1175bSopenharmony_ci{
190a8e1175bSopenharmony_ci    mbedtls_printf = printf_func;
191a8e1175bSopenharmony_ci    return 0;
192a8e1175bSopenharmony_ci}
193a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
194a8e1175bSopenharmony_ci
195a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
196a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
197a8e1175bSopenharmony_ci/*
198a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
199a8e1175bSopenharmony_ci */
200a8e1175bSopenharmony_cistatic int platform_fprintf_uninit(FILE *stream, const char *format, ...)
201a8e1175bSopenharmony_ci{
202a8e1175bSopenharmony_ci    ((void) stream);
203a8e1175bSopenharmony_ci    ((void) format);
204a8e1175bSopenharmony_ci    return 0;
205a8e1175bSopenharmony_ci}
206a8e1175bSopenharmony_ci
207a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_FPRINTF   platform_fprintf_uninit
208a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
209a8e1175bSopenharmony_ci
210a8e1175bSopenharmony_ciint (*mbedtls_fprintf)(FILE *, const char *, ...) =
211a8e1175bSopenharmony_ci    MBEDTLS_PLATFORM_STD_FPRINTF;
212a8e1175bSopenharmony_ci
213a8e1175bSopenharmony_ciint mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
214a8e1175bSopenharmony_ci{
215a8e1175bSopenharmony_ci    mbedtls_fprintf = fprintf_func;
216a8e1175bSopenharmony_ci    return 0;
217a8e1175bSopenharmony_ci}
218a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
219a8e1175bSopenharmony_ci
220a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
221a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
222a8e1175bSopenharmony_ci/*
223a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
224a8e1175bSopenharmony_ci */
225a8e1175bSopenharmony_cistatic void platform_setbuf_uninit(FILE *stream, char *buf)
226a8e1175bSopenharmony_ci{
227a8e1175bSopenharmony_ci    ((void) stream);
228a8e1175bSopenharmony_ci    ((void) buf);
229a8e1175bSopenharmony_ci}
230a8e1175bSopenharmony_ci
231a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_SETBUF   platform_setbuf_uninit
232a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_SETBUF */
233a8e1175bSopenharmony_civoid (*mbedtls_setbuf)(FILE *stream, char *buf) = MBEDTLS_PLATFORM_STD_SETBUF;
234a8e1175bSopenharmony_ci
235a8e1175bSopenharmony_ciint mbedtls_platform_set_setbuf(void (*setbuf_func)(FILE *stream, char *buf))
236a8e1175bSopenharmony_ci{
237a8e1175bSopenharmony_ci    mbedtls_setbuf = setbuf_func;
238a8e1175bSopenharmony_ci    return 0;
239a8e1175bSopenharmony_ci}
240a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
241a8e1175bSopenharmony_ci
242a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
243a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
244a8e1175bSopenharmony_ci/*
245a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
246a8e1175bSopenharmony_ci */
247a8e1175bSopenharmony_cistatic void platform_exit_uninit(int status)
248a8e1175bSopenharmony_ci{
249a8e1175bSopenharmony_ci    ((void) status);
250a8e1175bSopenharmony_ci}
251a8e1175bSopenharmony_ci
252a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_EXIT   platform_exit_uninit
253a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
254a8e1175bSopenharmony_ci
255a8e1175bSopenharmony_civoid (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
256a8e1175bSopenharmony_ci
257a8e1175bSopenharmony_ciint mbedtls_platform_set_exit(void (*exit_func)(int status))
258a8e1175bSopenharmony_ci{
259a8e1175bSopenharmony_ci    mbedtls_exit = exit_func;
260a8e1175bSopenharmony_ci    return 0;
261a8e1175bSopenharmony_ci}
262a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
263a8e1175bSopenharmony_ci
264a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_TIME)
265a8e1175bSopenharmony_ci
266a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_TIME_ALT)
267a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_TIME)
268a8e1175bSopenharmony_ci/*
269a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
270a8e1175bSopenharmony_ci */
271a8e1175bSopenharmony_cistatic mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
272a8e1175bSopenharmony_ci{
273a8e1175bSopenharmony_ci    ((void) timer);
274a8e1175bSopenharmony_ci    return 0;
275a8e1175bSopenharmony_ci}
276a8e1175bSopenharmony_ci
277a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_TIME   platform_time_uninit
278a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_TIME */
279a8e1175bSopenharmony_ci
280a8e1175bSopenharmony_cimbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
281a8e1175bSopenharmony_ci
282a8e1175bSopenharmony_ciint mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
283a8e1175bSopenharmony_ci{
284a8e1175bSopenharmony_ci    mbedtls_time = time_func;
285a8e1175bSopenharmony_ci    return 0;
286a8e1175bSopenharmony_ci}
287a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_TIME_ALT */
288a8e1175bSopenharmony_ci
289a8e1175bSopenharmony_ci#endif /* MBEDTLS_HAVE_TIME */
290a8e1175bSopenharmony_ci
291a8e1175bSopenharmony_ci#if defined(MBEDTLS_ENTROPY_NV_SEED)
292a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
293a8e1175bSopenharmony_ci/* Default implementations for the platform independent seed functions use
294a8e1175bSopenharmony_ci * standard libc file functions to read from and write to a pre-defined filename
295a8e1175bSopenharmony_ci */
296a8e1175bSopenharmony_ciint mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
297a8e1175bSopenharmony_ci{
298a8e1175bSopenharmony_ci    FILE *file;
299a8e1175bSopenharmony_ci    size_t n;
300a8e1175bSopenharmony_ci
301a8e1175bSopenharmony_ci    if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
302a8e1175bSopenharmony_ci        return -1;
303a8e1175bSopenharmony_ci    }
304a8e1175bSopenharmony_ci
305a8e1175bSopenharmony_ci    /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
306a8e1175bSopenharmony_ci    mbedtls_setbuf(file, NULL);
307a8e1175bSopenharmony_ci
308a8e1175bSopenharmony_ci    if ((n = fread(buf, 1, buf_len, file)) != buf_len) {
309a8e1175bSopenharmony_ci        fclose(file);
310a8e1175bSopenharmony_ci        mbedtls_platform_zeroize(buf, buf_len);
311a8e1175bSopenharmony_ci        return -1;
312a8e1175bSopenharmony_ci    }
313a8e1175bSopenharmony_ci
314a8e1175bSopenharmony_ci    fclose(file);
315a8e1175bSopenharmony_ci    return (int) n;
316a8e1175bSopenharmony_ci}
317a8e1175bSopenharmony_ci
318a8e1175bSopenharmony_ciint mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
319a8e1175bSopenharmony_ci{
320a8e1175bSopenharmony_ci    FILE *file;
321a8e1175bSopenharmony_ci    size_t n;
322a8e1175bSopenharmony_ci
323a8e1175bSopenharmony_ci    if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
324a8e1175bSopenharmony_ci        return -1;
325a8e1175bSopenharmony_ci    }
326a8e1175bSopenharmony_ci
327a8e1175bSopenharmony_ci    /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
328a8e1175bSopenharmony_ci    mbedtls_setbuf(file, NULL);
329a8e1175bSopenharmony_ci
330a8e1175bSopenharmony_ci    if ((n = fwrite(buf, 1, buf_len, file)) != buf_len) {
331a8e1175bSopenharmony_ci        fclose(file);
332a8e1175bSopenharmony_ci        return -1;
333a8e1175bSopenharmony_ci    }
334a8e1175bSopenharmony_ci
335a8e1175bSopenharmony_ci    fclose(file);
336a8e1175bSopenharmony_ci    return (int) n;
337a8e1175bSopenharmony_ci}
338a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
339a8e1175bSopenharmony_ci
340a8e1175bSopenharmony_ci#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
341a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
342a8e1175bSopenharmony_ci/*
343a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
344a8e1175bSopenharmony_ci */
345a8e1175bSopenharmony_cistatic int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
346a8e1175bSopenharmony_ci{
347a8e1175bSopenharmony_ci    ((void) buf);
348a8e1175bSopenharmony_ci    ((void) buf_len);
349a8e1175bSopenharmony_ci    return -1;
350a8e1175bSopenharmony_ci}
351a8e1175bSopenharmony_ci
352a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_NV_SEED_READ   platform_nv_seed_read_uninit
353a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
354a8e1175bSopenharmony_ci
355a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
356a8e1175bSopenharmony_ci/*
357a8e1175bSopenharmony_ci * Make dummy function to prevent NULL pointer dereferences
358a8e1175bSopenharmony_ci */
359a8e1175bSopenharmony_cistatic int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
360a8e1175bSopenharmony_ci{
361a8e1175bSopenharmony_ci    ((void) buf);
362a8e1175bSopenharmony_ci    ((void) buf_len);
363a8e1175bSopenharmony_ci    return -1;
364a8e1175bSopenharmony_ci}
365a8e1175bSopenharmony_ci
366a8e1175bSopenharmony_ci#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE   platform_nv_seed_write_uninit
367a8e1175bSopenharmony_ci#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
368a8e1175bSopenharmony_ci
369a8e1175bSopenharmony_ciint (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
370a8e1175bSopenharmony_ci    MBEDTLS_PLATFORM_STD_NV_SEED_READ;
371a8e1175bSopenharmony_ciint (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
372a8e1175bSopenharmony_ci    MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
373a8e1175bSopenharmony_ci
374a8e1175bSopenharmony_ciint mbedtls_platform_set_nv_seed(
375a8e1175bSopenharmony_ci    int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
376a8e1175bSopenharmony_ci    int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
377a8e1175bSopenharmony_ci{
378a8e1175bSopenharmony_ci    mbedtls_nv_seed_read = nv_seed_read_func;
379a8e1175bSopenharmony_ci    mbedtls_nv_seed_write = nv_seed_write_func;
380a8e1175bSopenharmony_ci    return 0;
381a8e1175bSopenharmony_ci}
382a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
383a8e1175bSopenharmony_ci#endif /* MBEDTLS_ENTROPY_NV_SEED */
384a8e1175bSopenharmony_ci
385a8e1175bSopenharmony_ci#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
386a8e1175bSopenharmony_ci/*
387a8e1175bSopenharmony_ci * Placeholder platform setup that does nothing by default
388a8e1175bSopenharmony_ci */
389a8e1175bSopenharmony_ciint mbedtls_platform_setup(mbedtls_platform_context *ctx)
390a8e1175bSopenharmony_ci{
391a8e1175bSopenharmony_ci    (void) ctx;
392a8e1175bSopenharmony_ci
393a8e1175bSopenharmony_ci    return 0;
394a8e1175bSopenharmony_ci}
395a8e1175bSopenharmony_ci
396a8e1175bSopenharmony_ci/*
397a8e1175bSopenharmony_ci * Placeholder platform teardown that does nothing by default
398a8e1175bSopenharmony_ci */
399a8e1175bSopenharmony_civoid mbedtls_platform_teardown(mbedtls_platform_context *ctx)
400a8e1175bSopenharmony_ci{
401a8e1175bSopenharmony_ci    (void) ctx;
402a8e1175bSopenharmony_ci}
403a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
404a8e1175bSopenharmony_ci
405a8e1175bSopenharmony_ci#endif /* MBEDTLS_PLATFORM_C */
406