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