2014-02-01 21:50:07 +00:00
|
|
|
/*
|
|
|
|
* Platform abstraction layer
|
|
|
|
*
|
2020-08-19 14:54:51 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* 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\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* 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.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2014-02-01 21:50:07 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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 21:50:07 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-02-01 21:50:07 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00: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 21:50:07 +00:00
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* 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\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* 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.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* 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.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 11:02:18 +00:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* **********
|
2014-02-01 21:50:07 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
2015-04-08 10:49:31 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 10:39:06 +00:00
|
|
|
#endif
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2017-12-20 11:52:49 +00:00
|
|
|
#if defined(MBEDTLS_ENTROPY_NV_SEED) && \
|
|
|
|
!defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
|
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
|
|
|
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-11 09:26:55 +00: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) && \
|
|
|
|
!( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
|
|
|
|
defined(MBEDTLS_PLATFORM_FREE_MACRO) )
|
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
|
|
|
static void *platform_calloc_uninit( size_t n, size_t size )
|
2014-02-04 16:30:24 +00:00
|
|
|
{
|
2015-05-26 14:15:20 +00:00
|
|
|
((void) n);
|
|
|
|
((void) size);
|
2014-02-04 16:30:24 +00:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
|
2014-02-04 16:30:24 +00:00
|
|
|
static void platform_free_uninit( void *ptr )
|
|
|
|
{
|
|
|
|
((void) ptr);
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_FREE */
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
|
2015-04-08 10:49:31 +00:00
|
|
|
void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-05-26 14:15:20 +00:00
|
|
|
int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
|
2014-02-04 16:30:24 +00:00
|
|
|
void (*free_func)( void * ) )
|
|
|
|
{
|
2015-05-26 14:15:20 +00:00
|
|
|
mbedtls_calloc = calloc_func;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_free = free_func;
|
2014-02-04 16:30:24 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2018-10-11 09:26:55 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_MEMORY &&
|
|
|
|
!( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
|
|
|
|
defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
|
2014-02-04 16:30:24 +00:00
|
|
|
|
2015-06-22 08:23:34 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <stdarg.h>
|
|
|
|
int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list argp;
|
|
|
|
|
2015-06-26 15:45:00 +00:00
|
|
|
/* Avoid calling the invalid parameter handler by checking ourselves */
|
|
|
|
if( s == NULL || n == 0 || fmt == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-06-22 08:23:34 +00:00
|
|
|
va_start( argp, fmt );
|
2017-09-06 14:49:10 +00:00
|
|
|
#if defined(_TRUNCATE) && !defined(__MINGW32__)
|
2015-06-22 08:23:34 +00:00
|
|
|
ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
|
2015-07-08 14:46:13 +00:00
|
|
|
#else
|
|
|
|
ret = _vsnprintf( s, n, fmt, argp );
|
|
|
|
if( ret < 0 || (size_t) ret == n )
|
|
|
|
{
|
|
|
|
s[n-1] = '\0';
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-22 08:23:34 +00:00
|
|
|
va_end( argp );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
|
2015-01-30 10:47:32 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static int platform_snprintf_uninit( char * s, size_t n,
|
|
|
|
const char * format, ... )
|
|
|
|
{
|
|
|
|
((void) s);
|
|
|
|
((void) n);
|
2015-06-03 09:20:33 +00:00
|
|
|
((void) format);
|
2015-01-30 10:47:32 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
|
2015-01-30 10:47:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int (*mbedtls_snprintf)( char * s, size_t n,
|
2015-01-30 10:47:32 +00:00
|
|
|
const char * format,
|
2015-04-08 10:49:31 +00:00
|
|
|
... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
|
2015-01-30 10:47:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
|
2015-01-30 10:47:32 +00:00
|
|
|
const char * format,
|
|
|
|
... ) )
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_snprintf = snprintf_func;
|
2015-01-30 10:47:32 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
|
2015-01-30 10:47:32 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
|
2014-02-01 21:50:07 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static int platform_printf_uninit( const char *format, ... )
|
|
|
|
{
|
|
|
|
((void) format);
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
|
2014-02-01 21:50:07 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_printf = printf_func;
|
2014-02-01 21:50:07 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
|
2014-02-01 21:50:07 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
|
|
|
|
{
|
|
|
|
((void) stream);
|
|
|
|
((void) format);
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
|
|
|
|
MBEDTLS_PLATFORM_STD_FPRINTF;
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
|
2014-02-01 21:50:07 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_fprintf = fprintf_func;
|
2014-02-01 21:50:07 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
|
2014-02-01 21:50:07 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_EXIT_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_EXIT)
|
2015-01-30 12:01:34 +00:00
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static void platform_exit_uninit( int status )
|
|
|
|
{
|
|
|
|
((void) status);
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_EXIT */
|
2015-01-30 12:01:34 +00:00
|
|
|
|
2015-06-03 09:33:55 +00:00
|
|
|
void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
|
2015-01-30 12:01:34 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
|
2015-01-30 12:01:34 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_exit = exit_func;
|
2015-01-30 12:01:34 +00:00
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_EXIT_ALT */
|
2015-01-30 12:01:34 +00:00
|
|
|
|
2016-07-13 12:31:08 +00:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
|
2016-04-26 06:43:27 +00:00
|
|
|
#if defined(MBEDTLS_PLATFORM_TIME_ALT)
|
|
|
|
#if !defined(MBEDTLS_PLATFORM_STD_TIME)
|
|
|
|
/*
|
|
|
|
* Make dummy function to prevent NULL pointer dereferences
|
|
|
|
*/
|
|
|
|
static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
|
|
|
|
{
|
|
|
|
((void) timer);
|
2016-04-26 18:51:29 +00:00
|
|
|
return( 0 );
|
2016-04-26 06:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_TIME */
|
|
|
|
|
2016-04-26 18:51:29 +00:00
|
|
|
mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
|
2016-04-26 06:43:27 +00:00
|
|
|
|
2016-04-26 18:51:29 +00:00
|
|
|
int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
|
2016-04-26 06:43:27 +00:00
|
|
|
{
|
|
|
|
mbedtls_time = time_func;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_TIME_ALT */
|
|
|
|
|
2016-07-13 12:31:08 +00:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2016-06-01 10:25:44 +00: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
|
|
|
|
*/
|
|
|
|
int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
|
2017-06-26 10:10:22 +00:00
|
|
|
return( -1 );
|
2016-06-01 10:25:44 +00:00
|
|
|
|
|
|
|
if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
|
|
|
|
{
|
|
|
|
fclose( file );
|
2017-06-26 10:10:22 +00:00
|
|
|
mbedtls_zeroize( buf, buf_len );
|
|
|
|
return( -1 );
|
2016-06-01 10:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( file );
|
2016-11-03 01:11:37 +00:00
|
|
|
return( (int)n );
|
2016-06-01 10:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
|
|
|
|
{
|
|
|
|
fclose( file );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( file );
|
2016-11-03 01:11:37 +00:00
|
|
|
return( (int)n );
|
2016-06-01 10:25:44 +00: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
|
|
|
|
*/
|
|
|
|
static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
|
|
|
|
{
|
|
|
|
((void) buf);
|
|
|
|
((void) buf_len);
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
*/
|
|
|
|
static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
|
|
|
|
{
|
|
|
|
((void) buf);
|
|
|
|
((void) buf_len);
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
|
|
|
|
#endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
int mbedtls_platform_set_nv_seed(
|
|
|
|
int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
|
|
|
|
int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
|
|
|
|
{
|
|
|
|
mbedtls_nv_seed_read = nv_seed_read_func;
|
|
|
|
mbedtls_nv_seed_write = nv_seed_write_func;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
|
|
|
|
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
|
|
|
|
2017-07-18 09:23:04 +00:00
|
|
|
#if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
|
2017-07-07 12:03:23 +00:00
|
|
|
/*
|
2017-07-12 10:25:17 +00:00
|
|
|
* Placeholder platform setup that does nothing by default
|
2017-07-07 12:03:23 +00:00
|
|
|
*/
|
2017-07-12 10:25:17 +00:00
|
|
|
int mbedtls_platform_setup( mbedtls_platform_context *ctx )
|
2017-07-07 12:03:23 +00:00
|
|
|
{
|
|
|
|
(void)ctx;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-12 10:25:17 +00:00
|
|
|
* Placeholder platform teardown that does nothing by default
|
2017-07-07 12:03:23 +00:00
|
|
|
*/
|
2017-07-12 10:25:17 +00:00
|
|
|
void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
|
2017-07-07 12:03:23 +00:00
|
|
|
{
|
|
|
|
(void)ctx;
|
|
|
|
}
|
2017-07-18 09:23:04 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
|
2017-07-07 12:03:23 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|