wc_port.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  1. /* port.c
  2. *
  3. * Copyright (C) 2006-2020 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/types.h>
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include <wolfssl/wolfcrypt/logging.h>
  28. #include <wolfssl/wolfcrypt/wc_port.h>
  29. #ifdef HAVE_ECC
  30. #include <wolfssl/wolfcrypt/ecc.h>
  31. #endif
  32. #ifdef WOLFSSL_ASYNC_CRYPT
  33. #include <wolfssl/wolfcrypt/async.h>
  34. #endif
  35. /* IPP header files for library initialization */
  36. #ifdef HAVE_FAST_RSA
  37. #include <ipp.h>
  38. #include <ippcp.h>
  39. #endif
  40. #ifdef FREESCALE_LTC_TFM
  41. #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
  42. #endif
  43. #ifdef WOLFSSL_PSOC6_CRYPTO
  44. #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
  45. #endif
  46. #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
  47. defined(WOLFSSL_ATECC608A)
  48. #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
  49. #endif
  50. #if defined(WOLFSSL_RENESAS_TSIP)
  51. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  52. #endif
  53. #if defined(WOLFSSL_STSAFEA100)
  54. #include <wolfssl/wolfcrypt/port/st/stsafe.h>
  55. #endif
  56. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  57. #include <wolfssl/openssl/evp.h>
  58. #endif
  59. #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
  60. #include <wolfssl/wolfcrypt/memory.h>
  61. #include <wolfssl/wolfcrypt/mem_track.h>
  62. #endif
  63. #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
  64. defined(WOLFSSL_IMX6UL_CAAM) || defined(WOLFSSL_IMX6_CAAM_BLOB)
  65. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  66. #endif
  67. #ifdef WOLFSSL_IMXRT_DCP
  68. #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
  69. #endif
  70. #ifdef WOLF_CRYPTO_CB
  71. #include <wolfssl/wolfcrypt/cryptocb.h>
  72. #endif
  73. #ifdef HAVE_INTEL_QA_SYNC
  74. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  75. #endif
  76. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  77. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  78. #endif
  79. #ifdef WOLFSSL_SCE
  80. #include "hal_data.h"
  81. #endif
  82. #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
  83. #include "rpcmem.h"
  84. #endif
  85. #ifdef _MSC_VER
  86. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  87. #pragma warning(disable: 4996)
  88. #endif
  89. /* prevent multiple mutex initializations */
  90. static volatile int initRefCount = 0;
  91. /* Used to initialize state for wolfcrypt
  92. return 0 on success
  93. */
  94. int wolfCrypt_Init(void)
  95. {
  96. int ret = 0;
  97. if (initRefCount == 0) {
  98. WOLFSSL_ENTER("wolfCrypt_Init");
  99. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  100. {
  101. word32 rngMallocFail;
  102. time_t seed = time(NULL);
  103. srand((word32)seed);
  104. rngMallocFail = rand() % 2000; /* max 2000 */
  105. printf("\n--- RNG MALLOC FAIL AT %d---\n", rngMallocFail);
  106. wolfSSL_SetMemFailCount(rngMallocFail);
  107. }
  108. #endif
  109. #ifdef WOLF_CRYPTO_CB
  110. wc_CryptoCb_Init();
  111. #endif
  112. #ifdef WOLFSSL_ASYNC_CRYPT
  113. ret = wolfAsync_HardwareStart();
  114. if (ret != 0) {
  115. WOLFSSL_MSG("Async hardware start failed");
  116. /* don't return failure, allow operation to continue */
  117. }
  118. #endif
  119. #if defined(WOLFSSL_RENESAS_TSIP_CRYPT)
  120. ret = tsip_Open( );
  121. if( ret != TSIP_SUCCESS ) {
  122. WOLFSSL_MSG("RENESAS TSIP Open failed");
  123. /* not return 1 since WOLFSSL_SUCCESS=1*/
  124. ret = -1;/* FATAL ERROR */
  125. return ret;
  126. }
  127. #endif
  128. #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
  129. ret = InitMemoryTracker();
  130. if (ret != 0) {
  131. WOLFSSL_MSG("InitMemoryTracker failed");
  132. return ret;
  133. }
  134. #endif
  135. #if WOLFSSL_CRYPT_HW_MUTEX
  136. /* If crypto hardware mutex protection is enabled, then initialize it */
  137. ret = wolfSSL_CryptHwMutexInit();
  138. if (ret != 0) {
  139. WOLFSSL_MSG("Hw crypt mutex init failed");
  140. return ret;
  141. }
  142. #endif
  143. /* if defined have fast RSA then initialize Intel IPP */
  144. #ifdef HAVE_FAST_RSA
  145. WOLFSSL_MSG("Attempting to use optimized IPP Library");
  146. if ((ret = ippInit()) != ippStsNoErr) {
  147. /* possible to get a CPU feature support status on optimized IPP
  148. library but still use default library and see competitive speeds */
  149. WOLFSSL_MSG("Warning when trying to set up optimization");
  150. WOLFSSL_MSG(ippGetStatusString(ret));
  151. WOLFSSL_MSG("Using default fast IPP library");
  152. ret = 0;
  153. (void)ret; /* suppress not read warning */
  154. }
  155. #endif
  156. #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
  157. ret = ksdk_port_init();
  158. if (ret != 0) {
  159. WOLFSSL_MSG("KSDK port init failed");
  160. return ret;
  161. }
  162. #endif
  163. #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
  164. defined(WOLFSSL_ATECC608A)
  165. ret = atmel_init();
  166. if (ret != 0) {
  167. WOLFSSL_MSG("CryptoAuthLib init failed");
  168. return ret;
  169. }
  170. #endif
  171. #if defined(WOLFSSL_CRYPTOCELL)
  172. /* enable and initialize the ARM CryptoCell 3xx runtime library */
  173. ret = cc310_Init();
  174. if (ret != 0) {
  175. WOLFSSL_MSG("CRYPTOCELL init failed");
  176. return ret;
  177. }
  178. #endif
  179. #if defined(WOLFSSL_STSAFEA100)
  180. stsafe_interface_init();
  181. #endif
  182. #if defined(WOLFSSL_PSOC6_CRYPTO)
  183. ret = psoc6_crypto_port_init();
  184. if (ret != 0) {
  185. WOLFSSL_MSG("PSoC6 crypto engine init failed");
  186. return ret;
  187. }
  188. #endif
  189. #ifdef WOLFSSL_SILABS_SE_ACCEL
  190. /* init handles if it is already initialized */
  191. ret = sl_se_init();
  192. #endif
  193. #ifdef WOLFSSL_ARMASM
  194. WOLFSSL_MSG("Using ARM hardware acceleration");
  195. #endif
  196. #ifdef WOLFSSL_AFALG
  197. WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
  198. #endif
  199. #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
  200. wolfSSL_EVP_init();
  201. #endif
  202. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  203. if ((ret = wc_LoggingInit()) != 0) {
  204. WOLFSSL_MSG("Error creating logging mutex");
  205. return ret;
  206. }
  207. #endif
  208. #ifdef HAVE_ECC
  209. #ifdef FP_ECC
  210. wc_ecc_fp_init();
  211. #endif
  212. #ifdef ECC_CACHE_CURVE
  213. if ((ret = wc_ecc_curve_cache_init()) != 0) {
  214. WOLFSSL_MSG("Error creating curve cache");
  215. return ret;
  216. }
  217. #endif
  218. #endif
  219. #ifdef WOLFSSL_SCE
  220. ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(
  221. WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg);
  222. if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) {
  223. WOLFSSL_MSG("SCE already open");
  224. ret = 0;
  225. }
  226. if (ret != SSP_SUCCESS) {
  227. WOLFSSL_MSG("Error opening SCE");
  228. return -1; /* FATAL_ERROR */
  229. }
  230. #endif
  231. #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
  232. defined(WOLFSSL_IMX6UL_CAAM) || defined(WOLFSSL_IMX6_CAAM_BLOB)
  233. if ((ret = wc_caamInit()) != 0) {
  234. return ret;
  235. }
  236. #endif
  237. #ifdef WOLFSSL_IMXRT_DCP
  238. if ((ret = wc_dcp_init()) != 0) {
  239. return ret;
  240. }
  241. #endif
  242. #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
  243. if ((ret = wolfSSL_InitHandle()) != 0) {
  244. return ret;
  245. }
  246. rpcmem_init();
  247. #endif
  248. }
  249. initRefCount++;
  250. return ret;
  251. }
  252. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  253. long wolfCrypt_heap_peakAllocs_checkpoint(void) {
  254. long ret = ourMemStats.peakAllocsTripOdometer;
  255. ourMemStats.peakAllocsTripOdometer = ourMemStats.totalAllocs -
  256. ourMemStats.totalDeallocs;
  257. return ret;
  258. }
  259. long wolfCrypt_heap_peakBytes_checkpoint(void) {
  260. long ret = ourMemStats.peakBytesTripOdometer;
  261. ourMemStats.peakBytesTripOdometer = ourMemStats.currentBytes;
  262. return ret;
  263. }
  264. #endif
  265. /* return success value is the same as wolfCrypt_Init */
  266. int wolfCrypt_Cleanup(void)
  267. {
  268. int ret = 0;
  269. initRefCount--;
  270. if (initRefCount < 0)
  271. initRefCount = 0;
  272. if (initRefCount == 0) {
  273. WOLFSSL_ENTER("wolfCrypt_Cleanup");
  274. #ifdef HAVE_ECC
  275. #ifdef FP_ECC
  276. wc_ecc_fp_free();
  277. #endif
  278. #ifdef ECC_CACHE_CURVE
  279. wc_ecc_curve_cache_free();
  280. #endif
  281. #endif /* HAVE_ECC */
  282. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  283. ret = wc_LoggingCleanup();
  284. #endif
  285. #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
  286. ShowMemoryTracker();
  287. #endif
  288. #ifdef WOLFSSL_ASYNC_CRYPT
  289. wolfAsync_HardwareStop();
  290. #endif
  291. #ifdef WOLFSSL_SCE
  292. WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl);
  293. #endif
  294. #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
  295. defined(WOLFSSL_IMX6_CAAM_BLOB)
  296. wc_caamFree();
  297. #endif
  298. #if defined(WOLFSSL_CRYPTOCELL)
  299. cc310_Free();
  300. #endif
  301. #ifdef WOLFSSL_SILABS_SE_ACCEL
  302. ret = sl_se_deinit();
  303. #endif
  304. #if defined(WOLFSSL_RENESAS_TSIP_CRYPT)
  305. tsip_Close();
  306. #endif
  307. #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
  308. rpcmem_deinit();
  309. wolfSSL_CleanupHandle();
  310. #endif
  311. }
  312. return ret;
  313. }
  314. #ifndef NO_FILESYSTEM
  315. /* Helpful function to load file into allocated buffer */
  316. int wc_FileLoad(const char* fname, unsigned char** buf, size_t* bufLen,
  317. void* heap)
  318. {
  319. int ret;
  320. size_t fileSz;
  321. XFILE f;
  322. if (fname == NULL || buf == NULL || bufLen == NULL) {
  323. return BAD_FUNC_ARG;
  324. }
  325. /* set defaults */
  326. *buf = NULL;
  327. *bufLen = 0;
  328. /* open file (read-only binary) */
  329. f = XFOPEN(fname, "rb");
  330. if (!f) {
  331. WOLFSSL_MSG("wc_LoadFile file load error");
  332. return BAD_PATH_ERROR;
  333. }
  334. XFSEEK(f, 0, XSEEK_END);
  335. fileSz = XFTELL(f);
  336. XREWIND(f);
  337. if (fileSz > 0) {
  338. *bufLen = fileSz;
  339. *buf = (byte*)XMALLOC(*bufLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
  340. if (*buf == NULL) {
  341. WOLFSSL_MSG("wc_LoadFile memory error");
  342. ret = MEMORY_E;
  343. }
  344. else {
  345. size_t readLen = XFREAD(*buf, 1, *bufLen, f);
  346. /* check response code */
  347. ret = (readLen == *bufLen) ? 0 : -1;
  348. }
  349. }
  350. else {
  351. ret = BUFFER_E;
  352. }
  353. XFCLOSE(f);
  354. (void)heap;
  355. return ret;
  356. }
  357. #if !defined(NO_WOLFSSL_DIR) && \
  358. !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
  359. /* File Handling Helpers */
  360. /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
  361. int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
  362. {
  363. int ret = WC_READDIR_NOFILE; /* default to no files found */
  364. int pathLen = 0;
  365. int dnameLen = 0;
  366. if (name)
  367. *name = NULL;
  368. if (ctx == NULL || path == NULL) {
  369. return BAD_FUNC_ARG;
  370. }
  371. XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
  372. pathLen = (int)XSTRLEN(path);
  373. #ifdef USE_WINDOWS_API
  374. if (pathLen > MAX_FILENAME_SZ - 3)
  375. return BAD_PATH_ERROR;
  376. XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
  377. XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
  378. ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
  379. if (ctx->hFind == INVALID_HANDLE_VALUE) {
  380. WOLFSSL_MSG("FindFirstFile for path verify locations failed");
  381. return BAD_PATH_ERROR;
  382. }
  383. do {
  384. if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  385. dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
  386. if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
  387. return BAD_PATH_ERROR;
  388. }
  389. XSTRNCPY(ctx->name, path, pathLen + 1);
  390. ctx->name[pathLen] = '\\';
  391. XSTRNCPY(ctx->name + pathLen + 1,
  392. ctx->FindFileData.cFileName,
  393. MAX_FILENAME_SZ - pathLen - 1);
  394. if (name)
  395. *name = ctx->name;
  396. return 0;
  397. }
  398. } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
  399. #elif defined(WOLFSSL_ZEPHYR)
  400. if (fs_opendir(&ctx->dir, path) != 0) {
  401. WOLFSSL_MSG("opendir path verify locations failed");
  402. return BAD_PATH_ERROR;
  403. }
  404. ctx->dirp = &ctx->dir;
  405. while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
  406. dnameLen = (int)XSTRLEN(ctx->entry.name);
  407. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  408. ret = BAD_PATH_ERROR;
  409. break;
  410. }
  411. XSTRNCPY(ctx->name, path, pathLen + 1);
  412. ctx->name[pathLen] = '/';
  413. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  414. * of earlier check it is known that dnameLen is less than
  415. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  416. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
  417. if (fs_stat(ctx->name, &ctx->s) != 0) {
  418. WOLFSSL_MSG("stat on name failed");
  419. ret = BAD_PATH_ERROR;
  420. break;
  421. } else if (ctx->s.type == FS_DIR_ENTRY_FILE) {
  422. if (name)
  423. *name = ctx->name;
  424. return 0;
  425. }
  426. }
  427. #elif defined(WOLFSSL_TELIT_M2MB)
  428. ctx->dir = m2mb_fs_opendir((const CHAR*)path);
  429. if (ctx->dir == NULL) {
  430. WOLFSSL_MSG("opendir path verify locations failed");
  431. return BAD_PATH_ERROR;
  432. }
  433. while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
  434. dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  435. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  436. ret = BAD_PATH_ERROR;
  437. break;
  438. }
  439. XSTRNCPY(ctx->name, path, pathLen + 1);
  440. ctx->name[pathLen] = '/';
  441. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  442. * of earlier check it is known that dnameLen is less than
  443. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  444. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
  445. if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
  446. WOLFSSL_MSG("stat on name failed");
  447. ret = BAD_PATH_ERROR;
  448. break;
  449. }
  450. else if (ctx->s.st_mode & M2MB_S_IFREG) {
  451. if (name)
  452. *name = ctx->name;
  453. return 0;
  454. }
  455. }
  456. #else
  457. ctx->dir = opendir(path);
  458. if (ctx->dir == NULL) {
  459. WOLFSSL_MSG("opendir path verify locations failed");
  460. return BAD_PATH_ERROR;
  461. }
  462. while ((ctx->entry = readdir(ctx->dir)) != NULL) {
  463. dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  464. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  465. ret = BAD_PATH_ERROR;
  466. break;
  467. }
  468. XSTRNCPY(ctx->name, path, pathLen + 1);
  469. ctx->name[pathLen] = '/';
  470. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  471. * of earlier check it is known that dnameLen is less than
  472. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  473. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
  474. if (stat(ctx->name, &ctx->s) != 0) {
  475. WOLFSSL_MSG("stat on name failed");
  476. ret = BAD_PATH_ERROR;
  477. break;
  478. } else if (S_ISREG(ctx->s.st_mode)) {
  479. if (name)
  480. *name = ctx->name;
  481. return 0;
  482. }
  483. }
  484. #endif
  485. wc_ReadDirClose(ctx);
  486. return ret;
  487. }
  488. /* returns 0 if file found, WC_READDIR_NOFILE if no more files */
  489. int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
  490. {
  491. int ret = WC_READDIR_NOFILE; /* default to no file found */
  492. int pathLen = 0;
  493. int dnameLen = 0;
  494. if (name)
  495. *name = NULL;
  496. if (ctx == NULL || path == NULL) {
  497. return BAD_FUNC_ARG;
  498. }
  499. XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
  500. pathLen = (int)XSTRLEN(path);
  501. #ifdef USE_WINDOWS_API
  502. while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
  503. if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  504. dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
  505. if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
  506. return BAD_PATH_ERROR;
  507. }
  508. XSTRNCPY(ctx->name, path, pathLen + 1);
  509. ctx->name[pathLen] = '\\';
  510. XSTRNCPY(ctx->name + pathLen + 1,
  511. ctx->FindFileData.cFileName,
  512. MAX_FILENAME_SZ - pathLen - 1);
  513. if (name)
  514. *name = ctx->name;
  515. return 0;
  516. }
  517. }
  518. #elif defined(WOLFSSL_ZEPHYR)
  519. while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
  520. dnameLen = (int)XSTRLEN(ctx->entry.name);
  521. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  522. ret = BAD_PATH_ERROR;
  523. break;
  524. }
  525. XSTRNCPY(ctx->name, path, pathLen + 1);
  526. ctx->name[pathLen] = '/';
  527. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  528. * of earlier check it is known that dnameLen is less than
  529. * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
  530. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
  531. if (fs_stat(ctx->name, &ctx->s) != 0) {
  532. WOLFSSL_MSG("stat on name failed");
  533. ret = BAD_PATH_ERROR;
  534. break;
  535. } else if (ctx->s.type == FS_DIR_ENTRY_FILE) {
  536. if (name)
  537. *name = ctx->name;
  538. return 0;
  539. }
  540. }
  541. #elif defined(WOLFSSL_TELIT_M2MB)
  542. while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
  543. dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  544. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  545. ret = BAD_PATH_ERROR;
  546. break;
  547. }
  548. XSTRNCPY(ctx->name, path, pathLen + 1);
  549. ctx->name[pathLen] = '/';
  550. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  551. * of earlier check it is known that dnameLen is less than
  552. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  553. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
  554. if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
  555. WOLFSSL_MSG("stat on name failed");
  556. ret = BAD_PATH_ERROR;
  557. break;
  558. }
  559. else if (ctx->s.st_mode & M2MB_S_IFREG) {
  560. if (name)
  561. *name = ctx->name;
  562. return 0;
  563. }
  564. }
  565. #else
  566. while ((ctx->entry = readdir(ctx->dir)) != NULL) {
  567. dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  568. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  569. ret = BAD_PATH_ERROR;
  570. break;
  571. }
  572. XSTRNCPY(ctx->name, path, pathLen + 1);
  573. ctx->name[pathLen] = '/';
  574. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  575. * of earlier check it is known that dnameLen is less than
  576. * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
  577. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
  578. if (stat(ctx->name, &ctx->s) != 0) {
  579. WOLFSSL_MSG("stat on name failed");
  580. ret = BAD_PATH_ERROR;
  581. break;
  582. } else if (S_ISREG(ctx->s.st_mode)) {
  583. if (name)
  584. *name = ctx->name;
  585. return 0;
  586. }
  587. }
  588. #endif
  589. wc_ReadDirClose(ctx);
  590. return ret;
  591. }
  592. void wc_ReadDirClose(ReadDirCtx* ctx)
  593. {
  594. if (ctx == NULL) {
  595. return;
  596. }
  597. #ifdef USE_WINDOWS_API
  598. if (ctx->hFind != INVALID_HANDLE_VALUE) {
  599. FindClose(ctx->hFind);
  600. ctx->hFind = INVALID_HANDLE_VALUE;
  601. }
  602. #elif defined(WOLFSSL_ZEPHYR)
  603. if (ctx->dirp) {
  604. fs_closedir(ctx->dirp);
  605. ctx->dirp = NULL;
  606. }
  607. #elif defined(WOLFSSL_TELIT_M2MB)
  608. if (ctx->dir) {
  609. m2mb_fs_closedir(ctx->dir);
  610. ctx->dir = NULL;
  611. }
  612. #else
  613. if (ctx->dir) {
  614. closedir(ctx->dir);
  615. ctx->dir = NULL;
  616. }
  617. #endif
  618. }
  619. #endif /* !NO_WOLFSSL_DIR */
  620. #endif /* !NO_FILESYSTEM */
  621. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_ZEPHYR)
  622. XFILE z_fs_open(const char* filename, const char* perm)
  623. {
  624. XFILE file;
  625. file = (XFILE)XMALLOC(sizeof(*file), NULL, DYNAMIC_TYPE_FILE);
  626. if (file != NULL) {
  627. if (fs_open(file, filename) != 0) {
  628. XFREE(file, NULL, DYNAMIC_TYPE_FILE);
  629. file = NULL;
  630. }
  631. }
  632. return file;
  633. }
  634. int z_fs_close(XFILE file)
  635. {
  636. int ret;
  637. if (file == NULL)
  638. return -1;
  639. ret = (fs_close(file) == 0) ? 0 : -1;
  640. XFREE(file, NULL, DYNAMIC_TYPE_FILE);
  641. return ret;
  642. }
  643. #endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */
  644. #if !defined(WOLFSSL_USER_MUTEX)
  645. wolfSSL_Mutex* wc_InitAndAllocMutex(void)
  646. {
  647. wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
  648. DYNAMIC_TYPE_MUTEX);
  649. if (m != NULL) {
  650. if (wc_InitMutex(m) != 0) {
  651. WOLFSSL_MSG("Init Mutex failed");
  652. XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
  653. m = NULL;
  654. }
  655. }
  656. else {
  657. WOLFSSL_MSG("Memory error with Mutex allocation");
  658. }
  659. return m;
  660. }
  661. #endif
  662. #ifdef USE_WOLF_STRTOK
  663. /* String token (delim) search. If str is null use nextp. */
  664. char* wc_strtok(char *str, const char *delim, char **nextp)
  665. {
  666. char* ret;
  667. int i, j;
  668. /* Use next if str is NULL */
  669. if (str == NULL && nextp)
  670. str = *nextp;
  671. /* verify str input */
  672. if (str == NULL || *str == '\0')
  673. return NULL;
  674. /* match on entire delim */
  675. for (i = 0; str[i]; i++) {
  676. for (j = 0; delim[j]; j++) {
  677. if (delim[j] == str[i])
  678. break;
  679. }
  680. if (!delim[j])
  681. break;
  682. }
  683. str += i;
  684. /* if end of string, not found so return NULL */
  685. if (*str == '\0')
  686. return NULL;
  687. ret = str;
  688. /* match on first delim */
  689. for (i = 0; str[i]; i++) {
  690. for (j = 0; delim[j]; j++) {
  691. if (delim[j] == str[i])
  692. break;
  693. }
  694. if (delim[j] == str[i])
  695. break;
  696. }
  697. str += i;
  698. /* null terminate found string */
  699. if (*str)
  700. *str++ = '\0';
  701. /* return pointer to next */
  702. if (nextp)
  703. *nextp = str;
  704. return ret;
  705. }
  706. #endif /* USE_WOLF_STRTOK */
  707. #ifdef USE_WOLF_STRSEP
  708. char* wc_strsep(char **stringp, const char *delim)
  709. {
  710. char *s, *tok;
  711. const char *spanp;
  712. /* null check */
  713. if (stringp == NULL || *stringp == NULL)
  714. return NULL;
  715. s = *stringp;
  716. for (tok = s; *tok; ++tok) {
  717. for (spanp = delim; *spanp; ++spanp) {
  718. /* found delimiter */
  719. if (*tok == *spanp) {
  720. *tok = '\0'; /* replace delim with null term */
  721. *stringp = tok + 1; /* return past delim */
  722. return s;
  723. }
  724. }
  725. }
  726. *stringp = NULL;
  727. return s;
  728. }
  729. #endif /* USE_WOLF_STRSEP */
  730. #if WOLFSSL_CRYPT_HW_MUTEX
  731. /* Mutex for protection of cryptography hardware */
  732. static wolfSSL_Mutex wcCryptHwMutex;
  733. static int wcCryptHwMutexInit = 0;
  734. int wolfSSL_CryptHwMutexInit(void)
  735. {
  736. int ret = 0;
  737. if (wcCryptHwMutexInit == 0) {
  738. ret = wc_InitMutex(&wcCryptHwMutex);
  739. if (ret == 0) {
  740. wcCryptHwMutexInit = 1;
  741. }
  742. }
  743. return ret;
  744. }
  745. int wolfSSL_CryptHwMutexLock(void)
  746. {
  747. int ret = BAD_MUTEX_E;
  748. /* Make sure HW Mutex has been initialized */
  749. ret = wolfSSL_CryptHwMutexInit();
  750. if (ret == 0) {
  751. ret = wc_LockMutex(&wcCryptHwMutex);
  752. }
  753. return ret;
  754. }
  755. int wolfSSL_CryptHwMutexUnLock(void)
  756. {
  757. int ret = BAD_MUTEX_E;
  758. if (wcCryptHwMutexInit) {
  759. ret = wc_UnLockMutex(&wcCryptHwMutex);
  760. }
  761. return ret;
  762. }
  763. #endif /* WOLFSSL_CRYPT_HW_MUTEX */
  764. /* ---------------------------------------------------------------------------*/
  765. /* Mutex Ports */
  766. /* ---------------------------------------------------------------------------*/
  767. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  768. static mutex_cb* compat_mutex_cb = NULL;
  769. /* Function that locks or unlocks a mutex based on the flag passed in.
  770. *
  771. * flag lock or unlock i.e. CRYPTO_LOCK
  772. * type the type of lock to unlock or lock
  773. * file name of the file calling
  774. * line the line number from file calling
  775. */
  776. int wc_LockMutex_ex(int flag, int type, const char* file, int line)
  777. {
  778. if (compat_mutex_cb != NULL) {
  779. compat_mutex_cb(flag, type, file, line);
  780. return 0;
  781. }
  782. else {
  783. WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
  784. return BAD_STATE_E;
  785. }
  786. }
  787. /* Set the callback function to use for locking/unlocking mutex
  788. *
  789. * cb callback function to use
  790. */
  791. int wc_SetMutexCb(mutex_cb* cb)
  792. {
  793. compat_mutex_cb = cb;
  794. return 0;
  795. }
  796. #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
  797. #ifdef SINGLE_THREADED
  798. int wc_InitMutex(wolfSSL_Mutex* m)
  799. {
  800. (void)m;
  801. return 0;
  802. }
  803. int wc_FreeMutex(wolfSSL_Mutex *m)
  804. {
  805. (void)m;
  806. return 0;
  807. }
  808. int wc_LockMutex(wolfSSL_Mutex *m)
  809. {
  810. (void)m;
  811. return 0;
  812. }
  813. int wc_UnLockMutex(wolfSSL_Mutex *m)
  814. {
  815. (void)m;
  816. return 0;
  817. }
  818. #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
  819. defined(FREESCALE_FREE_RTOS)
  820. int wc_InitMutex(wolfSSL_Mutex* m)
  821. {
  822. int iReturn;
  823. *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
  824. if( *m != NULL )
  825. iReturn = 0;
  826. else
  827. iReturn = BAD_MUTEX_E;
  828. return iReturn;
  829. }
  830. int wc_FreeMutex(wolfSSL_Mutex* m)
  831. {
  832. vSemaphoreDelete( *m );
  833. return 0;
  834. }
  835. int wc_LockMutex(wolfSSL_Mutex* m)
  836. {
  837. /* Assume an infinite block, or should there be zero block? */
  838. xSemaphoreTake( *m, portMAX_DELAY );
  839. return 0;
  840. }
  841. int wc_UnLockMutex(wolfSSL_Mutex* m)
  842. {
  843. xSemaphoreGive( *m );
  844. return 0;
  845. }
  846. #elif defined(RTTHREAD)
  847. int wc_InitMutex(wolfSSL_Mutex* m)
  848. {
  849. int iReturn;
  850. *m = ( wolfSSL_Mutex ) rt_mutex_create("mutex",RT_IPC_FLAG_FIFO);
  851. if( *m != NULL )
  852. iReturn = 0;
  853. else
  854. iReturn = BAD_MUTEX_E;
  855. return iReturn;
  856. }
  857. int wc_FreeMutex(wolfSSL_Mutex* m)
  858. {
  859. rt_mutex_delete( *m );
  860. return 0;
  861. }
  862. int wc_LockMutex(wolfSSL_Mutex* m)
  863. {
  864. /* Assume an infinite block, or should there be zero block? */
  865. return rt_mutex_take( *m, RT_WAITING_FOREVER );
  866. }
  867. int wc_UnLockMutex(wolfSSL_Mutex* m)
  868. {
  869. return rt_mutex_release( *m );
  870. }
  871. #elif defined(WOLFSSL_SAFERTOS)
  872. int wc_InitMutex(wolfSSL_Mutex* m)
  873. {
  874. vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
  875. if (m->mutex == NULL)
  876. return BAD_MUTEX_E;
  877. return 0;
  878. }
  879. int wc_FreeMutex(wolfSSL_Mutex* m)
  880. {
  881. (void)m;
  882. return 0;
  883. }
  884. int wc_LockMutex(wolfSSL_Mutex* m)
  885. {
  886. /* Assume an infinite block */
  887. xSemaphoreTake(m->mutex, portMAX_DELAY);
  888. return 0;
  889. }
  890. int wc_UnLockMutex(wolfSSL_Mutex* m)
  891. {
  892. xSemaphoreGive(m->mutex);
  893. return 0;
  894. }
  895. #elif defined(USE_WINDOWS_API)
  896. int wc_InitMutex(wolfSSL_Mutex* m)
  897. {
  898. InitializeCriticalSection(m);
  899. return 0;
  900. }
  901. int wc_FreeMutex(wolfSSL_Mutex* m)
  902. {
  903. DeleteCriticalSection(m);
  904. return 0;
  905. }
  906. int wc_LockMutex(wolfSSL_Mutex* m)
  907. {
  908. EnterCriticalSection(m);
  909. return 0;
  910. }
  911. int wc_UnLockMutex(wolfSSL_Mutex* m)
  912. {
  913. LeaveCriticalSection(m);
  914. return 0;
  915. }
  916. #elif defined(WOLFSSL_PTHREADS)
  917. int wc_InitMutex(wolfSSL_Mutex* m)
  918. {
  919. if (pthread_mutex_init(m, 0) == 0)
  920. return 0;
  921. else
  922. return BAD_MUTEX_E;
  923. }
  924. int wc_FreeMutex(wolfSSL_Mutex* m)
  925. {
  926. if (pthread_mutex_destroy(m) == 0)
  927. return 0;
  928. else
  929. return BAD_MUTEX_E;
  930. }
  931. int wc_LockMutex(wolfSSL_Mutex* m)
  932. {
  933. if (pthread_mutex_lock(m) == 0)
  934. return 0;
  935. else
  936. return BAD_MUTEX_E;
  937. }
  938. int wc_UnLockMutex(wolfSSL_Mutex* m)
  939. {
  940. if (pthread_mutex_unlock(m) == 0)
  941. return 0;
  942. else
  943. return BAD_MUTEX_E;
  944. }
  945. #elif defined(WOLFSSL_KTHREADS)
  946. /* Linux kernel mutex routines are voids, alas. */
  947. int wc_InitMutex(wolfSSL_Mutex* m)
  948. {
  949. mutex_init(m);
  950. return 0;
  951. }
  952. int wc_FreeMutex(wolfSSL_Mutex* m)
  953. {
  954. mutex_destroy(m);
  955. return 0;
  956. }
  957. int wc_LockMutex(wolfSSL_Mutex* m)
  958. {
  959. mutex_lock(m);
  960. return 0;
  961. }
  962. int wc_UnLockMutex(wolfSSL_Mutex* m)
  963. {
  964. mutex_unlock(m);
  965. return 0;
  966. }
  967. #elif defined(WOLFSSL_VXWORKS)
  968. int wc_InitMutex(wolfSSL_Mutex* m)
  969. {
  970. if (m) {
  971. if ((*m = semMCreate(0)) != SEM_ID_NULL)
  972. return 0;
  973. }
  974. return BAD_MUTEX_E;
  975. }
  976. int wc_FreeMutex(wolfSSL_Mutex* m)
  977. {
  978. if (m) {
  979. if (semDelete(*m) == OK)
  980. return 0;
  981. }
  982. return BAD_MUTEX_E;
  983. }
  984. int wc_LockMutex(wolfSSL_Mutex* m)
  985. {
  986. if (m) {
  987. if (semTake(*m, WAIT_FOREVER) == OK)
  988. return 0;
  989. }
  990. return BAD_MUTEX_E;
  991. }
  992. int wc_UnLockMutex(wolfSSL_Mutex* m)
  993. {
  994. if (m) {
  995. if (semGive(*m) == OK)
  996. return 0;
  997. }
  998. return BAD_MUTEX_E;
  999. }
  1000. #elif defined(THREADX)
  1001. int wc_InitMutex(wolfSSL_Mutex* m)
  1002. {
  1003. if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
  1004. return 0;
  1005. else
  1006. return BAD_MUTEX_E;
  1007. }
  1008. int wc_FreeMutex(wolfSSL_Mutex* m)
  1009. {
  1010. if (tx_mutex_delete(m) == 0)
  1011. return 0;
  1012. else
  1013. return BAD_MUTEX_E;
  1014. }
  1015. int wc_LockMutex(wolfSSL_Mutex* m)
  1016. {
  1017. if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
  1018. return 0;
  1019. else
  1020. return BAD_MUTEX_E;
  1021. }
  1022. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1023. {
  1024. if (tx_mutex_put(m) == 0)
  1025. return 0;
  1026. else
  1027. return BAD_MUTEX_E;
  1028. }
  1029. #elif defined(WOLFSSL_DEOS)
  1030. int wc_InitMutex(wolfSSL_Mutex* m)
  1031. {
  1032. mutexStatus mutStat;
  1033. /*
  1034. The empty string "" denotes an anonymous mutex, so objects do not cause name collisions.
  1035. `protectWolfSSLTemp` in an XML configuration element template describing a mutex.
  1036. */
  1037. if (m) {
  1038. mutStat = createMutex("", "protectWolfSSLTemp", m);
  1039. if (mutStat == mutexSuccess)
  1040. return 0;
  1041. else{
  1042. WOLFSSL_MSG("wc_InitMutex failed");
  1043. return mutStat;
  1044. }
  1045. }
  1046. return BAD_MUTEX_E;
  1047. }
  1048. int wc_FreeMutex(wolfSSL_Mutex* m)
  1049. {
  1050. mutexStatus mutStat;
  1051. if (m) {
  1052. mutStat = deleteMutex(*m);
  1053. if (mutStat == mutexSuccess)
  1054. return 0;
  1055. else{
  1056. WOLFSSL_MSG("wc_FreeMutex failed");
  1057. return mutStat;
  1058. }
  1059. }
  1060. return BAD_MUTEX_E;
  1061. }
  1062. int wc_LockMutex(wolfSSL_Mutex* m)
  1063. {
  1064. mutexStatus mutStat;
  1065. if (m) {
  1066. mutStat = lockMutex(*m);
  1067. if (mutStat == mutexSuccess)
  1068. return 0;
  1069. else{
  1070. WOLFSSL_MSG("wc_LockMutex failed");
  1071. return mutStat;
  1072. }
  1073. }
  1074. return BAD_MUTEX_E;
  1075. }
  1076. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1077. {
  1078. mutexStatus mutStat;
  1079. if (m) {
  1080. mutStat = unlockMutex(*m);
  1081. if (mutStat== mutexSuccess)
  1082. return 0;
  1083. else{
  1084. WOLFSSL_MSG("wc_UnLockMutex failed");
  1085. return mutStat;
  1086. }
  1087. }
  1088. return BAD_MUTEX_E;
  1089. }
  1090. #elif defined(MICRIUM)
  1091. #if (OS_VERSION < 50000)
  1092. #define MICRIUM_ERR_TYPE OS_ERR
  1093. #define MICRIUM_ERR_NONE OS_ERR_NONE
  1094. #define MICRIUM_ERR_CODE(err) err
  1095. #else
  1096. #define MICRIUM_ERR_TYPE RTOS_ERR
  1097. #define MICRIUM_ERR_NONE RTOS_ERR_NONE
  1098. #define MICRIUM_ERR_CODE(err) RTOS_ERR_CODE_GET(err)
  1099. #endif
  1100. int wc_InitMutex(wolfSSL_Mutex* m)
  1101. {
  1102. MICRIUM_ERR_TYPE err;
  1103. OSMutexCreate(m, "wolfSSL Mutex", &err);
  1104. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1105. return 0;
  1106. else
  1107. return BAD_MUTEX_E;
  1108. }
  1109. int wc_FreeMutex(wolfSSL_Mutex* m)
  1110. {
  1111. #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
  1112. MICRIUM_ERR_TYPE err;
  1113. OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
  1114. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1115. return 0;
  1116. else
  1117. return BAD_MUTEX_E;
  1118. #else
  1119. (void)m;
  1120. return 0;
  1121. #endif
  1122. }
  1123. int wc_LockMutex(wolfSSL_Mutex* m)
  1124. {
  1125. MICRIUM_ERR_TYPE err;
  1126. OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
  1127. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1128. return 0;
  1129. else
  1130. return BAD_MUTEX_E;
  1131. }
  1132. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1133. {
  1134. MICRIUM_ERR_TYPE err;
  1135. OSMutexPost(m, OS_OPT_POST_NONE, &err);
  1136. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1137. return 0;
  1138. else
  1139. return BAD_MUTEX_E;
  1140. }
  1141. #elif defined(EBSNET)
  1142. int wc_InitMutex(wolfSSL_Mutex* m)
  1143. {
  1144. if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
  1145. return BAD_MUTEX_E;
  1146. else
  1147. return 0;
  1148. }
  1149. int wc_FreeMutex(wolfSSL_Mutex* m)
  1150. {
  1151. rtp_sig_mutex_free(*m);
  1152. return 0;
  1153. }
  1154. int wc_LockMutex(wolfSSL_Mutex* m)
  1155. {
  1156. if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
  1157. return 0;
  1158. else
  1159. return BAD_MUTEX_E;
  1160. }
  1161. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1162. {
  1163. rtp_sig_mutex_release(*m);
  1164. return 0;
  1165. }
  1166. int ebsnet_fseek(int a, long b, int c)
  1167. {
  1168. int retval;
  1169. retval = vf_lseek(a, b, c);
  1170. if (retval > 0)
  1171. retval = 0;
  1172. else
  1173. retval = -1;
  1174. return(retval);
  1175. }
  1176. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  1177. int wc_InitMutex(wolfSSL_Mutex* m)
  1178. {
  1179. if (_mutex_init(m, NULL) == MQX_EOK)
  1180. return 0;
  1181. else
  1182. return BAD_MUTEX_E;
  1183. }
  1184. int wc_FreeMutex(wolfSSL_Mutex* m)
  1185. {
  1186. if (_mutex_destroy(m) == MQX_EOK)
  1187. return 0;
  1188. else
  1189. return BAD_MUTEX_E;
  1190. }
  1191. int wc_LockMutex(wolfSSL_Mutex* m)
  1192. {
  1193. if (_mutex_lock(m) == MQX_EOK)
  1194. return 0;
  1195. else
  1196. return BAD_MUTEX_E;
  1197. }
  1198. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1199. {
  1200. if (_mutex_unlock(m) == MQX_EOK)
  1201. return 0;
  1202. else
  1203. return BAD_MUTEX_E;
  1204. }
  1205. #elif defined(WOLFSSL_TIRTOS)
  1206. #include <xdc/runtime/Error.h>
  1207. int wc_InitMutex(wolfSSL_Mutex* m)
  1208. {
  1209. Semaphore_Params params;
  1210. Error_Block eb;
  1211. Error_init(&eb);
  1212. Semaphore_Params_init(&params);
  1213. params.mode = Semaphore_Mode_BINARY;
  1214. *m = Semaphore_create(1, &params, &eb);
  1215. if (Error_check(&eb)) {
  1216. Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
  1217. NULL);
  1218. return BAD_MUTEX_E;
  1219. }
  1220. else
  1221. return 0;
  1222. }
  1223. int wc_FreeMutex(wolfSSL_Mutex* m)
  1224. {
  1225. Semaphore_delete(m);
  1226. return 0;
  1227. }
  1228. int wc_LockMutex(wolfSSL_Mutex* m)
  1229. {
  1230. Semaphore_pend(*m, BIOS_WAIT_FOREVER);
  1231. return 0;
  1232. }
  1233. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1234. {
  1235. Semaphore_post(*m);
  1236. return 0;
  1237. }
  1238. #elif defined(WOLFSSL_uITRON4)
  1239. int wc_InitMutex(wolfSSL_Mutex* m)
  1240. {
  1241. int iReturn;
  1242. m->sem.sematr = TA_TFIFO;
  1243. m->sem.isemcnt = 1;
  1244. m->sem.maxsem = 1;
  1245. m->sem.name = NULL;
  1246. m->id = acre_sem(&m->sem);
  1247. if( m->id != E_OK )
  1248. iReturn = 0;
  1249. else
  1250. iReturn = BAD_MUTEX_E;
  1251. return iReturn;
  1252. }
  1253. int wc_FreeMutex(wolfSSL_Mutex* m)
  1254. {
  1255. del_sem( m->id );
  1256. return 0;
  1257. }
  1258. int wc_LockMutex(wolfSSL_Mutex* m)
  1259. {
  1260. wai_sem(m->id);
  1261. return 0;
  1262. }
  1263. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1264. {
  1265. sig_sem(m->id);
  1266. return 0;
  1267. }
  1268. /**** uITRON malloc/free ***/
  1269. static ID ID_wolfssl_MPOOL = 0;
  1270. static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
  1271. int uITRON4_minit(size_t poolsz) {
  1272. ER ercd;
  1273. wolfssl_MPOOL.mplsz = poolsz;
  1274. ercd = acre_mpl(&wolfssl_MPOOL);
  1275. if (ercd > 0) {
  1276. ID_wolfssl_MPOOL = ercd;
  1277. return 0;
  1278. } else {
  1279. return -1;
  1280. }
  1281. }
  1282. void *uITRON4_malloc(size_t sz) {
  1283. ER ercd;
  1284. void *p = NULL;
  1285. ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
  1286. if (ercd == E_OK) {
  1287. return p;
  1288. } else {
  1289. return 0;
  1290. }
  1291. }
  1292. void *uITRON4_realloc(void *p, size_t sz) {
  1293. ER ercd;
  1294. void *newp;
  1295. if(p) {
  1296. ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
  1297. if (ercd == E_OK) {
  1298. XMEMCPY(newp, p, sz);
  1299. ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1300. if (ercd == E_OK) {
  1301. return newp;
  1302. }
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. void uITRON4_free(void *p) {
  1308. ER ercd;
  1309. ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1310. if (ercd == E_OK) {
  1311. return;
  1312. } else {
  1313. return;
  1314. }
  1315. }
  1316. #elif defined(WOLFSSL_uTKERNEL2)
  1317. int wc_InitMutex(wolfSSL_Mutex* m)
  1318. {
  1319. int iReturn;
  1320. m->sem.sematr = TA_TFIFO;
  1321. m->sem.isemcnt = 1;
  1322. m->sem.maxsem = 1;
  1323. m->id = tk_cre_sem(&m->sem);
  1324. if( m->id != NULL )
  1325. iReturn = 0;
  1326. else
  1327. iReturn = BAD_MUTEX_E;
  1328. return iReturn;
  1329. }
  1330. int wc_FreeMutex(wolfSSL_Mutex* m)
  1331. {
  1332. tk_del_sem(m->id);
  1333. return 0;
  1334. }
  1335. int wc_LockMutex(wolfSSL_Mutex* m)
  1336. {
  1337. tk_wai_sem(m->id, 1, TMO_FEVR);
  1338. return 0;
  1339. }
  1340. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1341. {
  1342. tk_sig_sem(m->id, 1);
  1343. return 0;
  1344. }
  1345. /**** uT-Kernel malloc/free ***/
  1346. static ID ID_wolfssl_MPOOL = 0;
  1347. static T_CMPL wolfssl_MPOOL = {
  1348. NULL, /* Extended information */
  1349. TA_TFIFO, /* Memory pool attribute */
  1350. 0, /* Size of whole memory pool (byte) */
  1351. "wolfSSL" /* Object name (max 8-char) */
  1352. };
  1353. int uTKernel_init_mpool(unsigned int sz) {
  1354. ER ercd;
  1355. wolfssl_MPOOL.mplsz = sz;
  1356. ercd = tk_cre_mpl(&wolfssl_MPOOL);
  1357. if (ercd > 0) {
  1358. ID_wolfssl_MPOOL = ercd;
  1359. return 0;
  1360. } else {
  1361. return (int)ercd;
  1362. }
  1363. }
  1364. void *uTKernel_malloc(unsigned int sz) {
  1365. ER ercd;
  1366. void *p = NULL;
  1367. ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
  1368. if (ercd == E_OK) {
  1369. return p;
  1370. } else {
  1371. return 0;
  1372. }
  1373. }
  1374. void *uTKernel_realloc(void *p, unsigned int sz) {
  1375. ER ercd;
  1376. void *newp;
  1377. if (p) {
  1378. ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
  1379. if (ercd == E_OK) {
  1380. XMEMCPY(newp, p, sz);
  1381. ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1382. if (ercd == E_OK) {
  1383. return newp;
  1384. }
  1385. }
  1386. }
  1387. return 0;
  1388. }
  1389. void uTKernel_free(void *p) {
  1390. ER ercd;
  1391. ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1392. if (ercd == E_OK) {
  1393. return;
  1394. } else {
  1395. return;
  1396. }
  1397. }
  1398. #elif defined (WOLFSSL_FROSTED)
  1399. int wc_InitMutex(wolfSSL_Mutex* m)
  1400. {
  1401. *m = mutex_init();
  1402. if (*m)
  1403. return 0;
  1404. else
  1405. return -1;
  1406. }
  1407. int wc_FreeMutex(wolfSSL_Mutex* m)
  1408. {
  1409. mutex_destroy(*m);
  1410. return(0);
  1411. }
  1412. int wc_LockMutex(wolfSSL_Mutex* m)
  1413. {
  1414. mutex_lock(*m);
  1415. return 0;
  1416. }
  1417. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1418. {
  1419. mutex_unlock(*m);
  1420. return 0;
  1421. }
  1422. #elif defined(WOLFSSL_CMSIS_RTOS)
  1423. #define CMSIS_NMUTEX 10
  1424. osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
  1425. osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
  1426. osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
  1427. osMutexDef(wolfSSL_mt9);
  1428. static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
  1429. osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
  1430. osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
  1431. osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
  1432. static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
  1433. int wc_InitMutex(wolfSSL_Mutex* m)
  1434. {
  1435. int i;
  1436. for (i=0; i<CMSIS_NMUTEX; i++) {
  1437. if(CMSIS_mutexID[i] == 0) {
  1438. CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
  1439. (*m) = CMSIS_mutexID[i];
  1440. return 0;
  1441. }
  1442. }
  1443. return -1;
  1444. }
  1445. int wc_FreeMutex(wolfSSL_Mutex* m)
  1446. {
  1447. int i;
  1448. osMutexDelete (*m);
  1449. for (i=0; i<CMSIS_NMUTEX; i++) {
  1450. if(CMSIS_mutexID[i] == (*m)) {
  1451. CMSIS_mutexID[i] = 0;
  1452. return(0);
  1453. }
  1454. }
  1455. return(-1);
  1456. }
  1457. int wc_LockMutex(wolfSSL_Mutex* m)
  1458. {
  1459. osMutexWait(*m, osWaitForever);
  1460. return(0);
  1461. }
  1462. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1463. {
  1464. osMutexRelease (*m);
  1465. return 0;
  1466. }
  1467. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  1468. int wc_InitMutex(wolfSSL_Mutex *m)
  1469. {
  1470. static const osMutexAttr_t attr = {
  1471. "wolfSSL_mutex", osMutexRecursive, NULL, 0};
  1472. if ((*m = osMutexNew(&attr)) != NULL)
  1473. return 0;
  1474. else
  1475. return BAD_MUTEX_E;
  1476. }
  1477. int wc_FreeMutex(wolfSSL_Mutex *m)
  1478. {
  1479. if (osMutexDelete(*m) == osOK)
  1480. return 0;
  1481. else
  1482. return BAD_MUTEX_E;
  1483. }
  1484. int wc_LockMutex(wolfSSL_Mutex *m)
  1485. {
  1486. if (osMutexAcquire(*m, osWaitForever) == osOK)
  1487. return 0;
  1488. else
  1489. return BAD_MUTEX_E;
  1490. }
  1491. int wc_UnLockMutex(wolfSSL_Mutex *m)
  1492. {
  1493. if (osMutexRelease(*m) == osOK)
  1494. return 0;
  1495. else
  1496. return BAD_MUTEX_E;
  1497. }
  1498. #elif defined(WOLFSSL_MDK_ARM)
  1499. int wc_InitMutex(wolfSSL_Mutex* m)
  1500. {
  1501. os_mut_init (m);
  1502. return 0;
  1503. }
  1504. int wc_FreeMutex(wolfSSL_Mutex* m)
  1505. {
  1506. return(0);
  1507. }
  1508. int wc_LockMutex(wolfSSL_Mutex* m)
  1509. {
  1510. os_mut_wait (m, 0xffff);
  1511. return(0);
  1512. }
  1513. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1514. {
  1515. os_mut_release (m);
  1516. return 0;
  1517. }
  1518. #elif defined(INTIME_RTOS)
  1519. int wc_InitMutex(wolfSSL_Mutex* m)
  1520. {
  1521. int ret = 0;
  1522. if (m == NULL)
  1523. return BAD_FUNC_ARG;
  1524. *m = CreateRtSemaphore(
  1525. 1, /* initial unit count */
  1526. 1, /* maximum unit count */
  1527. PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
  1528. );
  1529. if (*m == BAD_RTHANDLE) {
  1530. ret = GetLastRtError();
  1531. if (ret != E_OK)
  1532. ret = BAD_MUTEX_E;
  1533. }
  1534. return ret;
  1535. }
  1536. int wc_FreeMutex(wolfSSL_Mutex* m)
  1537. {
  1538. int ret = 0;
  1539. BOOLEAN del;
  1540. if (m == NULL)
  1541. return BAD_FUNC_ARG;
  1542. del = DeleteRtSemaphore(
  1543. *m /* handle for RT semaphore */
  1544. );
  1545. if (del != TRUE)
  1546. ret = BAD_MUTEX_E;
  1547. return ret;
  1548. }
  1549. int wc_LockMutex(wolfSSL_Mutex* m)
  1550. {
  1551. int ret = 0;
  1552. DWORD lck;
  1553. if (m == NULL)
  1554. return BAD_FUNC_ARG;
  1555. lck = WaitForRtSemaphore(
  1556. *m, /* handle for RT semaphore */
  1557. 1, /* number of units to wait for */
  1558. WAIT_FOREVER /* number of milliseconds to wait for units */
  1559. );
  1560. if (lck == WAIT_FAILED) {
  1561. ret = GetLastRtError();
  1562. if (ret != E_OK)
  1563. ret = BAD_MUTEX_E;
  1564. }
  1565. return ret;
  1566. }
  1567. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1568. {
  1569. int ret = 0;
  1570. BOOLEAN rel;
  1571. if (m == NULL)
  1572. return BAD_FUNC_ARG;
  1573. rel = ReleaseRtSemaphore(
  1574. *m, /* handle for RT semaphore */
  1575. 1 /* number of units to release to semaphore */
  1576. );
  1577. if (rel != TRUE)
  1578. ret = BAD_MUTEX_E;
  1579. return ret;
  1580. }
  1581. #elif defined(WOLFSSL_NUCLEUS_1_2)
  1582. int wc_InitMutex(wolfSSL_Mutex* m)
  1583. {
  1584. /* Call the Nucleus function to create the semaphore */
  1585. if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
  1586. NU_PRIORITY) == NU_SUCCESS) {
  1587. return 0;
  1588. }
  1589. return BAD_MUTEX_E;
  1590. }
  1591. int wc_FreeMutex(wolfSSL_Mutex* m)
  1592. {
  1593. if (NU_Delete_Semaphore(m) == NU_SUCCESS)
  1594. return 0;
  1595. return BAD_MUTEX_E;
  1596. }
  1597. int wc_LockMutex(wolfSSL_Mutex* m)
  1598. {
  1599. /* passing suspend task option */
  1600. if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
  1601. return 0;
  1602. return BAD_MUTEX_E;
  1603. }
  1604. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1605. {
  1606. if (NU_Release_Semaphore(m) == NU_SUCCESS)
  1607. return 0;
  1608. return BAD_MUTEX_E;
  1609. }
  1610. #elif defined(WOLFSSL_ZEPHYR)
  1611. int wc_InitMutex(wolfSSL_Mutex* m)
  1612. {
  1613. k_mutex_init(m);
  1614. return 0;
  1615. }
  1616. int wc_FreeMutex(wolfSSL_Mutex* m)
  1617. {
  1618. return 0;
  1619. }
  1620. int wc_LockMutex(wolfSSL_Mutex* m)
  1621. {
  1622. int ret = 0;
  1623. if (k_mutex_lock(m, K_FOREVER) != 0)
  1624. ret = BAD_MUTEX_E;
  1625. return ret;
  1626. }
  1627. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1628. {
  1629. k_mutex_unlock(m);
  1630. return 0;
  1631. }
  1632. #elif defined(WOLFSSL_TELIT_M2MB)
  1633. int wc_InitMutex(wolfSSL_Mutex* m)
  1634. {
  1635. M2MB_OS_RESULT_E osRes;
  1636. M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle;
  1637. UINT32 inheritVal = 1;
  1638. osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle,
  1639. CMDS_ARGS(
  1640. M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL,
  1641. M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx",
  1642. M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal
  1643. )
  1644. );
  1645. if (osRes != M2MB_OS_SUCCESS) {
  1646. return BAD_MUTEX_E;
  1647. }
  1648. osRes = m2mb_os_mtx_init(m, &mtxAttrHandle);
  1649. if (osRes != M2MB_OS_SUCCESS) {
  1650. return BAD_MUTEX_E;
  1651. }
  1652. return 0;
  1653. }
  1654. int wc_FreeMutex(wolfSSL_Mutex* m)
  1655. {
  1656. M2MB_OS_RESULT_E osRes;
  1657. if (m == NULL)
  1658. return BAD_MUTEX_E;
  1659. osRes = m2mb_os_mtx_deinit(*m);
  1660. if (osRes != M2MB_OS_SUCCESS) {
  1661. return BAD_MUTEX_E;
  1662. }
  1663. return 0;
  1664. }
  1665. int wc_LockMutex(wolfSSL_Mutex* m)
  1666. {
  1667. M2MB_OS_RESULT_E osRes;
  1668. if (m == NULL)
  1669. return BAD_MUTEX_E;
  1670. osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER);
  1671. if (osRes != M2MB_OS_SUCCESS) {
  1672. return BAD_MUTEX_E;
  1673. }
  1674. return 0;
  1675. }
  1676. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1677. {
  1678. M2MB_OS_RESULT_E osRes;
  1679. if (m == NULL)
  1680. return BAD_MUTEX_E;
  1681. osRes = m2mb_os_mtx_put(*m);
  1682. if (osRes != M2MB_OS_SUCCESS) {
  1683. return BAD_MUTEX_E;
  1684. }
  1685. return 0;
  1686. }
  1687. #elif defined(WOLFSSL_USER_MUTEX)
  1688. /* Use user own mutex */
  1689. /*
  1690. int wc_InitMutex(wolfSSL_Mutex* m) { ... }
  1691. int wc_FreeMutex(wolfSSL_Mutex *m) { ... }
  1692. int wc_LockMutex(wolfSSL_Mutex *m) { ... }
  1693. int wc_UnLockMutex(wolfSSL_Mutex *m) { ... }
  1694. */
  1695. #else
  1696. #warning No mutex handling defined
  1697. #endif
  1698. #ifndef NO_ASN_TIME
  1699. #if defined(_WIN32_WCE)
  1700. time_t windows_time(time_t* timer)
  1701. {
  1702. SYSTEMTIME sysTime;
  1703. FILETIME fTime;
  1704. ULARGE_INTEGER intTime;
  1705. time_t localTime;
  1706. if (timer == NULL)
  1707. timer = &localTime;
  1708. GetSystemTime(&sysTime);
  1709. SystemTimeToFileTime(&sysTime, &fTime);
  1710. XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
  1711. /* subtract EPOCH */
  1712. intTime.QuadPart -= 0x19db1ded53e8000;
  1713. /* to secs */
  1714. intTime.QuadPart /= 10000000;
  1715. *timer = (time_t)intTime.QuadPart;
  1716. return *timer;
  1717. }
  1718. #endif /* _WIN32_WCE */
  1719. #if defined(WOLFSSL_APACHE_MYNEWT)
  1720. #include "os/os_time.h"
  1721. time_t mynewt_time(time_t* timer)
  1722. {
  1723. time_t now;
  1724. struct os_timeval tv;
  1725. os_gettimeofday(&tv, NULL);
  1726. now = (time_t)tv.tv_sec;
  1727. if(timer != NULL) {
  1728. *timer = now;
  1729. }
  1730. return now;
  1731. }
  1732. #endif /* WOLFSSL_APACHE_MYNEWT */
  1733. #if defined(WOLFSSL_GMTIME)
  1734. struct tm* gmtime(const time_t* timer)
  1735. {
  1736. #define YEAR0 1900
  1737. #define EPOCH_YEAR 1970
  1738. #define SECS_DAY (24L * 60L * 60L)
  1739. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
  1740. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  1741. static const int _ytab[2][12] =
  1742. {
  1743. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  1744. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  1745. };
  1746. static struct tm st_time;
  1747. struct tm* ret = &st_time;
  1748. time_t secs = *timer;
  1749. unsigned long dayclock, dayno;
  1750. int year = EPOCH_YEAR;
  1751. dayclock = (unsigned long)secs % SECS_DAY;
  1752. dayno = (unsigned long)secs / SECS_DAY;
  1753. ret->tm_sec = (int) dayclock % 60;
  1754. ret->tm_min = (int)(dayclock % 3600) / 60;
  1755. ret->tm_hour = (int) dayclock / 3600;
  1756. ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
  1757. while(dayno >= (unsigned long)YEARSIZE(year)) {
  1758. dayno -= YEARSIZE(year);
  1759. year++;
  1760. }
  1761. ret->tm_year = year - YEAR0;
  1762. ret->tm_yday = (int)dayno;
  1763. ret->tm_mon = 0;
  1764. while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
  1765. dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
  1766. ret->tm_mon++;
  1767. }
  1768. ret->tm_mday = (int)++dayno;
  1769. #ifndef WOLFSSL_LINUXKM
  1770. ret->tm_isdst = 0;
  1771. #endif
  1772. return ret;
  1773. }
  1774. #endif /* WOLFSSL_GMTIME */
  1775. #if defined(HAVE_RTP_SYS)
  1776. #define YEAR0 1900
  1777. struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
  1778. {
  1779. static struct tm st_time;
  1780. struct tm* ret = &st_time;
  1781. DC_RTC_CALENDAR cal;
  1782. dc_rtc_time_get(&cal, TRUE);
  1783. ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
  1784. ret->tm_mon = cal.month - 1; /* gm starts at 0 */
  1785. ret->tm_mday = cal.day;
  1786. ret->tm_hour = cal.hour;
  1787. ret->tm_min = cal.minute;
  1788. ret->tm_sec = cal.second;
  1789. return ret;
  1790. }
  1791. #endif /* HAVE_RTP_SYS */
  1792. #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
  1793. /*
  1794. * time() is just a stub in Microchip libraries. We need our own
  1795. * implementation. Use SNTP client to get seconds since epoch.
  1796. */
  1797. time_t pic32_time(time_t* timer)
  1798. {
  1799. #ifdef MICROCHIP_TCPIP_V5
  1800. DWORD sec = 0;
  1801. #else
  1802. word32 sec = 0;
  1803. #endif
  1804. time_t localTime;
  1805. if (timer == NULL)
  1806. timer = &localTime;
  1807. #ifdef MICROCHIP_MPLAB_HARMONY
  1808. sec = TCPIP_SNTP_UTCSecondsGet();
  1809. #else
  1810. sec = SNTPGetUTCSeconds();
  1811. #endif
  1812. *timer = (time_t) sec;
  1813. return *timer;
  1814. }
  1815. #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
  1816. #if defined(WOLFSSL_DEOS)
  1817. time_t deos_time(time_t* timer)
  1818. {
  1819. const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
  1820. word32 *systemTickPtr = systemTickPointer();
  1821. if (timer != NULL)
  1822. *timer = *systemTickPtr/systemTickTimeInHz;
  1823. #if defined(CURRENT_UNIX_TIMESTAMP)
  1824. /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */
  1825. return (time_t) *systemTickPtr/systemTickTimeInHz + CURRENT_UNIX_TIMESTAMP;
  1826. #else
  1827. return (time_t) *systemTickPtr/systemTickTimeInHz;
  1828. #endif
  1829. }
  1830. #endif /* WOLFSSL_DEOS */
  1831. #if defined(MICRIUM)
  1832. time_t micrium_time(time_t* timer)
  1833. {
  1834. CLK_TS_SEC sec;
  1835. Clk_GetTS_Unix(&sec);
  1836. if (timer != NULL)
  1837. *timer = sec;
  1838. return (time_t) sec;
  1839. }
  1840. #endif /* MICRIUM */
  1841. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  1842. time_t mqx_time(time_t* timer)
  1843. {
  1844. time_t localTime;
  1845. TIME_STRUCT time_s;
  1846. if (timer == NULL)
  1847. timer = &localTime;
  1848. _time_get(&time_s);
  1849. *timer = (time_t) time_s.SECONDS;
  1850. return *timer;
  1851. }
  1852. #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
  1853. #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME)
  1854. time_t XTIME(time_t * timer)
  1855. {
  1856. time_t sec = 0;
  1857. sec = (time_t) Seconds_get();
  1858. if (timer != NULL)
  1859. *timer = sec;
  1860. return sec;
  1861. }
  1862. #endif /* WOLFSSL_TIRTOS */
  1863. #if defined(WOLFSSL_XILINX)
  1864. #include "xrtcpsu.h"
  1865. time_t xilinx_time(time_t * timer)
  1866. {
  1867. time_t sec = 0;
  1868. XRtcPsu_Config* con;
  1869. XRtcPsu rtc;
  1870. con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
  1871. if (con != NULL) {
  1872. if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
  1873. sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
  1874. }
  1875. else {
  1876. WOLFSSL_MSG("Unable to initialize RTC");
  1877. }
  1878. }
  1879. if (timer != NULL)
  1880. *timer = sec;
  1881. return sec;
  1882. }
  1883. #endif /* WOLFSSL_XILINX */
  1884. #if defined(WOLFSSL_ZEPHYR)
  1885. time_t z_time(time_t * timer)
  1886. {
  1887. struct timespec ts;
  1888. if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
  1889. if (timer != NULL)
  1890. *timer = ts.tv_sec;
  1891. return ts.tv_sec;
  1892. }
  1893. #endif /* WOLFSSL_ZEPHYR */
  1894. #if defined(WOLFSSL_WICED)
  1895. #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME
  1896. #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time.
  1897. #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */
  1898. time_t wiced_pseudo_unix_epoch_time(time_t * timer)
  1899. {
  1900. time_t epoch_time;
  1901. /* The time() function return uptime on WICED platform. */
  1902. epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME;
  1903. if (timer != NULL) {
  1904. *timer = epoch_time;
  1905. }
  1906. return epoch_time;
  1907. }
  1908. #endif /* WOLFSSL_WICED */
  1909. #ifdef WOLFSSL_TELIT_M2MB
  1910. time_t m2mb_xtime(time_t * timer)
  1911. {
  1912. time_t myTime = 0;
  1913. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  1914. if (fd != -1) {
  1915. M2MB_RTC_TIMEVAL_T timeval;
  1916. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  1917. myTime = timeval.sec;
  1918. m2mb_rtc_close(fd);
  1919. }
  1920. return myTime;
  1921. }
  1922. #ifdef WOLFSSL_TLS13
  1923. time_t m2mb_xtime_ms(time_t * timer)
  1924. {
  1925. time_t myTime = 0;
  1926. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  1927. if (fd != -1) {
  1928. M2MB_RTC_TIMEVAL_T timeval;
  1929. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  1930. myTime = timeval.sec + timeval.msec;
  1931. m2mb_rtc_close(fd);
  1932. }
  1933. return myTime;
  1934. }
  1935. #endif /* WOLFSSL_TLS13 */
  1936. #ifndef NO_CRYPT_BENCHMARK
  1937. double m2mb_xtime_bench(int reset)
  1938. {
  1939. double myTime = 0;
  1940. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  1941. if (fd != -1) {
  1942. M2MB_RTC_TIMEVAL_T timeval;
  1943. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  1944. myTime = (double)timeval.sec + ((double)timeval.msec / 1000);
  1945. m2mb_rtc_close(fd);
  1946. }
  1947. return myTime;
  1948. }
  1949. #endif /* !NO_CRYPT_BENCHMARK */
  1950. #endif /* WOLFSSL_TELIT_M2MB */
  1951. #if defined(WOLFSSL_LINUXKM)
  1952. time_t time(time_t * timer)
  1953. {
  1954. time_t ret;
  1955. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  1956. struct timespec ts;
  1957. getnstimeofday(&ts);
  1958. ret = ts.tv_sec * 1000000000LL + ts.tv_nsec;
  1959. #else
  1960. ret = ktime_get_real_seconds();
  1961. #endif
  1962. if (timer)
  1963. *timer = ret;
  1964. return ret;
  1965. }
  1966. #endif /* WOLFSSL_LINUXKM */
  1967. #endif /* !NO_ASN_TIME */
  1968. #ifndef WOLFSSL_LEANPSK
  1969. char* mystrnstr(const char* s1, const char* s2, unsigned int n)
  1970. {
  1971. unsigned int s2_len = (unsigned int)XSTRLEN(s2);
  1972. if (s2_len == 0)
  1973. return (char*)s1;
  1974. while (n >= s2_len && s1[0]) {
  1975. if (s1[0] == s2[0])
  1976. if (XMEMCMP(s1, s2, s2_len) == 0)
  1977. return (char*)s1;
  1978. s1++;
  1979. n--;
  1980. }
  1981. return NULL;
  1982. }
  1983. #endif
  1984. /* custom memory wrappers */
  1985. #ifdef WOLFSSL_NUCLEUS_1_2
  1986. /* system memory pool */
  1987. extern NU_MEMORY_POOL System_Memory;
  1988. void* nucleus_malloc(unsigned long size, void* heap, int type)
  1989. {
  1990. STATUS status;
  1991. void* stack_ptr;
  1992. status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
  1993. NU_NO_SUSPEND);
  1994. if (status == NU_SUCCESS) {
  1995. return 0;
  1996. } else {
  1997. return stack_ptr;
  1998. }
  1999. }
  2000. void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
  2001. {
  2002. DM_HEADER* old_header;
  2003. word32 old_size, copy_size;
  2004. void* new_mem;
  2005. /* if ptr is NULL, behave like malloc */
  2006. new_mem = nucleus_malloc(size, NULL, 0);
  2007. if (new_mem == 0 || ptr == 0) {
  2008. return new_mem;
  2009. }
  2010. /* calculate old memory block size */
  2011. /* mem pointers stored in block headers (ref dm_defs.h) */
  2012. old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
  2013. old_size = (byte*)old_header->dm_next_memory - (byte*)ptr;
  2014. /* copy old to new */
  2015. if (old_size < size) {
  2016. copy_size = old_size;
  2017. } else {
  2018. copy_size = size;
  2019. }
  2020. XMEMCPY(new_mem, ptr, copy_size);
  2021. /* free old */
  2022. nucleus_free(ptr, NULL, 0);
  2023. return new_mem;
  2024. }
  2025. void nucleus_free(void* ptr, void* heap, int type)
  2026. {
  2027. if (ptr != NULL)
  2028. NU_Deallocate_Memory(ptr);
  2029. }
  2030. #endif /* WOLFSSL_NUCLEUS_1_2 */
  2031. #ifdef WOLFSSL_LINUXKM
  2032. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  2033. /* adapted from kvrealloc() draft by Changli Gao, 2010-05-13 */
  2034. void *lkm_realloc(void *ptr, size_t newsize) {
  2035. void *nptr;
  2036. size_t oldsize;
  2037. if (unlikely(newsize == 0)) {
  2038. kvfree(ptr);
  2039. return ZERO_SIZE_PTR;
  2040. }
  2041. if (unlikely(ptr == NULL))
  2042. return kvmalloc(newsize, GFP_KERNEL);
  2043. if (is_vmalloc_addr(ptr)) {
  2044. /* no way to discern the size of the old allocation,
  2045. * because the kernel doesn't export find_vm_area(). if
  2046. * it did, we could then call get_vm_area_size() on the
  2047. * returned struct vm_struct.
  2048. */
  2049. return NULL;
  2050. } else {
  2051. struct page *page;
  2052. page = virt_to_head_page(ptr);
  2053. if (PageSlab(page) || PageCompound(page)) {
  2054. if (newsize < PAGE_SIZE)
  2055. return krealloc(ptr, newsize, GFP_KERNEL);
  2056. oldsize = ksize(ptr);
  2057. } else {
  2058. oldsize = page->private;
  2059. if (newsize <= oldsize)
  2060. return ptr;
  2061. }
  2062. }
  2063. nptr = kvmalloc(newsize, GFP_KERNEL);
  2064. if (nptr != NULL) {
  2065. memcpy(nptr, ptr, oldsize);
  2066. kvfree(ptr);
  2067. }
  2068. return nptr;
  2069. }
  2070. #endif /* >= 4.12 */
  2071. #endif /* WOLFSSL_LINUXKM */
  2072. #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
  2073. #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
  2074. #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
  2075. #endif
  2076. #if defined(WOLFSSL_CRYPTOCELL)
  2077. #define WOLFSSL_CRYPTOCELL_C
  2078. #include <wolfcrypt/src/port/arm/cryptoCell.c> /* CC310, RTC and RNG */
  2079. #if !defined(NO_SHA256)
  2080. #define WOLFSSL_CRYPTOCELL_HASH_C
  2081. #include <wolfcrypt/src/port/arm/cryptoCellHash.c> /* sha256 */
  2082. #endif
  2083. #endif