Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Platform abstraction layer |
| 3 | * |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame^] | 31 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | a268da9 | 2017-12-20 12:52:49 +0100 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_PLATFORM_MEMORY) |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 34 | #if !defined(MBEDTLS_PLATFORM_STD_CALLOC) |
| 35 | static void *platform_calloc_uninit( size_t n, size_t size ) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 36 | { |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 37 | ((void) n); |
| 38 | ((void) size); |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 39 | return( NULL ); |
| 40 | } |
| 41 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 42 | #define MBEDTLS_PLATFORM_STD_CALLOC platform_calloc_uninit |
| 43 | #endif /* !MBEDTLS_PLATFORM_STD_CALLOC */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if !defined(MBEDTLS_PLATFORM_STD_FREE) |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 46 | static void platform_free_uninit( void *ptr ) |
| 47 | { |
| 48 | ((void) ptr); |
| 49 | } |
| 50 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #define MBEDTLS_PLATFORM_STD_FREE platform_free_uninit |
| 52 | #endif /* !MBEDTLS_PLATFORM_STD_FREE */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 54 | void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | void (*mbedtls_free)( void * ) = MBEDTLS_PLATFORM_STD_FREE; |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 57 | int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 58 | void (*free_func)( void * ) ) |
| 59 | { |
Manuel Pégourié-Gonnard | b9ef118 | 2015-05-26 16:15:20 +0200 | [diff] [blame] | 60 | mbedtls_calloc = calloc_func; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | mbedtls_free = free_func; |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 62 | return( 0 ); |
| 63 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #endif /* MBEDTLS_PLATFORM_MEMORY */ |
Paul Bakker | defc0ca | 2014-02-04 17:30:24 +0100 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 66 | #if defined(_WIN32) |
| 67 | #include <stdarg.h> |
| 68 | int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) |
| 69 | { |
| 70 | int ret; |
| 71 | va_list argp; |
| 72 | |
Manuel Pégourié-Gonnard | f659d2c | 2015-06-26 17:45:00 +0200 | [diff] [blame] | 73 | /* Avoid calling the invalid parameter handler by checking ourselves */ |
| 74 | if( s == NULL || n == 0 || fmt == NULL ) |
| 75 | return( -1 ); |
| 76 | |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 77 | va_start( argp, fmt ); |
Ron Eldor | bc18eb3 | 2017-09-06 17:49:10 +0300 | [diff] [blame] | 78 | #if defined(_TRUNCATE) && !defined(__MINGW32__) |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 79 | ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); |
Manuel Pégourié-Gonnard | a4f055f | 2015-07-08 16:46:13 +0200 | [diff] [blame] | 80 | #else |
| 81 | ret = _vsnprintf( s, n, fmt, argp ); |
| 82 | if( ret < 0 || (size_t) ret == n ) |
| 83 | { |
| 84 | s[n-1] = '\0'; |
| 85 | ret = -1; |
| 86 | } |
| 87 | #endif |
Manuel Pégourié-Gonnard | 6c0c8e0 | 2015-06-22 10:23:34 +0200 | [diff] [blame] | 88 | va_end( argp ); |
| 89 | |
| 90 | return( ret ); |
| 91 | } |
| 92 | #endif |
| 93 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | #if defined(MBEDTLS_PLATFORM_SNPRINTF_ALT) |
| 95 | #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 96 | /* |
| 97 | * Make dummy function to prevent NULL pointer dereferences |
| 98 | */ |
| 99 | static int platform_snprintf_uninit( char * s, size_t n, |
| 100 | const char * format, ... ) |
| 101 | { |
| 102 | ((void) s); |
| 103 | ((void) n); |
Manuel Pégourié-Gonnard | dccb80b | 2015-06-03 10:20:33 +0100 | [diff] [blame] | 104 | ((void) format); |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 105 | return( 0 ); |
| 106 | } |
| 107 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | #define MBEDTLS_PLATFORM_STD_SNPRINTF platform_snprintf_uninit |
| 109 | #endif /* !MBEDTLS_PLATFORM_STD_SNPRINTF */ |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | int (*mbedtls_snprintf)( char * s, size_t n, |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 112 | const char * format, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | ... ) = MBEDTLS_PLATFORM_STD_SNPRINTF; |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 116 | const char * format, |
| 117 | ... ) ) |
| 118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | mbedtls_snprintf = snprintf_func; |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 120 | return( 0 ); |
| 121 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ |
Rich Evans | 46b0a8d | 2015-01-30 10:47:32 +0000 | [diff] [blame] | 123 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) |
| 125 | #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 126 | /* |
| 127 | * Make dummy function to prevent NULL pointer dereferences |
| 128 | */ |
| 129 | static int platform_printf_uninit( const char *format, ... ) |
| 130 | { |
| 131 | ((void) format); |
| 132 | return( 0 ); |
| 133 | } |
| 134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | #define MBEDTLS_PLATFORM_STD_PRINTF platform_printf_uninit |
| 136 | #endif /* !MBEDTLS_PLATFORM_STD_PRINTF */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | int (*mbedtls_printf)( const char *, ... ) = MBEDTLS_PLATFORM_STD_PRINTF; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 141 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | mbedtls_printf = printf_func; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 143 | return( 0 ); |
| 144 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | #endif /* MBEDTLS_PLATFORM_PRINTF_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_PLATFORM_FPRINTF_ALT) |
| 148 | #if !defined(MBEDTLS_PLATFORM_STD_FPRINTF) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 149 | /* |
| 150 | * Make dummy function to prevent NULL pointer dereferences |
| 151 | */ |
| 152 | static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) |
| 153 | { |
| 154 | ((void) stream); |
| 155 | ((void) format); |
| 156 | return( 0 ); |
| 157 | } |
| 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #define MBEDTLS_PLATFORM_STD_FPRINTF platform_fprintf_uninit |
| 160 | #endif /* !MBEDTLS_PLATFORM_STD_FPRINTF */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | int (*mbedtls_fprintf)( FILE *, const char *, ... ) = |
| 163 | MBEDTLS_PLATFORM_STD_FPRINTF; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | int mbedtls_platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | mbedtls_fprintf = fprintf_func; |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 168 | return( 0 ); |
| 169 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | #endif /* MBEDTLS_PLATFORM_FPRINTF_ALT */ |
Paul Bakker | 747a83a | 2014-02-01 22:50:07 +0100 | [diff] [blame] | 171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | #if defined(MBEDTLS_PLATFORM_EXIT_ALT) |
| 173 | #if !defined(MBEDTLS_PLATFORM_STD_EXIT) |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 174 | /* |
| 175 | * Make dummy function to prevent NULL pointer dereferences |
| 176 | */ |
| 177 | static void platform_exit_uninit( int status ) |
| 178 | { |
| 179 | ((void) status); |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | #define MBEDTLS_PLATFORM_STD_EXIT platform_exit_uninit |
| 183 | #endif /* !MBEDTLS_PLATFORM_STD_EXIT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 184 | |
Manuel Pégourié-Gonnard | 7ee5ddd | 2015-06-03 10:33:55 +0100 | [diff] [blame] | 185 | void (*mbedtls_exit)( int status ) = MBEDTLS_PLATFORM_STD_EXIT; |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | int mbedtls_platform_set_exit( void (*exit_func)( int status ) ) |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 188 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | mbedtls_exit = exit_func; |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 190 | return( 0 ); |
| 191 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_PLATFORM_EXIT_ALT */ |
Rich Evans | c39cb49 | 2015-01-30 12:01:34 +0000 | [diff] [blame] | 193 | |
Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_HAVE_TIME) |
| 195 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 196 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) |
| 197 | #if !defined(MBEDTLS_PLATFORM_STD_TIME) |
| 198 | /* |
| 199 | * Make dummy function to prevent NULL pointer dereferences |
| 200 | */ |
| 201 | static mbedtls_time_t platform_time_uninit( mbedtls_time_t* timer ) |
| 202 | { |
| 203 | ((void) timer); |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 204 | return( 0 ); |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | #define MBEDTLS_PLATFORM_STD_TIME platform_time_uninit |
| 208 | #endif /* !MBEDTLS_PLATFORM_STD_TIME */ |
| 209 | |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 210 | mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* timer ) = MBEDTLS_PLATFORM_STD_TIME; |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 211 | |
Simon Butcher | 3fe6cd3 | 2016-04-26 19:51:29 +0100 | [diff] [blame] | 212 | int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* timer ) ) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 213 | { |
| 214 | mbedtls_time = time_func; |
| 215 | return( 0 ); |
| 216 | } |
| 217 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ |
| 218 | |
Simon Butcher | 23e9778 | 2016-07-13 13:31:08 +0100 | [diff] [blame] | 219 | #endif /* MBEDTLS_HAVE_TIME */ |
| 220 | |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 221 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 222 | #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) && defined(MBEDTLS_FS_IO) |
| 223 | /* Default implementations for the platform independent seed functions use |
| 224 | * standard libc file functions to read from and write to a pre-defined filename |
| 225 | */ |
| 226 | int mbedtls_platform_std_nv_seed_read( unsigned char *buf, size_t buf_len ) |
| 227 | { |
| 228 | FILE *file; |
| 229 | size_t n; |
| 230 | |
| 231 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb" ) ) == NULL ) |
Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 232 | return( -1 ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 233 | |
| 234 | if( ( n = fread( buf, 1, buf_len, file ) ) != buf_len ) |
| 235 | { |
| 236 | fclose( file ); |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame^] | 237 | mbedtls_platform_zeroize( buf, buf_len ); |
Andres Amaya Garcia | 79a2e7e | 2017-06-26 11:10:22 +0100 | [diff] [blame] | 238 | return( -1 ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | fclose( file ); |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 242 | return( (int)n ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | int mbedtls_platform_std_nv_seed_write( unsigned char *buf, size_t buf_len ) |
| 246 | { |
| 247 | FILE *file; |
| 248 | size_t n; |
| 249 | |
| 250 | if( ( file = fopen( MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w" ) ) == NULL ) |
| 251 | return -1; |
| 252 | |
| 253 | if( ( n = fwrite( buf, 1, buf_len, file ) ) != buf_len ) |
| 254 | { |
| 255 | fclose( file ); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | fclose( file ); |
Simon B | 3249cb7 | 2016-11-03 01:11:37 +0000 | [diff] [blame] | 260 | return( (int)n ); |
Paul Bakker | e021a4b | 2016-06-01 11:25:44 +0100 | [diff] [blame] | 261 | } |
| 262 | #endif /* MBEDTLS_PLATFORM_NO_STD_FUNCTIONS */ |
| 263 | |
| 264 | #if defined(MBEDTLS_PLATFORM_NV_SEED_ALT) |
| 265 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_READ) |
| 266 | /* |
| 267 | * Make dummy function to prevent NULL pointer dereferences |
| 268 | */ |
| 269 | static int platform_nv_seed_read_uninit( unsigned char *buf, size_t buf_len ) |
| 270 | { |
| 271 | ((void) buf); |
| 272 | ((void) buf_len); |
| 273 | return( -1 ); |
| 274 | } |
| 275 | |
| 276 | #define MBEDTLS_PLATFORM_STD_NV_SEED_READ platform_nv_seed_read_uninit |
| 277 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_READ */ |
| 278 | |
| 279 | #if !defined(MBEDTLS_PLATFORM_STD_NV_SEED_WRITE) |
| 280 | /* |
| 281 | * Make dummy function to prevent NULL pointer dereferences |
| 282 | */ |
| 283 | static int platform_nv_seed_write_uninit( unsigned char *buf, size_t buf_len ) |
| 284 | { |
| 285 | ((void) buf); |
| 286 | ((void) buf_len); |
| 287 | return( -1 ); |
| 288 | } |
| 289 | |
| 290 | #define MBEDTLS_PLATFORM_STD_NV_SEED_WRITE platform_nv_seed_write_uninit |
| 291 | #endif /* !MBEDTLS_PLATFORM_STD_NV_SEED_WRITE */ |
| 292 | |
| 293 | int (*mbedtls_nv_seed_read)( unsigned char *buf, size_t buf_len ) = |
| 294 | MBEDTLS_PLATFORM_STD_NV_SEED_READ; |
| 295 | int (*mbedtls_nv_seed_write)( unsigned char *buf, size_t buf_len ) = |
| 296 | MBEDTLS_PLATFORM_STD_NV_SEED_WRITE; |
| 297 | |
| 298 | int mbedtls_platform_set_nv_seed( |
| 299 | int (*nv_seed_read_func)( unsigned char *buf, size_t buf_len ), |
| 300 | int (*nv_seed_write_func)( unsigned char *buf, size_t buf_len ) ) |
| 301 | { |
| 302 | mbedtls_nv_seed_read = nv_seed_read_func; |
| 303 | mbedtls_nv_seed_write = nv_seed_write_func; |
| 304 | return( 0 ); |
| 305 | } |
| 306 | #endif /* MBEDTLS_PLATFORM_NV_SEED_ALT */ |
| 307 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 308 | |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 309 | #if !defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 310 | /* |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 311 | * Placeholder platform setup that does nothing by default |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 312 | */ |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 313 | int mbedtls_platform_setup( mbedtls_platform_context *ctx ) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 314 | { |
| 315 | (void)ctx; |
| 316 | |
| 317 | return( 0 ); |
| 318 | } |
| 319 | |
| 320 | /* |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 321 | * Placeholder platform teardown that does nothing by default |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 322 | */ |
Andres Amaya Garcia | 3c8a39d | 2017-07-12 11:25:17 +0100 | [diff] [blame] | 323 | void mbedtls_platform_teardown( mbedtls_platform_context *ctx ) |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 324 | { |
| 325 | (void)ctx; |
| 326 | } |
Andres Amaya Garcia | d91f99f | 2017-07-18 10:23:04 +0100 | [diff] [blame] | 327 | #endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */ |
Andres Amaya Garcia | 2a6f39c | 2017-07-07 13:03:23 +0100 | [diff] [blame] | 328 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 329 | #endif /* MBEDTLS_PLATFORM_C */ |