wc_port.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /* wc_port.h
  2. *
  3. * Copyright (C) 2006-2023 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. \file wolfssl/wolfcrypt/wc_port.h
  23. */
  24. #ifndef WOLF_CRYPT_PORT_H
  25. #define WOLF_CRYPT_PORT_H
  26. #include <wolfssl/wolfcrypt/settings.h>
  27. #include <wolfssl/wolfcrypt/visibility.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Detect if compiler supports C99. "NO_WOLF_C99" can be defined in
  32. * user_settings.h to disable checking for C99 support. */
  33. #if !defined(WOLF_C99) && defined(__STDC_VERSION__) && \
  34. !defined(WOLFSSL_ARDUINO) && !defined(NO_WOLF_C99)
  35. #if __STDC_VERSION__ >= 199901L
  36. #define WOLF_C99
  37. #endif
  38. #endif
  39. /* GENERIC INCLUDE SECTION */
  40. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  41. #include <mqx.h>
  42. #if (defined(MQX_USE_IO_OLD) && MQX_USE_IO_OLD) || \
  43. defined(FREESCALE_MQX_5_0)
  44. #include <fio.h>
  45. #else
  46. #include <nio.h>
  47. #endif
  48. #endif
  49. #ifdef WOLFSSL_LINUXKM
  50. #include "../../linuxkm/linuxkm_wc_port.h"
  51. #endif /* WOLFSSL_LINUXKM */
  52. /* THREADING/MUTEX SECTION */
  53. #ifdef USE_WINDOWS_API
  54. #ifdef WOLFSSL_GAME_BUILD
  55. #include "system/xtl.h"
  56. #else
  57. #ifndef WIN32_LEAN_AND_MEAN
  58. #define WIN32_LEAN_AND_MEAN
  59. #endif
  60. #ifndef WOLFSSL_SGX
  61. #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
  62. /* On WinCE winsock2.h must be included before windows.h */
  63. #include <winsock2.h>
  64. #endif
  65. #include <windows.h>
  66. #ifndef WOLFSSL_USER_IO
  67. #include <ws2tcpip.h> /* required for InetPton */
  68. #endif
  69. #endif /* WOLFSSL_SGX */
  70. #endif
  71. #ifndef SINGLE_THREADED
  72. #include <process.h>
  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(RTTHREAD)
  88. /* do nothing */
  89. #elif defined(EBSNET)
  90. /* do nothing */
  91. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  92. /* do nothing */
  93. #elif defined(FREESCALE_FREE_RTOS)
  94. #include "fsl_os_abstraction.h"
  95. #elif defined(WOLFSSL_VXWORKS)
  96. #include <semLib.h>
  97. #ifdef WOLFSSL_VXWORKS_6_x
  98. #ifndef SEM_ID_NULL
  99. #define SEM_ID_NULL ((SEM_ID)NULL)
  100. #endif
  101. #endif
  102. #elif defined(WOLFSSL_uITRON4)
  103. #include "stddef.h"
  104. #include "kernel.h"
  105. #elif defined(WOLFSSL_uTKERNEL2)
  106. #include "tk/tkernel.h"
  107. #elif defined(WOLFSSL_CMSIS_RTOS)
  108. #include "cmsis_os.h"
  109. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  110. #include "cmsis_os2.h"
  111. #elif defined(WOLFSSL_MDK_ARM)
  112. #if defined(WOLFSSL_MDK5)
  113. #include "cmsis_os.h"
  114. #else
  115. #include <rtl.h>
  116. #endif
  117. #elif defined(WOLFSSL_CMSIS_RTOS)
  118. #include "cmsis_os.h"
  119. #elif defined(WOLFSSL_TIRTOS)
  120. #include <ti/sysbios/BIOS.h>
  121. #include <ti/sysbios/knl/Semaphore.h>
  122. #elif defined(WOLFSSL_FROSTED)
  123. #include <semaphore.h>
  124. #elif defined(INTIME_RTOS)
  125. #include <rt.h>
  126. #include <io.h>
  127. #elif defined(WOLFSSL_NUCLEUS_1_2)
  128. /* NU_DEBUG needed struct access in nucleus_realloc */
  129. #define NU_DEBUG
  130. #include "plus/nucleus.h"
  131. #include "nucleus.h"
  132. #elif defined(WOLFSSL_APACHE_MYNEWT)
  133. /* do nothing */
  134. #elif defined(WOLFSSL_ZEPHYR)
  135. #ifndef SINGLE_THREADED
  136. #ifndef CONFIG_PTHREAD_IPC
  137. #error "Need CONFIG_PTHREAD_IPC for threading"
  138. #endif
  139. #include <zephyr/kernel.h>
  140. #include <zephyr/posix/posix_types.h>
  141. #include <zephyr/posix/pthread.h>
  142. #endif
  143. #elif defined(WOLFSSL_TELIT_M2MB)
  144. /* Telit SDK uses C++ compile option (--cpp), which causes link issue
  145. to API's if wrapped in extern "C" */
  146. #ifdef __cplusplus
  147. } /* extern "C" */
  148. #endif
  149. #include "m2mb_types.h"
  150. #include "m2mb_os_types.h"
  151. #include "m2mb_os_api.h"
  152. #include "m2mb_os.h"
  153. #include "m2mb_os_mtx.h"
  154. #ifndef NO_ASN_TIME
  155. #include "m2mb_rtc.h"
  156. #endif
  157. #ifndef NO_FILESYSTEM
  158. #include "m2mb_fs_posix.h"
  159. #endif
  160. #undef kB /* eliminate conflict in asn.h */
  161. #ifdef __cplusplus
  162. extern "C" {
  163. #endif
  164. #elif defined(WOLFSSL_EMBOS)
  165. /* do nothing */
  166. #else
  167. #ifndef SINGLE_THREADED
  168. #ifndef WOLFSSL_USER_MUTEX
  169. #ifdef WOLFSSL_LINUXKM
  170. /* definitions are in linuxkm/linuxkm_wc_port.h */
  171. #else
  172. #define WOLFSSL_PTHREADS
  173. #include <pthread.h>
  174. #endif
  175. #endif
  176. #endif
  177. #if (defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)) && \
  178. !defined(NO_FILESYSTEM)
  179. #ifdef FUSION_RTOS
  180. #include <fclunistd.h>
  181. #else
  182. #include <unistd.h> /* for close of BIO */
  183. #endif
  184. #endif
  185. #endif
  186. /* For FIPS keep the function names the same */
  187. #ifdef HAVE_FIPS
  188. #define wc_InitMutex InitMutex
  189. #define wc_FreeMutex FreeMutex
  190. #define wc_LockMutex LockMutex
  191. #define wc_UnLockMutex UnLockMutex
  192. #endif /* HAVE_FIPS */
  193. #ifdef SINGLE_THREADED
  194. typedef int wolfSSL_Mutex;
  195. #else /* MULTI_THREADED */
  196. /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
  197. #if defined(FREERTOS)
  198. #if ESP_IDF_VERSION_MAJOR >= 4
  199. typedef SemaphoreHandle_t wolfSSL_Mutex;
  200. #else
  201. typedef xSemaphoreHandle wolfSSL_Mutex;
  202. #endif
  203. #elif defined(FREERTOS_TCP)
  204. #include "FreeRTOS.h"
  205. #include "semphr.h"
  206. typedef SemaphoreHandle_t wolfSSL_Mutex;
  207. #elif defined (RTTHREAD)
  208. #include "rtthread.h"
  209. typedef rt_mutex_t wolfSSL_Mutex;
  210. #elif defined(WOLFSSL_SAFERTOS)
  211. typedef struct wolfSSL_Mutex {
  212. signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
  213. xSemaphoreHandle mutex;
  214. } wolfSSL_Mutex;
  215. #elif defined(USE_WINDOWS_API)
  216. typedef CRITICAL_SECTION wolfSSL_Mutex;
  217. #elif defined(MAXQ10XX_MUTEX)
  218. #include <sys/mman.h>
  219. #include <fcntl.h>
  220. #include <pthread.h>
  221. typedef pthread_mutex_t wolfSSL_Mutex;
  222. int maxq_CryptHwMutexTryLock(void);
  223. #elif defined(WOLFSSL_PTHREADS)
  224. #ifdef WOLFSSL_USE_RWLOCK
  225. typedef pthread_rwlock_t wolfSSL_RwLock;
  226. #endif
  227. typedef pthread_mutex_t wolfSSL_Mutex;
  228. #elif defined(THREADX)
  229. typedef TX_MUTEX wolfSSL_Mutex;
  230. #elif defined(WOLFSSL_DEOS)
  231. typedef mutex_handle_t wolfSSL_Mutex;
  232. #elif defined(MICRIUM)
  233. typedef OS_MUTEX wolfSSL_Mutex;
  234. #elif defined(EBSNET)
  235. #if (defined(RTPLATFORM) && (RTPLATFORM != 0))
  236. typedef RTP_MUTEX wolfSSL_Mutex;
  237. #else
  238. typedef KS_RTIPSEM wolfSSL_Mutex;
  239. #endif
  240. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  241. typedef MUTEX_STRUCT wolfSSL_Mutex;
  242. #elif defined(FREESCALE_FREE_RTOS)
  243. typedef mutex_t wolfSSL_Mutex;
  244. #elif defined(WOLFSSL_VXWORKS)
  245. typedef SEM_ID wolfSSL_Mutex;
  246. #elif defined(WOLFSSL_uITRON4)
  247. typedef struct wolfSSL_Mutex {
  248. T_CSEM sem ;
  249. ID id ;
  250. } wolfSSL_Mutex;
  251. #elif defined(WOLFSSL_uTKERNEL2)
  252. typedef struct wolfSSL_Mutex {
  253. T_CSEM sem ;
  254. ID id ;
  255. } wolfSSL_Mutex;
  256. #elif defined(WOLFSSL_MDK_ARM)
  257. #if defined(WOLFSSL_CMSIS_RTOS)
  258. typedef osMutexId wolfSSL_Mutex;
  259. #else
  260. typedef OS_MUT wolfSSL_Mutex;
  261. #endif
  262. #elif defined(WOLFSSL_CMSIS_RTOS)
  263. typedef osMutexId wolfSSL_Mutex;
  264. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  265. typedef osMutexId_t wolfSSL_Mutex;
  266. #elif defined(WOLFSSL_TIRTOS)
  267. typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
  268. #elif defined(WOLFSSL_FROSTED)
  269. typedef mutex_t * wolfSSL_Mutex;
  270. #elif defined(INTIME_RTOS)
  271. typedef RTHANDLE wolfSSL_Mutex;
  272. #elif defined(WOLFSSL_NUCLEUS_1_2)
  273. typedef NU_SEMAPHORE wolfSSL_Mutex;
  274. #elif defined(WOLFSSL_ZEPHYR)
  275. typedef struct k_mutex wolfSSL_Mutex;
  276. #elif defined(WOLFSSL_TELIT_M2MB)
  277. typedef M2MB_OS_MTX_HANDLE wolfSSL_Mutex;
  278. #elif defined(WOLFSSL_EMBOS)
  279. typedef OS_MUTEX wolfSSL_Mutex;
  280. #elif defined(WOLFSSL_USER_MUTEX)
  281. /* typedef User_Mutex wolfSSL_Mutex; */
  282. #elif defined(WOLFSSL_LINUXKM)
  283. /* definitions are in linuxkm/linuxkm_wc_port.h */
  284. #else
  285. #error Need a mutex type in multithreaded mode
  286. #endif /* USE_WINDOWS_API */
  287. #endif /* SINGLE_THREADED */
  288. #if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
  289. typedef wolfSSL_Mutex wolfSSL_RwLock;
  290. #endif
  291. #ifndef WOLFSSL_NO_ATOMICS
  292. #ifdef HAVE_C___ATOMIC
  293. #ifdef __cplusplus
  294. #if defined(__GNUC__) && defined(__ATOMIC_RELAXED)
  295. /* C++ using direct calls to compiler built-in functions */
  296. typedef volatile int wolfSSL_Atomic_Int;
  297. #define WOLFSSL_ATOMIC_OPS
  298. #endif
  299. #else
  300. #ifdef WOLFSSL_HAVE_ATOMIC_H
  301. /* Default C Implementation */
  302. #include <stdatomic.h>
  303. typedef atomic_int wolfSSL_Atomic_Int;
  304. #define WOLFSSL_ATOMIC_OPS
  305. #endif /* WOLFSSL_HAVE_ATOMIC_H */
  306. #endif
  307. #elif defined(_MSC_VER)
  308. /* Use MSVC compiler intrinsics for atomic ops */
  309. #include <intrin.h>
  310. typedef volatile long wolfSSL_Atomic_Int;
  311. #define WOLFSSL_ATOMIC_OPS
  312. #endif
  313. #endif /* WOLFSSL_NO_ATOMICS */
  314. #ifdef WOLFSSL_ATOMIC_OPS
  315. WOLFSSL_LOCAL void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i);
  316. /* Fetch* functions return the value of the counter immediately preceding
  317. * the effects of the function. */
  318. WOLFSSL_LOCAL int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i);
  319. WOLFSSL_LOCAL int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i);
  320. #endif
  321. /* Reference counting. */
  322. typedef struct wolfSSL_Ref {
  323. #if !defined(SINGLE_THREADED) && !defined(WOLFSSL_ATOMIC_OPS)
  324. wolfSSL_Mutex mutex;
  325. #endif
  326. #ifdef WOLFSSL_ATOMIC_OPS
  327. wolfSSL_Atomic_Int count;
  328. #else
  329. int count;
  330. #endif
  331. } wolfSSL_Ref;
  332. #ifdef SINGLE_THREADED
  333. #define wolfSSL_RefInit(ref, err) \
  334. do { \
  335. (ref)->count = 1; \
  336. *(err) = 0; \
  337. } while(0)
  338. #define wolfSSL_RefFree(ref) WC_DO_NOTHING
  339. #define wolfSSL_RefInc(ref, err) \
  340. do { \
  341. (ref)->count++; \
  342. *(err) = 0; \
  343. } while(0)
  344. #define wolfSSL_RefDec(ref, isZero, err) \
  345. do { \
  346. (ref)->count--; \
  347. *(isZero) = ((ref)->count == 0); \
  348. *(err) = 0; \
  349. } while(0)
  350. #elif defined(WOLFSSL_ATOMIC_OPS)
  351. #define wolfSSL_RefInit(ref, err) \
  352. do { \
  353. wolfSSL_Atomic_Int_Init(&(ref)->count, 1); \
  354. *(err) = 0; \
  355. } while(0)
  356. #define wolfSSL_RefFree(ref) WC_DO_NOTHING
  357. #define wolfSSL_RefInc(ref, err) \
  358. do { \
  359. (void)wolfSSL_Atomic_Int_FetchAdd(&(ref)->count, 1); \
  360. *(err) = 0; \
  361. } while(0)
  362. #define wolfSSL_RefDec(ref, isZero, err) \
  363. do { \
  364. int __prev = wolfSSL_Atomic_Int_FetchSub(&(ref)->count, 1); \
  365. /* __prev holds the value of count before subtracting 1 */ \
  366. *(isZero) = (__prev == 1); \
  367. *(err) = 0; \
  368. } while(0)
  369. #else
  370. #define WOLFSSL_REFCNT_ERROR_RETURN
  371. WOLFSSL_LOCAL void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err);
  372. WOLFSSL_LOCAL void wolfSSL_RefFree(wolfSSL_Ref* ref);
  373. WOLFSSL_LOCAL void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err);
  374. WOLFSSL_LOCAL void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err);
  375. #endif
  376. /* Enable crypt HW mutex for Freescale MMCAU, PIC32MZ or STM32 */
  377. #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ) || \
  378. defined(STM32_CRYPTO) || defined(STM32_HASH) || defined(STM32_RNG)
  379. #ifndef WOLFSSL_CRYPT_HW_MUTEX
  380. #define WOLFSSL_CRYPT_HW_MUTEX 1
  381. #endif
  382. #endif /* FREESCALE_MMCAU */
  383. #ifndef WOLFSSL_CRYPT_HW_MUTEX
  384. #define WOLFSSL_CRYPT_HW_MUTEX 0
  385. #endif
  386. #if WOLFSSL_CRYPT_HW_MUTEX
  387. /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
  388. however it's recommended to call this directly on Hw init to avoid possible
  389. race condition where two calls to wolfSSL_CryptHwMutexLock are made at
  390. the same time. */
  391. int wolfSSL_CryptHwMutexInit(void);
  392. int wolfSSL_CryptHwMutexLock(void);
  393. int wolfSSL_CryptHwMutexUnLock(void);
  394. #else
  395. /* Define stubs, since HW mutex is disabled */
  396. #define wolfSSL_CryptHwMutexInit() 0 /* Success */
  397. #define wolfSSL_CryptHwMutexLock() 0 /* Success */
  398. #define wolfSSL_CryptHwMutexUnLock() (void)0 /* Success */
  399. #endif /* WOLFSSL_CRYPT_HW_MUTEX */
  400. /* Mutex functions */
  401. WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex* m);
  402. WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
  403. WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex* m);
  404. WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex* m);
  405. WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex* m);
  406. /* RwLock functions. Fallback to Mutex when not implemented explicitly. */
  407. WOLFSSL_API int wc_InitRwLock(wolfSSL_RwLock* m);
  408. WOLFSSL_API int wc_FreeRwLock(wolfSSL_RwLock* m);
  409. WOLFSSL_API int wc_LockRwLock_Wr(wolfSSL_RwLock* m);
  410. WOLFSSL_API int wc_LockRwLock_Rd(wolfSSL_RwLock* m);
  411. WOLFSSL_API int wc_UnLockRwLock(wolfSSL_RwLock* m);
  412. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  413. /* dynamically set which mutex to use. unlock / lock is controlled by flag */
  414. typedef void (mutex_cb)(int flag, int type, const char* file, int line);
  415. WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line);
  416. WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
  417. WOLFSSL_API mutex_cb* wc_GetMutexCb(void);
  418. #endif
  419. /* main crypto initialization function */
  420. WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Init(void);
  421. WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
  422. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  423. WOLFSSL_API long wolfCrypt_heap_peakAllocs_checkpoint(void);
  424. WOLFSSL_API long wolfCrypt_heap_peakBytes_checkpoint(void);
  425. #endif
  426. /* FILESYSTEM SECTION */
  427. /* filesystem abstraction layer, used by ssl.c */
  428. #ifndef NO_FILESYSTEM
  429. #if defined(EBSNET)
  430. #include "vfapi.h"
  431. #include "vfile.h"
  432. int ebsnet_fseek(int a, long b, int c); /* Not prototyped in vfile.h per
  433. * EBSnet feedback */
  434. #define XFILE int
  435. #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0)
  436. #define XFSEEK ebsnet_fseek
  437. #define XFTELL vf_tell
  438. #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
  439. #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
  440. #define XFCLOSE vf_close
  441. #define XSEEK_SET VSEEK_SET
  442. #define XSEEK_END VSEEK_END
  443. #define XBADFILE -1
  444. #define XFGETS(b,s,f) -2 /* Not ported yet */
  445. #define XSNPRINTF rtp_snprintf
  446. #define XFPRINTF fprintf
  447. #elif defined(LSR_FS)
  448. #include <fs.h>
  449. #define XFILE struct fs_file*
  450. #define XFOPEN(NAME, MODE) fs_open((char*)NAME)
  451. #define XFSEEK(F, O, W) (void)F
  452. #define XFTELL(F) (F)->len
  453. #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
  454. #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
  455. #define XFCLOSE fs_close
  456. #define XSEEK_SET 0
  457. #define XSEEK_END 0
  458. #define XBADFILE NULL
  459. #define XFGETS(b,s,f) -2 /* Not ported yet */
  460. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  461. #define XFILE MQX_FILE_PTR
  462. #define XFOPEN fopen
  463. #define XFSEEK fseek
  464. #define XFTELL ftell
  465. #define XFREAD fread
  466. #define XFWRITE fwrite
  467. #define XFCLOSE fclose
  468. #define XSEEK_SET IO_SEEK_SET
  469. #define XSEEK_END IO_SEEK_END
  470. #define XBADFILE NULL
  471. #define XFGETS fgets
  472. #elif defined(WOLFSSL_DEOS)
  473. #define NO_FILESYSTEM
  474. #warning "TODO - DDC-I Certifiable Fast File System for Deos is not integrated"
  475. #elif defined(MICRIUM)
  476. #include <fs_api.h>
  477. #define XFILE FS_FILE*
  478. #define XFOPEN fs_fopen
  479. #define XFSEEK fs_fseek
  480. #define XFTELL fs_ftell
  481. #define XFREAD fs_fread
  482. #define XFWRITE fs_fwrite
  483. #define XFCLOSE fs_fclose
  484. #define XSEEK_SET FS_SEEK_SET
  485. #define XSEEK_END FS_SEEK_END
  486. #define XBADFILE NULL
  487. #define XFGETS(b,s,f) -2 /* Not ported yet */
  488. #elif defined(WOLFSSL_NUCLEUS_1_2)
  489. #include "fal/inc/fal.h"
  490. #define XFILE FILE*
  491. #define XFOPEN fopen
  492. #define XFSEEK fseek
  493. #define XFTELL ftell
  494. #define XFREAD fread
  495. #define XFWRITE fwrite
  496. #define XFCLOSE fclose
  497. #define XSEEK_SET PSEEK_SET
  498. #define XSEEK_END PSEEK_END
  499. #define XBADFILE NULL
  500. #elif defined(WOLFSSL_APACHE_MYNEWT)
  501. #include <fs/fs.h>
  502. #define XFILE struct fs_file*
  503. #define XFOPEN mynewt_fopen
  504. #define XFSEEK mynewt_fseek
  505. #define XFTELL mynewt_ftell
  506. #define XFREAD mynewt_fread
  507. #define XFWRITE mynewt_fwrite
  508. #define XFCLOSE mynewt_fclose
  509. #define XSEEK_SET 0
  510. #define XSEEK_END 2
  511. #define XBADFILE NULL
  512. #define XFGETS(b,s,f) -2 /* Not ported yet */
  513. #elif defined(WOLFSSL_ZEPHYR)
  514. #include <zephyr/fs/fs.h>
  515. #define XFILE struct fs_file_t*
  516. /* These are our wrappers for opening and closing files to
  517. * make the API more POSIX like. */
  518. XFILE z_fs_open(const char* filename, const char* mode);
  519. int z_fs_close(XFILE file);
  520. #define XFOPEN z_fs_open
  521. #define XFCLOSE z_fs_close
  522. #define XFFLUSH fs_sync
  523. #define XFSEEK fs_seek
  524. #define XFTELL fs_tell
  525. #define XFREWIND fs_rewind
  526. #define XFREAD(P,S,N,F) fs_read(F, P, S*N)
  527. #define XFWRITE(P,S,N,F) fs_write(F, P, S*N)
  528. #define XSEEK_SET FS_SEEK_SET
  529. #define XSEEK_END FS_SEEK_END
  530. #define XBADFILE NULL
  531. #define XFGETS(b,s,f) -2 /* Not ported yet */
  532. #define XSTAT fs_stat
  533. #define XSTAT_TYPE struct fs_dirent
  534. #define XS_ISREG(s) (s == FS_DIR_ENTRY_FILE)
  535. #define SEPARATOR_CHAR ':'
  536. #elif defined(WOLFSSL_TELIT_M2MB)
  537. #define XFILE INT32
  538. #define XFOPEN(NAME, MODE) m2mb_fs_open((NAME), 0, (MODE))
  539. #define XFSEEK(F, O, W) m2mb_fs_lseek((F), (O), (W))
  540. #define XFTELL(F) m2mb_fs_lseek((F), 0, M2MB_SEEK_END)
  541. #define XFREAD(BUF, SZ, AMT, F) m2mb_fs_read((F), (BUF), (SZ)*(AMT))
  542. #define XFWRITE(BUF, SZ, AMT, F) m2mb_fs_write((F), (BUF), (SZ)*(AMT))
  543. #define XFCLOSE m2mb_fs_close
  544. #define XSEEK_SET M2MB_SEEK_SET
  545. #define XSEEK_END M2MB_SEEK_END
  546. #define XBADFILE -1
  547. #define XFGETS(b,s,f) -2 /* Not ported yet */
  548. #elif defined (WOLFSSL_XILINX)
  549. #include "xsdps.h"
  550. #include "ff.h"
  551. /* workaround to declare variable and provide type */
  552. #define XFILE FIL curFile; FIL*
  553. #define XFOPEN(NAME, MODE) ({ FRESULT res; res = f_open(&curFile, (NAME), (FA_OPEN_ALWAYS | FA_WRITE | FA_READ)); (res == FR_OK) ? &curFile : NULL; })
  554. #define XFSEEK(F, O, W) f_lseek((F), (O))
  555. #define XFTELL(F) f_tell((F))
  556. #define XFREAD(BUF, SZ, AMT, F) ({ FRESULT res; UINT br; res = f_read((F), (BUF), (SZ)*(AMT), &br); (void)br; res; })
  557. #define XFWRITE(BUF, SZ, AMT, F) ({ FRESULT res; UINT written; res = f_write((F), (BUF), (SZ)*(AMT), &written); (void)written; res; })
  558. #define XFCLOSE(F) f_close((F))
  559. #define XSEEK_SET 0
  560. #define XSEEK_END 0
  561. #define XBADFILE NULL
  562. #define XFGETS(b,s,f) f_gets((b), (s), (f))
  563. #elif defined (_WIN32_WCE)
  564. /* stdio, WINCE case */
  565. #include <stdio.h>
  566. #define XFILE FILE*
  567. #define XFOPEN fopen
  568. #define XFDOPEN fdopen
  569. #define XFSEEK fseek
  570. #define XFTELL ftell
  571. #define XFREAD fread
  572. #define XFWRITE fwrite
  573. #define XFCLOSE fclose
  574. #define XSEEK_SET SEEK_SET
  575. #define XSEEK_END SEEK_END
  576. #define XBADFILE NULL
  577. #define XFGETS fgets
  578. #define XVSNPRINTF _vsnprintf
  579. #elif defined(FUSION_RTOS)
  580. #include <fclstdio.h>
  581. #include <fclunistd.h>
  582. #include <fcldirent.h>
  583. #include <sys/fclstat.h>
  584. #include <fclstring.h>
  585. #include <fcl_os.h>
  586. #define XFILE FCL_FILE*
  587. #define XFOPEN FCL_FOPEN
  588. #define XFSEEK FCL_FSEEK
  589. #define XFTELL FCL_FTELL
  590. #define XFREAD FCL_FREAD
  591. #define XFWRITE FCL_FWRITE
  592. #define XFCLOSE FCL_FCLOSE
  593. #define XSEEK_SET SEEK_SET
  594. #define XSEEK_END SEEK_END
  595. #define XBADFILE NULL
  596. #define XFGETS FCL_FGETS
  597. #define XFPUTS FCL_FPUTS
  598. #define XFPRINTF FCL_FPRINTF
  599. #define XVFPRINTF FCL_VFPRINTF
  600. #define XVSNPRINTF FCL_VSNPRINTF
  601. #define XSNPRINTF FCL_SNPRINTF
  602. #define XSPRINTF FCL_SPRINTF
  603. #define DIR FCL_DIR
  604. #define stat FCL_STAT
  605. #define opendir FCL_OPENDIR
  606. #define closedir FCL_CLOSEDIR
  607. #define readdir FCL_READDIR
  608. #define dirent fclDirent
  609. #define strncasecmp FCL_STRNCASECMP
  610. /* FUSION SPECIFIC ERROR CODE */
  611. #define FUSION_IO_SEND_E FCL_EWOULDBLOCK
  612. #elif defined(WOLFSSL_USER_FILESYSTEM)
  613. /* To be defined in user_settings.h */
  614. #else
  615. /* stdio, default case */
  616. #include <stdio.h>
  617. #define XFILE FILE*
  618. #if defined(WOLFSSL_MDK_ARM)
  619. extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
  620. #define XFOPEN wolfSSL_fopen
  621. #else
  622. #define XFOPEN fopen
  623. #endif
  624. #define XFDOPEN fdopen
  625. #define XFSEEK fseek
  626. #define XFTELL ftell
  627. #define XFREAD fread
  628. #define XFWRITE fwrite
  629. #define XFCLOSE fclose
  630. #define XSEEK_SET SEEK_SET
  631. #define XSEEK_END SEEK_END
  632. #define XBADFILE NULL
  633. #define XFGETS fgets
  634. #define XFPRINTF fprintf
  635. #define XFFLUSH fflush
  636. #if !defined(NO_WOLFSSL_DIR)\
  637. && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
  638. #if defined(USE_WINDOWS_API)
  639. #include <sys/stat.h>
  640. #ifndef XSTAT
  641. #define XSTAT _stat
  642. #endif
  643. #define XS_ISREG(s) (s & _S_IFREG)
  644. #define SEPARATOR_CHAR ';'
  645. #elif defined(INTIME_RTOS)
  646. #include <sys/stat.h>
  647. #ifndef XSTAT
  648. #define XSTAT _stat64
  649. #endif
  650. #define XS_ISREG(s) S_ISREG(s)
  651. #define SEPARATOR_CHAR ';'
  652. #define XWRITE write
  653. #define XREAD read
  654. #define XCLOSE close
  655. #elif defined(WOLFSSL_TELIT_M2MB)
  656. #ifndef XSTAT
  657. #define XSTAT m2mb_fs_stat
  658. #endif
  659. #define XS_ISREG(s) (s & M2MB_S_IFREG)
  660. #define SEPARATOR_CHAR ':'
  661. #else
  662. #include <dirent.h>
  663. #include <unistd.h>
  664. #include <sys/stat.h>
  665. #define XWRITE write
  666. #define XREAD read
  667. #define XCLOSE close
  668. #ifndef XSTAT
  669. #define XSTAT stat
  670. #endif
  671. #define XS_ISREG(s) S_ISREG(s)
  672. #define SEPARATOR_CHAR ':'
  673. #endif
  674. #ifndef XSTAT_TYPE
  675. #define XSTAT_TYPE struct XSTAT
  676. #endif
  677. #endif
  678. #endif
  679. #ifndef MAX_FILENAME_SZ
  680. #define MAX_FILENAME_SZ 256 /* max file name length */
  681. #endif
  682. #ifndef MAX_PATH
  683. #define MAX_PATH 256
  684. #endif
  685. WOLFSSL_LOCAL int wc_FileLoad(const char* fname, unsigned char** buf,
  686. size_t* bufLen, void* heap);
  687. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) && \
  688. !defined(WOLFSSL_NUCLEUS_1_2)
  689. typedef struct ReadDirCtx {
  690. #ifdef USE_WINDOWS_API
  691. WIN32_FIND_DATAA FindFileData;
  692. HANDLE hFind;
  693. XSTAT_TYPE s;
  694. #elif defined(WOLFSSL_ZEPHYR)
  695. struct fs_dirent entry;
  696. struct fs_dir_t dir;
  697. struct fs_dirent s;
  698. struct fs_dir_t* dirp;
  699. #elif defined(WOLFSSL_TELIT_M2MB)
  700. M2MB_DIR_T* dir;
  701. struct M2MB_DIRENT* entry;
  702. struct M2MB_STAT s;
  703. #elif defined(INTIME_RTOS)
  704. struct stat64 s;
  705. struct _find64 FindFileData;
  706. #define IntimeFindFirst(name, data) (0 == _findfirst64(name, data))
  707. #define IntimeFindNext(data) (0 == _findnext64(data))
  708. #define IntimeFindClose(data) (0 == _findclose64(data))
  709. #define IntimeFilename(ctx) ctx->FindFileData.f_filename
  710. #else
  711. struct dirent* entry;
  712. DIR* dir;
  713. XSTAT_TYPE s;
  714. #endif
  715. char name[MAX_FILENAME_SZ];
  716. } ReadDirCtx;
  717. #define WC_READDIR_NOFILE (-1)
  718. WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
  719. WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
  720. WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
  721. #endif /* !NO_WOLFSSL_DIR */
  722. #define WC_ISFILEEXIST_NOFILE (-1)
  723. WOLFSSL_API int wc_FileExists(const char* fname);
  724. #endif /* !NO_FILESYSTEM */
  725. /* Defaults, user may over-ride with user_settings.h or in a porting section
  726. * above
  727. */
  728. #ifndef XVFPRINTF
  729. #define XVFPRINTF vfprintf
  730. #endif
  731. #ifndef XVSNPRINTF
  732. #define XVSNPRINTF vsnprintf
  733. #endif
  734. #ifndef XFPUTS
  735. #define XFPUTS fputs
  736. #endif
  737. #ifndef XSPRINTF
  738. #define XSPRINTF sprintf
  739. #endif
  740. /* MIN/MAX MACRO SECTION */
  741. /* Windows API defines its own min() macro. */
  742. #if defined(USE_WINDOWS_API)
  743. #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
  744. #define WOLFSSL_HAVE_MIN
  745. #endif /* min */
  746. #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
  747. #define WOLFSSL_HAVE_MAX
  748. #endif /* max */
  749. #endif /* USE_WINDOWS_API */
  750. #ifdef __QNXNTO__
  751. #define WOLFSSL_HAVE_MIN
  752. #define WOLFSSL_HAVE_MAX
  753. #endif
  754. /* TIME SECTION */
  755. /* Time functions */
  756. #ifndef NO_ASN_TIME
  757. #if defined(USER_TIME)
  758. /* Use our gmtime and time_t/struct tm types.
  759. Only needs seconds since EPOCH using XTIME function.
  760. time_t XTIME(time_t * timer) {}
  761. */
  762. #define WOLFSSL_GMTIME
  763. #ifndef HAVE_TM_TYPE
  764. #define USE_WOLF_TM
  765. #endif
  766. #ifndef HAVE_TIME_T_TYPE
  767. #define USE_WOLF_TIME_T
  768. #endif
  769. #elif defined(TIME_OVERRIDES)
  770. /* Override XTIME() and XGMTIME() functionality.
  771. Requires user to provide these functions:
  772. time_t XTIME(time_t * timer) {}
  773. struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {}
  774. */
  775. #ifndef HAVE_TIME_T_TYPE
  776. #define USE_WOLF_TIME_T
  777. #endif
  778. #ifndef HAVE_TM_TYPE
  779. #define USE_WOLF_TM
  780. #endif
  781. #define NEED_TMP_TIME
  782. #elif defined(WOLFSSL_XILINX)
  783. #ifndef XTIME
  784. #define XTIME(t1) xilinx_time((t1))
  785. #endif
  786. #include <time.h>
  787. time_t xilinx_time(time_t * timer);
  788. #elif defined(HAVE_RTP_SYS)
  789. #include "os.h" /* dc_rtc_api needs */
  790. #include "dc_rtc_api.h" /* to get current time */
  791. /* uses partial <time.h> structures */
  792. #define XTIME(tl) (0)
  793. #define XGMTIME(c, t) rtpsys_gmtime((c))
  794. #elif defined(WOLFSSL_DEOS) || defined(WOLFSSL_DEOS_RTEMS)
  795. #include <time.h>
  796. #ifndef XTIME
  797. extern time_t deos_time(time_t* timer);
  798. #define XTIME(t1) deos_time((t1))
  799. #endif
  800. #elif defined(MICRIUM)
  801. #include <clk.h>
  802. #include <time.h>
  803. #define XTIME(t1) micrium_time((t1))
  804. #define WOLFSSL_GMTIME
  805. #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
  806. #include <time.h>
  807. extern time_t pic32_time(time_t* timer);
  808. #define XTIME(t1) pic32_time((t1))
  809. #define XGMTIME(c, t) gmtime((c))
  810. #elif defined(FREESCALE_RTC)
  811. #include <time.h>
  812. #include "fsl_rtc.h"
  813. #ifndef XTIME
  814. #define XTIME(t1) fsl_time((t1))
  815. #endif
  816. #elif defined(FREESCALE_SNVS_RTC)
  817. #include <time.h>
  818. #include "fsl_snvs_hp.h"
  819. time_t fsl_time(time_t* t);
  820. #ifndef XTIME
  821. #define XTIME(t1) fsl_time((t1))
  822. #endif
  823. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  824. #ifdef FREESCALE_MQX_4_0
  825. #include <time.h>
  826. extern time_t mqx_time(time_t* timer);
  827. #else
  828. #define HAVE_GMTIME_R
  829. #endif
  830. #define XTIME(t1) mqx_time((t1))
  831. #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
  832. #include <time.h>
  833. #ifndef XTIME
  834. /*extern time_t ksdk_time(time_t* timer);*/
  835. #define XTIME(t1) ksdk_time((t1))
  836. #endif
  837. #define XGMTIME(c, t) gmtime((c))
  838. #elif defined(WOLFSSL_ATMEL) && defined(WOLFSSL_ATMEL_TIME)
  839. #define XTIME(t1) atmel_get_curr_time_and_date((t1))
  840. #define WOLFSSL_GMTIME
  841. #define USE_WOLF_TM
  842. #define USE_WOLF_TIME_T
  843. #elif defined(WOLFSSL_WICED)
  844. #include <time.h>
  845. time_t wiced_pseudo_unix_epoch_time(time_t * timer);
  846. #define XTIME(t1) wiced_pseudo_unix_epoch_time((t1))
  847. #define HAVE_GMTIME_R
  848. #elif defined(IDIRECT_DEV_TIME)
  849. /*Gets the timestamp from cloak software owned by VT iDirect
  850. in place of time() from <time.h> */
  851. #include <time.h>
  852. #define XTIME(t1) idirect_time((t1))
  853. #define XGMTIME(c, t) gmtime((c))
  854. #elif defined(_WIN32_WCE)
  855. #include <windows.h>
  856. #include <stdlib.h> /* For file system */
  857. time_t windows_time(time_t* timer);
  858. #define FindNextFileA(h, d) FindNextFile(h, (LPWIN32_FIND_DATAW) d)
  859. #define FindFirstFileA(fn, d) FindFirstFile((LPCWSTR) fn, \
  860. (LPWIN32_FIND_DATAW) d)
  861. #define XTIME(t1) windows_time((t1))
  862. #define WOLFSSL_GMTIME
  863. /* if struct tm is not defined in WINCE SDK */
  864. #ifndef _TM_DEFINED
  865. struct tm {
  866. int tm_sec; /* seconds */
  867. int tm_min; /* minutes */
  868. int tm_hour; /* hours */
  869. int tm_mday; /* day of month (month specific) */
  870. int tm_mon; /* month */
  871. int tm_year; /* year */
  872. int tm_wday; /* day of week (out of 1-7)*/
  873. int tm_yday; /* day of year (out of 365) */
  874. int tm_isdst; /* is it daylight savings */
  875. };
  876. #define _TM_DEFINED
  877. #endif
  878. #elif defined(WOLFSSL_APACHE_MYNEWT)
  879. #include "os/os_time.h"
  880. typedef long time_t;
  881. extern time_t mynewt_time(time_t* timer);
  882. #define XTIME(t1) mynewt_time((t1))
  883. #define WOLFSSL_GMTIME
  884. #define USE_WOLF_TM
  885. #define USE_WOLF_TIME_T
  886. #elif defined(WOLFSSL_ZEPHYR)
  887. #ifndef _POSIX_C_SOURCE
  888. #include <zephyr/posix/time.h>
  889. #else
  890. #include <sys/time.h>
  891. #endif
  892. time_t z_time(time_t *timer);
  893. #define XTIME(tl) z_time((tl))
  894. #define XGMTIME(c, t) gmtime((c))
  895. #elif defined(WOLFSSL_TELIT_M2MB)
  896. typedef long time_t;
  897. extern time_t m2mb_xtime(time_t * timer);
  898. #define XTIME(tl) m2mb_xtime((tl))
  899. #ifdef WOLFSSL_TLS13
  900. extern time_t m2mb_xtime_ms(time_t * timer);
  901. #define XTIME_MS(tl) m2mb_xtime_ms((tl))
  902. #endif
  903. #ifndef NO_CRYPT_BENCHMARK
  904. extern double m2mb_xtime_bench(int reset);
  905. #define WOLFSSL_CURRTIME_REMAP m2mb_xtime_bench
  906. #endif
  907. #define XGMTIME(c, t) gmtime((c))
  908. #define WOLFSSL_GMTIME
  909. #define USE_WOLF_TM
  910. #elif defined(WOLFSSL_LINUXKM)
  911. /* definitions are in linuxkm/linuxkm_wc_port.h */
  912. #elif defined(HAL_RTC_MODULE_ENABLED)
  913. #include <time.h>
  914. WOLFSSL_LOCAL time_t stm32_hal_time(time_t* t1);
  915. #define XTIME(t1) stm32_hal_time(t1)
  916. #define WOLFSSL_GMTIME
  917. #else
  918. /* default */
  919. /* uses complete <time.h> facility */
  920. #include <time.h>
  921. #if defined(HAVE_SYS_TIME_H)
  922. #include <sys/time.h>
  923. #endif
  924. /* PowerPC time_t is int */
  925. #ifdef __PPC__
  926. #define TIME_T_NOT_64BIT
  927. #endif
  928. #define XMKTIME(tm) mktime(tm)
  929. #define XDIFFTIME(to, from) difftime(to, from)
  930. #endif
  931. #ifdef SIZEOF_TIME_T
  932. /* check if size of time_t from autoconf is less than 8 bytes (64bits) */
  933. #if SIZEOF_TIME_T < 8
  934. #undef TIME_T_NOT_64BIT
  935. #define TIME_T_NOT_64BIT
  936. #endif
  937. #endif
  938. #ifdef TIME_T_NOT_LONG
  939. /* one old reference to TIME_T_NOT_LONG in GCC-ARM example README
  940. * this keeps support for the old macro name */
  941. #undef TIME_T_NOT_64BIT
  942. #define TIME_T_NOT_64BIT
  943. #endif
  944. /* Map default time functions */
  945. #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
  946. #ifdef TEST_BEFORE_DATE
  947. #define XTIME(tl) (946681200UL) /* Jan 1, 2000 */
  948. #else
  949. #define XTIME(tl) time((tl))
  950. #endif
  951. #endif
  952. #if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
  953. /* Always use gmtime_r if available. */
  954. #if defined(HAVE_GMTIME_S)
  955. /* reentrant version */
  956. #define XGMTIME(c, t) gmtime_s((c), (t))
  957. #define NEED_TMP_TIME
  958. #elif defined(HAVE_GMTIME_R)
  959. #define XGMTIME(c, t) gmtime_r((c), (t))
  960. #define NEED_TMP_TIME
  961. #else
  962. #define XGMTIME(c, t) gmtime((c))
  963. #endif
  964. #endif
  965. #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
  966. #define USE_WOLF_VALIDDATE
  967. #define XVALIDATE_DATE(d, f, t) wc_ValidateDate((d), (f), (t))
  968. #endif
  969. /* wolf struct tm and time_t */
  970. #if defined(USE_WOLF_TM)
  971. struct tm {
  972. int tm_sec; /* seconds after the minute [0-60] */
  973. int tm_min; /* minutes after the hour [0-59] */
  974. int tm_hour; /* hours since midnight [0-23] */
  975. int tm_mday; /* day of the month [1-31] */
  976. int tm_mon; /* months since January [0-11] */
  977. int tm_year; /* years since 1900 */
  978. int tm_wday; /* days since Sunday [0-6] */
  979. int tm_yday; /* days since January 1 [0-365] */
  980. int tm_isdst; /* Daylight Savings Time flag */
  981. long tm_gmtoff; /* offset from CUT in seconds */
  982. char *tm_zone; /* timezone abbreviation */
  983. };
  984. #endif /* USE_WOLF_TM */
  985. #if defined(USE_WOLF_TIME_T)
  986. typedef long time_t;
  987. #endif
  988. #if defined(USE_WOLF_SUSECONDS_T)
  989. typedef long suseconds_t;
  990. #endif
  991. #if defined(USE_WOLF_TIMEVAL_T)
  992. struct timeval
  993. {
  994. time_t tv_sec;
  995. suseconds_t tv_usec;
  996. };
  997. #endif
  998. /* forward declarations */
  999. #if defined(USER_TIME)
  1000. struct tm* gmtime(const time_t* timer);
  1001. extern time_t XTIME(time_t * timer);
  1002. #ifdef STACK_TRAP
  1003. /* for stack trap tracking, don't call os gmtime on OS X/linux,
  1004. uses a lot of stack spce */
  1005. extern time_t time(time_t * timer);
  1006. #define XTIME(tl) time((tl))
  1007. #endif /* STACK_TRAP */
  1008. #elif defined(TIME_OVERRIDES)
  1009. extern time_t XTIME(time_t * timer);
  1010. extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
  1011. #elif defined(WOLFSSL_GMTIME)
  1012. struct tm* gmtime(const time_t* timer);
  1013. #endif
  1014. #endif /* NO_ASN_TIME */
  1015. #ifndef WOLFSSL_LEANPSK
  1016. char* mystrnstr(const char* s1, const char* s2, unsigned int n);
  1017. #endif
  1018. #ifndef FILE_BUFFER_SIZE
  1019. /* default static file buffer size for input, will use dynamic buffer if
  1020. * not big enough */
  1021. #ifdef WOLFSSL_CERT_EXT
  1022. #define FILE_BUFFER_SIZE (3*1024)
  1023. #else
  1024. #define FILE_BUFFER_SIZE (1*1024)
  1025. #endif
  1026. #endif
  1027. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1028. /* By default, the OCTEON's global variables are all thread local. This
  1029. * tag allows them to be shared between threads. */
  1030. #include "cvmx-platform.h"
  1031. #define WOLFSSL_GLOBAL CVMX_SHARED
  1032. #else
  1033. #define WOLFSSL_GLOBAL
  1034. #endif
  1035. #ifdef WOLFSSL_DSP
  1036. #include "wolfssl_dsp.h"
  1037. /* callbacks for setting handle */
  1038. typedef int (*wolfSSL_DSP_Handle_cb)(remote_handle64 *handle, int finished,
  1039. void *ctx);
  1040. WOLFSSL_API int wolfSSL_GetHandleCbSet();
  1041. WOLFSSL_API int wolfSSL_SetHandleCb(wolfSSL_DSP_Handle_cb in);
  1042. WOLFSSL_LOCAL int wolfSSL_InitHandle();
  1043. WOLFSSL_LOCAL void wolfSSL_CleanupHandle();
  1044. #endif
  1045. #ifdef WOLFSSL_SCE
  1046. #ifndef WOLFSSL_SCE_GSCE_HANDLE
  1047. #define WOLFSSL_SCE_GSCE_HANDLE g_sce
  1048. #endif
  1049. #endif
  1050. /* AFTER user_settings.h is loaded,
  1051. ** determine if POSIX multi-threaded: HAVE_PTHREAD */
  1052. #if defined(SINGLE_THREADED) || defined(__MINGW32__)
  1053. /* Never HAVE_PTHREAD in single thread, or non-POSIX mode.
  1054. ** Reminder: MING32 is win32 threads, not POSIX threads */
  1055. #undef HAVE_PTHREAD
  1056. #else
  1057. /* _POSIX_THREADS is defined by unistd.h so this check needs to happen
  1058. * after we include all the platform relevant libs. */
  1059. #ifdef _POSIX_THREADS
  1060. /* HAVE_PTHREAD == POSIX threads capable and enabled. */
  1061. #undef HAVE_PTHREAD
  1062. #define HAVE_PTHREAD 1
  1063. #endif
  1064. #endif
  1065. #ifdef __cplusplus
  1066. } /* extern "C" */
  1067. #endif
  1068. #endif /* WOLF_CRYPT_PORT_H */