mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /*
kenjiArai 0:5b88d5760320 2 * Platform abstraction layer
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
kenjiArai 0:5b88d5760320 5 * SPDX-License-Identifier: Apache-2.0
kenjiArai 0:5b88d5760320 6 *
kenjiArai 0:5b88d5760320 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kenjiArai 0:5b88d5760320 8 * not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 9 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 12 *
kenjiArai 0:5b88d5760320 13 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kenjiArai 0:5b88d5760320 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 16 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 17 * limitations under the License.
kenjiArai 0:5b88d5760320 18 *
kenjiArai 0:5b88d5760320 19 * This file is part of mbed TLS (https://tls.mbed.org)
kenjiArai 0:5b88d5760320 20 */
kenjiArai 0:5b88d5760320 21
kenjiArai 0:5b88d5760320 22 #if !defined(MBEDTLS_CONFIG_FILE)
kenjiArai 0:5b88d5760320 23 #include "mbedtls/config.h"
kenjiArai 0:5b88d5760320 24 #else
kenjiArai 0:5b88d5760320 25 #include MBEDTLS_CONFIG_FILE
kenjiArai 0:5b88d5760320 26 #endif
kenjiArai 0:5b88d5760320 27
kenjiArai 0:5b88d5760320 28 #if defined(MBEDTLS_PLATFORM_C)
kenjiArai 0:5b88d5760320 29
kenjiArai 0:5b88d5760320 30 #include "mbedtls/platform.h"
kenjiArai 0:5b88d5760320 31 #include "mbedtls/platform_util.h"
kenjiArai 0:5b88d5760320 32
kenjiArai 0:5b88d5760320 33 /* The compile time configuration of memory allocation via the macros
kenjiArai 0:5b88d5760320 34 * MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO takes precedence over the runtime
kenjiArai 0:5b88d5760320 35 * configuration via mbedtls_platform_set_calloc_free(). So, omit everything
kenjiArai 0:5b88d5760320 36 * related to the latter if MBEDTLS_PLATFORM_{FREE/CALLOC}_MACRO are defined. */
kenjiArai 0:5b88d5760320 37 #if defined(MBEDTLS_PLATFORM_MEMORY) && \
kenjiArai 0:5b88d5760320 38 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && \
kenjiArai 0:5b88d5760320 39 defined(MBEDTLS_PLATFORM_FREE_MACRO) )
kenjiArai 0:5b88d5760320 40
kenjiArai 0:5b88d5760320 41 #if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
kenjiArai 0:5b88d5760320 42 static void *platform_calloc_uninit( size_t n, size_t size )
kenjiArai 0:5b88d5760320 43 {
kenjiArai 0:5b88d5760320 44 ((void) n);
kenjiArai 0:5b88d5760320 45 ((void) size);
kenjiArai 0:5b88d5760320 46 return( NULL );
kenjiArai 0:5b88d5760320 47 }
kenjiArai 0:5b88d5760320 48
kenjiArai 0:5b88d5760320 49 #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit
kenjiArai 0:5b88d5760320 50 #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
kenjiArai 0:5b88d5760320 51
kenjiArai 0:5b88d5760320 52 #if !defined(MBEDTLS_PLATFORM_STD_FREE)
kenjiArai 0:5b88d5760320 53 static void platform_free_uninit( void *ptr )
kenjiArai 0:5b88d5760320 54 {
kenjiArai 0:5b88d5760320 55 ((void) ptr);
kenjiArai 0:5b88d5760320 56 }
kenjiArai 0:5b88d5760320 57
kenjiArai 0:5b88d5760320 58 #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit
kenjiArai 0:5b88d5760320 59 #endif /* !MBEDTLS_PLATFORM_STD_FREE */
kenjiArai 0:5b88d5760320 60
kenjiArai 0:5b88d5760320 61 static void * (*mbedtls_calloc_func)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
kenjiArai 0:5b88d5760320 62 static void (*mbedtls_free_func)( void * ) = MBEDTLS_PLATFORM_STD_FREE;
kenjiArai 0:5b88d5760320 63
kenjiArai 0:5b88d5760320 64 void * mbedtls_calloc( size_t nmemb, size_t size )
kenjiArai 0:5b88d5760320 65 {
kenjiArai 0:5b88d5760320 66 return (*mbedtls_calloc_func)( nmemb, size );
kenjiArai 0:5b88d5760320 67 }
kenjiArai 0:5b88d5760320 68
kenjiArai 0:5b88d5760320 69 void mbedtls_free( void * ptr )
kenjiArai 0:5b88d5760320 70 {
kenjiArai 0:5b88d5760320 71 (*mbedtls_free_func)( ptr );
kenjiArai 0:5b88d5760320 72 }
kenjiArai 0:5b88d5760320 73
kenjiArai 0:5b88d5760320 74 int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
kenjiArai 0:5b88d5760320 75 void (*free_func)( void * ) )
kenjiArai 0:5b88d5760320 76 {
kenjiArai 0:5b88d5760320 77 mbedtls_calloc_func = calloc_func;
kenjiArai 0:5b88d5760320 78 mbedtls_free_func = free_func;
kenjiArai 0:5b88d5760320 79 return( 0 );
kenjiArai 0:5b88d5760320 80 }
kenjiArai 0:5b88d5760320 81 #endif /* MBEDTLS_PLATFORM_MEMORY &&
kenjiArai 0:5b88d5760320 82 !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) &&
kenjiArai 0:5b88d5760320 83 defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */
kenjiArai 0:5b88d5760320 84
kenjiArai 0:5b88d5760320 85 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF)
kenjiArai 0:5b88d5760320 86 #include <stdarg.h>
kenjiArai 0:5b88d5760320 87 int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... )
kenjiArai 0:5b88d5760320 88 {
kenjiArai 0:5b88d5760320 89 int ret;
kenjiArai 0:5b88d5760320 90 va_list argp;
kenjiArai 0:5b88d5760320 91
kenjiArai 0:5b88d5760320 92 va_start( argp, fmt );
kenjiArai 0:5b88d5760320 93 ret = mbedtls_vsnprintf( s, n, fmt, argp );
kenjiArai 0:5b88d5760320 94 va_end( argp );
kenjiArai 0:5b88d5760320 95
kenjiArai 0:5b88d5760320 96 return( ret );
kenjiArai 0:5b88d5760320 97 }
kenjiArai 0:5b88d5760320 98 #endif
kenjiArai 0:5b88d5760320 99
kenjiArai 0:5b88d5760320 100 #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT)
kenjiArai 0:5b88d5760320 101 #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF)
kenjiArai 0:5b88d5760320 102 /*
kenjiArai 0:5b88d5760320 103 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 104 */
kenjiArai 0:5b88d5760320 105 static int platform_snprintf_uninit( char * s, size_t n,
kenjiArai 0:5b88d5760320 106 const char * format, ... )
kenjiArai 0:5b88d5760320 107 {
kenjiArai 0:5b88d5760320 108 ((void) s);
kenjiArai 0:5b88d5760320 109 ((void) n);
kenjiArai 0:5b88d5760320 110 ((void) format);
kenjiArai 0:5b88d5760320 111 return( 0 );
kenjiArai 0:5b88d5760320 112 }
kenjiArai 0:5b88d5760320 113
kenjiArai 0:5b88d5760320 114 #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
kenjiArai 0:5b88d5760320 115 #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */
kenjiArai 0:5b88d5760320 116
kenjiArai 0:5b88d5760320 117 int (*mbedtls_snprintf)( char * s, size_t n,
kenjiArai 0:5b88d5760320 118 const char * format,
kenjiArai 0:5b88d5760320 119 ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF;
kenjiArai 0:5b88d5760320 120
kenjiArai 0:5b88d5760320 121 int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
kenjiArai 0:5b88d5760320 122 const char * format,
kenjiArai 0:5b88d5760320 123 ... ) )
kenjiArai 0:5b88d5760320 124 {
kenjiArai 0:5b88d5760320 125 mbedtls_snprintf = snprintf_func;
kenjiArai 0:5b88d5760320 126 return( 0 );
kenjiArai 0:5b88d5760320 127 }
kenjiArai 0:5b88d5760320 128 #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */
kenjiArai 0:5b88d5760320 129
kenjiArai 0:5b88d5760320 130 #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF)
kenjiArai 0:5b88d5760320 131 #include <stdarg.h>
kenjiArai 0:5b88d5760320 132 int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
kenjiArai 0:5b88d5760320 133 {
kenjiArai 0:5b88d5760320 134 int ret;
kenjiArai 0:5b88d5760320 135
kenjiArai 0:5b88d5760320 136 /* Avoid calling the invalid parameter handler by checking ourselves */
kenjiArai 0:5b88d5760320 137 if( s == NULL || n == 0 || fmt == NULL )
kenjiArai 0:5b88d5760320 138 return( -1 );
kenjiArai 0:5b88d5760320 139
kenjiArai 0:5b88d5760320 140 #if defined(_TRUNCATE)
kenjiArai 0:5b88d5760320 141 ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg );
kenjiArai 0:5b88d5760320 142 #else
kenjiArai 0:5b88d5760320 143 ret = vsnprintf( s, n, fmt, arg );
kenjiArai 0:5b88d5760320 144 if( ret < 0 || (size_t) ret == n )
kenjiArai 0:5b88d5760320 145 {
kenjiArai 0:5b88d5760320 146 s[n-1] = '\0';
kenjiArai 0:5b88d5760320 147 ret = -1;
kenjiArai 0:5b88d5760320 148 }
kenjiArai 0:5b88d5760320 149 #endif
kenjiArai 0:5b88d5760320 150
kenjiArai 0:5b88d5760320 151 return( ret );
kenjiArai 0:5b88d5760320 152 }
kenjiArai 0:5b88d5760320 153 #endif
kenjiArai 0:5b88d5760320 154
kenjiArai 0:5b88d5760320 155 #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT)
kenjiArai 0:5b88d5760320 156 #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF)
kenjiArai 0:5b88d5760320 157 /*
kenjiArai 0:5b88d5760320 158 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 159 */
kenjiArai 0:5b88d5760320 160 static int platform_vsnprintf_uninit( char * s, size_t n,
kenjiArai 0:5b88d5760320 161 const char * format, va_list arg )
kenjiArai 0:5b88d5760320 162 {
kenjiArai 0:5b88d5760320 163 ((void) s);
kenjiArai 0:5b88d5760320 164 ((void) n);
kenjiArai 0:5b88d5760320 165 ((void) format);
kenjiArai 0:5b88d5760320 166 ((void) arg);
kenjiArai 0:5b88d5760320 167 return( -1 );
kenjiArai 0:5b88d5760320 168 }
kenjiArai 0:5b88d5760320 169
kenjiArai 0:5b88d5760320 170 #define MBEDTLS_PLATFORM_STD_VSNPRINTF platform_vsnprintf_uninit
kenjiArai 0:5b88d5760320 171 #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */
kenjiArai 0:5b88d5760320 172
kenjiArai 0:5b88d5760320 173 int (*mbedtls_vsnprintf)( char * s, size_t n,
kenjiArai 0:5b88d5760320 174 const char * format,
kenjiArai 0:5b88d5760320 175 va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF;
kenjiArai 0:5b88d5760320 176
kenjiArai 0:5b88d5760320 177 int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n,
kenjiArai 0:5b88d5760320 178 const char * format,
kenjiArai 0:5b88d5760320 179 va_list arg ) )
kenjiArai 0:5b88d5760320 180 {
kenjiArai 0:5b88d5760320 181 mbedtls_vsnprintf = vsnprintf_func;
kenjiArai 0:5b88d5760320 182 return( 0 );
kenjiArai 0:5b88d5760320 183 }
kenjiArai 0:5b88d5760320 184 #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */
kenjiArai 0:5b88d5760320 185
kenjiArai 0:5b88d5760320 186 #if defined(MBEDTLS_PLATFORM_PRINTF_ALT)
kenjiArai 0:5b88d5760320 187 #if !defined(MBEDTLS_PLATFORM_STD_PRINTF)
kenjiArai 0:5b88d5760320 188 /*
kenjiArai 0:5b88d5760320 189 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 190 */
kenjiArai 0:5b88d5760320 191 static int platform_printf_uninit( const char *format, ... )
kenjiArai 0:5b88d5760320 192 {
kenjiArai 0:5b88d5760320 193 ((void) format);
kenjiArai 0:5b88d5760320 194 return( 0 );
kenjiArai 0:5b88d5760320 195 }
kenjiArai 0:5b88d5760320 196
kenjiArai 0:5b88d5760320 197 #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit
kenjiArai 0:5b88d5760320 198 #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */
kenjiArai 0:5b88d5760320 199
kenjiArai 0:5b88d5760320 200 int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF;
kenjiArai 0:5b88d5760320 201
kenjiArai 0:5b88d5760320 202 int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) )
kenjiArai 0:5b88d5760320 203 {
kenjiArai 0:5b88d5760320 204 mbedtls_printf = printf_func;
kenjiArai 0:5b88d5760320 205 return( 0 );
kenjiArai 0:5b88d5760320 206 }
kenjiArai 0:5b88d5760320 207 #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */
kenjiArai 0:5b88d5760320 208
kenjiArai 0:5b88d5760320 209 #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT)
kenjiArai 0:5b88d5760320 210 #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF)
kenjiArai 0:5b88d5760320 211 /*
kenjiArai 0:5b88d5760320 212 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 213 */
kenjiArai 0:5b88d5760320 214 static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
kenjiArai 0:5b88d5760320 215 {
kenjiArai 0:5b88d5760320 216 ((void) stream);
kenjiArai 0:5b88d5760320 217 ((void) format);
kenjiArai 0:5b88d5760320 218 return( 0 );
kenjiArai 0:5b88d5760320 219 }
kenjiArai 0:5b88d5760320 220
kenjiArai 0:5b88d5760320 221 #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit
kenjiArai 0:5b88d5760320 222 #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */
kenjiArai 0:5b88d5760320 223
kenjiArai 0:5b88d5760320 224 int (*mbedtls_fprintf)( FILE *, const char *, ... ) =
kenjiArai 0:5b88d5760320 225 MBEDTLS_PLATFORM_STD_FPRINTF;
kenjiArai 0:5b88d5760320 226
kenjiArai 0:5b88d5760320 227 int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
kenjiArai 0:5b88d5760320 228 {
kenjiArai 0:5b88d5760320 229 mbedtls_fprintf = fprintf_func;
kenjiArai 0:5b88d5760320 230 return( 0 );
kenjiArai 0:5b88d5760320 231 }
kenjiArai 0:5b88d5760320 232 #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */
kenjiArai 0:5b88d5760320 233
kenjiArai 0:5b88d5760320 234 #if defined(MBEDTLS_PLATFORM_EXIT_ALT)
kenjiArai 0:5b88d5760320 235 #if !defined(MBEDTLS_PLATFORM_STD_EXIT)
kenjiArai 0:5b88d5760320 236 /*
kenjiArai 0:5b88d5760320 237 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 238 */
kenjiArai 0:5b88d5760320 239 static void platform_exit_uninit( int status )
kenjiArai 0:5b88d5760320 240 {
kenjiArai 0:5b88d5760320 241 ((void) status);
kenjiArai 0:5b88d5760320 242 }
kenjiArai 0:5b88d5760320 243
kenjiArai 0:5b88d5760320 244 #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit
kenjiArai 0:5b88d5760320 245 #endif /* !MBEDTLS_PLATFORM_STD_EXIT */
kenjiArai 0:5b88d5760320 246
kenjiArai 0:5b88d5760320 247 void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT;
kenjiArai 0:5b88d5760320 248
kenjiArai 0:5b88d5760320 249 int mbedtls_platform_set_exit( void (*exit_func)( int status ) )
kenjiArai 0:5b88d5760320 250 {
kenjiArai 0:5b88d5760320 251 mbedtls_exit = exit_func;
kenjiArai 0:5b88d5760320 252 return( 0 );
kenjiArai 0:5b88d5760320 253 }
kenjiArai 0:5b88d5760320 254 #endif /* MBEDTLS_PLATFORM_EXIT_ALT */
kenjiArai 0:5b88d5760320 255
kenjiArai 0:5b88d5760320 256 #if defined(MBEDTLS_HAVE_TIME)
kenjiArai 0:5b88d5760320 257
kenjiArai 0:5b88d5760320 258 #if defined(MBEDTLS_PLATFORM_TIME_ALT)
kenjiArai 0:5b88d5760320 259 #if !defined(MBEDTLS_PLATFORM_STD_TIME)
kenjiArai 0:5b88d5760320 260 /*
kenjiArai 0:5b88d5760320 261 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 262 */
kenjiArai 0:5b88d5760320 263 static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer )
kenjiArai 0:5b88d5760320 264 {
kenjiArai 0:5b88d5760320 265 ((void) timer);
kenjiArai 0:5b88d5760320 266 return( 0 );
kenjiArai 0:5b88d5760320 267 }
kenjiArai 0:5b88d5760320 268
kenjiArai 0:5b88d5760320 269 #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit
kenjiArai 0:5b88d5760320 270 #endif /* !MBEDTLS_PLATFORM_STD_TIME */
kenjiArai 0:5b88d5760320 271
kenjiArai 0:5b88d5760320 272 mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME;
kenjiArai 0:5b88d5760320 273
kenjiArai 0:5b88d5760320 274 int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) )
kenjiArai 0:5b88d5760320 275 {
kenjiArai 0:5b88d5760320 276 mbedtls_time = time_func;
kenjiArai 0:5b88d5760320 277 return( 0 );
kenjiArai 0:5b88d5760320 278 }
kenjiArai 0:5b88d5760320 279 #endif /* MBEDTLS_PLATFORM_TIME_ALT */
kenjiArai 0:5b88d5760320 280
kenjiArai 0:5b88d5760320 281 #endif /* MBEDTLS_HAVE_TIME */
kenjiArai 0:5b88d5760320 282
kenjiArai 0:5b88d5760320 283 #if defined(MBEDTLS_ENTROPY_NV_SEED)
kenjiArai 0:5b88d5760320 284 #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO)
kenjiArai 0:5b88d5760320 285 /* Default implementations for the platform independent seed functions use
kenjiArai 0:5b88d5760320 286 * standard libc file functions to read from and write to a pre-defined filename
kenjiArai 0:5b88d5760320 287 */
kenjiArai 0:5b88d5760320 288 int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len )
kenjiArai 0:5b88d5760320 289 {
kenjiArai 0:5b88d5760320 290 FILE *file;
kenjiArai 0:5b88d5760320 291 size_t n;
kenjiArai 0:5b88d5760320 292
kenjiArai 0:5b88d5760320 293 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL )
kenjiArai 0:5b88d5760320 294 return( -1 );
kenjiArai 0:5b88d5760320 295
kenjiArai 0:5b88d5760320 296 if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len )
kenjiArai 0:5b88d5760320 297 {
kenjiArai 0:5b88d5760320 298 fclose( file );
kenjiArai 0:5b88d5760320 299 mbedtls_platform_zeroize( buf, buf_len );
kenjiArai 0:5b88d5760320 300 return( -1 );
kenjiArai 0:5b88d5760320 301 }
kenjiArai 0:5b88d5760320 302
kenjiArai 0:5b88d5760320 303 fclose( file );
kenjiArai 0:5b88d5760320 304 return( (int)n );
kenjiArai 0:5b88d5760320 305 }
kenjiArai 0:5b88d5760320 306
kenjiArai 0:5b88d5760320 307 int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len )
kenjiArai 0:5b88d5760320 308 {
kenjiArai 0:5b88d5760320 309 FILE *file;
kenjiArai 0:5b88d5760320 310 size_t n;
kenjiArai 0:5b88d5760320 311
kenjiArai 0:5b88d5760320 312 if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL )
kenjiArai 0:5b88d5760320 313 return -1;
kenjiArai 0:5b88d5760320 314
kenjiArai 0:5b88d5760320 315 if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len )
kenjiArai 0:5b88d5760320 316 {
kenjiArai 0:5b88d5760320 317 fclose( file );
kenjiArai 0:5b88d5760320 318 return -1;
kenjiArai 0:5b88d5760320 319 }
kenjiArai 0:5b88d5760320 320
kenjiArai 0:5b88d5760320 321 fclose( file );
kenjiArai 0:5b88d5760320 322 return( (int)n );
kenjiArai 0:5b88d5760320 323 }
kenjiArai 0:5b88d5760320 324 #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */
kenjiArai 0:5b88d5760320 325
kenjiArai 0:5b88d5760320 326 #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT)
kenjiArai 0:5b88d5760320 327 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ)
kenjiArai 0:5b88d5760320 328 /*
kenjiArai 0:5b88d5760320 329 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 330 */
kenjiArai 0:5b88d5760320 331 static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len )
kenjiArai 0:5b88d5760320 332 {
kenjiArai 0:5b88d5760320 333 ((void) buf);
kenjiArai 0:5b88d5760320 334 ((void) buf_len);
kenjiArai 0:5b88d5760320 335 return( -1 );
kenjiArai 0:5b88d5760320 336 }
kenjiArai 0:5b88d5760320 337
kenjiArai 0:5b88d5760320 338 #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit
kenjiArai 0:5b88d5760320 339 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */
kenjiArai 0:5b88d5760320 340
kenjiArai 0:5b88d5760320 341 #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE)
kenjiArai 0:5b88d5760320 342 /*
kenjiArai 0:5b88d5760320 343 * Make dummy function to prevent NULL pointer dereferences
kenjiArai 0:5b88d5760320 344 */
kenjiArai 0:5b88d5760320 345 static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len )
kenjiArai 0:5b88d5760320 346 {
kenjiArai 0:5b88d5760320 347 ((void) buf);
kenjiArai 0:5b88d5760320 348 ((void) buf_len);
kenjiArai 0:5b88d5760320 349 return( -1 );
kenjiArai 0:5b88d5760320 350 }
kenjiArai 0:5b88d5760320 351
kenjiArai 0:5b88d5760320 352 #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit
kenjiArai 0:5b88d5760320 353 #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */
kenjiArai 0:5b88d5760320 354
kenjiArai 0:5b88d5760320 355 int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) =
kenjiArai 0:5b88d5760320 356 MBEDTLS_PLATFORM_STD_NV_SEED_READ;
kenjiArai 0:5b88d5760320 357 int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) =
kenjiArai 0:5b88d5760320 358 MBEDTLS_PLATFORM_STD_NV_SEED_WRITE;
kenjiArai 0:5b88d5760320 359
kenjiArai 0:5b88d5760320 360 int mbedtls_platform_set_nv_seed(
kenjiArai 0:5b88d5760320 361 int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ),
kenjiArai 0:5b88d5760320 362 int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) )
kenjiArai 0:5b88d5760320 363 {
kenjiArai 0:5b88d5760320 364 mbedtls_nv_seed_read = nv_seed_read_func;
kenjiArai 0:5b88d5760320 365 mbedtls_nv_seed_write = nv_seed_write_func;
kenjiArai 0:5b88d5760320 366 return( 0 );
kenjiArai 0:5b88d5760320 367 }
kenjiArai 0:5b88d5760320 368 #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */
kenjiArai 0:5b88d5760320 369 #endif /* MBEDTLS_ENTROPY_NV_SEED */
kenjiArai 0:5b88d5760320 370
kenjiArai 0:5b88d5760320 371 #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
kenjiArai 0:5b88d5760320 372 /*
kenjiArai 0:5b88d5760320 373 * Placeholder platform setup that does nothing by default
kenjiArai 0:5b88d5760320 374 */
kenjiArai 0:5b88d5760320 375 int mbedtls_platform_setup( mbedtls_platform_context *ctx )
kenjiArai 0:5b88d5760320 376 {
kenjiArai 0:5b88d5760320 377 (void)ctx;
kenjiArai 0:5b88d5760320 378
kenjiArai 0:5b88d5760320 379 return( 0 );
kenjiArai 0:5b88d5760320 380 }
kenjiArai 0:5b88d5760320 381
kenjiArai 0:5b88d5760320 382 /*
kenjiArai 0:5b88d5760320 383 * Placeholder platform teardown that does nothing by default
kenjiArai 0:5b88d5760320 384 */
kenjiArai 0:5b88d5760320 385 void mbedtls_platform_teardown( mbedtls_platform_context *ctx )
kenjiArai 0:5b88d5760320 386 {
kenjiArai 0:5b88d5760320 387 (void)ctx;
kenjiArai 0:5b88d5760320 388 }
kenjiArai 0:5b88d5760320 389 #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
kenjiArai 0:5b88d5760320 390
kenjiArai 0:5b88d5760320 391 #endif /* MBEDTLS_PLATFORM_C */