wc_port.c 62 KB

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