2014-02-01 22:50:07 +01:00
|
|
|
/*
|
|
|
|
* Platform abstraction layer
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2014-02-01 22:50:07 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2014-02-01 22:50:07 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-02-01 22:50:07 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-02-01 22:50:07 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2017-12-20 12:52:49 +01:00
|
|
|
|
2018-10-11 11:26:55 +02:00
|
|
|
/* The compile time configuration of memory allocation via the macros
|
|
|
|
* MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
|
|
|
|
* configuration via mbedtls_platform_set_calloc_free(). So, omit everything
|
|
|
|
* related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
|
|
|
|
#if defined(MBEDTLS_PLATFORM_MEMORY) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
!(defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
|
|
|
|
defined(MBEDTLS_PLATFORM_FREE_MACRO))
|
2018-10-11 11:26:55 +02:00
|
|
|
|
2015-05-26 16:15:20 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
2023-01-11 14:50:10 +01:00
|
|
|
static void *platform_calloc_uninit(size_t n, size_t size)
|
2014-02-04 17:30:24 +01:00
|
|
|
{
|
2015-05-26 16:15:20 +02:00
|
|
|
((void) n);
|
|
|
|
((void) size);
|
2023-01-11 14:50:10 +01:00
|
|
|
return NULL;
|
2014-02-04 17:30:24 +01:00
|
|
|
}
|
|
|
|
|
2015-05-26 16:15:20 +02:00
|
|
|
#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
|
2014-02-04 17:30:24 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
|
2023-01-11 14:50:10 +01:00
|
|
|
static void platform_free_uninit(void *ptr)
|
2014-02-04 17:30:24 +01:00
|
|
|
{
|
|
|
|
((void) ptr);
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_FREE */
|
2014-02-04 17:30:24 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void * (*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
|
|
|
|
static void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
|
2018-06-04 14:54:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void *mbedtls_calloc(size_t nmemb, size_t size)
|
2018-06-04 14:54:09 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (*mbedtls_calloc_func)(nmemb, size);
|
2018-06-04 14:54:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_free(void *ptr)
|
2018-06-04 14:54:09 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
(*mbedtls_free_func)(ptr);
|
2018-06-04 14:54:09 +02:00
|
|
|
}
|
2014-02-04 17:30:24 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
|
|
|
|
void (*free_func)(void *))
|
2014-02-04 17:30:24 +01:00
|
|
|
{
|
2018-06-04 14:54:09 +02:00
|
|
|
mbedtls_calloc_func = calloc_func;
|
|
|
|
mbedtls_free_func = free_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-02-04 17:30:24 +01:00
|
|
|
}
|
2018-10-11 11:26:55 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_MEMORY &&
|
|
|
|
!( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
|
|
|
|
defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
|
2014-02-04 17:30:24 +01:00
|
|
|
|
2018-07-16 14:27:07 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
|
2015-06-22 10:23:34 +02:00
|
|
|
#include <stdarg.h>
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
|
2015-06-22 10:23:34 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-06-22 10:23:34 +02:00
|
|
|
va_list argp;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
va_start(argp, fmt);
|
|
|
|
ret = mbedtls_vsnprintf(s, n, fmt, argp);
|
|
|
|
va_end(argp);
|
2015-06-22 10:23:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2015-06-22 10:23:34 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
|
2015-01-30 11:47:32 +01:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int platform_snprintf_uninit(char *s, size_t n,
|
|
|
|
const char *format, ...)
|
2015-01-30 11:47:32 +01:00
|
|
|
{
|
|
|
|
((void) s);
|
|
|
|
((void) n);
|
2015-06-03 11:20:33 +02:00
|
|
|
((void) format);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-01-30 11:47:32 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
|
2015-01-30 11:47:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*mbedtls_snprintf)(char *s, size_t n,
|
|
|
|
const char *format,
|
|
|
|
...) = MBEDTLS_PLATFORM_STD_SNPRINTF;
|
2015-01-30 11:47:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
|
|
|
|
const char *format,
|
|
|
|
...))
|
2015-01-30 11:47:32 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_snprintf = snprintf_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-01-30 11:47:32 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
|
2015-01-30 11:47:32 +01:00
|
|
|
|
2018-07-16 14:27:07 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
|
|
|
|
#include <stdarg.h>
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_win32_vsnprintf(char *s, size_t n, const char *fmt, va_list arg)
|
2018-07-16 14:27:07 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-07-16 14:27:07 +02:00
|
|
|
|
|
|
|
/* Avoid calling the invalid parameter handler by checking ourselves */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (s == NULL || n == 0 || fmt == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-16 14:27:07 +02:00
|
|
|
|
|
|
|
#if defined(_TRUNCATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = vsnprintf_s(s, n, _TRUNCATE, fmt, arg);
|
2018-07-16 14:27:07 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = vsnprintf(s, n, fmt, arg);
|
|
|
|
if (ret < 0 || (size_t) ret == n) {
|
2018-07-16 14:27:07 +02:00
|
|
|
s[n-1] = '\0';
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2018-07-16 14:27:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
|
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int platform_vsnprintf_uninit(char *s, size_t n,
|
|
|
|
const char *format, va_list arg)
|
2018-07-16 14:27:07 +02:00
|
|
|
{
|
|
|
|
((void) s);
|
|
|
|
((void) n);
|
|
|
|
((void) format);
|
|
|
|
((void) arg);
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2018-07-16 14:27:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*mbedtls_vsnprintf)(char *s, size_t n,
|
|
|
|
const char *format,
|
|
|
|
va_list arg) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
|
2018-07-16 14:27:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_vsnprintf(int (*vsnprintf_func)(char *s, size_t n,
|
|
|
|
const char *format,
|
|
|
|
va_list arg))
|
2018-07-16 14:27:07 +02:00
|
|
|
{
|
|
|
|
mbedtls_vsnprintf = vsnprintf_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2018-07-16 14:27:07 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
|
2014-02-01 22:50:07 +01:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int platform_printf_uninit(const char *format, ...)
|
2014-02-01 22:50:07 +01:00
|
|
|
{
|
|
|
|
((void) format);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-02-01 22:50:07 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
|
2014-02-01 22:50:07 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf = printf_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-02-01 22:50:07 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
|
2014-02-01 22:50:07 +01:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
|
2014-02-01 22:50:07 +01:00
|
|
|
{
|
|
|
|
((void) stream);
|
|
|
|
((void) format);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-02-01 22:50:07 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*mbedtls_fprintf)(FILE *, const char *, ...) =
|
|
|
|
MBEDTLS_PLATFORM_STD_FPRINTF;
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
|
2014-02-01 22:50:07 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_fprintf = fprintf_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-02-01 22:50:07 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
|
2014-02-01 22:50:07 +01:00
|
|
|
|
2022-06-30 17:01:40 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_SETBUF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_SETBUF)
|
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void platform_setbuf_uninit(FILE *stream, char *buf)
|
2022-06-30 17:01:40 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
((void) stream);
|
|
|
|
((void) buf);
|
2022-06-30 17:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_SETBUF platform_setbuf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_SETBUF */
|
2023-01-11 14:50:10 +01:00
|
|
|
void (*mbedtls_setbuf)(FILE *stream, char *buf) = MBEDTLS_PLATFORM_STD_SETBUF;
|
2022-06-30 17:01:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_setbuf(void (*setbuf_func)(FILE *stream, char *buf))
|
2022-06-30 17:01:40 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_setbuf = setbuf_func;
|
|
|
|
return 0;
|
2022-06-30 17:01:40 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_SETBUF_ALT */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
|
2015-01-30 13:01:34 +01:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void platform_exit_uninit(int status)
|
2015-01-30 13:01:34 +01:00
|
|
|
{
|
|
|
|
((void) status);
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
|
2015-01-30 13:01:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
|
2015-01-30 13:01:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_exit(void (*exit_func)(int status))
|
2015-01-30 13:01:34 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_exit = exit_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-01-30 13:01:34 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
|
2015-01-30 13:01:34 +01:00
|
|
|
|
2016-07-13 14:31:08 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
|
2016-04-26 08:43:27 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_TIME_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_TIME)
|
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
|
2016-04-26 08:43:27 +02:00
|
|
|
{
|
|
|
|
((void) timer);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2016-04-26 08:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_TIME */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
|
2016-04-26 08:43:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
|
2016-04-26 08:43:27 +02:00
|
|
|
{
|
|
|
|
mbedtls_time = time_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2016-04-26 08:43:27 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_TIME_ALT */
|
|
|
|
|
2016-07-13 14:31:08 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2016-06-01 12:25:44 +02:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
|
|
|
|
/* Default implementations for the platform independent seed functions use
|
|
|
|
* standard libc file functions to read from and write to a pre-defined filename
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
|
2016-06-01 12:25:44 +02:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
size_t n;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-01 12:25:44 +02:00
|
|
|
|
2022-06-30 17:01:40 +02:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_setbuf(file, NULL);
|
2022-06-30 17:01:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((n = fread(buf, 1, buf_len, file)) != buf_len) {
|
|
|
|
fclose(file);
|
|
|
|
mbedtls_platform_zeroize(buf, buf_len);
|
|
|
|
return -1;
|
2016-06-01 12:25:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
fclose(file);
|
|
|
|
return (int) n;
|
2016-06-01 12:25:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
|
2016-06-01 12:25:44 +02:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
size_t n;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
|
2016-06-01 12:25:44 +02:00
|
|
|
return -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2016-06-01 12:25:44 +02:00
|
|
|
|
2022-06-30 17:01:40 +02:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_setbuf(file, NULL);
|
2022-06-30 17:01:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((n = fwrite(buf, 1, buf_len, file)) != buf_len) {
|
|
|
|
fclose(file);
|
2016-06-01 12:25:44 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
fclose(file);
|
|
|
|
return (int) n;
|
2016-06-01 12:25:44 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
|
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
|
2016-06-01 12:25:44 +02:00
|
|
|
{
|
|
|
|
((void) buf);
|
|
|
|
((void) buf_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2016-06-01 12:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
|
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
|
2016-06-01 12:25:44 +02:00
|
|
|
{
|
|
|
|
((void) buf);
|
|
|
|
((void) buf_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2016-06-01 12:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
|
|
|
|
MBEDTLS_PLATFORM_STD_NV_SEED_READ;
|
|
|
|
int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
|
|
|
|
MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
|
2016-06-01 12:25:44 +02:00
|
|
|
|
|
|
|
int mbedtls_platform_set_nv_seed(
|
2023-01-11 14:50:10 +01:00
|
|
|
int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
|
|
|
|
int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
|
2016-06-01 12:25:44 +02:00
|
|
|
{
|
|
|
|
mbedtls_nv_seed_read = nv_seed_read_func;
|
|
|
|
mbedtls_nv_seed_write = nv_seed_write_func;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2016-06-01 12:25:44 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
|
|
|
|
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
|
|
|
|
2017-07-18 11:23:04 +02:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
2017-07-07 14:03:23 +02:00
|
|
|
/*
|
2017-07-12 12:25:17 +02:00
|
|
|
* Placeholder platform setup that does nothing by default
|
2017-07-07 14:03:23 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
|
2017-07-07 14:03:23 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) ctx;
|
2017-07-07 14:03:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2017-07-07 14:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-12 12:25:17 +02:00
|
|
|
* Placeholder platform teardown that does nothing by default
|
2017-07-07 14:03:23 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
|
2017-07-07 14:03:23 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) ctx;
|
2017-07-07 14:03:23 +02:00
|
|
|
}
|
2017-07-18 11:23:04 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
|
2017-07-07 14:03:23 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|