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