proxmark3/common/mbedtls/platform.c

339 lines
8.9 KiB
C
Raw Normal View History

2018-11-08 00:05:02 +08:00
/*
* Platform abstraction layer
*
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: GPL-2.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#include "mbedtls/platform_util.h"
#if defined(MBEDTLS_PLATFORM_MEMORY)
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
2019-03-10 07:00:59 +08:00
static void *platform_calloc_uninit(size_t n, size_t size)
2018-11-08 00:05:02 +08:00
{
((void) n);
((void) size);
2019-03-10 07:00:59 +08:00
return (NULL);
2018-11-08 00:05:02 +08:00
}
#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
2019-03-10 07:00:59 +08:00
static void platform_free_uninit(void *ptr)
2018-11-08 00:05:02 +08:00
{
((void) ptr);
}
#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
#endif /* !MBEDTLS_PLATFORM_STD_FREE */
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
void *mbedtls_calloc(size_t nmemb, size_t size)
2018-11-08 00:05:02 +08:00
{
2019-03-10 07:00:59 +08:00
return (*mbedtls_calloc_func)(nmemb, size);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
void mbedtls_free(void *ptr)
2018-11-08 00:05:02 +08:00
{
2019-03-10 07:00:59 +08:00
(*mbedtls_free_func)(ptr);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
void (*free_func)(void *))
2018-11-08 00:05:02 +08:00
{
mbedtls_calloc_func = calloc_func;
mbedtls_free_func = free_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_MEMORY */
#if defined(_WIN32)
#include <stdarg.h>
2019-03-10 07:00:59 +08:00
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
2018-11-08 00:05:02 +08:00
{
int ret;
va_list argp;
/* Avoid calling the invalid parameter handler by checking ourselves */
2019-03-10 07:00:59 +08:00
if (s == NULL || n == 0 || fmt == NULL)
return (-1);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
va_start(argp, fmt);
2018-11-08 00:05:02 +08:00
#if defined(_TRUNCATE) && !defined(__MINGW32__)
2019-03-10 07:00:59 +08:00
ret = _vsnprintf_s(s, n, _TRUNCATE, fmt, argp);
2018-11-08 00:05:02 +08:00
#else
2019-03-10 07:00:59 +08:00
ret = _vsnprintf(s, n, fmt, argp);
if (ret < 0 || (size_t) ret == n) {
s[n - 1] = '\0';
2018-11-08 00:05:02 +08:00
ret = -1;
}
#endif
2019-03-10 07:00:59 +08:00
va_end(argp);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return (ret);
2018-11-08 00:05:02 +08:00
}
#endif
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
2019-03-10 07:00:59 +08:00
static int platform_snprintf_uninit(char *s, size_t n,
const char *format, ...)
2018-11-08 00:05:02 +08:00
{
((void) s);
((void) n);
((void) format);
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
2019-03-10 07:00:59 +08:00
int (*mbedtls_snprintf)(char *s, size_t n,
const char *format,
...) = MBEDTLS_PLATFORM_STD_SNPRINTF;
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
const char *format,
...))
2018-11-08 00:05:02 +08:00
{
mbedtls_snprintf = snprintf_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
2019-03-10 07:00:59 +08:00
static int platform_printf_uninit(const char *format, ...)
2018-11-08 00:05:02 +08:00
{
((void) format);
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
2019-03-10 07:00:59 +08:00
int (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
2018-11-08 00:05:02 +08:00
{
mbedtls_printf = printf_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
2019-03-10 07:00:59 +08:00
static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
2018-11-08 00:05:02 +08:00
{
((void) stream);
((void) format);
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
2019-03-10 07:00:59 +08:00
int (*mbedtls_fprintf)(FILE *, const char *, ...) =
MBEDTLS_PLATFORM_STD_FPRINTF;
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
2018-11-08 00:05:02 +08:00
{
mbedtls_fprintf = fprintf_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
2019-03-10 07:00:59 +08:00
static void platform_exit_uninit(int status)
2018-11-08 00:05:02 +08:00
{
((void) status);
}
#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
2019-03-10 07:00:59 +08:00
void (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
int mbedtls_platform_set_exit(void (*exit_func)(int status))
2018-11-08 00:05:02 +08:00
{
mbedtls_exit = exit_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
#if defined(MBEDTLS_HAVE_TIME)
#if defined(MBEDTLS_PLATFORM_TIME_ALT)
#if !defined(MBEDTLS_PLATFORM_STD_TIME)
/*
* Make dummy function to prevent NULL pointer dereferences
*/
2019-03-10 07:00:59 +08:00
static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
2018-11-08 00:05:02 +08:00
{
((void) timer);
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
#endif /* !MBEDTLS_PLATFORM_STD_TIME */
2019-03-10 07:00:59 +08:00
mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
2018-11-08 00:05:02 +08:00
{
mbedtls_time = time_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_TIME_ALT */
#endif /* MBEDTLS_HAVE_TIME */
#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
*/
2019-03-10 07:00:59 +08:00
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
2018-11-08 00:05:02 +08:00
{
FILE *file;
size_t n;
2019-03-10 07:00:59 +08:00
if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL)
return (-1);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
if ((n = fread(buf, 1, buf_len, file)) != buf_len) {
fclose(file);
mbedtls_platform_zeroize(buf, buf_len);
return (-1);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
fclose(file);
return ((int)n);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
2018-11-08 00:05:02 +08:00
{
FILE *file;
size_t n;
2019-03-10 07:00:59 +08:00
if ((file = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL)
2018-11-08 00:05:02 +08:00
return -1;
2019-03-10 07:00:59 +08:00
if ((n = fwrite(buf, 1, buf_len, file)) != buf_len) {
fclose(file);
2018-11-08 00:05:02 +08:00
return -1;
}
2019-03-10 07:00:59 +08:00
fclose(file);
return ((int)n);
2018-11-08 00:05:02 +08: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
*/
2019-03-10 07:00:59 +08:00
static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
2018-11-08 00:05:02 +08:00
{
((void) buf);
((void) buf_len);
2019-03-10 07:00:59 +08:00
return (-1);
2018-11-08 00:05:02 +08: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
*/
2019-03-10 07:00:59 +08:00
static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
2018-11-08 00:05:02 +08:00
{
((void) buf);
((void) buf_len);
2019-03-10 07:00:59 +08:00
return (-1);
2018-11-08 00:05:02 +08:00
}
#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
2019-03-10 07:00:59 +08: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;
2018-11-08 00:05:02 +08:00
int mbedtls_platform_set_nv_seed(
2019-03-10 07:00:59 +08: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))
2018-11-08 00:05:02 +08:00
{
mbedtls_nv_seed_read = nv_seed_read_func;
mbedtls_nv_seed_write = nv_seed_write_func;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
#endif /* MBEDTLS_ENTROPY_NV_SEED */
#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
/*
* Placeholder platform setup that does nothing by default
*/
2019-03-10 07:00:59 +08:00
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
2018-11-08 00:05:02 +08:00
{
(void)ctx;
2019-03-10 07:00:59 +08:00
return (0);
2018-11-08 00:05:02 +08:00
}
/*
* Placeholder platform teardown that does nothing by default
*/
2019-03-10 07:00:59 +08:00
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
2018-11-08 00:05:02 +08:00
{
(void)ctx;
}
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
#endif /* MBEDTLS_PLATFORM_C */