wc_port.c 60 KB

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