wc_port.h 33 KB

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