blob: 890c4cbaba7fd5f8d8f9b8d3de3239a3c66d34ba [file] [log] [blame]
Paul Bakker747a83a2014-02-01 22:50:07 +01001/*
2 * Platform abstraction layer
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker747a83a2014-02-01 22:50:07 +01006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker747a83a2014-02-01 22:50:07 +01009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050013#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000014#include "mbedtls/error.h"
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010015
Hanno Beckercfa2e332018-10-11 10:26:55 +010016/* The compile time configuration of memory allocation via the macros
17 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
18 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
19 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
20#if defined(MBEDTLS_PLATFORM_MEMORY) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010021 !(defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
22 defined(MBEDTLS_PLATFORM_FREE_MACRO))
Hanno Beckercfa2e332018-10-11 10:26:55 +010023
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020024#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Gilles Peskine449bd832023-01-11 14:50:10 +010025static void *platform_calloc_uninit(size_t n, size_t size)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010026{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020027 ((void) n);
28 ((void) size);
Gilles Peskine449bd832023-01-11 14:50:10 +010029 return NULL;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010030}
31
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020032#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
33#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Gilles Peskine449bd832023-01-11 14:50:10 +010036static void platform_free_uninit(void *ptr)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010037{
38 ((void) ptr);
39}
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
42#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044static void * (*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
45static void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
Roberto Vargas7decfe82018-06-04 13:54:09 +010046
Gilles Peskine449bd832023-01-11 14:50:10 +010047void *mbedtls_calloc(size_t nmemb, size_t size)
Roberto Vargas7decfe82018-06-04 13:54:09 +010048{
Gilles Peskine449bd832023-01-11 14:50:10 +010049 return (*mbedtls_calloc_func)(nmemb, size);
Roberto Vargas7decfe82018-06-04 13:54:09 +010050}
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052void mbedtls_free(void *ptr)
Roberto Vargas7decfe82018-06-04 13:54:09 +010053{
Gilles Peskine449bd832023-01-11 14:50:10 +010054 (*mbedtls_free_func)(ptr);
Roberto Vargas7decfe82018-06-04 13:54:09 +010055}
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010056
Gilles Peskine449bd832023-01-11 14:50:10 +010057int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
58 void (*free_func)(void *))
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010059{
Roberto Vargas7decfe82018-06-04 13:54:09 +010060 mbedtls_calloc_func = calloc_func;
61 mbedtls_free_func = free_func;
Gilles Peskine449bd832023-01-11 14:50:10 +010062 return 0;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010063}
Hanno Beckercfa2e332018-10-11 10:26:55 +010064#endif /* MBEDTLS_PLATFORM_MEMORY &&
65 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
66 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010067
k-stachowiak723f8672018-07-16 14:27:07 +020068#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020069#include <stdarg.h>
Gilles Peskine449bd832023-01-11 14:50:10 +010070int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020071{
Janos Follath24eed8d2019-11-22 13:21:35 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020073 va_list argp;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 va_start(argp, fmt);
76 ret = mbedtls_vsnprintf(s, n, fmt, argp);
77 va_end(argp);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return ret;
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020080}
81#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
84#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000085/*
86 * Make dummy function to prevent NULL pointer dereferences
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088static int platform_snprintf_uninit(char *s, size_t n,
89 const char *format, ...)
Rich Evans46b0a8d2015-01-30 10:47:32 +000090{
91 ((void) s);
92 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +010093 ((void) format);
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return 0;
Rich Evans46b0a8d2015-01-30 10:47:32 +000095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
98#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +000099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100int (*mbedtls_snprintf)(char *s, size_t n,
101 const char *format,
102 ...) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
105 const char *format,
106 ...))
Rich Evans46b0a8d2015-01-30 10:47:32 +0000107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_snprintf = snprintf_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return 0;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000112
k-stachowiak723f8672018-07-16 14:27:07 +0200113#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
114#include <stdarg.h>
Gilles Peskine449bd832023-01-11 14:50:10 +0100115int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg)
k-stachowiak723f8672018-07-16 14:27:07 +0200116{
Janos Follath24eed8d2019-11-22 13:21:35 +0000117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
k-stachowiak723f8672018-07-16 14:27:07 +0200118
119 /* Avoid calling the invalid parameter handler by checking ourselves */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (s == NULL || n == 0 || fmt == NULL) {
121 return -1;
122 }
k-stachowiak723f8672018-07-16 14:27:07 +0200123
124#if defined(_TRUNCATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 ret = vsnprintf_s(s, n, _TRUNCATE, fmt, arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200126#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 ret = vsnprintf(s, n, fmt, arg);
128 if (ret < 0 || (size_t) ret == n) {
k-stachowiak723f8672018-07-16 14:27:07 +0200129 s[n-1] = '\0';
130 ret = -1;
131 }
132#endif
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 return ret;
k-stachowiak723f8672018-07-16 14:27:07 +0200135}
136#endif
137
138#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
139#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
140/*
141 * Make dummy function to prevent NULL pointer dereferences
142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143static int platform_vsnprintf_uninit(char *s, size_t n,
144 const char *format, va_list arg)
k-stachowiak723f8672018-07-16 14:27:07 +0200145{
146 ((void) s);
147 ((void) n);
148 ((void) format);
149 ((void) arg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return -1;
k-stachowiak723f8672018-07-16 14:27:07 +0200151}
152
153#define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
154#endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int (*mbedtls_vsnprintf)(char *s, size_t n,
157 const char *format,
158 va_list arg) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
k-stachowiak723f8672018-07-16 14:27:07 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
161 const char *format,
162 va_list arg))
k-stachowiak723f8672018-07-16 14:27:07 +0200163{
164 mbedtls_vsnprintf = vsnprintf_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return 0;
k-stachowiak723f8672018-07-16 14:27:07 +0200166}
167#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
170#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100171/*
172 * Make dummy function to prevent NULL pointer dereferences
173 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174static int platform_printf_uninit(const char *format, ...)
Paul Bakker747a83a2014-02-01 22:50:07 +0100175{
176 ((void) format);
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100178}
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
181#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183int (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
Paul Bakker747a83a2014-02-01 22:50:07 +0100186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf = printf_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
193#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100194/*
195 * Make dummy function to prevent NULL pointer dereferences
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
Paul Bakker747a83a2014-02-01 22:50:07 +0100198{
199 ((void) stream);
200 ((void) format);
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100202}
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
205#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int (*mbedtls_fprintf)(FILE *, const char *, ...) =
208 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
Paul Bakker747a83a2014-02-01 22:50:07 +0100211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_fprintf = fprintf_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100216
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200217#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
218#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
219/*
220 * Make dummy function to prevent NULL pointer dereferences
221 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static void platform_setbuf_uninit(FILE *stream, char *buf)
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200223{
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 ((void) stream);
225 ((void) buf);
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200226}
227
228#define MBEDTLS_PLATFORM_STD_SETBUF platform_setbuf_uninit
229#endif /* !MBEDTLS_PLATFORM_STD_SETBUF */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230void (*mbedtls_setbuf)(FILE *stream, char *buf) = MBEDTLS_PLATFORM_STD_SETBUF;
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232int mbedtls_platform_set_setbuf(void (*setbuf_func)(FILE *stream, char *buf))
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200233{
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_setbuf = setbuf_func;
235 return 0;
Gilles Peskine6497b5a2022-06-30 17:01:40 +0200236}
237#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
240#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000241/*
242 * Make dummy function to prevent NULL pointer dereferences
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244static void platform_exit_uninit(int status)
Rich Evansc39cb492015-01-30 12:01:34 +0000245{
246 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000247}
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
250#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252void (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254int mbedtls_platform_set_exit(void (*exit_func)(int status))
Rich Evansc39cb492015-01-30 12:01:34 +0000255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_exit = exit_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return 0;
Rich Evansc39cb492015-01-30 12:01:34 +0000258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000260
Simon Butcher23e97782016-07-13 13:31:08 +0100261#if defined(MBEDTLS_HAVE_TIME)
262
SimonBd5800b72016-04-26 07:43:27 +0100263#if defined(MBEDTLS_PLATFORM_TIME_ALT)
264#if !defined(MBEDTLS_PLATFORM_STD_TIME)
265/*
266 * Make dummy function to prevent NULL pointer dereferences
267 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100268static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
SimonBd5800b72016-04-26 07:43:27 +0100269{
270 ((void) timer);
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return 0;
SimonBd5800b72016-04-26 07:43:27 +0100272}
273
274#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
275#endif /* !MBEDTLS_PLATFORM_STD_TIME */
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
SimonBd5800b72016-04-26 07:43:27 +0100280{
281 mbedtls_time = time_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return 0;
SimonBd5800b72016-04-26 07:43:27 +0100283}
284#endif /* MBEDTLS_PLATFORM_TIME_ALT */
285
Simon Butcher23e97782016-07-13 13:31:08 +0100286#endif /* MBEDTLS_HAVE_TIME */
287
Paul Bakkere021a4b2016-06-01 11:25:44 +0100288#if defined(MBEDTLS_ENTROPY_NV_SEED)
289#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
290/* Default implementations for the platform independent seed functions use
291 * standard libc file functions to read from and write to a pre-defined filename
292 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100294{
295 FILE *file;
296 size_t n;
297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
299 return -1;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
303 mbedtls_setbuf(file, NULL);
304
305 if ((n = fread(buf, 1, buf_len, file)) != buf_len) {
306 fclose(file);
307 mbedtls_platform_zeroize(buf, buf_len);
308 return -1;
309 }
310
311 fclose(file);
312 return (int) n;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100313}
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100316{
317 FILE *file;
318 size_t n;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
Paul Bakkere021a4b2016-06-01 11:25:44 +0100321 return -1;
322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
325 mbedtls_setbuf(file, NULL);
326
327 if ((n = fwrite(buf, 1, buf_len, file)) != buf_len) {
328 fclose(file);
329 return -1;
330 }
331
332 fclose(file);
333 return (int) n;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100334}
335#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
336
337#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
338#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
339/*
340 * Make dummy function to prevent NULL pointer dereferences
341 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100343{
344 ((void) buf);
345 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return -1;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100347}
348
349#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
350#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
351
352#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
353/*
354 * Make dummy function to prevent NULL pointer dereferences
355 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100357{
358 ((void) buf);
359 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return -1;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100361}
362
363#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
364#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
367 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
368int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
369 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100370
371int mbedtls_platform_set_nv_seed(
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
373 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
Paul Bakkere021a4b2016-06-01 11:25:44 +0100374{
375 mbedtls_nv_seed_read = nv_seed_read_func;
376 mbedtls_nv_seed_write = nv_seed_write_func;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 return 0;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100378}
379#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
380#endif /* MBEDTLS_ENTROPY_NV_SEED */
381
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100382#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100383/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100384 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386int mbedtls_platform_setup(mbedtls_platform_context *ctx)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100387{
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 (void) ctx;
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return 0;
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100391}
392
393/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100394 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100395 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100397{
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 (void) ctx;
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100399}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100400#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#endif /* MBEDTLS_PLATFORM_C */