blob: c8b0328d1da2a5664cabc7123a3ecc85357b63ce [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
Paul Bakker747a83a2014-02-01 22:50:07 +010018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker747a83a2014-02-01 22:50:07 +010021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Paul Bakker747a83a2014-02-01 22:50:07 +010023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000026#include "mbedtls/error.h"
Manuel Pégourié-Gonnarda268da92017-12-20 12:52:49 +010027
Hanno Beckercfa2e332018-10-11 10:26:55 +010028/* 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) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033 !(defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
34 defined(MBEDTLS_PLATFORM_FREE_MACRO))
Hanno Beckercfa2e332018-10-11 10:26:55 +010035
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020036#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037static void *platform_calloc_uninit(size_t n, size_t size)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010038{
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020039 ((void) n);
40 ((void) size);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 return NULL;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010042}
43
Manuel Pégourié-Gonnardb9ef1182015-05-26 16:15:20 +020044#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
45#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_PLATFORM_STD_FREE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048static void platform_free_uninit(void *ptr)
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010049{
50 ((void) ptr);
51}
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
54#endif /* !MBEDTLS_PLATFORM_STD_FREE */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056static void * (*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
57static void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
Roberto Vargas7decfe82018-06-04 13:54:09 +010058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059void *mbedtls_calloc(size_t nmemb, size_t size)
Roberto Vargas7decfe82018-06-04 13:54:09 +010060{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 return (*mbedtls_calloc_func)(nmemb, size);
Roberto Vargas7decfe82018-06-04 13:54:09 +010062}
63
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064void mbedtls_free(void *ptr)
Roberto Vargas7decfe82018-06-04 13:54:09 +010065{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 (*mbedtls_free_func)(ptr);
Roberto Vargas7decfe82018-06-04 13:54:09 +010067}
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
70 void (*free_func)(void *))
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010071{
Roberto Vargas7decfe82018-06-04 13:54:09 +010072 mbedtls_calloc_func = calloc_func;
73 mbedtls_free_func = free_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 return 0;
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010075}
Hanno Beckercfa2e332018-10-11 10:26:55 +010076#endif /* MBEDTLS_PLATFORM_MEMORY &&
77 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
78 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
Paul Bakkerdefc0ca2014-02-04 17:30:24 +010079
k-stachowiak723f8672018-07-16 14:27:07 +020080#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020081#include <stdarg.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020083{
Janos Follath24eed8d2019-11-22 13:21:35 +000084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020085 va_list argp;
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087 va_start(argp, fmt);
88 ret = mbedtls_vsnprintf(s, n, fmt, argp);
89 va_end(argp);
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020090
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 return ret;
Manuel Pégourié-Gonnard6c0c8e02015-06-22 10:23:34 +020092}
93#endif
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
96#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
Rich Evans46b0a8d2015-01-30 10:47:32 +000097/*
98 * Make dummy function to prevent NULL pointer dereferences
99 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100static int platform_snprintf_uninit(char *s, size_t n,
101 const char *format, ...)
Rich Evans46b0a8d2015-01-30 10:47:32 +0000102{
103 ((void) s);
104 ((void) n);
Manuel Pégourié-Gonnarddccb80b2015-06-03 10:20:33 +0100105 ((void) format);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 return 0;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000107}
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
110#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112int (*mbedtls_snprintf)(char *s, size_t n,
113 const char *format,
114 ...) = MBEDTLS_PLATFORM_STD_SNPRINTF;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
117 const char *format,
118 ...))
Rich Evans46b0a8d2015-01-30 10:47:32 +0000119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_snprintf = snprintf_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 return 0;
Rich Evans46b0a8d2015-01-30 10:47:32 +0000122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
Rich Evans46b0a8d2015-01-30 10:47:32 +0000124
k-stachowiak723f8672018-07-16 14:27:07 +0200125#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
126#include <stdarg.h>
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg)
k-stachowiak723f8672018-07-16 14:27:07 +0200128{
Janos Follath24eed8d2019-11-22 13:21:35 +0000129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
k-stachowiak723f8672018-07-16 14:27:07 +0200130
131 /* Avoid calling the invalid parameter handler by checking ourselves */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 if (s == NULL || n == 0 || fmt == NULL) {
133 return -1;
134 }
k-stachowiak723f8672018-07-16 14:27:07 +0200135
136#if defined(_TRUNCATE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 ret = vsnprintf_s(s, n, _TRUNCATE, fmt, arg);
k-stachowiak723f8672018-07-16 14:27:07 +0200138#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 ret = vsnprintf(s, n, fmt, arg);
140 if (ret < 0 || (size_t) ret == n) {
k-stachowiak723f8672018-07-16 14:27:07 +0200141 s[n-1] = '\0';
142 ret = -1;
143 }
144#endif
145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 return ret;
k-stachowiak723f8672018-07-16 14:27:07 +0200147}
148#endif
149
150#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
151#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
152/*
153 * Make dummy function to prevent NULL pointer dereferences
154 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155static int platform_vsnprintf_uninit(char *s, size_t n,
156 const char *format, va_list arg)
k-stachowiak723f8672018-07-16 14:27:07 +0200157{
158 ((void) s);
159 ((void) n);
160 ((void) format);
161 ((void) arg);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 return -1;
k-stachowiak723f8672018-07-16 14:27:07 +0200163}
164
165#define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
166#endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168int (*mbedtls_vsnprintf)(char *s, size_t n,
169 const char *format,
170 va_list arg) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
k-stachowiak723f8672018-07-16 14:27:07 +0200171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
173 const char *format,
174 va_list arg))
k-stachowiak723f8672018-07-16 14:27:07 +0200175{
176 mbedtls_vsnprintf = vsnprintf_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 return 0;
k-stachowiak723f8672018-07-16 14:27:07 +0200178}
179#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
182#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100183/*
184 * Make dummy function to prevent NULL pointer dereferences
185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186static int platform_printf_uninit(const char *format, ...)
Paul Bakker747a83a2014-02-01 22:50:07 +0100187{
188 ((void) format);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100190}
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
193#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195int (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
Paul Bakker747a83a2014-02-01 22:50:07 +0100198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_printf = printf_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
205#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
Paul Bakker747a83a2014-02-01 22:50:07 +0100206/*
207 * Make dummy function to prevent NULL pointer dereferences
208 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
Paul Bakker747a83a2014-02-01 22:50:07 +0100210{
211 ((void) stream);
212 ((void) format);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100214}
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
217#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
Paul Bakker747a83a2014-02-01 22:50:07 +0100218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219int (*mbedtls_fprintf)(FILE *, const char *, ...) =
220 MBEDTLS_PLATFORM_STD_FPRINTF;
Paul Bakker747a83a2014-02-01 22:50:07 +0100221
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
Paul Bakker747a83a2014-02-01 22:50:07 +0100223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_fprintf = fprintf_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 return 0;
Paul Bakker747a83a2014-02-01 22:50:07 +0100226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
Paul Bakker747a83a2014-02-01 22:50:07 +0100228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
230#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
Rich Evansc39cb492015-01-30 12:01:34 +0000231/*
232 * Make dummy function to prevent NULL pointer dereferences
233 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234static void platform_exit_uninit(int status)
Rich Evansc39cb492015-01-30 12:01:34 +0000235{
236 ((void) status);
Rich Evansc39cb492015-01-30 12:01:34 +0000237}
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
240#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
Rich Evansc39cb492015-01-30 12:01:34 +0000241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242void (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
Rich Evansc39cb492015-01-30 12:01:34 +0000243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244int mbedtls_platform_set_exit(void (*exit_func)(int status))
Rich Evansc39cb492015-01-30 12:01:34 +0000245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_exit = exit_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return 0;
Rich Evansc39cb492015-01-30 12:01:34 +0000248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
Rich Evansc39cb492015-01-30 12:01:34 +0000250
Simon Butcher23e97782016-07-13 13:31:08 +0100251#if defined(MBEDTLS_HAVE_TIME)
252
SimonBd5800b72016-04-26 07:43:27 +0100253#if defined(MBEDTLS_PLATFORM_TIME_ALT)
254#if !defined(MBEDTLS_PLATFORM_STD_TIME)
255/*
256 * Make dummy function to prevent NULL pointer dereferences
257 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
SimonBd5800b72016-04-26 07:43:27 +0100259{
260 ((void) timer);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return 0;
SimonBd5800b72016-04-26 07:43:27 +0100262}
263
264#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
265#endif /* !MBEDTLS_PLATFORM_STD_TIME */
266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
SimonBd5800b72016-04-26 07:43:27 +0100268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
SimonBd5800b72016-04-26 07:43:27 +0100270{
271 mbedtls_time = time_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 return 0;
SimonBd5800b72016-04-26 07:43:27 +0100273}
274#endif /* MBEDTLS_PLATFORM_TIME_ALT */
275
Simon Butcher23e97782016-07-13 13:31:08 +0100276#endif /* MBEDTLS_HAVE_TIME */
277
Paul Bakkere021a4b2016-06-01 11:25:44 +0100278#if defined(MBEDTLS_ENTROPY_NV_SEED)
279#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
280/* Default implementations for the platform independent seed functions use
281 * standard libc file functions to read from and write to a pre-defined filename
282 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100284{
285 FILE *file;
286 size_t n;
287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
289 return -1;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100290 }
291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 if ((n = fread(buf, 1, buf_len, file)) != buf_len) {
293 fclose(file);
294 mbedtls_platform_zeroize(buf, buf_len);
295 return -1;
296 }
297
298 fclose(file);
299 return (int) n;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100300}
301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100303{
304 FILE *file;
305 size_t n;
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
Paul Bakkere021a4b2016-06-01 11:25:44 +0100308 return -1;
309 }
310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if ((n = fwrite(buf, 1, buf_len, file)) != buf_len) {
312 fclose(file);
313 return -1;
314 }
315
316 fclose(file);
317 return (int) n;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100318}
319#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
320
321#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
322#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
323/*
324 * Make dummy function to prevent NULL pointer dereferences
325 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100327{
328 ((void) buf);
329 ((void) buf_len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 return -1;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100331}
332
333#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
334#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
335
336#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
337/*
338 * Make dummy function to prevent NULL pointer dereferences
339 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
Paul Bakkere021a4b2016-06-01 11:25:44 +0100341{
342 ((void) buf);
343 ((void) buf_len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return -1;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100345}
346
347#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
348#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
351 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
352int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
353 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100354
355int mbedtls_platform_set_nv_seed(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
357 int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
Paul Bakkere021a4b2016-06-01 11:25:44 +0100358{
359 mbedtls_nv_seed_read = nv_seed_read_func;
360 mbedtls_nv_seed_write = nv_seed_write_func;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return 0;
Paul Bakkere021a4b2016-06-01 11:25:44 +0100362}
363#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
364#endif /* MBEDTLS_ENTROPY_NV_SEED */
365
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100366#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100367/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100368 * Placeholder platform setup that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100369 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370int mbedtls_platform_setup(mbedtls_platform_context *ctx)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100371{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 (void) ctx;
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 return 0;
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100375}
376
377/*
Andres Amaya Garcia3c8a39d2017-07-12 11:25:17 +0100378 * Placeholder platform teardown that does nothing by default
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100379 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100381{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 (void) ctx;
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100383}
Andres Amaya Garciad91f99f2017-07-18 10:23:04 +0100384#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
Andres Amaya Garcia2a6f39c2017-07-07 13:03:23 +0100385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#endif /* MBEDTLS_PLATFORM_C */