[457] | 1 | /* wc_port.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2020 wolfSSL Inc.
|
---|
| 4 | *
|
---|
| 5 | * This file is part of wolfSSL.
|
---|
| 6 | *
|
---|
| 7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with this program; if not, write to the Free Software
|
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | /*!
|
---|
| 23 | \file wolfssl/wolfcrypt/wc_port.h
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | #ifndef WOLF_CRYPT_PORT_H
|
---|
| 27 | #define WOLF_CRYPT_PORT_H
|
---|
| 28 |
|
---|
| 29 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
| 30 | #include <wolfssl/wolfcrypt/visibility.h>
|
---|
| 31 |
|
---|
| 32 | #ifdef __cplusplus
|
---|
| 33 | extern "C" {
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | /* Detect if compiler supports C99. "NO_WOLF_C99" can be defined in
|
---|
| 37 | * user_settings.h to disable checking for C99 support. */
|
---|
| 38 | #if !defined(WOLF_C99) && defined(__STDC_VERSION__) && \
|
---|
| 39 | !defined(WOLFSSL_ARDUINO) && !defined(NO_WOLF_C99)
|
---|
| 40 | #if __STDC_VERSION__ >= 199901L
|
---|
| 41 | #define WOLF_C99
|
---|
| 42 | #endif
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /* GENERIC INCLUDE SECTION */
|
---|
| 47 | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 48 | #include <mqx.h>
|
---|
| 49 | #if (defined(MQX_USE_IO_OLD) && MQX_USE_IO_OLD) || \
|
---|
| 50 | defined(FREESCALE_MQX_5_0)
|
---|
| 51 | #include <fio.h>
|
---|
| 52 | #else
|
---|
| 53 | #include <nio.h>
|
---|
| 54 | #endif
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | /* THREADING/MUTEX SECTION */
|
---|
| 59 | #ifdef USE_WINDOWS_API
|
---|
| 60 | #ifdef WOLFSSL_GAME_BUILD
|
---|
| 61 | #include "system/xtl.h"
|
---|
| 62 | #else
|
---|
| 63 | #ifndef WIN32_LEAN_AND_MEAN
|
---|
| 64 | #define WIN32_LEAN_AND_MEAN
|
---|
| 65 | #endif
|
---|
| 66 | #ifndef WOLFSSL_SGX
|
---|
| 67 | #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
|
---|
| 68 | /* On WinCE winsock2.h must be included before windows.h */
|
---|
| 69 | #include <winsock2.h>
|
---|
| 70 | #endif
|
---|
| 71 | #include <windows.h>
|
---|
| 72 | #endif /* WOLFSSL_SGX */
|
---|
| 73 | #endif
|
---|
| 74 | #elif defined(THREADX)
|
---|
| 75 | #ifndef SINGLE_THREADED
|
---|
| 76 | #ifdef NEED_THREADX_TYPES
|
---|
| 77 | #include <types.h>
|
---|
| 78 | #endif
|
---|
| 79 | #include <tx_api.h>
|
---|
| 80 | #endif
|
---|
| 81 | #elif defined(WOLFSSL_DEOS)
|
---|
| 82 | #include "mutexapi.h"
|
---|
| 83 | #elif defined(MICRIUM)
|
---|
| 84 | /* do nothing, just don't pick Unix */
|
---|
| 85 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
|
---|
| 86 | /* do nothing */
|
---|
| 87 | #elif defined(EBSNET)
|
---|
| 88 | /* do nothing */
|
---|
| 89 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 90 | /* do nothing */
|
---|
| 91 | #elif defined(FREESCALE_FREE_RTOS)
|
---|
| 92 | #include "fsl_os_abstraction.h"
|
---|
| 93 | #elif defined(WOLFSSL_VXWORKS)
|
---|
| 94 | #include <semLib.h>
|
---|
| 95 | #elif defined(WOLFSSL_uITRON4)
|
---|
| 96 | #include "stddef.h"
|
---|
| 97 | #include "kernel.h"
|
---|
| 98 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
| 99 | #include "tk/tkernel.h"
|
---|
| 100 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 101 | #include "cmsis_os.h"
|
---|
| 102 | #elif defined(WOLFSSL_CMSIS_RTOSv2)
|
---|
| 103 | #include "cmsis_os2.h"
|
---|
| 104 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
| 105 | #if defined(WOLFSSL_MDK5)
|
---|
| 106 | #include "cmsis_os.h"
|
---|
| 107 | #else
|
---|
| 108 | #include <rtl.h>
|
---|
| 109 | #endif
|
---|
| 110 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 111 | #include "cmsis_os.h"
|
---|
| 112 | #elif defined(WOLFSSL_TIRTOS)
|
---|
| 113 | #include <ti/sysbios/BIOS.h>
|
---|
| 114 | #include <ti/sysbios/knl/Semaphore.h>
|
---|
| 115 | #elif defined(WOLFSSL_FROSTED)
|
---|
| 116 | #include <semaphore.h>
|
---|
| 117 | #elif defined(INTIME_RTOS)
|
---|
| 118 | #include <rt.h>
|
---|
| 119 | #include <io.h>
|
---|
| 120 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 121 | /* NU_DEBUG needed struct access in nucleus_realloc */
|
---|
| 122 | #define NU_DEBUG
|
---|
| 123 | #include "plus/nucleus.h"
|
---|
| 124 | #include "nucleus.h"
|
---|
| 125 | #elif defined(WOLFSSL_APACHE_MYNEWT)
|
---|
| 126 | /* do nothing */
|
---|
| 127 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
| 128 | #ifndef SINGLE_THREADED
|
---|
| 129 | #include <kernel.h>
|
---|
| 130 | #endif
|
---|
| 131 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
| 132 |
|
---|
| 133 | /* Telit SDK uses C++ compile option (--cpp), which causes link issue
|
---|
| 134 | to API's if wrapped in extern "C" */
|
---|
| 135 | #ifdef __cplusplus
|
---|
| 136 | } /* extern "C" */
|
---|
| 137 | #endif
|
---|
| 138 |
|
---|
| 139 | #include "m2mb_types.h"
|
---|
| 140 | #include "m2mb_os_types.h"
|
---|
| 141 | #include "m2mb_os_api.h"
|
---|
| 142 | #include "m2mb_os.h"
|
---|
| 143 | #include "m2mb_os_mtx.h"
|
---|
| 144 | #ifndef NO_ASN_TIME
|
---|
| 145 | #include "m2mb_rtc.h"
|
---|
| 146 | #endif
|
---|
| 147 | #ifndef NO_FILESYSTEM
|
---|
| 148 | #include "m2mb_fs_posix.h"
|
---|
| 149 | #endif
|
---|
| 150 |
|
---|
| 151 | #undef kB /* eliminate conflict in asn.h */
|
---|
| 152 |
|
---|
| 153 | #ifdef __cplusplus
|
---|
| 154 | extern "C" {
|
---|
| 155 | #endif
|
---|
| 156 |
|
---|
| 157 | #else
|
---|
| 158 | #ifndef SINGLE_THREADED
|
---|
| 159 | #define WOLFSSL_PTHREADS
|
---|
| 160 | #include <pthread.h>
|
---|
| 161 | #endif
|
---|
| 162 | #if (defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)) && \
|
---|
| 163 | !defined(NO_FILESYSTEM)
|
---|
| 164 | #include <unistd.h> /* for close of BIO */
|
---|
| 165 | #endif
|
---|
| 166 | #endif
|
---|
| 167 |
|
---|
| 168 | /* For FIPS keep the function names the same */
|
---|
| 169 | #ifdef HAVE_FIPS
|
---|
| 170 | #define wc_InitMutex InitMutex
|
---|
| 171 | #define wc_FreeMutex FreeMutex
|
---|
| 172 | #define wc_LockMutex LockMutex
|
---|
| 173 | #define wc_UnLockMutex UnLockMutex
|
---|
| 174 | #endif /* HAVE_FIPS */
|
---|
| 175 |
|
---|
| 176 | #ifdef SINGLE_THREADED
|
---|
| 177 | typedef int wolfSSL_Mutex;
|
---|
| 178 | #else /* MULTI_THREADED */
|
---|
| 179 | /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
|
---|
| 180 | #if defined(FREERTOS)
|
---|
| 181 | typedef xSemaphoreHandle wolfSSL_Mutex;
|
---|
| 182 | #elif defined(FREERTOS_TCP)
|
---|
| 183 | #include "FreeRTOS.h"
|
---|
| 184 | #include "semphr.h"
|
---|
| 185 | typedef SemaphoreHandle_t wolfSSL_Mutex;
|
---|
| 186 | #elif defined(WOLFSSL_SAFERTOS)
|
---|
| 187 | typedef struct wolfSSL_Mutex {
|
---|
| 188 | signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
|
---|
| 189 | xSemaphoreHandle mutex;
|
---|
| 190 | } wolfSSL_Mutex;
|
---|
| 191 | #elif defined(USE_WINDOWS_API)
|
---|
| 192 | typedef CRITICAL_SECTION wolfSSL_Mutex;
|
---|
| 193 | #elif defined(WOLFSSL_PTHREADS)
|
---|
| 194 | typedef pthread_mutex_t wolfSSL_Mutex;
|
---|
| 195 | #elif defined(THREADX)
|
---|
| 196 | typedef TX_MUTEX wolfSSL_Mutex;
|
---|
| 197 | #elif defined(WOLFSSL_DEOS)
|
---|
| 198 | typedef mutex_handle_t wolfSSL_Mutex;
|
---|
| 199 | #elif defined(MICRIUM)
|
---|
| 200 | typedef OS_MUTEX wolfSSL_Mutex;
|
---|
| 201 | #elif defined(EBSNET)
|
---|
| 202 | typedef RTP_MUTEX wolfSSL_Mutex;
|
---|
| 203 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 204 | typedef MUTEX_STRUCT wolfSSL_Mutex;
|
---|
| 205 | #elif defined(FREESCALE_FREE_RTOS)
|
---|
| 206 | typedef mutex_t wolfSSL_Mutex;
|
---|
| 207 | #elif defined(WOLFSSL_VXWORKS)
|
---|
| 208 | typedef SEM_ID wolfSSL_Mutex;
|
---|
| 209 | #elif defined(WOLFSSL_uITRON4)
|
---|
| 210 | typedef struct wolfSSL_Mutex {
|
---|
| 211 | T_CSEM sem ;
|
---|
| 212 | ID id ;
|
---|
| 213 | } wolfSSL_Mutex;
|
---|
| 214 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
| 215 | typedef struct wolfSSL_Mutex {
|
---|
| 216 | T_CSEM sem ;
|
---|
| 217 | ID id ;
|
---|
| 218 | } wolfSSL_Mutex;
|
---|
| 219 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
| 220 | #if defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 221 | typedef osMutexId wolfSSL_Mutex;
|
---|
| 222 | #else
|
---|
| 223 | typedef OS_MUT wolfSSL_Mutex;
|
---|
| 224 | #endif
|
---|
| 225 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 226 | typedef osMutexId wolfSSL_Mutex;
|
---|
| 227 | #elif defined(WOLFSSL_CMSIS_RTOSv2)
|
---|
| 228 | typedef osMutexId_t wolfSSL_Mutex;
|
---|
| 229 | #elif defined(WOLFSSL_TIRTOS)
|
---|
| 230 | typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
|
---|
| 231 | #elif defined(WOLFSSL_FROSTED)
|
---|
| 232 | typedef mutex_t * wolfSSL_Mutex;
|
---|
| 233 | #elif defined(INTIME_RTOS)
|
---|
| 234 | typedef RTHANDLE wolfSSL_Mutex;
|
---|
| 235 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 236 | typedef NU_SEMAPHORE wolfSSL_Mutex;
|
---|
| 237 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
| 238 | typedef struct k_mutex wolfSSL_Mutex;
|
---|
| 239 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
| 240 | typedef M2MB_OS_MTX_HANDLE wolfSSL_Mutex;
|
---|
| 241 | #else
|
---|
| 242 | #error Need a mutex type in multithreaded mode
|
---|
| 243 | #endif /* USE_WINDOWS_API */
|
---|
| 244 | #endif /* SINGLE_THREADED */
|
---|
| 245 |
|
---|
| 246 | /* Enable crypt HW mutex for Freescale MMCAU, PIC32MZ or STM32 */
|
---|
| 247 | #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ) || \
|
---|
| 248 | defined(STM32_CRYPTO)
|
---|
| 249 | #ifndef WOLFSSL_CRYPT_HW_MUTEX
|
---|
| 250 | #define WOLFSSL_CRYPT_HW_MUTEX 1
|
---|
| 251 | #endif
|
---|
| 252 | #endif /* FREESCALE_MMCAU */
|
---|
| 253 |
|
---|
| 254 | #ifndef WOLFSSL_CRYPT_HW_MUTEX
|
---|
| 255 | #define WOLFSSL_CRYPT_HW_MUTEX 0
|
---|
| 256 | #endif
|
---|
| 257 |
|
---|
| 258 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
| 259 | /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
|
---|
| 260 | however it's recommended to call this directly on Hw init to avoid possible
|
---|
| 261 | race condition where two calls to wolfSSL_CryptHwMutexLock are made at
|
---|
| 262 | the same time. */
|
---|
| 263 | int wolfSSL_CryptHwMutexInit(void);
|
---|
| 264 | int wolfSSL_CryptHwMutexLock(void);
|
---|
| 265 | int wolfSSL_CryptHwMutexUnLock(void);
|
---|
| 266 | #else
|
---|
| 267 | /* Define stubs, since HW mutex is disabled */
|
---|
| 268 | #define wolfSSL_CryptHwMutexInit() 0 /* Success */
|
---|
| 269 | #define wolfSSL_CryptHwMutexLock() 0 /* Success */
|
---|
| 270 | #define wolfSSL_CryptHwMutexUnLock() (void)0 /* Success */
|
---|
| 271 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */
|
---|
| 272 |
|
---|
| 273 | /* Mutex functions */
|
---|
| 274 | WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*);
|
---|
| 275 | WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
|
---|
| 276 | WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*);
|
---|
| 277 | WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*);
|
---|
| 278 | WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex*);
|
---|
| 279 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
---|
| 280 | /* dynamically set which mutex to use. unlock / lock is controlled by flag */
|
---|
| 281 | typedef void (mutex_cb)(int flag, int type, const char* file, int line);
|
---|
| 282 |
|
---|
| 283 | WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line);
|
---|
| 284 | WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
|
---|
| 285 | #endif
|
---|
| 286 |
|
---|
| 287 | /* main crypto initialization function */
|
---|
| 288 | WOLFSSL_API int wolfCrypt_Init(void);
|
---|
| 289 | WOLFSSL_API int wolfCrypt_Cleanup(void);
|
---|
| 290 |
|
---|
| 291 |
|
---|
| 292 | /* FILESYSTEM SECTION */
|
---|
| 293 | /* filesystem abstraction layer, used by ssl.c */
|
---|
| 294 | #ifndef NO_FILESYSTEM
|
---|
| 295 |
|
---|
| 296 | #if defined(EBSNET)
|
---|
| 297 | #include "vfapi.h"
|
---|
| 298 | #include "vfile.h"
|
---|
| 299 |
|
---|
| 300 | int ebsnet_fseek(int a, long b, int c); /* Not prototyped in vfile.h per
|
---|
| 301 | * EBSnet feedback */
|
---|
| 302 |
|
---|
| 303 | #define XFILE int
|
---|
| 304 | #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0);
|
---|
| 305 | #define XFSEEK ebsnet_fseek
|
---|
| 306 | #define XFTELL vf_tell
|
---|
| 307 | #define XREWIND vf_rewind
|
---|
| 308 | #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
|
---|
| 309 | #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
|
---|
| 310 | #define XFCLOSE vf_close
|
---|
| 311 | #define XSEEK_END VSEEK_END
|
---|
| 312 | #define XBADFILE -1
|
---|
| 313 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 314 | #elif defined(LSR_FS)
|
---|
| 315 | #include <fs.h>
|
---|
| 316 | #define XFILE struct fs_file*
|
---|
| 317 | #define XFOPEN(NAME, MODE) fs_open((char*)NAME);
|
---|
| 318 | #define XFSEEK(F, O, W) (void)F
|
---|
| 319 | #define XFTELL(F) (F)->len
|
---|
| 320 | #define XREWIND(F) (void)F
|
---|
| 321 | #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
|
---|
| 322 | #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
|
---|
| 323 | #define XFCLOSE fs_close
|
---|
| 324 | #define XSEEK_END 0
|
---|
| 325 | #define XBADFILE NULL
|
---|
| 326 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 327 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 328 | #define XFILE MQX_FILE_PTR
|
---|
| 329 | #define XFOPEN fopen
|
---|
| 330 | #define XFSEEK fseek
|
---|
| 331 | #define XFTELL ftell
|
---|
| 332 | #define XREWIND(F) fseek(F, 0, IO_SEEK_SET)
|
---|
| 333 | #define XFREAD fread
|
---|
| 334 | #define XFWRITE fwrite
|
---|
| 335 | #define XFCLOSE fclose
|
---|
| 336 | #define XSEEK_END IO_SEEK_END
|
---|
| 337 | #define XBADFILE NULL
|
---|
| 338 | #define XFGETS fgets
|
---|
| 339 | #elif defined(WOLFSSL_DEOS)
|
---|
| 340 | #define NO_FILESYSTEM
|
---|
| 341 | #warning "TODO - DDC-I Certifiable Fast File System for Deos is not integrated"
|
---|
| 342 | //#define XFILE bfd *
|
---|
| 343 |
|
---|
| 344 | #elif defined(MICRIUM)
|
---|
| 345 | #include <fs_api.h>
|
---|
| 346 | #define XFILE FS_FILE*
|
---|
| 347 | #define XFOPEN fs_fopen
|
---|
| 348 | #define XFSEEK fs_fseek
|
---|
| 349 | #define XFTELL fs_ftell
|
---|
| 350 | #define XREWIND fs_rewind
|
---|
| 351 | #define XFREAD fs_fread
|
---|
| 352 | #define XFWRITE fs_fwrite
|
---|
| 353 | #define XFCLOSE fs_fclose
|
---|
| 354 | #define XSEEK_END FS_SEEK_END
|
---|
| 355 | #define XBADFILE NULL
|
---|
| 356 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 357 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 358 | #include "fal/inc/fal.h"
|
---|
| 359 | #define XFILE FILE*
|
---|
| 360 | #define XFOPEN fopen
|
---|
| 361 | #define XFSEEK fseek
|
---|
| 362 | #define XFTELL ftell
|
---|
| 363 | #define XREWIND rewind
|
---|
| 364 | #define XFREAD fread
|
---|
| 365 | #define XFWRITE fwrite
|
---|
| 366 | #define XFCLOSE fclose
|
---|
| 367 | #define XSEEK_END PSEEK_END
|
---|
| 368 | #define XBADFILE NULL
|
---|
| 369 | #elif defined(WOLFSSL_APACHE_MYNEWT)
|
---|
| 370 | #include <fs/fs.h>
|
---|
| 371 | #define XFILE struct fs_file*
|
---|
| 372 |
|
---|
| 373 | #define XFOPEN mynewt_fopen
|
---|
| 374 | #define XFSEEK mynewt_fseek
|
---|
| 375 | #define XFTELL mynewt_ftell
|
---|
| 376 | #define XREWIND mynewt_rewind
|
---|
| 377 | #define XFREAD mynewt_fread
|
---|
| 378 | #define XFWRITE mynewt_fwrite
|
---|
| 379 | #define XFCLOSE mynewt_fclose
|
---|
| 380 | #define XSEEK_END 2
|
---|
| 381 | #define XBADFILE NULL
|
---|
| 382 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 383 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
| 384 | #include <fs.h>
|
---|
| 385 |
|
---|
| 386 | #define XFILE struct fs_file_t*
|
---|
| 387 | #define STAT struct fs_dirent
|
---|
| 388 |
|
---|
| 389 | XFILE z_fs_open(const char* filename, const char* perm);
|
---|
| 390 | int z_fs_close(XFILE file);
|
---|
| 391 |
|
---|
| 392 | #define XFOPEN z_fs_open
|
---|
| 393 | #define XFCLOSE z_fs_close
|
---|
| 394 | #define XFSEEK fs_seek
|
---|
| 395 | #define XFTELL fs_tell
|
---|
| 396 | #define XFREWIND fs_rewind
|
---|
| 397 | #define XREWIND(F) fs_seek(F, 0, FS_SEEK_SET)
|
---|
| 398 | #define XFREAD(P,S,N,F) fs_read(F, P, S*N)
|
---|
| 399 | #define XFWRITE(P,S,N,F) fs_write(F, P, S*N)
|
---|
| 400 | #define XSEEK_END FS_SEEK_END
|
---|
| 401 | #define XBADFILE NULL
|
---|
| 402 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 403 |
|
---|
| 404 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
| 405 | #define XFILE INT32
|
---|
| 406 | #define XFOPEN(NAME, MODE) m2mb_fs_open((NAME), 0, (MODE))
|
---|
| 407 | #define XFSEEK(F, O, W) m2mb_fs_lseek((F), (O), (W))
|
---|
| 408 | #define XFTELL(F) m2mb_fs_lseek((F), 0, M2MB_SEEK_END)
|
---|
| 409 | #define XREWIND(F) (void)F
|
---|
| 410 | #define XFREAD(BUF, SZ, AMT, F) m2mb_fs_read((F), (BUF), (SZ)*(AMT))
|
---|
| 411 | #define XFWRITE(BUF, SZ, AMT, F) m2mb_fs_write((F), (BUF), (SZ)*(AMT))
|
---|
| 412 | #define XFCLOSE m2mb_fs_close
|
---|
| 413 | #define XSEEK_END M2MB_SEEK_END
|
---|
| 414 | #define XBADFILE -1
|
---|
| 415 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 416 |
|
---|
| 417 | #elif defined(WOLFSSL_USER_FILESYSTEM)
|
---|
| 418 | /* To be defined in user_settings.h */
|
---|
| 419 | #else
|
---|
| 420 | /* stdio, default case */
|
---|
| 421 | #include <stdio.h>
|
---|
| 422 | #define XFILE FILE*
|
---|
| 423 | #if defined(WOLFSSL_MDK_ARM)
|
---|
| 424 | extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
|
---|
| 425 | #define XFOPEN wolfSSL_fopen
|
---|
| 426 | #else
|
---|
| 427 | #define XFOPEN fopen
|
---|
| 428 | #endif
|
---|
| 429 | #define XFSEEK fseek
|
---|
| 430 | #define XFTELL ftell
|
---|
| 431 | #define XREWIND rewind
|
---|
| 432 | #define XFREAD fread
|
---|
| 433 | #define XFWRITE fwrite
|
---|
| 434 | #define XFCLOSE fclose
|
---|
| 435 | #define XSEEK_END SEEK_END
|
---|
| 436 | #define XBADFILE NULL
|
---|
| 437 | #define XFGETS fgets
|
---|
| 438 |
|
---|
| 439 | #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)\
|
---|
| 440 | && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 441 | #include <dirent.h>
|
---|
| 442 | #include <unistd.h>
|
---|
| 443 | #include <sys/stat.h>
|
---|
| 444 | #endif
|
---|
| 445 | #endif
|
---|
| 446 |
|
---|
| 447 | #ifndef MAX_FILENAME_SZ
|
---|
| 448 | #define MAX_FILENAME_SZ 256 /* max file name length */
|
---|
| 449 | #endif
|
---|
| 450 | #ifndef MAX_PATH
|
---|
| 451 | #define MAX_PATH 256
|
---|
| 452 | #endif
|
---|
| 453 |
|
---|
| 454 | #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) && \
|
---|
| 455 | !defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 456 | typedef struct ReadDirCtx {
|
---|
| 457 | #ifdef USE_WINDOWS_API
|
---|
| 458 | WIN32_FIND_DATAA FindFileData;
|
---|
| 459 | HANDLE hFind;
|
---|
| 460 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
| 461 | struct fs_dirent entry;
|
---|
| 462 | struct fs_dir_t dir;
|
---|
| 463 | struct fs_dirent s;
|
---|
| 464 | struct fs_dir_t* dirp;
|
---|
| 465 |
|
---|
| 466 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
| 467 | M2MB_DIR_T* dir;
|
---|
| 468 | struct M2MB_DIRENT* entry;
|
---|
| 469 | struct M2MB_STAT s;
|
---|
| 470 | #else
|
---|
| 471 | struct dirent* entry;
|
---|
| 472 | DIR* dir;
|
---|
| 473 | struct stat s;
|
---|
| 474 | #endif
|
---|
| 475 | char name[MAX_FILENAME_SZ];
|
---|
| 476 | } ReadDirCtx;
|
---|
| 477 |
|
---|
| 478 | #define WC_READDIR_NOFILE -1
|
---|
| 479 |
|
---|
| 480 | WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
|
---|
| 481 | WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
|
---|
| 482 | WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
|
---|
| 483 | #endif /* !NO_WOLFSSL_DIR */
|
---|
| 484 |
|
---|
| 485 | #endif /* !NO_FILESYSTEM */
|
---|
| 486 |
|
---|
| 487 |
|
---|
| 488 | /* MIN/MAX MACRO SECTION */
|
---|
| 489 | /* Windows API defines its own min() macro. */
|
---|
| 490 | #if defined(USE_WINDOWS_API)
|
---|
| 491 | #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
|
---|
| 492 | #define WOLFSSL_HAVE_MIN
|
---|
| 493 | #endif /* min */
|
---|
| 494 | #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
|
---|
| 495 | #define WOLFSSL_HAVE_MAX
|
---|
| 496 | #endif /* max */
|
---|
| 497 | #endif /* USE_WINDOWS_API */
|
---|
| 498 |
|
---|
| 499 |
|
---|
| 500 | /* TIME SECTION */
|
---|
| 501 | /* Time functions */
|
---|
| 502 | #ifndef NO_ASN_TIME
|
---|
| 503 | #if defined(USER_TIME)
|
---|
| 504 | /* Use our gmtime and time_t/struct tm types.
|
---|
| 505 | Only needs seconds since EPOCH using XTIME function.
|
---|
| 506 | time_t XTIME(time_t * timer) {}
|
---|
| 507 | */
|
---|
| 508 | #define WOLFSSL_GMTIME
|
---|
| 509 | #ifndef HAVE_TM_TYPE
|
---|
| 510 | #define USE_WOLF_TM
|
---|
| 511 | #endif
|
---|
| 512 | #ifndef HAVE_TIME_T_TYPE
|
---|
| 513 | #define USE_WOLF_TIME_T
|
---|
| 514 | #endif
|
---|
| 515 |
|
---|
| 516 | #elif defined(TIME_OVERRIDES)
|
---|
| 517 | /* Override XTIME() and XGMTIME() functionality.
|
---|
| 518 | Requires user to provide these functions:
|
---|
| 519 | time_t XTIME(time_t * timer) {}
|
---|
| 520 | struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {}
|
---|
| 521 | */
|
---|
| 522 | #ifndef HAVE_TIME_T_TYPE
|
---|
| 523 | #define USE_WOLF_TIME_T
|
---|
| 524 | #endif
|
---|
| 525 | #ifndef HAVE_TM_TYPE
|
---|
| 526 | #define USE_WOLF_TM
|
---|
| 527 | #endif
|
---|
| 528 | #define NEED_TMP_TIME
|
---|
| 529 |
|
---|
| 530 | #elif defined(WOLFSSL_XILINX)
|
---|
| 531 | #define USER_TIME
|
---|
| 532 | #include <time.h>
|
---|
| 533 |
|
---|
| 534 | #elif defined(HAVE_RTP_SYS)
|
---|
| 535 | #include "os.h" /* dc_rtc_api needs */
|
---|
| 536 | #include "dc_rtc_api.h" /* to get current time */
|
---|
| 537 |
|
---|
| 538 | /* uses parital <time.h> structures */
|
---|
| 539 | #define XTIME(tl) (0)
|
---|
| 540 | #define XGMTIME(c, t) rtpsys_gmtime((c))
|
---|
| 541 |
|
---|
| 542 | #elif defined(WOLFSSL_DEOS)
|
---|
| 543 | #define XTIME(t1) deos_time((t1))
|
---|
| 544 | #define WOLFSSL_GMTIME
|
---|
| 545 | #define USE_WOLF_TM
|
---|
| 546 | #define USE_WOLF_TIME_T
|
---|
| 547 |
|
---|
| 548 | #elif defined(MICRIUM)
|
---|
| 549 | #include <clk.h>
|
---|
| 550 | #include <time.h>
|
---|
| 551 | #define XTIME(t1) micrium_time((t1))
|
---|
| 552 | #define WOLFSSL_GMTIME
|
---|
| 553 |
|
---|
| 554 | #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
|
---|
| 555 | #include <time.h>
|
---|
| 556 | #define XTIME(t1) pic32_time((t1))
|
---|
| 557 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 558 |
|
---|
| 559 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 560 | #ifdef FREESCALE_MQX_4_0
|
---|
| 561 | #include <time.h>
|
---|
| 562 | extern time_t mqx_time(time_t* timer);
|
---|
| 563 | #else
|
---|
| 564 | #define HAVE_GMTIME_R
|
---|
| 565 | #endif
|
---|
| 566 | #define XTIME(t1) mqx_time((t1))
|
---|
| 567 |
|
---|
| 568 | #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
|
---|
| 569 | #include <time.h>
|
---|
| 570 | #ifndef XTIME
|
---|
| 571 | /*extern time_t ksdk_time(time_t* timer);*/
|
---|
| 572 | #define XTIME(t1) ksdk_time((t1))
|
---|
| 573 | #endif
|
---|
| 574 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 575 |
|
---|
| 576 | #elif defined(WOLFSSL_ATMEL) && defined(WOLFSSL_ATMEL_TIME)
|
---|
| 577 | #define XTIME(t1) atmel_get_curr_time_and_date((t1))
|
---|
| 578 | #define WOLFSSL_GMTIME
|
---|
| 579 | #define USE_WOLF_TM
|
---|
| 580 | #define USE_WOLF_TIME_T
|
---|
| 581 |
|
---|
| 582 | #elif defined(WOLFSSL_WICED)
|
---|
| 583 | #include <time.h>
|
---|
| 584 | time_t wiced_pseudo_unix_epoch_time(time_t * timer);
|
---|
| 585 | #define XTIME(t1) wiced_pseudo_unix_epoch_time((t1))
|
---|
| 586 | #define HAVE_GMTIME_R
|
---|
| 587 |
|
---|
| 588 | #elif defined(IDIRECT_DEV_TIME)
|
---|
| 589 | /*Gets the timestamp from cloak software owned by VT iDirect
|
---|
| 590 | in place of time() from <time.h> */
|
---|
| 591 | #include <time.h>
|
---|
| 592 | #define XTIME(t1) idirect_time((t1))
|
---|
| 593 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 594 |
|
---|
| 595 | #elif defined(_WIN32_WCE)
|
---|
| 596 | #include <windows.h>
|
---|
| 597 | #define XTIME(t1) windows_time((t1))
|
---|
| 598 | #define WOLFSSL_GMTIME
|
---|
| 599 |
|
---|
| 600 | #elif defined(WOLFSSL_APACHE_MYNEWT)
|
---|
| 601 | #include "os/os_time.h"
|
---|
| 602 | #define XTIME(t1) mynewt_time((t1))
|
---|
| 603 | #define WOLFSSL_GMTIME
|
---|
| 604 | #define USE_WOLF_TM
|
---|
| 605 | #define USE_WOLF_TIME_T
|
---|
| 606 |
|
---|
| 607 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
| 608 | #ifndef _POSIX_C_SOURCE
|
---|
| 609 | #include <posix/time.h>
|
---|
| 610 | #else
|
---|
| 611 | #include <sys/time.h>
|
---|
| 612 | #endif
|
---|
| 613 |
|
---|
| 614 | typedef signed int time_t;
|
---|
| 615 |
|
---|
| 616 | time_t z_time(time_t *timer);
|
---|
| 617 |
|
---|
| 618 | #define XTIME(tl) z_time((tl))
|
---|
| 619 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 620 | #define WOLFSSL_GMTIME
|
---|
| 621 |
|
---|
| 622 | #define USE_WOLF_TM
|
---|
| 623 |
|
---|
| 624 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
| 625 | typedef long time_t;
|
---|
| 626 | extern time_t m2mb_xtime(time_t * timer);
|
---|
| 627 | #define XTIME(tl) m2mb_xtime((tl))
|
---|
| 628 | #ifdef WOLFSSL_TLS13
|
---|
| 629 | extern time_t m2mb_xtime_ms(time_t * timer);
|
---|
| 630 | #define XTIME_MS(tl) m2mb_xtime_ms((tl))
|
---|
| 631 | #endif
|
---|
| 632 | #ifndef NO_CRYPT_BENCHMARK
|
---|
| 633 | extern double m2mb_xtime_bench(int reset);
|
---|
| 634 | #define WOLFSSL_CURRTIME_REMAP m2mb_xtime_bench
|
---|
| 635 | #endif
|
---|
| 636 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 637 | #define WOLFSSL_GMTIME
|
---|
| 638 | #define USE_WOLF_TM
|
---|
| 639 |
|
---|
| 640 | #else
|
---|
| 641 | /* default */
|
---|
| 642 | /* uses complete <time.h> facility */
|
---|
| 643 | #include <time.h>
|
---|
| 644 | #if defined(HAVE_SYS_TIME_H)
|
---|
| 645 | #include <sys/time.h>
|
---|
| 646 | #endif
|
---|
| 647 |
|
---|
| 648 | /* PowerPC time_t is int */
|
---|
| 649 | #ifdef __PPC__
|
---|
| 650 | #define TIME_T_NOT_64BIT
|
---|
| 651 | #endif
|
---|
| 652 | #endif
|
---|
| 653 |
|
---|
| 654 | #ifdef SIZEOF_TIME_T
|
---|
| 655 | /* check if size of time_t from autoconf is less than 8 bytes (64bits) */
|
---|
| 656 | #if SIZEOF_TIME_T < 8
|
---|
| 657 | #undef TIME_T_NOT_64BIT
|
---|
| 658 | #define TIME_T_NOT_64BIT
|
---|
| 659 | #endif
|
---|
| 660 | #endif
|
---|
| 661 | #ifdef TIME_T_NOT_LONG
|
---|
| 662 | /* one old reference to TIME_T_NOT_LONG in GCC-ARM example README
|
---|
| 663 | * this keeps support for the old macro name */
|
---|
| 664 | #undef TIME_T_NOT_64BIT
|
---|
| 665 | #define TIME_T_NOT_64BIT
|
---|
| 666 | #endif
|
---|
| 667 |
|
---|
| 668 | /* Map default time functions */
|
---|
| 669 | #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
|
---|
| 670 | #ifdef TEST_BEFORE_DATE
|
---|
| 671 | #define XTIME(tl) (946681200UL) /* Jan 1, 2000 */
|
---|
| 672 | #else
|
---|
| 673 | #define XTIME(tl) time((tl))
|
---|
| 674 | #endif
|
---|
| 675 | #endif
|
---|
| 676 | #if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
|
---|
| 677 | #if defined(WOLFSSL_GMTIME) || !defined(HAVE_GMTIME_R) || defined(WOLF_C99)
|
---|
| 678 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 679 | #else
|
---|
| 680 | #define XGMTIME(c, t) gmtime_r((c), (t))
|
---|
| 681 | #define NEED_TMP_TIME
|
---|
| 682 | #endif
|
---|
| 683 | #endif
|
---|
| 684 | #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
|
---|
| 685 | #define USE_WOLF_VALIDDATE
|
---|
| 686 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
|
---|
| 687 | #endif
|
---|
| 688 |
|
---|
| 689 | /* wolf struct tm and time_t */
|
---|
| 690 | #if defined(USE_WOLF_TM)
|
---|
| 691 | struct tm {
|
---|
| 692 | int tm_sec; /* seconds after the minute [0-60] */
|
---|
| 693 | int tm_min; /* minutes after the hour [0-59] */
|
---|
| 694 | int tm_hour; /* hours since midnight [0-23] */
|
---|
| 695 | int tm_mday; /* day of the month [1-31] */
|
---|
| 696 | int tm_mon; /* months since January [0-11] */
|
---|
| 697 | int tm_year; /* years since 1900 */
|
---|
| 698 | int tm_wday; /* days since Sunday [0-6] */
|
---|
| 699 | int tm_yday; /* days since January 1 [0-365] */
|
---|
| 700 | int tm_isdst; /* Daylight Savings Time flag */
|
---|
| 701 | long tm_gmtoff; /* offset from CUT in seconds */
|
---|
| 702 | char *tm_zone; /* timezone abbreviation */
|
---|
| 703 | };
|
---|
| 704 | #endif /* USE_WOLF_TM */
|
---|
| 705 | #if defined(USE_WOLF_TIME_T)
|
---|
| 706 | typedef long time_t;
|
---|
| 707 | #endif
|
---|
| 708 | #if defined(USE_WOLF_SUSECONDS_T)
|
---|
| 709 | typedef long suseconds_t;
|
---|
| 710 | #endif
|
---|
| 711 | #if defined(USE_WOLF_TIMEVAL_T)
|
---|
| 712 | struct timeval
|
---|
| 713 | {
|
---|
| 714 | time_t tv_sec;
|
---|
| 715 | suseconds_t tv_usec;
|
---|
| 716 | };
|
---|
| 717 | #endif
|
---|
| 718 |
|
---|
| 719 | /* forward declarations */
|
---|
| 720 | #if defined(USER_TIME)
|
---|
| 721 | struct tm* gmtime(const time_t* timer);
|
---|
| 722 | extern time_t XTIME(time_t * timer);
|
---|
| 723 |
|
---|
| 724 | #ifdef STACK_TRAP
|
---|
| 725 | /* for stack trap tracking, don't call os gmtime on OS X/linux,
|
---|
| 726 | uses a lot of stack spce */
|
---|
| 727 | extern time_t time(time_t * timer);
|
---|
| 728 | #define XTIME(tl) time((tl))
|
---|
| 729 | #endif /* STACK_TRAP */
|
---|
| 730 |
|
---|
| 731 | #elif defined(TIME_OVERRIDES)
|
---|
| 732 | extern time_t XTIME(time_t * timer);
|
---|
| 733 | extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
|
---|
| 734 | #elif defined(WOLFSSL_GMTIME)
|
---|
| 735 | struct tm* gmtime(const time_t* timer);
|
---|
| 736 | #endif
|
---|
| 737 | #endif /* NO_ASN_TIME */
|
---|
| 738 |
|
---|
| 739 |
|
---|
| 740 | #ifndef WOLFSSL_LEANPSK
|
---|
| 741 | char* mystrnstr(const char* s1, const char* s2, unsigned int n);
|
---|
| 742 | #endif
|
---|
| 743 |
|
---|
| 744 | #ifndef FILE_BUFFER_SIZE
|
---|
| 745 | #define FILE_BUFFER_SIZE 1024 /* default static file buffer size for input,
|
---|
| 746 | will use dynamic buffer if not big enough */
|
---|
| 747 | #endif
|
---|
| 748 |
|
---|
| 749 | #ifdef HAVE_CAVIUM_OCTEON_SYNC
|
---|
| 750 | /* By default, the OCTEON's global variables are all thread local. This
|
---|
| 751 | * tag allows them to be shared between threads. */
|
---|
| 752 | #include "cvmx-platform.h"
|
---|
| 753 | #define WOLFSSL_GLOBAL CVMX_SHARED
|
---|
| 754 | #else
|
---|
| 755 | #define WOLFSSL_GLOBAL
|
---|
| 756 | #endif
|
---|
| 757 |
|
---|
| 758 | #ifdef WOLFSSL_DSP
|
---|
| 759 | #include "wolfssl_dsp.h"
|
---|
| 760 |
|
---|
| 761 | /* callbacks for setting handle */
|
---|
| 762 | typedef int (*wolfSSL_DSP_Handle_cb)(remote_handle64 *handle, int finished,
|
---|
| 763 | void *ctx);
|
---|
| 764 | WOLFSSL_API int wolfSSL_GetHandleCbSet();
|
---|
| 765 | WOLFSSL_API int wolfSSL_SetHandleCb(wolfSSL_DSP_Handle_cb in);
|
---|
| 766 | WOLFSSL_LOCAL int wolfSSL_InitHandle();
|
---|
| 767 | WOLFSSL_LOCAL void wolfSSL_CleanupHandle();
|
---|
| 768 | #endif
|
---|
| 769 |
|
---|
| 770 | #ifdef WOLFSSL_SCE
|
---|
| 771 | #ifndef WOLFSSL_SCE_GSCE_HANDLE
|
---|
| 772 | #define WOLFSSL_SCE_GSCE_HANDLE g_sce
|
---|
| 773 | #endif
|
---|
| 774 | #endif
|
---|
| 775 |
|
---|
| 776 | #ifdef __cplusplus
|
---|
| 777 | } /* extern "C" */
|
---|
| 778 | #endif
|
---|
| 779 |
|
---|
| 780 | #endif /* WOLF_CRYPT_PORT_H */
|
---|