[388] | 1 | /* wc_port.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2017 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 | #ifdef USE_WINDOWS_API
|
---|
| 46 | #ifdef WOLFSSL_GAME_BUILD
|
---|
| 47 | #include "system/xtl.h"
|
---|
| 48 | #else
|
---|
| 49 | #ifndef WIN32_LEAN_AND_MEAN
|
---|
| 50 | #define WIN32_LEAN_AND_MEAN
|
---|
| 51 | #endif
|
---|
| 52 | #ifndef WOLFSSL_SGX
|
---|
| 53 | #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
|
---|
| 54 | /* On WinCE winsock2.h must be included before windows.h */
|
---|
| 55 | #include <winsock2.h>
|
---|
| 56 | #endif
|
---|
| 57 | #include <windows.h>
|
---|
| 58 | #endif /* WOLFSSL_SGX */
|
---|
| 59 | #endif
|
---|
| 60 | #elif defined(THREADX)
|
---|
| 61 | #ifndef SINGLE_THREADED
|
---|
| 62 | #ifdef NEED_THREADX_TYPES
|
---|
| 63 | #include <types.h>
|
---|
| 64 | #endif
|
---|
| 65 | #include <tx_api.h>
|
---|
| 66 | #endif
|
---|
| 67 | #elif defined(MICRIUM)
|
---|
| 68 | /* do nothing, just don't pick Unix */
|
---|
| 69 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
|
---|
| 70 | /* do nothing */
|
---|
| 71 | #elif defined(EBSNET)
|
---|
| 72 | /* do nothing */
|
---|
| 73 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 74 | /* do nothing */
|
---|
| 75 | #elif defined(FREESCALE_FREE_RTOS)
|
---|
| 76 | #include "fsl_os_abstraction.h"
|
---|
| 77 | #elif defined(WOLFSSL_VXWORKS)
|
---|
| 78 | #include <semLib.h>
|
---|
| 79 | #elif defined(WOLFSSL_uITRON4)
|
---|
| 80 | #include "stddef.h"
|
---|
| 81 | #include "kernel.h"
|
---|
| 82 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
| 83 | #include "tk/tkernel.h"
|
---|
| 84 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 85 | #include "cmsis_os.h"
|
---|
| 86 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
| 87 | #if defined(WOLFSSL_MDK5)
|
---|
| 88 | #include "cmsis_os.h"
|
---|
| 89 | #else
|
---|
| 90 | #include <rtl.h>
|
---|
| 91 | #endif
|
---|
| 92 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 93 | #include "cmsis_os.h"
|
---|
| 94 | #elif defined(WOLFSSL_TIRTOS)
|
---|
| 95 | #include <ti/sysbios/BIOS.h>
|
---|
| 96 | #include <ti/sysbios/knl/Semaphore.h>
|
---|
| 97 | #elif defined(WOLFSSL_FROSTED)
|
---|
| 98 | #include <semaphore.h>
|
---|
| 99 | #elif defined(INTIME_RTOS)
|
---|
| 100 | #include <rt.h>
|
---|
| 101 | #include <io.h>
|
---|
| 102 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 103 | /* NU_DEBUG needed struct access in nucleus_realloc */
|
---|
| 104 | #define NU_DEBUG
|
---|
| 105 | #include "plus/nucleus.h"
|
---|
| 106 | #include "nucleus.h"
|
---|
| 107 | #elif defined(WOLFSSL_APACHE_MYNEWT)
|
---|
| 108 | /* do nothing */
|
---|
| 109 | #else
|
---|
| 110 | #ifndef SINGLE_THREADED
|
---|
| 111 | #define WOLFSSL_PTHREADS
|
---|
| 112 | #include <pthread.h>
|
---|
| 113 | #endif
|
---|
| 114 | #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
|
---|
| 115 | #include <unistd.h> /* for close of BIO */
|
---|
| 116 | #endif
|
---|
| 117 | #endif
|
---|
| 118 |
|
---|
| 119 | /* For FIPS keep the function names the same */
|
---|
| 120 | #ifdef HAVE_FIPS
|
---|
| 121 | #define wc_InitMutex InitMutex
|
---|
| 122 | #define wc_FreeMutex FreeMutex
|
---|
| 123 | #define wc_LockMutex LockMutex
|
---|
| 124 | #define wc_UnLockMutex UnLockMutex
|
---|
| 125 | #endif /* HAVE_FIPS */
|
---|
| 126 |
|
---|
| 127 | #ifdef SINGLE_THREADED
|
---|
| 128 | typedef int wolfSSL_Mutex;
|
---|
| 129 | #else /* MULTI_THREADED */
|
---|
| 130 | /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
|
---|
| 131 | #if defined(FREERTOS)
|
---|
| 132 | typedef xSemaphoreHandle wolfSSL_Mutex;
|
---|
| 133 | #elif defined(FREERTOS_TCP)
|
---|
| 134 | #include "FreeRTOS.h"
|
---|
| 135 | #include "semphr.h"
|
---|
| 136 | typedef SemaphoreHandle_t wolfSSL_Mutex;
|
---|
| 137 | #elif defined(WOLFSSL_SAFERTOS)
|
---|
| 138 | typedef struct wolfSSL_Mutex {
|
---|
| 139 | signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
|
---|
| 140 | xSemaphoreHandle mutex;
|
---|
| 141 | } wolfSSL_Mutex;
|
---|
| 142 | #elif defined(USE_WINDOWS_API)
|
---|
| 143 | typedef CRITICAL_SECTION wolfSSL_Mutex;
|
---|
| 144 | #elif defined(WOLFSSL_PTHREADS)
|
---|
| 145 | typedef pthread_mutex_t wolfSSL_Mutex;
|
---|
| 146 | #elif defined(THREADX)
|
---|
| 147 | typedef TX_MUTEX wolfSSL_Mutex;
|
---|
| 148 | #elif defined(MICRIUM)
|
---|
| 149 | typedef OS_MUTEX wolfSSL_Mutex;
|
---|
| 150 | #elif defined(EBSNET)
|
---|
| 151 | typedef RTP_MUTEX wolfSSL_Mutex;
|
---|
| 152 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 153 | typedef MUTEX_STRUCT wolfSSL_Mutex;
|
---|
| 154 | #elif defined(FREESCALE_FREE_RTOS)
|
---|
| 155 | typedef mutex_t wolfSSL_Mutex;
|
---|
| 156 | #elif defined(WOLFSSL_VXWORKS)
|
---|
| 157 | typedef SEM_ID wolfSSL_Mutex;
|
---|
| 158 | #elif defined(WOLFSSL_uITRON4)
|
---|
| 159 | typedef struct wolfSSL_Mutex {
|
---|
| 160 | T_CSEM sem ;
|
---|
| 161 | ID id ;
|
---|
| 162 | } wolfSSL_Mutex;
|
---|
| 163 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
| 164 | typedef struct wolfSSL_Mutex {
|
---|
| 165 | T_CSEM sem ;
|
---|
| 166 | ID id ;
|
---|
| 167 | } wolfSSL_Mutex;
|
---|
| 168 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
| 169 | #if defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 170 | typedef osMutexId wolfSSL_Mutex;
|
---|
| 171 | #else
|
---|
| 172 | typedef OS_MUT wolfSSL_Mutex;
|
---|
| 173 | #endif
|
---|
| 174 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
| 175 | typedef osMutexId wolfSSL_Mutex;
|
---|
| 176 | #elif defined(WOLFSSL_TIRTOS)
|
---|
| 177 | typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
|
---|
| 178 | #elif defined(WOLFSSL_FROSTED)
|
---|
| 179 | typedef mutex_t * wolfSSL_Mutex;
|
---|
| 180 | #elif defined(INTIME_RTOS)
|
---|
| 181 | typedef RTHANDLE wolfSSL_Mutex;
|
---|
| 182 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 183 | typedef NU_SEMAPHORE wolfSSL_Mutex;
|
---|
| 184 | #else
|
---|
| 185 | #error Need a mutex type in multithreaded mode
|
---|
| 186 | #endif /* USE_WINDOWS_API */
|
---|
| 187 | #endif /* SINGLE_THREADED */
|
---|
| 188 |
|
---|
| 189 | /* Enable crypt HW mutex for Freescale MMCAU or PIC32MZ */
|
---|
| 190 | #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ)
|
---|
| 191 | #ifndef WOLFSSL_CRYPT_HW_MUTEX
|
---|
| 192 | #define WOLFSSL_CRYPT_HW_MUTEX 1
|
---|
| 193 | #endif
|
---|
| 194 | #endif /* FREESCALE_MMCAU */
|
---|
| 195 |
|
---|
| 196 | #ifndef WOLFSSL_CRYPT_HW_MUTEX
|
---|
| 197 | #define WOLFSSL_CRYPT_HW_MUTEX 0
|
---|
| 198 | #endif
|
---|
| 199 |
|
---|
| 200 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
| 201 | /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
|
---|
| 202 | however it's recommended to call this directly on Hw init to avoid possible
|
---|
| 203 | race condition where two calls to wolfSSL_CryptHwMutexLock are made at
|
---|
| 204 | the same time. */
|
---|
| 205 | int wolfSSL_CryptHwMutexInit(void);
|
---|
| 206 | int wolfSSL_CryptHwMutexLock(void);
|
---|
| 207 | int wolfSSL_CryptHwMutexUnLock(void);
|
---|
| 208 | #else
|
---|
| 209 | /* Define stubs, since HW mutex is disabled */
|
---|
| 210 | #define wolfSSL_CryptHwMutexInit() 0 /* Success */
|
---|
| 211 | #define wolfSSL_CryptHwMutexLock() 0 /* Success */
|
---|
| 212 | #define wolfSSL_CryptHwMutexUnLock() (void)0 /* Success */
|
---|
| 213 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */
|
---|
| 214 |
|
---|
| 215 | /* Mutex functions */
|
---|
| 216 | WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*);
|
---|
| 217 | WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
|
---|
| 218 | WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*);
|
---|
| 219 | WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*);
|
---|
| 220 | WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex*);
|
---|
| 221 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
---|
| 222 | /* dynamiclly set which mutex to use. unlock / lock is controlled by flag */
|
---|
| 223 | typedef void (mutex_cb)(int flag, int type, const char* file, int line);
|
---|
| 224 |
|
---|
| 225 | WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line);
|
---|
| 226 | WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
|
---|
| 227 | #endif
|
---|
| 228 |
|
---|
| 229 | /* main crypto initialization function */
|
---|
| 230 | WOLFSSL_API int wolfCrypt_Init(void);
|
---|
| 231 | WOLFSSL_API int wolfCrypt_Cleanup(void);
|
---|
| 232 |
|
---|
| 233 | /* filesystem abstraction layer, used by ssl.c */
|
---|
| 234 | #ifndef NO_FILESYSTEM
|
---|
| 235 |
|
---|
| 236 | #if defined(EBSNET)
|
---|
| 237 | #include "vfapi.h"
|
---|
| 238 | #include "vfile.h"
|
---|
| 239 |
|
---|
| 240 | #define XFILE int
|
---|
| 241 | #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0);
|
---|
| 242 | #define XFSEEK vf_lseek
|
---|
| 243 | #define XFTELL vf_tell
|
---|
| 244 | #define XREWIND vf_rewind
|
---|
| 245 | #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
|
---|
| 246 | #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
|
---|
| 247 | #define XFCLOSE vf_close
|
---|
| 248 | #define XSEEK_END VSEEK_END
|
---|
| 249 | #define XBADFILE -1
|
---|
| 250 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 251 | #elif defined(LSR_FS)
|
---|
| 252 | #include <fs.h>
|
---|
| 253 | #define XFILE struct fs_file*
|
---|
| 254 | #define XFOPEN(NAME, MODE) fs_open((char*)NAME);
|
---|
| 255 | #define XFSEEK(F, O, W) (void)F
|
---|
| 256 | #define XFTELL(F) (F)->len
|
---|
| 257 | #define XREWIND(F) (void)F
|
---|
| 258 | #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
|
---|
| 259 | #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
|
---|
| 260 | #define XFCLOSE fs_close
|
---|
| 261 | #define XSEEK_END 0
|
---|
| 262 | #define XBADFILE NULL
|
---|
| 263 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 264 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 265 | #define XFILE MQX_FILE_PTR
|
---|
| 266 | #define XFOPEN fopen
|
---|
| 267 | #define XFSEEK fseek
|
---|
| 268 | #define XFTELL ftell
|
---|
| 269 | #define XREWIND(F) fseek(F, 0, IO_SEEK_SET)
|
---|
| 270 | #define XFREAD fread
|
---|
| 271 | #define XFWRITE fwrite
|
---|
| 272 | #define XFCLOSE fclose
|
---|
| 273 | #define XSEEK_END IO_SEEK_END
|
---|
| 274 | #define XBADFILE NULL
|
---|
| 275 | #define XFGETS fgets
|
---|
| 276 | #elif defined(MICRIUM)
|
---|
| 277 | #include <fs_api.h>
|
---|
| 278 | #define XFILE FS_FILE*
|
---|
| 279 | #define XFOPEN fs_fopen
|
---|
| 280 | #define XFSEEK fs_fseek
|
---|
| 281 | #define XFTELL fs_ftell
|
---|
| 282 | #define XREWIND fs_rewind
|
---|
| 283 | #define XFREAD fs_fread
|
---|
| 284 | #define XFWRITE fs_fwrite
|
---|
| 285 | #define XFCLOSE fs_fclose
|
---|
| 286 | #define XSEEK_END FS_SEEK_END
|
---|
| 287 | #define XBADFILE NULL
|
---|
| 288 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 289 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 290 | #include "fal/inc/fal.h"
|
---|
| 291 | #define XFILE FILE*
|
---|
| 292 | #define XFOPEN fopen
|
---|
| 293 | #define XFSEEK fseek
|
---|
| 294 | #define XFTELL ftell
|
---|
| 295 | #define XREWIND rewind
|
---|
| 296 | #define XFREAD fread
|
---|
| 297 | #define XFWRITE fwrite
|
---|
| 298 | #define XFCLOSE fclose
|
---|
| 299 | #define XSEEK_END PSEEK_END
|
---|
| 300 | #define XBADFILE NULL
|
---|
| 301 | #elif defined(WOLFSSL_APACHE_MYNEWT)
|
---|
| 302 | #include <fs/fs.h>
|
---|
| 303 | #define XFILE struct fs_file*
|
---|
| 304 |
|
---|
| 305 | #define XFOPEN mynewt_fopen
|
---|
| 306 | #define XFSEEK mynewt_fseek
|
---|
| 307 | #define XFTELL mynewt_ftell
|
---|
| 308 | #define XREWIND mynewt_rewind
|
---|
| 309 | #define XFREAD mynewt_fread
|
---|
| 310 | #define XFWRITE mynewt_fwrite
|
---|
| 311 | #define XFCLOSE mynewt_fclose
|
---|
| 312 | #define XSEEK_END 2
|
---|
| 313 | #define XBADFILE NULL
|
---|
| 314 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
| 315 | #elif defined(WOLFSSL_USER_FILESYSTEM)
|
---|
| 316 | /* To be defined in user_settings.h */
|
---|
| 317 | #else
|
---|
| 318 | /* stdio, default case */
|
---|
| 319 | #include <stdio.h>
|
---|
| 320 | #define XFILE FILE*
|
---|
| 321 | #if defined(WOLFSSL_MDK_ARM)
|
---|
| 322 | extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
|
---|
| 323 | #define XFOPEN wolfSSL_fopen
|
---|
| 324 | #else
|
---|
| 325 | #define XFOPEN fopen
|
---|
| 326 | #endif
|
---|
| 327 | #define XFSEEK fseek
|
---|
| 328 | #define XFTELL ftell
|
---|
| 329 | #define XREWIND rewind
|
---|
| 330 | #define XFREAD fread
|
---|
| 331 | #define XFWRITE fwrite
|
---|
| 332 | #define XFCLOSE fclose
|
---|
| 333 | #define XSEEK_END SEEK_END
|
---|
| 334 | #define XBADFILE NULL
|
---|
| 335 | #define XFGETS fgets
|
---|
| 336 |
|
---|
| 337 | #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)\
|
---|
| 338 | && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 339 | #include <dirent.h>
|
---|
| 340 | #include <unistd.h>
|
---|
| 341 | #include <sys/stat.h>
|
---|
| 342 | #endif
|
---|
| 343 | #endif
|
---|
| 344 |
|
---|
| 345 | #ifndef MAX_FILENAME_SZ
|
---|
| 346 | #define MAX_FILENAME_SZ 256 /* max file name length */
|
---|
| 347 | #endif
|
---|
| 348 | #ifndef MAX_PATH
|
---|
| 349 | #define MAX_PATH 256
|
---|
| 350 | #endif
|
---|
| 351 |
|
---|
| 352 | #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) && \
|
---|
| 353 | !defined(WOLFSSL_NUCLEUS_1_2)
|
---|
| 354 | typedef struct ReadDirCtx {
|
---|
| 355 | #ifdef USE_WINDOWS_API
|
---|
| 356 | WIN32_FIND_DATAA FindFileData;
|
---|
| 357 | HANDLE hFind;
|
---|
| 358 | #else
|
---|
| 359 | struct dirent* entry;
|
---|
| 360 | DIR* dir;
|
---|
| 361 | struct stat s;
|
---|
| 362 | #endif
|
---|
| 363 | char name[MAX_FILENAME_SZ];
|
---|
| 364 | } ReadDirCtx;
|
---|
| 365 |
|
---|
| 366 | #define WC_READDIR_NOFILE -1
|
---|
| 367 |
|
---|
| 368 | WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
|
---|
| 369 | WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
|
---|
| 370 | WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
|
---|
| 371 | #endif /* !NO_WOLFSSL_DIR */
|
---|
| 372 |
|
---|
| 373 | #endif /* !NO_FILESYSTEM */
|
---|
| 374 |
|
---|
| 375 | /* Windows API defines its own min() macro. */
|
---|
| 376 | #if defined(USE_WINDOWS_API)
|
---|
| 377 | #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
|
---|
| 378 | #define WOLFSSL_HAVE_MIN
|
---|
| 379 | #endif /* min */
|
---|
| 380 | #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
|
---|
| 381 | #define WOLFSSL_HAVE_MAX
|
---|
| 382 | #endif /* max */
|
---|
| 383 | #endif /* USE_WINDOWS_API */
|
---|
| 384 |
|
---|
| 385 | /* Time functions */
|
---|
| 386 | #ifndef NO_ASN_TIME
|
---|
| 387 | #if defined(USER_TIME)
|
---|
| 388 | /* Use our gmtime and time_t/struct tm types.
|
---|
| 389 | Only needs seconds since EPOCH using XTIME function.
|
---|
| 390 | time_t XTIME(time_t * timer) {}
|
---|
| 391 | */
|
---|
| 392 | #define WOLFSSL_GMTIME
|
---|
| 393 | #define USE_WOLF_TM
|
---|
| 394 | #define USE_WOLF_TIME_T
|
---|
| 395 |
|
---|
| 396 | #elif defined(TIME_OVERRIDES)
|
---|
| 397 | /* Override XTIME() and XGMTIME() functionality.
|
---|
| 398 | Requires user to provide these functions:
|
---|
| 399 | time_t XTIME(time_t * timer) {}
|
---|
| 400 | struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {}
|
---|
| 401 | */
|
---|
| 402 | #ifndef HAVE_TIME_T_TYPE
|
---|
| 403 | #define USE_WOLF_TIME_T
|
---|
| 404 | #endif
|
---|
| 405 | #ifndef HAVE_TM_TYPE
|
---|
| 406 | #define USE_WOLF_TM
|
---|
| 407 | #endif
|
---|
| 408 | #define NEED_TMP_TIME
|
---|
| 409 |
|
---|
| 410 | #elif defined(HAVE_RTP_SYS)
|
---|
| 411 | #include "os.h" /* dc_rtc_api needs */
|
---|
| 412 | #include "dc_rtc_api.h" /* to get current time */
|
---|
| 413 |
|
---|
| 414 | /* uses parital <time.h> structures */
|
---|
| 415 | #define XTIME(tl) (0)
|
---|
| 416 | #define XGMTIME(c, t) rtpsys_gmtime((c))
|
---|
| 417 |
|
---|
| 418 | #elif defined(MICRIUM)
|
---|
| 419 | #include <clk.h>
|
---|
| 420 | #include <time.h>
|
---|
| 421 | #define XTIME(t1) micrium_time((t1))
|
---|
| 422 | #define WOLFSSL_GMTIME
|
---|
| 423 |
|
---|
| 424 | #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
|
---|
| 425 | #include <time.h>
|
---|
| 426 | #define XTIME(t1) pic32_time((t1))
|
---|
| 427 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 428 |
|
---|
| 429 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
| 430 | #ifdef FREESCALE_MQX_4_0
|
---|
| 431 | #include <time.h>
|
---|
| 432 | extern time_t mqx_time(time_t* timer);
|
---|
| 433 | #else
|
---|
| 434 | #define HAVE_GMTIME_R
|
---|
| 435 | #endif
|
---|
| 436 | #define XTIME(t1) mqx_time((t1))
|
---|
| 437 |
|
---|
| 438 | #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
|
---|
| 439 | #include <time.h>
|
---|
| 440 | #ifndef XTIME
|
---|
| 441 | /*extern time_t ksdk_time(time_t* timer);*/
|
---|
| 442 | #define XTIME(t1) ksdk_time((t1))
|
---|
| 443 | #endif
|
---|
| 444 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 445 |
|
---|
| 446 | #elif defined(WOLFSSL_ATMEL) && defined(WOLFSSL_ATMEL_TIME)
|
---|
| 447 | #define XTIME(t1) atmel_get_curr_time_and_date((t1))
|
---|
| 448 | #define WOLFSSL_GMTIME
|
---|
| 449 | #define USE_WOLF_TM
|
---|
| 450 | #define USE_WOLF_TIME_T
|
---|
| 451 |
|
---|
| 452 | #elif defined(IDIRECT_DEV_TIME)
|
---|
| 453 | /*Gets the timestamp from cloak software owned by VT iDirect
|
---|
| 454 | in place of time() from <time.h> */
|
---|
| 455 | #include <time.h>
|
---|
| 456 | #define XTIME(t1) idirect_time((t1))
|
---|
| 457 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 458 |
|
---|
| 459 | #elif defined(_WIN32_WCE)
|
---|
| 460 | #include <windows.h>
|
---|
| 461 | #define XTIME(t1) windows_time((t1))
|
---|
| 462 | #define WOLFSSL_GMTIME
|
---|
| 463 |
|
---|
| 464 | #elif defined(WOLFSSL_APACHE_MYNEWT)
|
---|
| 465 | #include "os/os_time.h"
|
---|
| 466 | #define XTIME(t1) mynewt_time((t1))
|
---|
| 467 | #define WOLFSSL_GMTIME
|
---|
| 468 | #define USE_WOLF_TM
|
---|
| 469 | #define USE_WOLF_TIME_T
|
---|
| 470 | #else
|
---|
| 471 | /* default */
|
---|
| 472 | /* uses complete <time.h> facility */
|
---|
| 473 | #include <time.h>
|
---|
| 474 | #if defined(HAVE_SYS_TIME_H)
|
---|
| 475 | #include <sys/time.h>
|
---|
| 476 | #endif
|
---|
| 477 |
|
---|
| 478 | /* PowerPC time_t is int */
|
---|
| 479 | #ifdef __PPC__
|
---|
| 480 | #define TIME_T_NOT_64BIT
|
---|
| 481 | #endif
|
---|
| 482 | #endif
|
---|
| 483 |
|
---|
| 484 | #ifdef SIZEOF_TIME_T
|
---|
| 485 | /* check if size of time_t from autoconf is less than 8 bytes (64bits) */
|
---|
| 486 | #if SIZEOF_TIME_T < 8
|
---|
| 487 | #undef TIME_T_NOT_64BIT
|
---|
| 488 | #define TIME_T_NOT_64BIT
|
---|
| 489 | #endif
|
---|
| 490 | #endif
|
---|
| 491 | #ifdef TIME_T_NOT_LONG
|
---|
| 492 | /* one old reference to TIME_T_NOT_LONG in GCC-ARM example README
|
---|
| 493 | * this keeps support for the old macro name */
|
---|
| 494 | #undef TIME_T_NOT_64BIT
|
---|
| 495 | #define TIME_T_NOT_64BIT
|
---|
| 496 | #endif
|
---|
| 497 |
|
---|
| 498 | /* Map default time functions */
|
---|
| 499 | #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
|
---|
| 500 | #define XTIME(tl) time((tl))
|
---|
| 501 | #endif
|
---|
| 502 | #if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
|
---|
| 503 | #if defined(WOLFSSL_GMTIME) || !defined(HAVE_GMTIME_R) || defined(WOLF_C99)
|
---|
| 504 | #define XGMTIME(c, t) gmtime((c))
|
---|
| 505 | #else
|
---|
| 506 | #define XGMTIME(c, t) gmtime_r((c), (t))
|
---|
| 507 | #define NEED_TMP_TIME
|
---|
| 508 | #endif
|
---|
| 509 | #endif
|
---|
| 510 | #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
|
---|
| 511 | #define USE_WOLF_VALIDDATE
|
---|
| 512 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
|
---|
| 513 | #endif
|
---|
| 514 |
|
---|
| 515 | /* wolf struct tm and time_t */
|
---|
| 516 | #if defined(USE_WOLF_TM)
|
---|
| 517 | struct tm {
|
---|
| 518 | int tm_sec; /* seconds after the minute [0-60] */
|
---|
| 519 | int tm_min; /* minutes after the hour [0-59] */
|
---|
| 520 | int tm_hour; /* hours since midnight [0-23] */
|
---|
| 521 | int tm_mday; /* day of the month [1-31] */
|
---|
| 522 | int tm_mon; /* months since January [0-11] */
|
---|
| 523 | int tm_year; /* years since 1900 */
|
---|
| 524 | int tm_wday; /* days since Sunday [0-6] */
|
---|
| 525 | int tm_yday; /* days since January 1 [0-365] */
|
---|
| 526 | int tm_isdst; /* Daylight Savings Time flag */
|
---|
| 527 | long tm_gmtoff; /* offset from CUT in seconds */
|
---|
| 528 | char *tm_zone; /* timezone abbreviation */
|
---|
| 529 | };
|
---|
| 530 | #endif /* USE_WOLF_TM */
|
---|
| 531 | #if defined(USE_WOLF_TIME_T)
|
---|
| 532 | typedef long time_t;
|
---|
| 533 | #endif
|
---|
| 534 | #if defined(USE_WOLF_SUSECONDS_T)
|
---|
| 535 | typedef long suseconds_t;
|
---|
| 536 | #endif
|
---|
| 537 | #if defined(USE_WOLF_TIMEVAL_T)
|
---|
| 538 | struct timeval
|
---|
| 539 | {
|
---|
| 540 | time_t tv_sec;
|
---|
| 541 | suseconds_t tv_usec;
|
---|
| 542 | };
|
---|
| 543 | #endif
|
---|
| 544 |
|
---|
| 545 | /* forward declarations */
|
---|
| 546 | #if defined(USER_TIME)
|
---|
| 547 | struct tm* gmtime(const time_t* timer);
|
---|
| 548 | extern time_t XTIME(time_t * timer);
|
---|
| 549 |
|
---|
| 550 | #ifdef STACK_TRAP
|
---|
| 551 | /* for stack trap tracking, don't call os gmtime on OS X/linux,
|
---|
| 552 | uses a lot of stack spce */
|
---|
| 553 | extern time_t time(time_t * timer);
|
---|
| 554 | #define XTIME(tl) time((tl))
|
---|
| 555 | #endif /* STACK_TRAP */
|
---|
| 556 |
|
---|
| 557 | #elif defined(TIME_OVERRIDES)
|
---|
| 558 | extern time_t XTIME(time_t * timer);
|
---|
| 559 | extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
|
---|
| 560 | #elif defined(WOLFSSL_GMTIME)
|
---|
| 561 | struct tm* gmtime(const time_t* timer);
|
---|
| 562 | #endif
|
---|
| 563 | #endif /* NO_ASN_TIME */
|
---|
| 564 |
|
---|
| 565 | #ifndef WOLFSSL_LEANPSK
|
---|
| 566 | char* mystrnstr(const char* s1, const char* s2, unsigned int n);
|
---|
| 567 | #endif
|
---|
| 568 |
|
---|
| 569 | #ifndef FILE_BUFFER_SIZE
|
---|
| 570 | #define FILE_BUFFER_SIZE 1024 /* default static file buffer size for input,
|
---|
| 571 | will use dynamic buffer if not big enough */
|
---|
| 572 | #endif
|
---|
| 573 |
|
---|
| 574 |
|
---|
| 575 | #ifdef __cplusplus
|
---|
| 576 | } /* extern "C" */
|
---|
| 577 | #endif
|
---|
| 578 |
|
---|
| 579 | #endif /* WOLF_CRYPT_PORT_H */
|
---|
| 580 |
|
---|