wc_port.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* wc_port.h
  2. *
  3. * Copyright (C) 2006-2017 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifndef WOLF_CRYPT_PORT_H
  22. #define WOLF_CRYPT_PORT_H
  23. #include <wolfssl/wolfcrypt/settings.h>
  24. #include <wolfssl/wolfcrypt/visibility.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #ifdef USE_WINDOWS_API
  29. #ifdef WOLFSSL_GAME_BUILD
  30. #include "system/xtl.h"
  31. #else
  32. #ifndef WIN32_LEAN_AND_MEAN
  33. #define WIN32_LEAN_AND_MEAN
  34. #endif
  35. #ifndef WOLFSSL_SGX
  36. #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
  37. /* On WinCE winsock2.h must be included before windows.h */
  38. #include <winsock2.h>
  39. #endif
  40. #include <windows.h>
  41. #endif /* WOLFSSL_SGX */
  42. #endif
  43. #elif defined(THREADX)
  44. #ifndef SINGLE_THREADED
  45. #ifdef NEED_THREADX_TYPES
  46. #include <types.h>
  47. #endif
  48. #include <tx_api.h>
  49. #endif
  50. #elif defined(MICRIUM)
  51. /* do nothing, just don't pick Unix */
  52. #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
  53. /* do nothing */
  54. #elif defined(EBSNET)
  55. /* do nothing */
  56. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  57. /* do nothing */
  58. #elif defined(FREESCALE_FREE_RTOS)
  59. #include "fsl_os_abstraction.h"
  60. #elif defined(WOLFSSL_uITRON4)
  61. #include "stddef.h"
  62. #include "kernel.h"
  63. #elif defined(WOLFSSL_uTKERNEL2)
  64. #include "tk/tkernel.h"
  65. #elif defined(WOLFSSL_CMSIS_RTOS)
  66. #include "cmsis_os.h"
  67. #elif defined(WOLFSSL_MDK_ARM)
  68. #if defined(WOLFSSL_MDK5)
  69. #include "cmsis_os.h"
  70. #else
  71. #include <rtl.h>
  72. #endif
  73. #elif defined(WOLFSSL_CMSIS_RTOS)
  74. #include "cmsis_os.h"
  75. #elif defined(WOLFSSL_TIRTOS)
  76. #include <ti/sysbios/BIOS.h>
  77. #include <ti/sysbios/knl/Semaphore.h>
  78. #elif defined(WOLFSSL_FROSTED)
  79. #include <semaphore.h>
  80. #elif defined(INTIME_RTOS)
  81. #include <rt.h>
  82. #include <io.h>
  83. #else
  84. #ifndef SINGLE_THREADED
  85. #define WOLFSSL_PTHREADS
  86. #include <pthread.h>
  87. #endif
  88. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  89. #include <unistd.h> /* for close of BIO */
  90. #endif
  91. #endif
  92. /* For FIPS keep the function names the same */
  93. #ifdef HAVE_FIPS
  94. #define wc_InitMutex InitMutex
  95. #define wc_FreeMutex FreeMutex
  96. #define wc_LockMutex LockMutex
  97. #define wc_UnLockMutex UnLockMutex
  98. #endif /* HAVE_FIPS */
  99. #ifdef SINGLE_THREADED
  100. typedef int wolfSSL_Mutex;
  101. #else /* MULTI_THREADED */
  102. /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
  103. #if defined(FREERTOS)
  104. typedef xSemaphoreHandle wolfSSL_Mutex;
  105. #elif defined(FREERTOS_TCP)
  106. #include "FreeRTOS.h"
  107. #include "semphr.h"
  108. typedef SemaphoreHandle_t wolfSSL_Mutex;
  109. #elif defined(WOLFSSL_SAFERTOS)
  110. typedef struct wolfSSL_Mutex {
  111. signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
  112. xSemaphoreHandle mutex;
  113. } wolfSSL_Mutex;
  114. #elif defined(USE_WINDOWS_API)
  115. typedef CRITICAL_SECTION wolfSSL_Mutex;
  116. #elif defined(WOLFSSL_PTHREADS)
  117. typedef pthread_mutex_t wolfSSL_Mutex;
  118. #elif defined(THREADX)
  119. typedef TX_MUTEX wolfSSL_Mutex;
  120. #elif defined(MICRIUM)
  121. typedef OS_MUTEX wolfSSL_Mutex;
  122. #elif defined(EBSNET)
  123. typedef RTP_MUTEX wolfSSL_Mutex;
  124. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  125. typedef MUTEX_STRUCT wolfSSL_Mutex;
  126. #elif defined(FREESCALE_FREE_RTOS)
  127. typedef mutex_t wolfSSL_Mutex;
  128. #elif defined(WOLFSSL_uITRON4)
  129. typedef struct wolfSSL_Mutex {
  130. T_CSEM sem ;
  131. ID id ;
  132. } wolfSSL_Mutex;
  133. #elif defined(WOLFSSL_uTKERNEL2)
  134. typedef struct wolfSSL_Mutex {
  135. T_CSEM sem ;
  136. ID id ;
  137. } wolfSSL_Mutex;
  138. #elif defined(WOLFSSL_MDK_ARM)
  139. #if defined(WOLFSSL_CMSIS_RTOS)
  140. typedef osMutexId wolfSSL_Mutex;
  141. #else
  142. typedef OS_MUT wolfSSL_Mutex;
  143. #endif
  144. #elif defined(WOLFSSL_CMSIS_RTOS)
  145. typedef osMutexId wolfSSL_Mutex;
  146. #elif defined(WOLFSSL_TIRTOS)
  147. typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
  148. #elif defined(WOLFSSL_FROSTED)
  149. typedef mutex_t * wolfSSL_Mutex;
  150. #elif defined(INTIME_RTOS)
  151. typedef RTHANDLE wolfSSL_Mutex;
  152. #else
  153. #error Need a mutex type in multithreaded mode
  154. #endif /* USE_WINDOWS_API */
  155. #endif /* SINGLE_THREADED */
  156. /* Enable crypt HW mutex for Freescale MMCAU or PIC32MZ */
  157. #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ)
  158. #ifndef WOLFSSL_CRYPT_HW_MUTEX
  159. #define WOLFSSL_CRYPT_HW_MUTEX 1
  160. #endif
  161. #endif /* FREESCALE_MMCAU */
  162. #ifndef WOLFSSL_CRYPT_HW_MUTEX
  163. #define WOLFSSL_CRYPT_HW_MUTEX 0
  164. #endif
  165. #if WOLFSSL_CRYPT_HW_MUTEX
  166. /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
  167. however it's recommended to call this directly on Hw init to avoid possible
  168. race condition where two calls to wolfSSL_CryptHwMutexLock are made at
  169. the same time. */
  170. int wolfSSL_CryptHwMutexInit(void);
  171. int wolfSSL_CryptHwMutexLock(void);
  172. int wolfSSL_CryptHwMutexUnLock(void);
  173. #else
  174. /* Define stubs, since HW mutex is disabled */
  175. #define wolfSSL_CryptHwMutexInit() 0 /* Success */
  176. #define wolfSSL_CryptHwMutexLock() 0 /* Success */
  177. #define wolfSSL_CryptHwMutexUnLock() 0 /* Success */
  178. #endif /* WOLFSSL_CRYPT_HW_MUTEX */
  179. /* Mutex functions */
  180. WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*);
  181. WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
  182. WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*);
  183. WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*);
  184. WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex*);
  185. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  186. /* dynamiclly set which mutex to use. unlock / lock is controlled by flag */
  187. typedef void (mutex_cb)(int flag, int type, const char* file, int line);
  188. WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line);
  189. WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
  190. #endif
  191. /* main crypto initialization function */
  192. WOLFSSL_API int wolfCrypt_Init(void);
  193. WOLFSSL_API int wolfCrypt_Cleanup(void);
  194. /* filesystem abstraction layer, used by ssl.c */
  195. #ifndef NO_FILESYSTEM
  196. #if defined(EBSNET)
  197. #include "vfapi.h"
  198. #include "vfile.h"
  199. #define XFILE int
  200. #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0);
  201. #define XFSEEK vf_lseek
  202. #define XFTELL vf_tell
  203. #define XREWIND vf_rewind
  204. #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
  205. #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
  206. #define XFCLOSE vf_close
  207. #define XSEEK_END VSEEK_END
  208. #define XBADFILE -1
  209. #define XFGETS(b,s,f) -2 /* Not ported yet */
  210. #elif defined(LSR_FS)
  211. #include <fs.h>
  212. #define XFILE struct fs_file*
  213. #define XFOPEN(NAME, MODE) fs_open((char*)NAME);
  214. #define XFSEEK(F, O, W) (void)F
  215. #define XFTELL(F) (F)->len
  216. #define XREWIND(F) (void)F
  217. #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
  218. #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
  219. #define XFCLOSE fs_close
  220. #define XSEEK_END 0
  221. #define XBADFILE NULL
  222. #define XFGETS(b,s,f) -2 /* Not ported yet */
  223. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  224. #define XFILE MQX_FILE_PTR
  225. #define XFOPEN fopen
  226. #define XFSEEK fseek
  227. #define XFTELL ftell
  228. #define XREWIND(F) fseek(F, 0, IO_SEEK_SET)
  229. #define XFREAD fread
  230. #define XFWRITE fwrite
  231. #define XFCLOSE fclose
  232. #define XSEEK_END IO_SEEK_END
  233. #define XBADFILE NULL
  234. #define XFGETS fgets
  235. #elif defined(MICRIUM)
  236. #include <fs_api.h>
  237. #define XFILE FS_FILE*
  238. #define XFOPEN fs_fopen
  239. #define XFSEEK fs_fseek
  240. #define XFTELL fs_ftell
  241. #define XREWIND fs_rewind
  242. #define XFREAD fs_fread
  243. #define XFWRITE fs_fwrite
  244. #define XFCLOSE fs_fclose
  245. #define XSEEK_END FS_SEEK_END
  246. #define XBADFILE NULL
  247. #define XFGETS(b,s,f) -2 /* Not ported yet */
  248. #else
  249. /* stdio, default case */
  250. #include <stdio.h>
  251. #define XFILE FILE*
  252. #if defined(WOLFSSL_MDK_ARM)
  253. extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
  254. #define XFOPEN wolfSSL_fopen
  255. #else
  256. #define XFOPEN fopen
  257. #endif
  258. #define XFSEEK fseek
  259. #define XFTELL ftell
  260. #define XREWIND rewind
  261. #define XFREAD fread
  262. #define XFWRITE fwrite
  263. #define XFCLOSE fclose
  264. #define XSEEK_END SEEK_END
  265. #define XBADFILE NULL
  266. #define XFGETS fgets
  267. #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)
  268. #include <dirent.h>
  269. #include <unistd.h>
  270. #include <sys/stat.h>
  271. #endif
  272. #endif
  273. #ifndef MAX_FILENAME_SZ
  274. #define MAX_FILENAME_SZ 256 /* max file name length */
  275. #endif
  276. #ifndef MAX_PATH
  277. #define MAX_PATH 256
  278. #endif
  279. #if !defined(NO_WOLFSSL_DIR)
  280. typedef struct ReadDirCtx {
  281. #ifdef USE_WINDOWS_API
  282. WIN32_FIND_DATAA FindFileData;
  283. HANDLE hFind;
  284. #else
  285. struct dirent* entry;
  286. DIR* dir;
  287. struct stat s;
  288. #endif
  289. char name[MAX_FILENAME_SZ];
  290. } ReadDirCtx;
  291. WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
  292. WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
  293. WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
  294. #endif /* !NO_WOLFSSL_DIR */
  295. #endif /* !NO_FILESYSTEM */
  296. #ifdef USE_WOLF_STRTOK
  297. WOLFSSL_LOCAL char* wc_strtok(char *str, const char *delim, char **nextp);
  298. #endif
  299. /* Windows API defines its own min() macro. */
  300. #if defined(USE_WINDOWS_API)
  301. #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
  302. #define WOLFSSL_HAVE_MIN
  303. #endif /* min */
  304. #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
  305. #define WOLFSSL_HAVE_MAX
  306. #endif /* max */
  307. #endif /* USE_WINDOWS_API */
  308. /* Time functions */
  309. #ifndef NO_ASN_TIME
  310. #if defined(USER_TIME)
  311. /* Use our gmtime and time_t/struct tm types.
  312. Only needs seconds since EPOCH using XTIME function.
  313. time_t XTIME(time_t * timer) {}
  314. */
  315. #define WOLFSSL_GMTIME
  316. #define USE_WOLF_TM
  317. #define USE_WOLF_TIME_T
  318. #elif defined(TIME_OVERRIDES)
  319. /* Override XTIME() and XGMTIME() functionality.
  320. Requires user to provide these functions:
  321. time_t XTIME(time_t * timer) {}
  322. struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {}
  323. */
  324. #ifndef HAVE_TIME_T_TYPE
  325. #define USE_WOLF_TIME_T
  326. #endif
  327. #ifndef HAVE_TM_TYPE
  328. #define USE_WOLF_TM
  329. #endif
  330. #define NEED_TMP_TIME
  331. #elif defined(HAVE_RTP_SYS)
  332. /* uses parital <time.h> structures */
  333. #define XTIME(tl) (0)
  334. #define XGMTIME(c, t) rtpsys_gmtime((c))
  335. #elif defined(MICRIUM)
  336. #include <clk.h>
  337. #include <time.h>
  338. #define XTIME(t1) micrium_time((t1))
  339. #define WOLFSSL_GMTIME
  340. #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
  341. #include <time.h>
  342. #define XTIME(t1) pic32_time((t1))
  343. #define XGMTIME(c, t) gmtime((c))
  344. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  345. #define XTIME(t1) mqx_time((t1))
  346. #define HAVE_GMTIME_R
  347. #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
  348. #include <time.h>
  349. #ifndef XTIME
  350. /*extern time_t ksdk_time(time_t* timer);*/
  351. #define XTIME(t1) ksdk_time((t1))
  352. #endif
  353. #define XGMTIME(c, t) gmtime((c))
  354. #elif defined(WOLFSSL_ATMEL)
  355. #define XTIME(t1) atmel_get_curr_time_and_date((t1))
  356. #define WOLFSSL_GMTIME
  357. #define USE_WOLF_TM
  358. #define USE_WOLF_TIME_T
  359. #elif defined(IDIRECT_DEV_TIME)
  360. /*Gets the timestamp from cloak software owned by VT iDirect
  361. in place of time() from <time.h> */
  362. #include <time.h>
  363. #define XTIME(t1) idirect_time((t1))
  364. #define XGMTIME(c, t) gmtime((c))
  365. #elif defined(_WIN32_WCE)
  366. #include <windows.h>
  367. #define XTIME(t1) windows_time((t1))
  368. #define WOLFSSL_GMTIME
  369. #else
  370. /* default */
  371. /* uses complete <time.h> facility */
  372. #include <time.h>
  373. /* PowerPC time_t is int */
  374. #ifdef __PPC__
  375. #define TIME_T_NOT_LONG
  376. #endif
  377. #endif
  378. /* Map default time functions */
  379. #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
  380. #define XTIME(tl) time((tl))
  381. #endif
  382. #if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
  383. #if defined(WOLFSSL_GMTIME) || !defined(HAVE_GMTIME_R)
  384. #define XGMTIME(c, t) gmtime((c))
  385. #else
  386. #define XGMTIME(c, t) gmtime_r((c), (t))
  387. #define NEED_TMP_TIME
  388. #endif
  389. #endif
  390. #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
  391. #define USE_WOLF_VALIDDATE
  392. #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
  393. #endif
  394. /* wolf struct tm and time_t */
  395. #if defined(USE_WOLF_TM)
  396. struct tm {
  397. int tm_sec; /* seconds after the minute [0-60] */
  398. int tm_min; /* minutes after the hour [0-59] */
  399. int tm_hour; /* hours since midnight [0-23] */
  400. int tm_mday; /* day of the month [1-31] */
  401. int tm_mon; /* months since January [0-11] */
  402. int tm_year; /* years since 1900 */
  403. int tm_wday; /* days since Sunday [0-6] */
  404. int tm_yday; /* days since January 1 [0-365] */
  405. int tm_isdst; /* Daylight Savings Time flag */
  406. long tm_gmtoff; /* offset from CUT in seconds */
  407. char *tm_zone; /* timezone abbreviation */
  408. };
  409. #endif /* USE_WOLF_TM */
  410. #if defined(USE_WOLF_TIME_T)
  411. typedef long time_t;
  412. #endif
  413. /* forward declarations */
  414. #if defined(USER_TIME)
  415. struct tm* gmtime(const time_t* timer);
  416. extern time_t XTIME(time_t * timer);
  417. #ifdef STACK_TRAP
  418. /* for stack trap tracking, don't call os gmtime on OS X/linux,
  419. uses a lot of stack spce */
  420. extern time_t time(time_t * timer);
  421. #define XTIME(tl) time((tl))
  422. #endif /* STACK_TRAP */
  423. #elif defined(TIME_OVERRIDES)
  424. extern time_t XTIME(time_t * timer);
  425. extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
  426. #elif defined(WOLFSSL_GMTIME)
  427. struct tm* gmtime(const time_t* timer);
  428. #endif
  429. #endif /* NO_ASN_TIME */
  430. #ifdef __cplusplus
  431. } /* extern "C" */
  432. #endif
  433. #endif /* WOLF_CRYPT_PORT_H */