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