wc_port.h 39 KB

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