wc_port.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942
  1. /* port.c
  2. *
  3. * Copyright (C) 2006-2023 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. #ifdef MAXQ10XX_MODULE_INIT
  47. #include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h>
  48. #endif
  49. #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
  50. defined(WOLFSSL_ATECC608A)
  51. #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
  52. #endif
  53. #if defined(WOLFSSL_RENESAS_TSIP)
  54. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  55. #endif
  56. #if defined(WOLFSSL_RENESAS_FSPSM)
  57. #include <wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h>
  58. #endif
  59. #if defined(WOLFSSL_RENESAS_RX64_HASH)
  60. #include <wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h>
  61. #endif
  62. #if defined(WOLFSSL_STSAFEA100)
  63. #include <wolfssl/wolfcrypt/port/st/stsafe.h>
  64. #endif
  65. #if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) \
  66. && !defined(WOLFCRYPT_ONLY)
  67. #include <wolfssl/openssl/evp.h>
  68. #endif
  69. #include <wolfssl/wolfcrypt/memory.h>
  70. #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
  71. #include <wolfssl/wolfcrypt/mem_track.h>
  72. #endif
  73. #if defined(WOLFSSL_CAAM)
  74. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  75. #endif
  76. #if defined(HAVE_ARIA)
  77. #include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h>
  78. #endif
  79. #if defined(WOLFSSL_DEVCRYPTO)
  80. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  81. #endif
  82. #ifdef WOLFSSL_IMXRT_DCP
  83. #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
  84. #endif
  85. #ifdef WOLF_CRYPTO_CB
  86. #include <wolfssl/wolfcrypt/cryptocb.h>
  87. #endif
  88. #ifdef HAVE_INTEL_QA_SYNC
  89. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  90. #endif
  91. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  92. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  93. #endif
  94. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT)
  95. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  96. #endif
  97. #ifdef WOLFSSL_SCE
  98. #include "hal_data.h"
  99. #endif
  100. #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
  101. #include "rpcmem.h"
  102. #endif
  103. #ifdef _MSC_VER
  104. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  105. #pragma warning(disable: 4996)
  106. #endif
  107. #if defined(WOLFSSL_HAVE_PSA)
  108. #include <wolfssl/wolfcrypt/port/psa/psa.h>
  109. #endif
  110. /* prevent multiple mutex initializations */
  111. static volatile int initRefCount = 0;
  112. /* Used to initialize state for wolfcrypt
  113. return 0 on success
  114. */
  115. WOLFSSL_ABI
  116. int wolfCrypt_Init(void)
  117. {
  118. int ret = 0;
  119. if (initRefCount == 0) {
  120. WOLFSSL_ENTER("wolfCrypt_Init");
  121. #ifdef WOLFSSL_CHECK_MEM_ZERO
  122. /* Initialize the mutex for access to the list of memory locations that
  123. * must be freed. */
  124. wc_MemZero_Init();
  125. #endif
  126. #ifdef WOLFSSL_MEM_FAIL_COUNT
  127. wc_MemFailCount_Init();
  128. #endif
  129. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  130. {
  131. word32 rngMallocFail;
  132. time_t seed = time(NULL);
  133. srand((word32)seed);
  134. rngMallocFail = rand() % 2000; /* max 2000 */
  135. fprintf(stderr, "\n--- RNG MALLOC FAIL AT %u ---\n", rngMallocFail);
  136. wolfSSL_SetMemFailCount(rngMallocFail);
  137. }
  138. #endif
  139. #ifdef WOLF_CRYPTO_CB
  140. wc_CryptoCb_Init();
  141. #endif
  142. #ifdef WOLFSSL_ASYNC_CRYPT
  143. ret = wolfAsync_HardwareStart();
  144. if (ret != 0) {
  145. WOLFSSL_MSG("Async hardware start failed");
  146. /* don't return failure, allow operation to continue */
  147. }
  148. #endif
  149. #if defined(WOLFSSL_RENESAS_TSIP)
  150. ret = tsip_Open( );
  151. if( ret != TSIP_SUCCESS ) {
  152. WOLFSSL_MSG("RENESAS TSIP Open failed");
  153. /* not return 1 since WOLFSSL_SUCCESS=1*/
  154. ret = -1;/* FATAL ERROR */
  155. return ret;
  156. }
  157. #endif
  158. #if defined(WOLFSSL_RENESAS_RX64_HASH)
  159. ret = rx64_hw_Open();
  160. if( ret != 0 ) {
  161. WOLFSSL_MSG("Renesas RX64 HW Open failed");
  162. /* not return 1 since WOLFSSL_SUCCESS=1*/
  163. ret = -1;/* FATAL ERROR */
  164. return ret;
  165. }
  166. #endif
  167. #if defined(WOLFSSL_RENESAS_FSPSM)
  168. ret = wc_fspsm_Open( );
  169. if( ret != FSP_SUCCESS ) {
  170. WOLFSSL_MSG("RENESAS SCE Open failed");
  171. /* not return 1 since WOLFSSL_SUCCESS=1*/
  172. ret = -1;/* FATAL ERROR */
  173. return ret;
  174. }
  175. #endif
  176. #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
  177. ret = InitMemoryTracker();
  178. if (ret != 0) {
  179. WOLFSSL_MSG("InitMemoryTracker failed");
  180. return ret;
  181. }
  182. #endif
  183. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  184. ret = allocate_wolfcrypt_linuxkm_fpu_states();
  185. if (ret != 0) {
  186. WOLFSSL_MSG("allocate_wolfcrypt_linuxkm_fpu_states failed");
  187. return ret;
  188. }
  189. #endif
  190. #if WOLFSSL_CRYPT_HW_MUTEX
  191. /* If crypto hardware mutex protection is enabled, then initialize it */
  192. ret = wolfSSL_CryptHwMutexInit();
  193. if (ret != 0) {
  194. WOLFSSL_MSG("Hw crypt mutex init failed");
  195. return ret;
  196. }
  197. #endif
  198. /* if defined have fast RSA then initialize Intel IPP */
  199. #ifdef HAVE_FAST_RSA
  200. WOLFSSL_MSG("Attempting to use optimized IPP Library");
  201. if ((ret = ippInit()) != ippStsNoErr) {
  202. /* possible to get a CPU feature support status on optimized IPP
  203. library but still use default library and see competitive speeds */
  204. WOLFSSL_MSG("Warning when trying to set up optimization");
  205. WOLFSSL_MSG(ippGetStatusString(ret));
  206. WOLFSSL_MSG("Using default fast IPP library");
  207. ret = 0;
  208. (void)ret; /* suppress not read warning */
  209. }
  210. #endif
  211. #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
  212. ret = ksdk_port_init();
  213. if (ret != 0) {
  214. WOLFSSL_MSG("KSDK port init failed");
  215. return ret;
  216. }
  217. #endif
  218. #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
  219. defined(WOLFSSL_ATECC608A)
  220. ret = atmel_init();
  221. if (ret != 0) {
  222. WOLFSSL_MSG("CryptoAuthLib init failed");
  223. return ret;
  224. }
  225. #endif
  226. #if defined(WOLFSSL_CRYPTOCELL)
  227. /* enable and initialize the ARM CryptoCell 3xx runtime library */
  228. ret = cc310_Init();
  229. if (ret != 0) {
  230. WOLFSSL_MSG("CRYPTOCELL init failed");
  231. return ret;
  232. }
  233. #endif
  234. #if defined(WOLFSSL_STSAFEA100)
  235. stsafe_interface_init();
  236. #endif
  237. #if defined(WOLFSSL_PSOC6_CRYPTO)
  238. ret = psoc6_crypto_port_init();
  239. if (ret != 0) {
  240. WOLFSSL_MSG("PSoC6 crypto engine init failed");
  241. return ret;
  242. }
  243. #endif
  244. #ifdef MAXQ10XX_MODULE_INIT
  245. ret = maxq10xx_port_init();
  246. if (ret != 0) {
  247. WOLFSSL_MSG("MAXQ10xx port init failed");
  248. return ret;
  249. }
  250. #endif
  251. #ifdef WOLFSSL_SILABS_SE_ACCEL
  252. /* init handles if it is already initialized */
  253. ret = sl_se_init();
  254. #endif
  255. #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT)
  256. ret = wc_se050_init(NULL);
  257. if (ret != 0) {
  258. WOLFSSL_MSG("SE050 init failed");
  259. return ret;
  260. }
  261. #endif
  262. #ifdef WOLFSSL_ARMASM
  263. WOLFSSL_MSG("Using ARM hardware acceleration");
  264. #endif
  265. #ifdef WOLFSSL_AFALG
  266. WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
  267. #endif
  268. #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
  269. wolfSSL_EVP_init();
  270. #endif
  271. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  272. if ((ret = wc_LoggingInit()) != 0) {
  273. WOLFSSL_MSG("Error creating logging mutex");
  274. return ret;
  275. }
  276. #endif
  277. #if defined(WOLFSSL_HAVE_PSA)
  278. if ((ret = wc_psa_init()) != 0)
  279. return ret;
  280. #endif
  281. #ifdef HAVE_ENTROPY_MEMUSE
  282. ret = Entropy_Init();
  283. if (ret != 0) {
  284. WOLFSSL_MSG("Error initializing entropy");
  285. return ret;
  286. }
  287. #endif
  288. #ifdef HAVE_ECC
  289. #ifdef FP_ECC
  290. wc_ecc_fp_init();
  291. #endif
  292. #ifdef ECC_CACHE_CURVE
  293. if ((ret = wc_ecc_curve_cache_init()) != 0) {
  294. WOLFSSL_MSG("Error creating curve cache");
  295. return ret;
  296. }
  297. #endif
  298. #endif
  299. #ifdef WOLFSSL_SCE
  300. ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(
  301. WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg);
  302. if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) {
  303. WOLFSSL_MSG("SCE already open");
  304. ret = 0;
  305. }
  306. if (ret != SSP_SUCCESS) {
  307. WOLFSSL_MSG("Error opening SCE");
  308. return -1; /* FATAL_ERROR */
  309. }
  310. #endif
  311. #if defined(WOLFSSL_DEVCRYPTO)
  312. if ((ret = wc_DevCryptoInit()) != 0) {
  313. return ret;
  314. }
  315. #endif
  316. #if defined(WOLFSSL_CAAM)
  317. if ((ret = wc_caamInit()) != 0) {
  318. return ret;
  319. }
  320. #endif
  321. #if defined(HAVE_ARIA)
  322. if ((ret = wc_AriaInit()) != 0) {
  323. return ret;
  324. }
  325. #endif
  326. #ifdef WOLFSSL_IMXRT_DCP
  327. if ((ret = wc_dcp_init()) != 0) {
  328. return ret;
  329. }
  330. #endif
  331. #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
  332. if ((ret = wolfSSL_InitHandle()) != 0) {
  333. return ret;
  334. }
  335. rpcmem_init();
  336. #endif
  337. }
  338. initRefCount++;
  339. return ret;
  340. }
  341. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  342. long wolfCrypt_heap_peakAllocs_checkpoint(void) {
  343. long ret = ourMemStats.peakAllocsTripOdometer;
  344. ourMemStats.peakAllocsTripOdometer = ourMemStats.totalAllocs -
  345. ourMemStats.totalDeallocs;
  346. return ret;
  347. }
  348. long wolfCrypt_heap_peakBytes_checkpoint(void) {
  349. long ret = ourMemStats.peakBytesTripOdometer;
  350. ourMemStats.peakBytesTripOdometer = ourMemStats.currentBytes;
  351. return ret;
  352. }
  353. #endif
  354. /* return success value is the same as wolfCrypt_Init */
  355. WOLFSSL_ABI
  356. int wolfCrypt_Cleanup(void)
  357. {
  358. int ret = 0;
  359. initRefCount--;
  360. if (initRefCount < 0)
  361. initRefCount = 0;
  362. if (initRefCount == 0) {
  363. WOLFSSL_ENTER("wolfCrypt_Cleanup");
  364. #ifdef HAVE_ECC
  365. #ifdef FP_ECC
  366. wc_ecc_fp_free();
  367. #endif
  368. #ifdef ECC_CACHE_CURVE
  369. wc_ecc_curve_cache_free();
  370. #endif
  371. #endif /* HAVE_ECC */
  372. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  373. ret = wc_LoggingCleanup();
  374. #endif
  375. #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
  376. ShowMemoryTracker();
  377. #endif
  378. #ifdef WOLFSSL_ASYNC_CRYPT
  379. wolfAsync_HardwareStop();
  380. #endif
  381. #ifdef WOLFSSL_RENESAS_TSIP
  382. tsip_Close();
  383. #endif
  384. #if defined(WOLFSSL_RENESAS_RX64_HASH)
  385. rx64_hw_Close();
  386. #endif
  387. #if defined(WOLFSSL_RENESAS_FSPSM)
  388. wc_fspsm_Close();
  389. #endif
  390. #ifdef WOLFSSL_SCE
  391. WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl);
  392. #endif
  393. #if defined(WOLFSSL_CAAM)
  394. wc_caamFree();
  395. #endif
  396. #if defined(WOLFSSL_CRYPTOCELL)
  397. cc310_Free();
  398. #endif
  399. #ifdef WOLFSSL_SILABS_SE_ACCEL
  400. ret = sl_se_deinit();
  401. #endif
  402. #if defined(WOLFSSL_RENESAS_TSIP)
  403. tsip_Close();
  404. #endif
  405. #if defined(WOLFSSL_DEVCRYPTO)
  406. wc_DevCryptoCleanup();
  407. #endif
  408. #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
  409. rpcmem_deinit();
  410. wolfSSL_CleanupHandle();
  411. #endif
  412. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  413. free_wolfcrypt_linuxkm_fpu_states();
  414. #endif
  415. #ifdef HAVE_ENTROPY_MEMUSE
  416. Entropy_Final();
  417. #endif
  418. #ifdef WOLF_CRYPTO_CB
  419. wc_CryptoCb_Cleanup();
  420. #endif
  421. #if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
  422. wc_MemFailCount_Free();
  423. #endif
  424. #ifdef WOLFSSL_CHECK_MEM_ZERO
  425. /* Free the mutex for access to the list of memory locations that
  426. * must be freed. */
  427. wc_MemZero_Free();
  428. #endif
  429. }
  430. return ret;
  431. }
  432. #ifndef NO_FILESYSTEM
  433. /* Helpful function to load file into allocated buffer */
  434. int wc_FileLoad(const char* fname, unsigned char** buf, size_t* bufLen,
  435. void* heap)
  436. {
  437. int ret;
  438. ssize_t fileSz;
  439. XFILE f;
  440. if (fname == NULL || buf == NULL || bufLen == NULL) {
  441. return BAD_FUNC_ARG;
  442. }
  443. /* set defaults */
  444. *buf = NULL;
  445. *bufLen = 0;
  446. /* open file (read-only binary) */
  447. f = XFOPEN(fname, "rb");
  448. if (!f) {
  449. WOLFSSL_MSG("wc_LoadFile file load error");
  450. return BAD_PATH_ERROR;
  451. }
  452. if (XFSEEK(f, 0, XSEEK_END) != 0) {
  453. WOLFSSL_MSG("wc_LoadFile file seek error");
  454. XFCLOSE(f);
  455. return BAD_PATH_ERROR;
  456. }
  457. fileSz = XFTELL(f);
  458. if (fileSz < 0) {
  459. WOLFSSL_MSG("wc_LoadFile ftell error");
  460. XFCLOSE(f);
  461. return BAD_PATH_ERROR;
  462. }
  463. if (XFSEEK(f, 0, XSEEK_SET) != 0) {
  464. WOLFSSL_MSG("wc_LoadFile file seek error");
  465. XFCLOSE(f);
  466. return BAD_PATH_ERROR;
  467. }
  468. if (fileSz > 0) {
  469. *bufLen = (size_t)fileSz;
  470. *buf = (byte*)XMALLOC(*bufLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
  471. if (*buf == NULL) {
  472. WOLFSSL_MSG("wc_LoadFile memory error");
  473. ret = MEMORY_E;
  474. }
  475. else {
  476. size_t readLen = XFREAD(*buf, 1, *bufLen, f);
  477. /* check response code */
  478. ret = (readLen == *bufLen) ? 0 : -1;
  479. }
  480. }
  481. else {
  482. ret = BUFFER_E;
  483. }
  484. XFCLOSE(f);
  485. (void)heap;
  486. return ret;
  487. }
  488. #if !defined(NO_WOLFSSL_DIR) && \
  489. !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
  490. /* File Handling Helper */
  491. /* returns 0 if file exists, WC_ISFILEEXIST_NOFILE if file doesn't exist */
  492. int wc_FileExists(const char* fname)
  493. {
  494. struct ReadDirCtx ctx;
  495. XMEMSET(&ctx, 0, sizeof(ctx));
  496. if (fname == NULL)
  497. return 0;
  498. if (XSTAT(fname, &ctx.s) != 0) {
  499. WOLFSSL_MSG("stat on name failed");
  500. return BAD_PATH_ERROR;
  501. } else {
  502. #if defined(USE_WINDOWS_API)
  503. if (XS_ISREG(ctx.s.st_mode)) {
  504. return 0;
  505. }
  506. #elif defined(WOLFSSL_ZEPHYR)
  507. if (XS_ISREG(ctx.s.type)) {
  508. return 0;
  509. }
  510. #elif defined(WOLFSSL_TELIT_M2MB)
  511. if (XS_ISREG(ctx.s.st_mode)) {
  512. return 0;
  513. }
  514. #else
  515. if (XS_ISREG(ctx.s.st_mode)) {
  516. return 0;
  517. }
  518. #endif
  519. }
  520. return WC_ISFILEEXIST_NOFILE;
  521. }
  522. /* File Handling Helpers */
  523. /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
  524. int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
  525. {
  526. int ret = WC_READDIR_NOFILE; /* default to no files found */
  527. int pathLen = 0;
  528. if (name)
  529. *name = NULL;
  530. if (ctx == NULL || path == NULL) {
  531. return BAD_FUNC_ARG;
  532. }
  533. XMEMSET(ctx, 0, sizeof(ReadDirCtx));
  534. pathLen = (int)XSTRLEN(path);
  535. #ifdef USE_WINDOWS_API
  536. if (pathLen > MAX_FILENAME_SZ - 3)
  537. return BAD_PATH_ERROR;
  538. XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
  539. XSTRNCPY(ctx->name + pathLen, "\\*", (size_t)(MAX_FILENAME_SZ - pathLen));
  540. ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
  541. if (ctx->hFind == INVALID_HANDLE_VALUE) {
  542. WOLFSSL_MSG("FindFirstFile for path verify locations failed");
  543. return BAD_PATH_ERROR;
  544. }
  545. do {
  546. if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  547. int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
  548. if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
  549. return BAD_PATH_ERROR;
  550. }
  551. XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
  552. ctx->name[pathLen] = '\\';
  553. XSTRNCPY(ctx->name + pathLen + 1,
  554. ctx->FindFileData.cFileName,
  555. (size_t)(MAX_FILENAME_SZ - pathLen - 1));
  556. if (name)
  557. *name = ctx->name;
  558. return 0;
  559. }
  560. } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
  561. #elif defined(INTIME_RTOS)
  562. if (pathLen > MAX_FILENAME_SZ - 3)
  563. return BAD_PATH_ERROR;
  564. XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
  565. XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
  566. if (!IntimeFindFirst(ctx->name, &ctx->FindFileData)) {
  567. WOLFSSL_MSG("FindFirstFile for path verify locations failed");
  568. return BAD_PATH_ERROR;
  569. }
  570. do {
  571. int dnameLen = (int)XSTRLEN(IntimeFilename(ctx));
  572. if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
  573. return BAD_PATH_ERROR;
  574. }
  575. XSTRNCPY(ctx->name, path, pathLen + 1);
  576. ctx->name[pathLen] = '\\';
  577. XSTRNCPY(ctx->name + pathLen + 1,
  578. IntimeFilename(ctx),
  579. MAX_FILENAME_SZ - pathLen - 1);
  580. if (0 == wc_FileExists(ctx->name)) {
  581. if (name)
  582. *name = ctx->name;
  583. return 0;
  584. }
  585. } while (IntimeFindNext(&ctx->FindFileData));
  586. #elif defined(WOLFSSL_ZEPHYR)
  587. if (fs_opendir(&ctx->dir, path) != 0) {
  588. WOLFSSL_MSG("opendir path verify locations failed");
  589. return BAD_PATH_ERROR;
  590. }
  591. ctx->dirp = &ctx->dir;
  592. while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
  593. int dnameLen = (int)XSTRLEN(ctx->entry.name);
  594. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  595. ret = BAD_PATH_ERROR;
  596. break;
  597. }
  598. XSTRNCPY(ctx->name, path, pathLen + 1);
  599. ctx->name[pathLen] = '/';
  600. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  601. * of earlier check it is known that dnameLen is less than
  602. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  603. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
  604. if ((ret = wc_FileExists(ctx->name)) == 0) {
  605. if (name)
  606. *name = ctx->name;
  607. return 0;
  608. }
  609. }
  610. #elif defined(WOLFSSL_TELIT_M2MB)
  611. ctx->dir = m2mb_fs_opendir((const CHAR*)path);
  612. if (ctx->dir == NULL) {
  613. WOLFSSL_MSG("opendir path verify locations failed");
  614. return BAD_PATH_ERROR;
  615. }
  616. while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
  617. int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  618. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  619. ret = BAD_PATH_ERROR;
  620. break;
  621. }
  622. XSTRNCPY(ctx->name, path, pathLen + 1);
  623. ctx->name[pathLen] = '/';
  624. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  625. * of earlier check it is known that dnameLen is less than
  626. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  627. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
  628. if ((ret = wc_FileExists(ctx->name)) == 0) {
  629. if (name)
  630. *name = ctx->name;
  631. return 0;
  632. }
  633. }
  634. #else
  635. ctx->dir = opendir(path);
  636. if (ctx->dir == NULL) {
  637. WOLFSSL_MSG("opendir path verify locations failed");
  638. return BAD_PATH_ERROR;
  639. }
  640. while ((ctx->entry = readdir(ctx->dir)) != NULL) {
  641. int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  642. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  643. ret = BAD_PATH_ERROR;
  644. break;
  645. }
  646. XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
  647. ctx->name[pathLen] = '/';
  648. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  649. * of earlier check it is known that dnameLen is less than
  650. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  651. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1);
  652. if ((ret = wc_FileExists(ctx->name)) == 0) {
  653. if (name)
  654. *name = ctx->name;
  655. return 0;
  656. }
  657. }
  658. #endif
  659. wc_ReadDirClose(ctx);
  660. return ret;
  661. }
  662. /* returns 0 if file found, WC_READDIR_NOFILE if no more files */
  663. int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
  664. {
  665. int ret = WC_READDIR_NOFILE; /* default to no file found */
  666. int pathLen = 0;
  667. if (name)
  668. *name = NULL;
  669. if (ctx == NULL || path == NULL) {
  670. return BAD_FUNC_ARG;
  671. }
  672. XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
  673. pathLen = (int)XSTRLEN(path);
  674. #ifdef USE_WINDOWS_API
  675. while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
  676. if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
  677. int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
  678. if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
  679. return BAD_PATH_ERROR;
  680. }
  681. XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
  682. ctx->name[pathLen] = '\\';
  683. XSTRNCPY(ctx->name + pathLen + 1,
  684. ctx->FindFileData.cFileName,
  685. (size_t)(MAX_FILENAME_SZ - pathLen - 1));
  686. if (name)
  687. *name = ctx->name;
  688. return 0;
  689. }
  690. }
  691. #elif defined(INTIME_RTOS)
  692. while (IntimeFindNext(&ctx->FindFileData)) {
  693. int dnameLen = (int)XSTRLEN(IntimeFilename(ctx));
  694. if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
  695. return BAD_PATH_ERROR;
  696. }
  697. XSTRNCPY(ctx->name, path, pathLen + 1);
  698. ctx->name[pathLen] = '\\';
  699. XSTRNCPY(ctx->name + pathLen + 1,
  700. IntimeFilename(ctx),
  701. MAX_FILENAME_SZ - pathLen - 1);
  702. if (0 == wc_FileExists(ctx->name)) {
  703. if (name)
  704. *name = ctx->name;
  705. return 0;
  706. }
  707. }
  708. #elif defined(WOLFSSL_ZEPHYR)
  709. while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
  710. int dnameLen = (int)XSTRLEN(ctx->entry.name);
  711. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  712. ret = BAD_PATH_ERROR;
  713. break;
  714. }
  715. XSTRNCPY(ctx->name, path, pathLen + 1);
  716. ctx->name[pathLen] = '/';
  717. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  718. * of earlier check it is known that dnameLen is less than
  719. * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
  720. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
  721. if ((ret = wc_FileExists(ctx->name)) == 0) {
  722. if (name)
  723. *name = ctx->name;
  724. return 0;
  725. }
  726. }
  727. #elif defined(WOLFSSL_TELIT_M2MB)
  728. while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
  729. int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  730. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  731. ret = BAD_PATH_ERROR;
  732. break;
  733. }
  734. XSTRNCPY(ctx->name, path, pathLen + 1);
  735. ctx->name[pathLen] = '/';
  736. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  737. * of earlier check it is known that dnameLen is less than
  738. * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
  739. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
  740. if ((ret = wc_FileExists(ctx->name)) == 0) {
  741. if (name)
  742. *name = ctx->name;
  743. return 0;
  744. }
  745. }
  746. #else
  747. while ((ctx->entry = readdir(ctx->dir)) != NULL) {
  748. int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
  749. if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
  750. ret = BAD_PATH_ERROR;
  751. break;
  752. }
  753. XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
  754. ctx->name[pathLen] = '/';
  755. /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
  756. * of earlier check it is known that dnameLen is less than
  757. * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
  758. XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1);
  759. if ((ret = wc_FileExists(ctx->name)) == 0) {
  760. if (name)
  761. *name = ctx->name;
  762. return 0;
  763. }
  764. }
  765. #endif
  766. wc_ReadDirClose(ctx);
  767. return ret;
  768. }
  769. void wc_ReadDirClose(ReadDirCtx* ctx)
  770. {
  771. if (ctx == NULL) {
  772. return;
  773. }
  774. #ifdef USE_WINDOWS_API
  775. if (ctx->hFind != INVALID_HANDLE_VALUE) {
  776. FindClose(ctx->hFind);
  777. ctx->hFind = INVALID_HANDLE_VALUE;
  778. }
  779. #elif defined(INTIME_RTOS)
  780. IntimeFindClose(&ctx->FindFileData);
  781. #elif defined(WOLFSSL_ZEPHYR)
  782. if (ctx->dirp) {
  783. fs_closedir(ctx->dirp);
  784. ctx->dirp = NULL;
  785. }
  786. #elif defined(WOLFSSL_TELIT_M2MB)
  787. if (ctx->dir) {
  788. m2mb_fs_closedir(ctx->dir);
  789. ctx->dir = NULL;
  790. }
  791. #else
  792. if (ctx->dir) {
  793. if (closedir(ctx->dir) < 0)
  794. WOLFSSL_MSG("closedir() failed");
  795. ctx->dir = NULL;
  796. }
  797. #endif
  798. }
  799. #endif /* !NO_WOLFSSL_DIR */
  800. #endif /* !NO_FILESYSTEM */
  801. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_ZEPHYR)
  802. XFILE z_fs_open(const char* filename, const char* mode)
  803. {
  804. XFILE file;
  805. fs_mode_t flags = 0;
  806. if (mode == NULL)
  807. return NULL;
  808. /* Parse mode */
  809. switch (*mode++) {
  810. case 'r':
  811. flags |= FS_O_READ;
  812. break;
  813. case 'w':
  814. flags |= FS_O_WRITE|FS_O_CREATE;
  815. break;
  816. case 'a':
  817. flags |= FS_O_APPEND|FS_O_CREATE;
  818. break;
  819. default:
  820. return NULL;
  821. }
  822. /* Ignore binary flag */
  823. if (*mode == 'b')
  824. mode++;
  825. if (*mode == '+') {
  826. flags |= FS_O_READ;
  827. /* Don't add write flag if already appending */
  828. if (!(flags & FS_O_APPEND))
  829. flags |= FS_O_RDWR;
  830. }
  831. /* Ignore binary flag */
  832. if (*mode == 'b')
  833. mode++;
  834. /* Incorrect mode string */
  835. if (*mode != '\0')
  836. return NULL;
  837. file = (XFILE)XMALLOC(sizeof(*file), NULL, DYNAMIC_TYPE_FILE);
  838. if (file != NULL) {
  839. fs_file_t_init(file);
  840. if (fs_open(file, filename, flags) != 0) {
  841. XFREE(file, NULL, DYNAMIC_TYPE_FILE);
  842. file = NULL;
  843. }
  844. }
  845. return file;
  846. }
  847. int z_fs_close(XFILE file)
  848. {
  849. int ret;
  850. if (file == NULL)
  851. return -1;
  852. ret = (fs_close(file) == 0) ? 0 : -1;
  853. XFREE(file, NULL, DYNAMIC_TYPE_FILE);
  854. return ret;
  855. }
  856. #endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */
  857. #if !defined(WOLFSSL_USER_MUTEX)
  858. wolfSSL_Mutex* wc_InitAndAllocMutex(void)
  859. {
  860. wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
  861. DYNAMIC_TYPE_MUTEX);
  862. if (m != NULL) {
  863. if (wc_InitMutex(m) != 0) {
  864. WOLFSSL_MSG("Init Mutex failed");
  865. XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
  866. m = NULL;
  867. }
  868. }
  869. else {
  870. WOLFSSL_MSG("Memory error with Mutex allocation");
  871. }
  872. return m;
  873. }
  874. #endif
  875. #ifdef USE_WOLF_STRTOK
  876. /* String token (delim) search. If str is null use nextp. */
  877. char* wc_strtok(char *str, const char *delim, char **nextp)
  878. {
  879. char* ret;
  880. int i, j;
  881. /* Use next if str is NULL */
  882. if (str == NULL && nextp)
  883. str = *nextp;
  884. /* verify str input */
  885. if (str == NULL || *str == '\0')
  886. return NULL;
  887. /* match on entire delim */
  888. for (i = 0; str[i]; i++) {
  889. for (j = 0; delim[j]; j++) {
  890. if (delim[j] == str[i])
  891. break;
  892. }
  893. if (!delim[j])
  894. break;
  895. }
  896. str += i;
  897. /* if end of string, not found so return NULL */
  898. if (*str == '\0')
  899. return NULL;
  900. ret = str;
  901. /* match on first delim */
  902. for (i = 0; str[i]; i++) {
  903. for (j = 0; delim[j]; j++) {
  904. if (delim[j] == str[i])
  905. break;
  906. }
  907. if (delim[j] == str[i])
  908. break;
  909. }
  910. str += i;
  911. /* null terminate found string */
  912. if (*str)
  913. *str++ = '\0';
  914. /* return pointer to next */
  915. if (nextp)
  916. *nextp = str;
  917. return ret;
  918. }
  919. #endif /* USE_WOLF_STRTOK */
  920. #ifdef USE_WOLF_STRSEP
  921. char* wc_strsep(char **stringp, const char *delim)
  922. {
  923. char *s, *tok;
  924. const char *spanp;
  925. /* null check */
  926. if (stringp == NULL || *stringp == NULL)
  927. return NULL;
  928. s = *stringp;
  929. for (tok = s; *tok; ++tok) {
  930. for (spanp = delim; *spanp; ++spanp) {
  931. /* found delimiter */
  932. if (*tok == *spanp) {
  933. *tok = '\0'; /* replace delim with null term */
  934. *stringp = tok + 1; /* return past delim */
  935. return s;
  936. }
  937. }
  938. }
  939. *stringp = NULL;
  940. return s;
  941. }
  942. #endif /* USE_WOLF_STRSEP */
  943. #ifdef USE_WOLF_STRLCPY
  944. size_t wc_strlcpy(char *dst, const char *src, size_t dstSize)
  945. {
  946. size_t i;
  947. if (!dstSize)
  948. return 0;
  949. /* Always have to leave a space for NULL */
  950. for (i = 0; i < (dstSize - 1) && *src != '\0'; i++) {
  951. *dst++ = *src++;
  952. }
  953. *dst = '\0';
  954. return i; /* return length without NULL */
  955. }
  956. #endif /* USE_WOLF_STRLCPY */
  957. #ifdef USE_WOLF_STRLCAT
  958. size_t wc_strlcat(char *dst, const char *src, size_t dstSize)
  959. {
  960. size_t dstLen;
  961. if (!dstSize)
  962. return 0;
  963. dstLen = XSTRLEN(dst);
  964. if (dstSize < dstLen)
  965. return dstLen + XSTRLEN(src);
  966. return dstLen + wc_strlcpy(dst + dstLen, src, dstSize - dstLen);
  967. }
  968. #endif /* USE_WOLF_STRLCAT */
  969. #ifdef USE_WOLF_STRCASECMP
  970. int wc_strcasecmp(const char *s1, const char *s2)
  971. {
  972. char c1, c2;
  973. for (;;++s1, ++s2) {
  974. c1 = *s1;
  975. if ((c1 >= 'a') && (c1 <= 'z'))
  976. c1 -= ('a' - 'A');
  977. c2 = *s2;
  978. if ((c2 >= 'a') && (c2 <= 'z'))
  979. c2 -= ('a' - 'A');
  980. if ((c1 != c2) || (c1 == 0))
  981. break;
  982. }
  983. return (c1 - c2);
  984. }
  985. #endif /* USE_WOLF_STRCASECMP */
  986. #ifdef USE_WOLF_STRNCASECMP
  987. int wc_strncasecmp(const char *s1, const char *s2, size_t n)
  988. {
  989. char c1, c2;
  990. for (c1 = 0, c2 = 0; n > 0; --n, ++s1, ++s2) {
  991. c1 = *s1;
  992. if ((c1 >= 'a') && (c1 <= 'z'))
  993. c1 -= ('a' - 'A');
  994. c2 = *s2;
  995. if ((c2 >= 'a') && (c2 <= 'z'))
  996. c2 -= ('a' - 'A');
  997. if ((c1 != c2) || (c1 == 0))
  998. break;
  999. }
  1000. return (c1 - c2);
  1001. }
  1002. #endif /* USE_WOLF_STRNCASECMP */
  1003. #ifdef WOLFSSL_ATOMIC_OPS
  1004. #ifdef HAVE_C___ATOMIC
  1005. /* Atomic ops using standard C lib */
  1006. #ifdef __cplusplus
  1007. /* C++ using direct calls to compiler built-in functions */
  1008. void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
  1009. {
  1010. *c = i;
  1011. }
  1012. int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
  1013. {
  1014. return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
  1015. }
  1016. int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
  1017. {
  1018. return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
  1019. }
  1020. #else
  1021. /* Default C Implementation */
  1022. void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
  1023. {
  1024. atomic_init(c, i);
  1025. }
  1026. int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
  1027. {
  1028. return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
  1029. }
  1030. int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
  1031. {
  1032. return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
  1033. }
  1034. #endif /* __cplusplus */
  1035. #elif defined(_MSC_VER)
  1036. /* Default C Implementation */
  1037. void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
  1038. {
  1039. *c = i;
  1040. }
  1041. int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
  1042. {
  1043. return (int)_InterlockedExchangeAdd(c, (long)i);
  1044. }
  1045. int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
  1046. {
  1047. return (int)_InterlockedExchangeAdd(c, (long)-i);
  1048. }
  1049. #endif
  1050. #endif /* WOLFSSL_ATOMIC_OPS */
  1051. #if !defined(SINGLE_THREADED) && !defined(WOLFSSL_ATOMIC_OPS)
  1052. void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err)
  1053. {
  1054. int ret = wc_InitMutex(&ref->mutex);
  1055. if (ret != 0) {
  1056. WOLFSSL_MSG("Failed to create mutex for reference counting!");
  1057. }
  1058. ref->count = 1;
  1059. *err = ret;
  1060. }
  1061. void wolfSSL_RefFree(wolfSSL_Ref* ref)
  1062. {
  1063. if (wc_FreeMutex(&ref->mutex) != 0) {
  1064. WOLFSSL_MSG("Failed to free mutex of reference counting!");
  1065. }
  1066. }
  1067. void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err)
  1068. {
  1069. int ret = wc_LockMutex(&ref->mutex);
  1070. if (ret != 0) {
  1071. WOLFSSL_MSG("Failed to lock mutex for reference increment!");
  1072. }
  1073. else {
  1074. ref->count++;
  1075. wc_UnLockMutex(&ref->mutex);
  1076. }
  1077. *err = ret;
  1078. }
  1079. void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err)
  1080. {
  1081. int ret = wc_LockMutex(&ref->mutex);
  1082. if (ret != 0) {
  1083. WOLFSSL_MSG("Failed to lock mutex for reference decrement!");
  1084. /* Can't say count is zero. */
  1085. *isZero = 0;
  1086. }
  1087. else {
  1088. if (ref->count > 0) {
  1089. ref->count--;
  1090. }
  1091. *isZero = (ref->count == 0);
  1092. wc_UnLockMutex(&ref->mutex);
  1093. }
  1094. *err = ret;
  1095. }
  1096. #endif
  1097. #if WOLFSSL_CRYPT_HW_MUTEX
  1098. /* Mutex for protection of cryptography hardware */
  1099. static wolfSSL_Mutex wcCryptHwMutex;
  1100. static int wcCryptHwMutexInit = 0;
  1101. int wolfSSL_CryptHwMutexInit(void)
  1102. {
  1103. int ret = 0;
  1104. if (wcCryptHwMutexInit == 0) {
  1105. ret = wc_InitMutex(&wcCryptHwMutex);
  1106. if (ret == 0) {
  1107. wcCryptHwMutexInit = 1;
  1108. }
  1109. }
  1110. return ret;
  1111. }
  1112. int wolfSSL_CryptHwMutexLock(void)
  1113. {
  1114. int ret = BAD_MUTEX_E;
  1115. /* Make sure HW Mutex has been initialized */
  1116. ret = wolfSSL_CryptHwMutexInit();
  1117. if (ret == 0) {
  1118. ret = wc_LockMutex(&wcCryptHwMutex);
  1119. }
  1120. return ret;
  1121. }
  1122. int wolfSSL_CryptHwMutexUnLock(void)
  1123. {
  1124. int ret = BAD_MUTEX_E;
  1125. if (wcCryptHwMutexInit) {
  1126. ret = wc_UnLockMutex(&wcCryptHwMutex);
  1127. }
  1128. return ret;
  1129. }
  1130. #endif /* WOLFSSL_CRYPT_HW_MUTEX */
  1131. /* ---------------------------------------------------------------------------*/
  1132. /* Mutex Ports */
  1133. /* ---------------------------------------------------------------------------*/
  1134. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1135. static mutex_cb* compat_mutex_cb = NULL;
  1136. /* Function that locks or unlocks a mutex based on the flag passed in.
  1137. *
  1138. * flag lock or unlock i.e. CRYPTO_LOCK
  1139. * type the type of lock to unlock or lock
  1140. * file name of the file calling
  1141. * line the line number from file calling
  1142. */
  1143. int wc_LockMutex_ex(int flag, int type, const char* file, int line)
  1144. {
  1145. if (compat_mutex_cb != NULL) {
  1146. compat_mutex_cb(flag, type, file, line);
  1147. return 0;
  1148. }
  1149. else {
  1150. WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
  1151. return BAD_STATE_E;
  1152. }
  1153. }
  1154. /* Set the callback function to use for locking/unlocking mutex
  1155. *
  1156. * cb callback function to use
  1157. */
  1158. int wc_SetMutexCb(mutex_cb* cb)
  1159. {
  1160. compat_mutex_cb = cb;
  1161. return 0;
  1162. }
  1163. /* Gets the current callback function in use for locking/unlocking mutex
  1164. *
  1165. */
  1166. mutex_cb* wc_GetMutexCb(void)
  1167. {
  1168. return compat_mutex_cb;
  1169. }
  1170. #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
  1171. #ifdef SINGLE_THREADED
  1172. int wc_InitMutex(wolfSSL_Mutex* m)
  1173. {
  1174. (void)m;
  1175. return 0;
  1176. }
  1177. int wc_FreeMutex(wolfSSL_Mutex *m)
  1178. {
  1179. (void)m;
  1180. return 0;
  1181. }
  1182. int wc_LockMutex(wolfSSL_Mutex *m)
  1183. {
  1184. (void)m;
  1185. return 0;
  1186. }
  1187. int wc_UnLockMutex(wolfSSL_Mutex *m)
  1188. {
  1189. (void)m;
  1190. return 0;
  1191. }
  1192. #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
  1193. defined(FREESCALE_FREE_RTOS)
  1194. int wc_InitMutex(wolfSSL_Mutex* m)
  1195. {
  1196. int iReturn;
  1197. *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
  1198. if( *m != NULL )
  1199. iReturn = 0;
  1200. else
  1201. iReturn = BAD_MUTEX_E;
  1202. return iReturn;
  1203. }
  1204. int wc_FreeMutex(wolfSSL_Mutex* m)
  1205. {
  1206. vSemaphoreDelete( *m );
  1207. return 0;
  1208. }
  1209. int wc_LockMutex(wolfSSL_Mutex* m)
  1210. {
  1211. /* Assume an infinite block, or should there be zero block? */
  1212. xSemaphoreTake( *m, portMAX_DELAY );
  1213. return 0;
  1214. }
  1215. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1216. {
  1217. xSemaphoreGive( *m );
  1218. return 0;
  1219. }
  1220. #elif defined(RTTHREAD)
  1221. int wc_InitMutex(wolfSSL_Mutex* m)
  1222. {
  1223. int iReturn;
  1224. *m = ( wolfSSL_Mutex ) rt_mutex_create("mutex",RT_IPC_FLAG_FIFO);
  1225. if( *m != NULL )
  1226. iReturn = 0;
  1227. else
  1228. iReturn = BAD_MUTEX_E;
  1229. return iReturn;
  1230. }
  1231. int wc_FreeMutex(wolfSSL_Mutex* m)
  1232. {
  1233. rt_mutex_delete( *m );
  1234. return 0;
  1235. }
  1236. int wc_LockMutex(wolfSSL_Mutex* m)
  1237. {
  1238. /* Assume an infinite block, or should there be zero block? */
  1239. return rt_mutex_take( *m, RT_WAITING_FOREVER );
  1240. }
  1241. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1242. {
  1243. return rt_mutex_release( *m );
  1244. }
  1245. #elif defined(WOLFSSL_SAFERTOS)
  1246. int wc_InitMutex(wolfSSL_Mutex* m)
  1247. {
  1248. vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
  1249. if (m->mutex == NULL)
  1250. return BAD_MUTEX_E;
  1251. return 0;
  1252. }
  1253. int wc_FreeMutex(wolfSSL_Mutex* m)
  1254. {
  1255. (void)m;
  1256. return 0;
  1257. }
  1258. int wc_LockMutex(wolfSSL_Mutex* m)
  1259. {
  1260. /* Assume an infinite block */
  1261. xSemaphoreTake(m->mutex, portMAX_DELAY);
  1262. return 0;
  1263. }
  1264. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1265. {
  1266. xSemaphoreGive(m->mutex);
  1267. return 0;
  1268. }
  1269. #elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS)
  1270. int wc_InitMutex(wolfSSL_Mutex* m)
  1271. {
  1272. InitializeCriticalSection(m);
  1273. return 0;
  1274. }
  1275. int wc_FreeMutex(wolfSSL_Mutex* m)
  1276. {
  1277. DeleteCriticalSection(m);
  1278. return 0;
  1279. }
  1280. int wc_LockMutex(wolfSSL_Mutex* m)
  1281. {
  1282. EnterCriticalSection(m);
  1283. return 0;
  1284. }
  1285. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1286. {
  1287. LeaveCriticalSection(m);
  1288. return 0;
  1289. }
  1290. #elif defined(MAXQ10XX_MUTEX)
  1291. static pthread_mutex_t *wcCryptHwSharedMutexPtr;
  1292. static pthread_once_t key_once_own_hw_mutex = PTHREAD_ONCE_INIT;
  1293. static pthread_key_t key_own_hw_mutex;
  1294. static void destruct_key(void *buf)
  1295. {
  1296. if (buf != NULL) {
  1297. free(buf);
  1298. }
  1299. }
  1300. static void make_key_own_hw_mutex(void)
  1301. {
  1302. (void)pthread_key_create(&key_own_hw_mutex, destruct_key);
  1303. }
  1304. int wc_InitMutex(wolfSSL_Mutex* m)
  1305. {
  1306. int created = 0;
  1307. void *addr = NULL;
  1308. if (m != &wcCryptHwMutex) {
  1309. if (pthread_mutex_init(m, 0) == 0) {
  1310. return 0;
  1311. }
  1312. return BAD_MUTEX_E;
  1313. }
  1314. /* try to open mutex memory */
  1315. int shm_fd = shm_open("/maxq-mutex", O_RDWR, 0666);
  1316. if (shm_fd < 0) {
  1317. /* create mutex memory */
  1318. shm_fd = shm_open("/maxq-mutex", O_RDWR | O_CREAT | O_EXCL, 0666);
  1319. created = 1;
  1320. }
  1321. if (shm_fd < 0) {
  1322. WOLFSSL_MSG("wc_InitMutex: shm_open() failed");
  1323. return BAD_MUTEX_E;
  1324. }
  1325. if (ftruncate(shm_fd, sizeof(pthread_mutex_t))) {
  1326. WOLFSSL_MSG("wc_InitMutex: ftruncate() failed");
  1327. return BAD_MUTEX_E;
  1328. }
  1329. addr = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE,
  1330. MAP_SHARED, shm_fd, 0);
  1331. if (addr == MAP_FAILED) {
  1332. WOLFSSL_MSG("wc_InitMutex: mmap() failed");
  1333. return BAD_MUTEX_E;
  1334. }
  1335. wcCryptHwSharedMutexPtr = (pthread_mutex_t *)addr;
  1336. if (close(shm_fd)) {
  1337. WOLFSSL_MSG("wc_InitMutex: close() failed");
  1338. return BAD_MUTEX_E;
  1339. }
  1340. if (created) {
  1341. /* initialize mutex */
  1342. pthread_mutexattr_t attr;
  1343. if (pthread_mutexattr_init(&attr)) {
  1344. WOLFSSL_MSG("wc_InitMutex: pthread_mutexattr_init() failed");
  1345. return BAD_MUTEX_E;
  1346. }
  1347. if (pthread_mutexattr_setpshared(&attr,
  1348. PTHREAD_PROCESS_SHARED)) {
  1349. WOLFSSL_MSG(
  1350. "wc_InitMutex: pthread_mutexattr_setpshared() failed");
  1351. return BAD_MUTEX_E;
  1352. }
  1353. if (pthread_mutex_init(wcCryptHwSharedMutexPtr, &attr)) {
  1354. WOLFSSL_MSG("wc_InitMutex: pthread_mutex_init() failed");
  1355. return BAD_MUTEX_E;
  1356. }
  1357. }
  1358. if (pthread_once(&key_once_own_hw_mutex, make_key_own_hw_mutex)) {
  1359. WOLFSSL_MSG("wc_InitMutex: pthread_once() failed");
  1360. return BAD_MUTEX_E;
  1361. }
  1362. return 0;
  1363. }
  1364. int wc_FreeMutex(wolfSSL_Mutex* m)
  1365. {
  1366. void *key_ptr = NULL;
  1367. if (m != &wcCryptHwMutex) {
  1368. if (pthread_mutex_destroy(m) == 0) {
  1369. return 0;
  1370. }
  1371. return BAD_MUTEX_E;
  1372. }
  1373. if (wcCryptHwSharedMutexPtr) {
  1374. if (munmap((void *)wcCryptHwSharedMutexPtr,
  1375. sizeof(pthread_mutex_t))) {
  1376. WOLFSSL_MSG("wc_FreeMutex: munmap() failed");
  1377. return BAD_MUTEX_E;
  1378. }
  1379. wcCryptHwSharedMutexPtr = NULL;
  1380. }
  1381. key_ptr = pthread_getspecific(key_own_hw_mutex);
  1382. if (key_ptr) {
  1383. *((int *)key_ptr) = 0;
  1384. }
  1385. return 0;
  1386. }
  1387. static int maxq_LockMutex(wolfSSL_Mutex* m, int trylock)
  1388. {
  1389. void *key_ptr = NULL;
  1390. int ret = 0;
  1391. if (m != &wcCryptHwMutex) {
  1392. if (pthread_mutex_lock(m) == 0) {
  1393. return 0;
  1394. }
  1395. return BAD_MUTEX_E;
  1396. }
  1397. if (wcCryptHwSharedMutexPtr == NULL) {
  1398. return BAD_MUTEX_E;
  1399. }
  1400. key_ptr = pthread_getspecific(key_own_hw_mutex);
  1401. if (key_ptr == NULL) {
  1402. key_ptr = malloc(sizeof(int));
  1403. if (key_ptr == NULL) {
  1404. return MEMORY_E;
  1405. }
  1406. memset(key_ptr, 0, sizeof(int));
  1407. if (pthread_setspecific(key_own_hw_mutex, key_ptr)) {
  1408. return THREAD_STORE_SET_E;
  1409. }
  1410. }
  1411. else {
  1412. if ((trylock == 0) && (*((int *)key_ptr) > 0)) {
  1413. *((int *)key_ptr) = *((int *)key_ptr) + 1;
  1414. return 0;
  1415. }
  1416. }
  1417. if (trylock) {
  1418. ret = pthread_mutex_trylock(wcCryptHwSharedMutexPtr);
  1419. }
  1420. else {
  1421. ret = pthread_mutex_lock(wcCryptHwSharedMutexPtr);
  1422. }
  1423. if (ret != 0) {
  1424. return BAD_MUTEX_E;
  1425. }
  1426. *((int *)key_ptr) = 1;
  1427. return 0;
  1428. }
  1429. int wc_LockMutex(wolfSSL_Mutex* m)
  1430. {
  1431. return maxq_LockMutex(m, 0);
  1432. }
  1433. int maxq_CryptHwMutexTryLock()
  1434. {
  1435. int ret = BAD_MUTEX_E;
  1436. /* Make sure HW Mutex has been initialized */
  1437. ret = wolfSSL_CryptHwMutexInit();
  1438. if (ret == 0) {
  1439. ret = maxq_LockMutex(&wcCryptHwMutex, 1);
  1440. }
  1441. return ret;
  1442. }
  1443. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1444. {
  1445. void *key_ptr = NULL;
  1446. if (m != &wcCryptHwMutex) {
  1447. if (pthread_mutex_unlock(m) == 0) {
  1448. return 0;
  1449. }
  1450. return BAD_MUTEX_E;
  1451. }
  1452. if (wcCryptHwSharedMutexPtr == NULL) {
  1453. return BAD_MUTEX_E;
  1454. }
  1455. key_ptr = pthread_getspecific(key_own_hw_mutex);
  1456. if (key_ptr) {
  1457. if (*((int *)key_ptr) > 0) {
  1458. *((int *)key_ptr) = *((int *)key_ptr) - 1;
  1459. if (*((int *)key_ptr) > 0) {
  1460. return 0;
  1461. }
  1462. }
  1463. }
  1464. if (pthread_mutex_unlock(wcCryptHwSharedMutexPtr) != 0) {
  1465. return BAD_MUTEX_E;
  1466. }
  1467. return 0;
  1468. }
  1469. #elif defined(WOLFSSL_PTHREADS)
  1470. #ifdef WOLFSSL_USE_RWLOCK
  1471. int wc_InitRwLock(wolfSSL_RwLock* m)
  1472. {
  1473. if (pthread_rwlock_init(m, NULL) == 0)
  1474. return 0;
  1475. else
  1476. return BAD_MUTEX_E;
  1477. }
  1478. int wc_FreeRwLock(wolfSSL_RwLock* m)
  1479. {
  1480. if (pthread_rwlock_destroy(m) == 0)
  1481. return 0;
  1482. else
  1483. return BAD_MUTEX_E;
  1484. }
  1485. int wc_LockRwLock_Wr(wolfSSL_RwLock* m)
  1486. {
  1487. if (pthread_rwlock_wrlock(m) == 0)
  1488. return 0;
  1489. else
  1490. return BAD_MUTEX_E;
  1491. }
  1492. int wc_LockRwLock_Rd(wolfSSL_RwLock* m)
  1493. {
  1494. if (pthread_rwlock_rdlock(m) == 0)
  1495. return 0;
  1496. else
  1497. return BAD_MUTEX_E;
  1498. }
  1499. int wc_UnLockRwLock(wolfSSL_RwLock* m)
  1500. {
  1501. if (pthread_rwlock_unlock(m) == 0)
  1502. return 0;
  1503. else
  1504. return BAD_MUTEX_E;
  1505. }
  1506. #endif
  1507. int wc_InitMutex(wolfSSL_Mutex* m)
  1508. {
  1509. if (pthread_mutex_init(m, NULL) == 0)
  1510. return 0;
  1511. else
  1512. return BAD_MUTEX_E;
  1513. }
  1514. int wc_FreeMutex(wolfSSL_Mutex* m)
  1515. {
  1516. if (pthread_mutex_destroy(m) == 0)
  1517. return 0;
  1518. else
  1519. return BAD_MUTEX_E;
  1520. }
  1521. int wc_LockMutex(wolfSSL_Mutex* m)
  1522. {
  1523. if (pthread_mutex_lock(m) == 0)
  1524. return 0;
  1525. else
  1526. return BAD_MUTEX_E;
  1527. }
  1528. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1529. {
  1530. if (pthread_mutex_unlock(m) == 0)
  1531. return 0;
  1532. else
  1533. return BAD_MUTEX_E;
  1534. }
  1535. #elif defined(WOLFSSL_LINUXKM)
  1536. /* Linux kernel mutex routines are voids, alas. */
  1537. int wc_InitMutex(wolfSSL_Mutex* m)
  1538. {
  1539. mutex_init(m);
  1540. return 0;
  1541. }
  1542. int wc_FreeMutex(wolfSSL_Mutex* m)
  1543. {
  1544. mutex_destroy(m);
  1545. return 0;
  1546. }
  1547. int wc_LockMutex(wolfSSL_Mutex* m)
  1548. {
  1549. mutex_lock(m);
  1550. return 0;
  1551. }
  1552. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1553. {
  1554. mutex_unlock(m);
  1555. return 0;
  1556. }
  1557. #elif defined(WOLFSSL_VXWORKS)
  1558. int wc_InitMutex(wolfSSL_Mutex* m)
  1559. {
  1560. if (m) {
  1561. if ((*m = semMCreate(0)) != SEM_ID_NULL)
  1562. return 0;
  1563. }
  1564. return BAD_MUTEX_E;
  1565. }
  1566. int wc_FreeMutex(wolfSSL_Mutex* m)
  1567. {
  1568. if (m) {
  1569. if (semDelete(*m) == OK)
  1570. return 0;
  1571. }
  1572. return BAD_MUTEX_E;
  1573. }
  1574. int wc_LockMutex(wolfSSL_Mutex* m)
  1575. {
  1576. if (m) {
  1577. if (semTake(*m, WAIT_FOREVER) == OK)
  1578. return 0;
  1579. }
  1580. return BAD_MUTEX_E;
  1581. }
  1582. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1583. {
  1584. if (m) {
  1585. if (semGive(*m) == OK)
  1586. return 0;
  1587. }
  1588. return BAD_MUTEX_E;
  1589. }
  1590. #elif defined(THREADX)
  1591. int wc_InitMutex(wolfSSL_Mutex* m)
  1592. {
  1593. if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
  1594. return 0;
  1595. else
  1596. return BAD_MUTEX_E;
  1597. }
  1598. int wc_FreeMutex(wolfSSL_Mutex* m)
  1599. {
  1600. if (tx_mutex_delete(m) == 0)
  1601. return 0;
  1602. else
  1603. return BAD_MUTEX_E;
  1604. }
  1605. int wc_LockMutex(wolfSSL_Mutex* m)
  1606. {
  1607. if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
  1608. return 0;
  1609. else
  1610. return BAD_MUTEX_E;
  1611. }
  1612. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1613. {
  1614. if (tx_mutex_put(m) == 0)
  1615. return 0;
  1616. else
  1617. return BAD_MUTEX_E;
  1618. }
  1619. #elif defined(WOLFSSL_DEOS)
  1620. int wc_InitMutex(wolfSSL_Mutex* m)
  1621. {
  1622. mutexStatus mutStat;
  1623. /*
  1624. The empty string "" denotes an anonymous mutex, so objects do not cause name collisions.
  1625. `protectWolfSSLTemp` in an XML configuration element template describing a mutex.
  1626. */
  1627. if (m) {
  1628. mutStat = createMutex("", "protectWolfSSLTemp", m);
  1629. if (mutStat == mutexSuccess)
  1630. return 0;
  1631. else{
  1632. WOLFSSL_MSG("wc_InitMutex failed");
  1633. return mutStat;
  1634. }
  1635. }
  1636. return BAD_MUTEX_E;
  1637. }
  1638. int wc_FreeMutex(wolfSSL_Mutex* m)
  1639. {
  1640. mutexStatus mutStat;
  1641. if (m) {
  1642. mutStat = deleteMutex(*m);
  1643. if (mutStat == mutexSuccess)
  1644. return 0;
  1645. else{
  1646. WOLFSSL_MSG("wc_FreeMutex failed");
  1647. return mutStat;
  1648. }
  1649. }
  1650. return BAD_MUTEX_E;
  1651. }
  1652. int wc_LockMutex(wolfSSL_Mutex* m)
  1653. {
  1654. mutexStatus mutStat;
  1655. if (m) {
  1656. mutStat = lockMutex(*m);
  1657. if (mutStat == mutexSuccess)
  1658. return 0;
  1659. else{
  1660. WOLFSSL_MSG("wc_LockMutex failed");
  1661. return mutStat;
  1662. }
  1663. }
  1664. return BAD_MUTEX_E;
  1665. }
  1666. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1667. {
  1668. mutexStatus mutStat;
  1669. if (m) {
  1670. mutStat = unlockMutex(*m);
  1671. if (mutStat== mutexSuccess)
  1672. return 0;
  1673. else{
  1674. WOLFSSL_MSG("wc_UnLockMutex failed");
  1675. return mutStat;
  1676. }
  1677. }
  1678. return BAD_MUTEX_E;
  1679. }
  1680. #elif defined(MICRIUM)
  1681. #if (OS_VERSION < 50000)
  1682. #define MICRIUM_ERR_TYPE OS_ERR
  1683. #define MICRIUM_ERR_NONE OS_ERR_NONE
  1684. #define MICRIUM_ERR_CODE(err) err
  1685. #else
  1686. #define MICRIUM_ERR_TYPE RTOS_ERR
  1687. #define MICRIUM_ERR_NONE RTOS_ERR_NONE
  1688. #define MICRIUM_ERR_CODE(err) RTOS_ERR_CODE_GET(err)
  1689. #endif
  1690. int wc_InitMutex(wolfSSL_Mutex* m)
  1691. {
  1692. MICRIUM_ERR_TYPE err;
  1693. OSMutexCreate(m, "wolfSSL Mutex", &err);
  1694. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1695. return 0;
  1696. else
  1697. return BAD_MUTEX_E;
  1698. }
  1699. int wc_FreeMutex(wolfSSL_Mutex* m)
  1700. {
  1701. #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
  1702. MICRIUM_ERR_TYPE err;
  1703. OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
  1704. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1705. return 0;
  1706. else
  1707. return BAD_MUTEX_E;
  1708. #else
  1709. (void)m;
  1710. return 0;
  1711. #endif
  1712. }
  1713. int wc_LockMutex(wolfSSL_Mutex* m)
  1714. {
  1715. MICRIUM_ERR_TYPE err;
  1716. OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
  1717. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1718. return 0;
  1719. else
  1720. return BAD_MUTEX_E;
  1721. }
  1722. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1723. {
  1724. MICRIUM_ERR_TYPE err;
  1725. OSMutexPost(m, OS_OPT_POST_NONE, &err);
  1726. if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
  1727. return 0;
  1728. else
  1729. return BAD_MUTEX_E;
  1730. }
  1731. #elif defined(EBSNET)
  1732. #if (defined(RTPLATFORM) && (RTPLATFORM != 0))
  1733. int wc_InitMutex(wolfSSL_Mutex* m)
  1734. {
  1735. if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
  1736. return BAD_MUTEX_E;
  1737. else
  1738. return 0;
  1739. }
  1740. int wc_FreeMutex(wolfSSL_Mutex* m)
  1741. {
  1742. rtp_sig_mutex_free(*m);
  1743. return 0;
  1744. }
  1745. int wc_LockMutex(wolfSSL_Mutex* m)
  1746. {
  1747. if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
  1748. return 0;
  1749. else
  1750. return BAD_MUTEX_E;
  1751. }
  1752. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1753. {
  1754. rtp_sig_mutex_release(*m);
  1755. return 0;
  1756. }
  1757. int ebsnet_fseek(int a, long b, int c)
  1758. {
  1759. int retval;
  1760. retval = vf_lseek(a, b, c);
  1761. if (retval > 0)
  1762. retval = 0;
  1763. else
  1764. retval = -1;
  1765. return(retval);
  1766. }
  1767. #else
  1768. static int rtip_semaphore_build(wolfSSL_Mutex *m)
  1769. {
  1770. KS_SEMAPHORE_BUILD(m)
  1771. return(RTP_TRUE);
  1772. }
  1773. int wc_InitMutex(wolfSSL_Mutex* m)
  1774. {
  1775. if (rtip_semaphore_build(m) == RTP_FALSE)
  1776. return BAD_MUTEX_E;
  1777. else
  1778. return 0;
  1779. }
  1780. int wc_FreeMutex(wolfSSL_Mutex* m)
  1781. {
  1782. KS_SEMAPHORE_FREE(*m);
  1783. return 0;
  1784. }
  1785. int wc_LockMutex(wolfSSL_Mutex* m)
  1786. {
  1787. if (KS_SEMAPHORE_GET(*m))
  1788. return 0;
  1789. else
  1790. return BAD_MUTEX_E;
  1791. }
  1792. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1793. {
  1794. KS_SEMAPHORE_GIVE(*m);
  1795. return 0;
  1796. }
  1797. #endif
  1798. int ebsnet_fseek(int a, long b, int c)
  1799. {
  1800. int retval;
  1801. retval = (int)vf_lseek(a, b, c);
  1802. if (retval > 0)
  1803. retval = 0;
  1804. else
  1805. retval = -1;
  1806. return(retval);
  1807. }
  1808. int strcasecmp(const char *s1, const char *s2)
  1809. {
  1810. while (rtp_tolower(*s1) == rtp_tolower(*s2)) {
  1811. if (*s1 == '\0' || *s2 == '\0')
  1812. break;
  1813. s1++;
  1814. s2++;
  1815. }
  1816. return rtp_tolower(*(unsigned char *) s1) -
  1817. rtp_tolower(*(unsigned char *) s2);
  1818. }
  1819. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  1820. int wc_InitMutex(wolfSSL_Mutex* m)
  1821. {
  1822. if (_mutex_init(m, NULL) == MQX_EOK)
  1823. return 0;
  1824. else
  1825. return BAD_MUTEX_E;
  1826. }
  1827. int wc_FreeMutex(wolfSSL_Mutex* m)
  1828. {
  1829. if (_mutex_destroy(m) == MQX_EOK)
  1830. return 0;
  1831. else
  1832. return BAD_MUTEX_E;
  1833. }
  1834. int wc_LockMutex(wolfSSL_Mutex* m)
  1835. {
  1836. if (_mutex_lock(m) == MQX_EOK)
  1837. return 0;
  1838. else
  1839. return BAD_MUTEX_E;
  1840. }
  1841. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1842. {
  1843. if (_mutex_unlock(m) == MQX_EOK)
  1844. return 0;
  1845. else
  1846. return BAD_MUTEX_E;
  1847. }
  1848. #elif defined(WOLFSSL_TIRTOS)
  1849. #include <xdc/runtime/Error.h>
  1850. int wc_InitMutex(wolfSSL_Mutex* m)
  1851. {
  1852. Semaphore_Params params;
  1853. Error_Block eb;
  1854. Error_init(&eb);
  1855. Semaphore_Params_init(&params);
  1856. params.mode = Semaphore_Mode_BINARY;
  1857. *m = Semaphore_create(1, &params, &eb);
  1858. if (Error_check(&eb)) {
  1859. Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
  1860. NULL);
  1861. return BAD_MUTEX_E;
  1862. }
  1863. else
  1864. return 0;
  1865. }
  1866. int wc_FreeMutex(wolfSSL_Mutex* m)
  1867. {
  1868. Semaphore_delete(m);
  1869. return 0;
  1870. }
  1871. int wc_LockMutex(wolfSSL_Mutex* m)
  1872. {
  1873. Semaphore_pend(*m, BIOS_WAIT_FOREVER);
  1874. return 0;
  1875. }
  1876. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1877. {
  1878. Semaphore_post(*m);
  1879. return 0;
  1880. }
  1881. #elif defined(WOLFSSL_uITRON4)
  1882. int wc_InitMutex(wolfSSL_Mutex* m)
  1883. {
  1884. int iReturn;
  1885. m->sem.sematr = TA_TFIFO;
  1886. m->sem.isemcnt = 1;
  1887. m->sem.maxsem = 1;
  1888. m->sem.name = NULL;
  1889. m->id = acre_sem(&m->sem);
  1890. if( m->id != E_OK )
  1891. iReturn = 0;
  1892. else
  1893. iReturn = BAD_MUTEX_E;
  1894. return iReturn;
  1895. }
  1896. int wc_FreeMutex(wolfSSL_Mutex* m)
  1897. {
  1898. del_sem( m->id );
  1899. return 0;
  1900. }
  1901. int wc_LockMutex(wolfSSL_Mutex* m)
  1902. {
  1903. wai_sem(m->id);
  1904. return 0;
  1905. }
  1906. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1907. {
  1908. sig_sem(m->id);
  1909. return 0;
  1910. }
  1911. /**** uITRON malloc/free ***/
  1912. static ID ID_wolfssl_MPOOL = 0;
  1913. static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
  1914. int uITRON4_minit(size_t poolsz) {
  1915. ER ercd;
  1916. wolfssl_MPOOL.mplsz = poolsz;
  1917. ercd = acre_mpl(&wolfssl_MPOOL);
  1918. if (ercd > 0) {
  1919. ID_wolfssl_MPOOL = ercd;
  1920. return 0;
  1921. } else {
  1922. return -1;
  1923. }
  1924. }
  1925. void *uITRON4_malloc(size_t sz) {
  1926. ER ercd;
  1927. void *p = NULL;
  1928. ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
  1929. if (ercd == E_OK) {
  1930. return p;
  1931. } else {
  1932. return 0;
  1933. }
  1934. }
  1935. void *uITRON4_realloc(void *p, size_t sz) {
  1936. ER ercd;
  1937. void *newp = NULL;
  1938. if(p) {
  1939. ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
  1940. if ((ercd == E_OK) && (newp != NULL)) {
  1941. XMEMCPY(newp, p, sz);
  1942. ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1943. if (ercd == E_OK) {
  1944. return newp;
  1945. }
  1946. }
  1947. }
  1948. return 0;
  1949. }
  1950. void uITRON4_free(void *p) {
  1951. ER ercd;
  1952. ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1953. if (ercd == E_OK) {
  1954. return;
  1955. } else {
  1956. return;
  1957. }
  1958. }
  1959. #elif defined(WOLFSSL_uTKERNEL2)
  1960. int wc_InitMutex(wolfSSL_Mutex* m)
  1961. {
  1962. int iReturn;
  1963. m->sem.sematr = TA_TFIFO;
  1964. m->sem.isemcnt = 1;
  1965. m->sem.maxsem = 1;
  1966. m->id = tk_cre_sem(&m->sem);
  1967. if( m->id != NULL )
  1968. iReturn = 0;
  1969. else
  1970. iReturn = BAD_MUTEX_E;
  1971. return iReturn;
  1972. }
  1973. int wc_FreeMutex(wolfSSL_Mutex* m)
  1974. {
  1975. tk_del_sem(m->id);
  1976. return 0;
  1977. }
  1978. int wc_LockMutex(wolfSSL_Mutex* m)
  1979. {
  1980. tk_wai_sem(m->id, 1, TMO_FEVR);
  1981. return 0;
  1982. }
  1983. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1984. {
  1985. tk_sig_sem(m->id, 1);
  1986. return 0;
  1987. }
  1988. /**** uT-Kernel malloc/free ***/
  1989. static ID ID_wolfssl_MPOOL = 0;
  1990. static T_CMPL wolfssl_MPOOL = {
  1991. NULL, /* Extended information */
  1992. TA_TFIFO, /* Memory pool attribute */
  1993. 0, /* Size of whole memory pool (byte) */
  1994. "wolfSSL" /* Object name (max 8-char) */
  1995. };
  1996. int uTKernel_init_mpool(unsigned int sz) {
  1997. ER ercd;
  1998. wolfssl_MPOOL.mplsz = sz;
  1999. ercd = tk_cre_mpl(&wolfssl_MPOOL);
  2000. if (ercd > 0) {
  2001. ID_wolfssl_MPOOL = ercd;
  2002. return 0;
  2003. } else {
  2004. return (int)ercd;
  2005. }
  2006. }
  2007. void *uTKernel_malloc(unsigned int sz) {
  2008. ER ercd;
  2009. void *p = NULL;
  2010. ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
  2011. if (ercd == E_OK) {
  2012. return p;
  2013. } else {
  2014. return 0;
  2015. }
  2016. }
  2017. void *uTKernel_realloc(void *p, unsigned int sz) {
  2018. ER ercd;
  2019. void *newp = NULL;
  2020. if (p) {
  2021. ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
  2022. if ((ercd == E_OK) && (newp != NULL)) {
  2023. XMEMCPY(newp, p, sz);
  2024. ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  2025. if (ercd == E_OK) {
  2026. return newp;
  2027. }
  2028. }
  2029. }
  2030. return 0;
  2031. }
  2032. void uTKernel_free(void *p) {
  2033. ER ercd;
  2034. ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  2035. if (ercd == E_OK) {
  2036. return;
  2037. } else {
  2038. return;
  2039. }
  2040. }
  2041. #elif defined (WOLFSSL_FROSTED)
  2042. int wc_InitMutex(wolfSSL_Mutex* m)
  2043. {
  2044. *m = mutex_init();
  2045. if (*m)
  2046. return 0;
  2047. else
  2048. return -1;
  2049. }
  2050. int wc_FreeMutex(wolfSSL_Mutex* m)
  2051. {
  2052. mutex_destroy(*m);
  2053. return(0);
  2054. }
  2055. int wc_LockMutex(wolfSSL_Mutex* m)
  2056. {
  2057. mutex_lock(*m);
  2058. return 0;
  2059. }
  2060. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2061. {
  2062. mutex_unlock(*m);
  2063. return 0;
  2064. }
  2065. #elif defined(WOLFSSL_CMSIS_RTOS)
  2066. #define CMSIS_NMUTEX 10
  2067. osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
  2068. osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
  2069. osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
  2070. osMutexDef(wolfSSL_mt9);
  2071. static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
  2072. osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
  2073. osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
  2074. osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
  2075. static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
  2076. int wc_InitMutex(wolfSSL_Mutex* m)
  2077. {
  2078. int i;
  2079. for (i=0; i<CMSIS_NMUTEX; i++) {
  2080. if(CMSIS_mutexID[i] == 0) {
  2081. CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
  2082. (*m) = CMSIS_mutexID[i];
  2083. return 0;
  2084. }
  2085. }
  2086. return -1;
  2087. }
  2088. int wc_FreeMutex(wolfSSL_Mutex* m)
  2089. {
  2090. int i;
  2091. osMutexDelete (*m);
  2092. for (i=0; i<CMSIS_NMUTEX; i++) {
  2093. if(CMSIS_mutexID[i] == (*m)) {
  2094. CMSIS_mutexID[i] = 0;
  2095. return(0);
  2096. }
  2097. }
  2098. return(-1);
  2099. }
  2100. int wc_LockMutex(wolfSSL_Mutex* m)
  2101. {
  2102. osMutexWait(*m, osWaitForever);
  2103. return(0);
  2104. }
  2105. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2106. {
  2107. osMutexRelease (*m);
  2108. return 0;
  2109. }
  2110. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  2111. int wc_InitMutex(wolfSSL_Mutex *m)
  2112. {
  2113. static const osMutexAttr_t attr = {
  2114. "wolfSSL_mutex", osMutexRecursive, NULL, 0};
  2115. if ((*m = osMutexNew(&attr)) != NULL)
  2116. return 0;
  2117. else
  2118. return BAD_MUTEX_E;
  2119. }
  2120. int wc_FreeMutex(wolfSSL_Mutex *m)
  2121. {
  2122. if (osMutexDelete(*m) == osOK)
  2123. return 0;
  2124. else
  2125. return BAD_MUTEX_E;
  2126. }
  2127. int wc_LockMutex(wolfSSL_Mutex *m)
  2128. {
  2129. if (osMutexAcquire(*m, osWaitForever) == osOK)
  2130. return 0;
  2131. else
  2132. return BAD_MUTEX_E;
  2133. }
  2134. int wc_UnLockMutex(wolfSSL_Mutex *m)
  2135. {
  2136. if (osMutexRelease(*m) == osOK)
  2137. return 0;
  2138. else
  2139. return BAD_MUTEX_E;
  2140. }
  2141. #elif defined(WOLFSSL_MDK_ARM)
  2142. int wc_InitMutex(wolfSSL_Mutex* m)
  2143. {
  2144. os_mut_init (m);
  2145. return 0;
  2146. }
  2147. int wc_FreeMutex(wolfSSL_Mutex* m)
  2148. {
  2149. return(0);
  2150. }
  2151. int wc_LockMutex(wolfSSL_Mutex* m)
  2152. {
  2153. os_mut_wait (m, 0xffff);
  2154. return(0);
  2155. }
  2156. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2157. {
  2158. os_mut_release (m);
  2159. return 0;
  2160. }
  2161. #elif defined(INTIME_RTOS)
  2162. int wc_InitMutex(wolfSSL_Mutex* m)
  2163. {
  2164. int ret = 0;
  2165. if (m == NULL)
  2166. return BAD_FUNC_ARG;
  2167. *m = CreateRtSemaphore(
  2168. 1, /* initial unit count */
  2169. 1, /* maximum unit count */
  2170. PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
  2171. );
  2172. if (*m == BAD_RTHANDLE) {
  2173. ret = GetLastRtError();
  2174. if (ret != E_OK)
  2175. ret = BAD_MUTEX_E;
  2176. }
  2177. return ret;
  2178. }
  2179. int wc_FreeMutex(wolfSSL_Mutex* m)
  2180. {
  2181. int ret = 0;
  2182. BOOLEAN del;
  2183. if (m == NULL)
  2184. return BAD_FUNC_ARG;
  2185. del = DeleteRtSemaphore(
  2186. *m /* handle for RT semaphore */
  2187. );
  2188. if (del != TRUE)
  2189. ret = BAD_MUTEX_E;
  2190. return ret;
  2191. }
  2192. int wc_LockMutex(wolfSSL_Mutex* m)
  2193. {
  2194. int ret = 0;
  2195. DWORD lck;
  2196. if (m == NULL)
  2197. return BAD_FUNC_ARG;
  2198. lck = WaitForRtSemaphore(
  2199. *m, /* handle for RT semaphore */
  2200. 1, /* number of units to wait for */
  2201. WAIT_FOREVER /* number of milliseconds to wait for units */
  2202. );
  2203. if (lck == WAIT_FAILED) {
  2204. ret = GetLastRtError();
  2205. if (ret != E_OK)
  2206. ret = BAD_MUTEX_E;
  2207. }
  2208. return ret;
  2209. }
  2210. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2211. {
  2212. int ret = 0;
  2213. BOOLEAN rel;
  2214. if (m == NULL)
  2215. return BAD_FUNC_ARG;
  2216. rel = ReleaseRtSemaphore(
  2217. *m, /* handle for RT semaphore */
  2218. 1 /* number of units to release to semaphore */
  2219. );
  2220. if (rel != TRUE)
  2221. ret = BAD_MUTEX_E;
  2222. return ret;
  2223. }
  2224. #elif defined(WOLFSSL_NUCLEUS_1_2)
  2225. int wc_InitMutex(wolfSSL_Mutex* m)
  2226. {
  2227. /* Call the Nucleus function to create the semaphore */
  2228. if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
  2229. NU_PRIORITY) == NU_SUCCESS) {
  2230. return 0;
  2231. }
  2232. return BAD_MUTEX_E;
  2233. }
  2234. int wc_FreeMutex(wolfSSL_Mutex* m)
  2235. {
  2236. if (NU_Delete_Semaphore(m) == NU_SUCCESS)
  2237. return 0;
  2238. return BAD_MUTEX_E;
  2239. }
  2240. int wc_LockMutex(wolfSSL_Mutex* m)
  2241. {
  2242. /* passing suspend task option */
  2243. if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
  2244. return 0;
  2245. return BAD_MUTEX_E;
  2246. }
  2247. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2248. {
  2249. if (NU_Release_Semaphore(m) == NU_SUCCESS)
  2250. return 0;
  2251. return BAD_MUTEX_E;
  2252. }
  2253. #elif defined(WOLFSSL_ZEPHYR)
  2254. int wc_InitMutex(wolfSSL_Mutex* m)
  2255. {
  2256. k_mutex_init(m);
  2257. return 0;
  2258. }
  2259. int wc_FreeMutex(wolfSSL_Mutex* m)
  2260. {
  2261. return 0;
  2262. }
  2263. int wc_LockMutex(wolfSSL_Mutex* m)
  2264. {
  2265. int ret = 0;
  2266. if (k_mutex_lock(m, K_FOREVER) != 0)
  2267. ret = BAD_MUTEX_E;
  2268. return ret;
  2269. }
  2270. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2271. {
  2272. k_mutex_unlock(m);
  2273. return 0;
  2274. }
  2275. #elif defined(WOLFSSL_TELIT_M2MB)
  2276. int wc_InitMutex(wolfSSL_Mutex* m)
  2277. {
  2278. M2MB_OS_RESULT_E osRes;
  2279. M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle;
  2280. UINT32 inheritVal = 1;
  2281. osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle,
  2282. CMDS_ARGS(
  2283. M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL,
  2284. M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx",
  2285. M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal
  2286. )
  2287. );
  2288. if (osRes != M2MB_OS_SUCCESS) {
  2289. return BAD_MUTEX_E;
  2290. }
  2291. osRes = m2mb_os_mtx_init(m, &mtxAttrHandle);
  2292. if (osRes != M2MB_OS_SUCCESS) {
  2293. return BAD_MUTEX_E;
  2294. }
  2295. return 0;
  2296. }
  2297. int wc_FreeMutex(wolfSSL_Mutex* m)
  2298. {
  2299. M2MB_OS_RESULT_E osRes;
  2300. if (m == NULL)
  2301. return BAD_MUTEX_E;
  2302. osRes = m2mb_os_mtx_deinit(*m);
  2303. if (osRes != M2MB_OS_SUCCESS) {
  2304. return BAD_MUTEX_E;
  2305. }
  2306. return 0;
  2307. }
  2308. int wc_LockMutex(wolfSSL_Mutex* m)
  2309. {
  2310. M2MB_OS_RESULT_E osRes;
  2311. if (m == NULL)
  2312. return BAD_MUTEX_E;
  2313. osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER);
  2314. if (osRes != M2MB_OS_SUCCESS) {
  2315. return BAD_MUTEX_E;
  2316. }
  2317. return 0;
  2318. }
  2319. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2320. {
  2321. M2MB_OS_RESULT_E osRes;
  2322. if (m == NULL)
  2323. return BAD_MUTEX_E;
  2324. osRes = m2mb_os_mtx_put(*m);
  2325. if (osRes != M2MB_OS_SUCCESS) {
  2326. return BAD_MUTEX_E;
  2327. }
  2328. return 0;
  2329. }
  2330. #elif defined(WOLFSSL_EMBOS)
  2331. int wc_InitMutex(wolfSSL_Mutex* m)
  2332. {
  2333. int ret;
  2334. OS_MUTEX_Create((OS_MUTEX*) m);
  2335. if (m != NULL)
  2336. ret = 0;
  2337. else
  2338. ret = BAD_MUTEX_E;
  2339. return ret;
  2340. }
  2341. int wc_FreeMutex(wolfSSL_Mutex* m)
  2342. {
  2343. OS_MUTEX_Delete((OS_MUTEX*) m);
  2344. return 0;
  2345. }
  2346. int wc_LockMutex(wolfSSL_Mutex* m)
  2347. {
  2348. OS_MUTEX_LockBlocked((OS_MUTEX*) m);
  2349. return 0;
  2350. }
  2351. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2352. {
  2353. OS_MUTEX_Unlock((OS_MUTEX*) m);
  2354. return 0;
  2355. }
  2356. #elif defined(NETOS)
  2357. int wc_InitMutex(wolfSSL_Mutex* m)
  2358. {
  2359. if (tx_mutex_create(&ready->mutex, "wolfSSL Lock", TX_INHERIT)
  2360. == TX_SUCCESS)
  2361. return 0;
  2362. else
  2363. return BAD_MUTEX_E;
  2364. }
  2365. int wc_FreeMutex(wolfSSL_Mutex* m)
  2366. {
  2367. if (tx_mutex_delete(&ready->mutex) == TX_SUCCESS)
  2368. return 0;
  2369. else
  2370. return BAD_MUTEX_E;
  2371. }
  2372. int wc_LockMutex(wolfSSL_Mutex* m)
  2373. {
  2374. }
  2375. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2376. {
  2377. }
  2378. #elif defined(WOLFSSL_USER_MUTEX)
  2379. /* Use user own mutex */
  2380. /*
  2381. int wc_InitMutex(wolfSSL_Mutex* m) { ... }
  2382. int wc_FreeMutex(wolfSSL_Mutex *m) { ... }
  2383. int wc_LockMutex(wolfSSL_Mutex *m) { ... }
  2384. int wc_UnLockMutex(wolfSSL_Mutex *m) { ... }
  2385. */
  2386. #else
  2387. #warning No mutex handling defined
  2388. #endif
  2389. #if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
  2390. int wc_InitRwLock(wolfSSL_RwLock* m)
  2391. {
  2392. return wc_InitMutex(m);
  2393. }
  2394. int wc_FreeRwLock(wolfSSL_RwLock* m)
  2395. {
  2396. return wc_FreeMutex(m);
  2397. }
  2398. int wc_LockRwLock_Wr(wolfSSL_RwLock* m)
  2399. {
  2400. return wc_LockMutex(m);
  2401. }
  2402. int wc_LockRwLock_Rd(wolfSSL_RwLock* m)
  2403. {
  2404. return wc_LockMutex(m);
  2405. }
  2406. int wc_UnLockRwLock(wolfSSL_RwLock* m)
  2407. {
  2408. return wc_UnLockMutex(m);
  2409. }
  2410. #endif
  2411. #ifndef NO_ASN_TIME
  2412. #if defined(_WIN32_WCE)
  2413. time_t windows_time(time_t* timer)
  2414. {
  2415. SYSTEMTIME sysTime;
  2416. FILETIME fTime;
  2417. ULARGE_INTEGER intTime;
  2418. GetSystemTime(&sysTime);
  2419. SystemTimeToFileTime(&sysTime, &fTime);
  2420. XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
  2421. /* subtract EPOCH */
  2422. intTime.QuadPart -= 0x19db1ded53e8000;
  2423. /* to secs */
  2424. intTime.QuadPart /= 10000000;
  2425. if (timer != NULL)
  2426. *timer = (time_t)intTime.QuadPart;
  2427. return (time_t)intTime.QuadPart;
  2428. }
  2429. #endif /* _WIN32_WCE */
  2430. #if defined(WOLFSSL_APACHE_MYNEWT)
  2431. #include "os/os_time.h"
  2432. time_t mynewt_time(time_t* timer)
  2433. {
  2434. time_t now;
  2435. struct os_timeval tv;
  2436. os_gettimeofday(&tv, NULL);
  2437. now = (time_t)tv.tv_sec;
  2438. if(timer != NULL) {
  2439. *timer = now;
  2440. }
  2441. return now;
  2442. }
  2443. #endif /* WOLFSSL_APACHE_MYNEWT */
  2444. #if defined(WOLFSSL_GMTIME)
  2445. struct tm* gmtime_r(const time_t* timer, struct tm *ret)
  2446. {
  2447. #define YEAR0 1900
  2448. #define EPOCH_YEAR 1970
  2449. #define SECS_DAY (24L * 60L * 60L)
  2450. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
  2451. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  2452. static const int _ytab[2][12] =
  2453. {
  2454. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  2455. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  2456. };
  2457. time_t secs = *timer;
  2458. unsigned long dayclock, dayno;
  2459. int year = EPOCH_YEAR;
  2460. dayclock = (unsigned long)secs % SECS_DAY;
  2461. dayno = (unsigned long)secs / SECS_DAY;
  2462. ret->tm_sec = (int) dayclock % 60;
  2463. ret->tm_min = (int)(dayclock % 3600) / 60;
  2464. ret->tm_hour = (int) dayclock / 3600;
  2465. ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
  2466. while(dayno >= (unsigned long)YEARSIZE(year)) {
  2467. dayno -= YEARSIZE(year);
  2468. year++;
  2469. }
  2470. ret->tm_year = year - YEAR0;
  2471. ret->tm_yday = (int)dayno;
  2472. ret->tm_mon = 0;
  2473. while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
  2474. dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
  2475. ret->tm_mon++;
  2476. }
  2477. ret->tm_mday = (int)++dayno;
  2478. #ifndef WOLFSSL_LINUXKM
  2479. ret->tm_isdst = 0;
  2480. #endif
  2481. return ret;
  2482. }
  2483. struct tm* gmtime(const time_t* timer) {
  2484. static struct tm st_time;
  2485. return gmtime_r(timer, &st_time);
  2486. }
  2487. #endif /* WOLFSSL_GMTIME */
  2488. #if defined(HAVE_RTP_SYS)
  2489. #define YEAR0 1900
  2490. struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
  2491. {
  2492. static struct tm st_time;
  2493. struct tm* ret = &st_time;
  2494. DC_RTC_CALENDAR cal;
  2495. dc_rtc_time_get(&cal, TRUE);
  2496. ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
  2497. ret->tm_mon = cal.month - 1; /* gm starts at 0 */
  2498. ret->tm_mday = cal.day;
  2499. ret->tm_hour = cal.hour;
  2500. ret->tm_min = cal.minute;
  2501. ret->tm_sec = cal.second;
  2502. return ret;
  2503. }
  2504. #endif /* HAVE_RTP_SYS */
  2505. #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
  2506. /*
  2507. * time() is just a stub in Microchip libraries. We need our own
  2508. * implementation. Use SNTP client to get seconds since epoch.
  2509. */
  2510. time_t pic32_time(time_t* timer)
  2511. {
  2512. #ifdef MICROCHIP_TCPIP_V5
  2513. DWORD sec = 0;
  2514. #else
  2515. word32 sec = 0;
  2516. #endif
  2517. #ifdef MICROCHIP_MPLAB_HARMONY
  2518. sec = TCPIP_SNTP_UTCSecondsGet();
  2519. #else
  2520. sec = SNTPGetUTCSeconds();
  2521. #endif
  2522. if (timer != NULL)
  2523. *timer = (time_t)sec;
  2524. return (time_t)sec;
  2525. }
  2526. #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
  2527. #if defined(WOLFSSL_DEOS) || defined(WOLFSSL_DEOS_RTEMS)
  2528. time_t deos_time(time_t* timer)
  2529. {
  2530. const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
  2531. const volatile word32 *systemTickPtr = systemTickPointer();
  2532. if (timer != NULL)
  2533. *timer = *systemTickPtr/systemTickTimeInHz;
  2534. #if defined(CURRENT_UNIX_TIMESTAMP)
  2535. /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */
  2536. return (time_t) (*systemTickPtr/systemTickTimeInHz) + CURRENT_UNIX_TIMESTAMP;
  2537. #else
  2538. return (time_t) *systemTickPtr/systemTickTimeInHz;
  2539. #endif
  2540. }
  2541. #endif /* WOLFSSL_DEOS || WOLFSSL_DEOS_RTEMS */
  2542. #if defined(FREESCALE_RTC)
  2543. #include "fsl_rtc.h"
  2544. time_t fsl_time(time_t* t)
  2545. {
  2546. *t = RTC_GetSecondsTimerCount(RTC);
  2547. return *t;
  2548. }
  2549. #endif
  2550. #if defined(FREESCALE_SNVS_RTC)
  2551. time_t fsl_time(time_t* t)
  2552. {
  2553. struct tm tm_time;
  2554. time_t ret;
  2555. snvs_hp_rtc_datetime_t rtcDate;
  2556. snvs_hp_rtc_config_t snvsRtcConfig;
  2557. SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
  2558. SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
  2559. SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
  2560. tm_time.tm_year = rtcDate.year;
  2561. tm_time.tm_mon = rtcDate.month;
  2562. tm_time.tm_mday = rtcDate.day;
  2563. tm_time.tm_hour = rtcDate.hour;
  2564. tm_time.tm_min = rtcDate.minute;
  2565. tm_time.tm_sec = rtcDate.second;
  2566. ret = mktime(&tm_time);
  2567. if (t != NULL)
  2568. *t = ret;
  2569. return ret;
  2570. }
  2571. #endif
  2572. #if defined(MICRIUM)
  2573. time_t micrium_time(time_t* timer)
  2574. {
  2575. CLK_TS_SEC sec;
  2576. Clk_GetTS_Unix(&sec);
  2577. if (timer != NULL)
  2578. *timer = sec;
  2579. return (time_t) sec;
  2580. }
  2581. #endif /* MICRIUM */
  2582. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  2583. time_t mqx_time(time_t* timer)
  2584. {
  2585. TIME_STRUCT time_s;
  2586. _time_get(&time_s);
  2587. if (timer != NULL)
  2588. *timer = (time_t)time_s.SECONDS;
  2589. return (time_t)time_s.SECONDS;
  2590. }
  2591. #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
  2592. #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME)
  2593. time_t XTIME(time_t * timer)
  2594. {
  2595. time_t sec = 0;
  2596. sec = (time_t) Seconds_get();
  2597. if (timer != NULL)
  2598. *timer = sec;
  2599. return sec;
  2600. }
  2601. #endif /* WOLFSSL_TIRTOS */
  2602. #if defined(WOLFSSL_XILINX)
  2603. #include "xrtcpsu.h"
  2604. time_t xilinx_time(time_t * timer)
  2605. {
  2606. time_t sec = 0;
  2607. XRtcPsu_Config* con;
  2608. XRtcPsu rtc;
  2609. con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
  2610. if (con != NULL) {
  2611. if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
  2612. sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
  2613. }
  2614. else {
  2615. WOLFSSL_MSG("Unable to initialize RTC");
  2616. }
  2617. }
  2618. if (timer != NULL)
  2619. *timer = sec;
  2620. return sec;
  2621. }
  2622. #endif /* WOLFSSL_XILINX */
  2623. #if defined(WOLFSSL_ZEPHYR)
  2624. time_t z_time(time_t * timer)
  2625. {
  2626. struct timespec ts;
  2627. #if defined(CONFIG_RTC) && \
  2628. (defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC))
  2629. /* Try to obtain the actual time from an RTC */
  2630. static const struct device *rtc = DEVICE_DT_GET(DT_NODELABEL(rtc));
  2631. if (device_is_ready(rtc)) {
  2632. struct rtc_time rtc_time;
  2633. struct tm *tm_time = rtc_time_to_tm(&rtc_time);
  2634. int ret = rtc_get_time(rtc, &rtc_time);
  2635. if (ret == 0) {
  2636. time_t epochTime = mktime(tm_time);
  2637. if (timer != NULL)
  2638. *timer = epochTime;
  2639. return epochTime;
  2640. }
  2641. }
  2642. #endif
  2643. /* Fallback to uptime since boot. This works for relative times, but
  2644. * not for ASN.1 date validation */
  2645. if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
  2646. if (timer != NULL)
  2647. *timer = ts.tv_sec;
  2648. return ts.tv_sec;
  2649. }
  2650. #endif /* WOLFSSL_ZEPHYR */
  2651. #if defined(WOLFSSL_WICED)
  2652. #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME
  2653. #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time.
  2654. #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */
  2655. time_t wiced_pseudo_unix_epoch_time(time_t * timer)
  2656. {
  2657. time_t epoch_time;
  2658. /* The time() function return uptime on WICED platform. */
  2659. epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME;
  2660. if (timer != NULL) {
  2661. *timer = epoch_time;
  2662. }
  2663. return epoch_time;
  2664. }
  2665. #endif /* WOLFSSL_WICED */
  2666. #ifdef WOLFSSL_TELIT_M2MB
  2667. time_t m2mb_xtime(time_t * timer)
  2668. {
  2669. time_t myTime = 0;
  2670. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2671. if (fd != -1) {
  2672. M2MB_RTC_TIMEVAL_T timeval;
  2673. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2674. myTime = timeval.sec;
  2675. m2mb_rtc_close(fd);
  2676. }
  2677. return myTime;
  2678. }
  2679. #ifdef WOLFSSL_TLS13
  2680. time_t m2mb_xtime_ms(time_t * timer)
  2681. {
  2682. time_t myTime = 0;
  2683. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2684. if (fd != -1) {
  2685. M2MB_RTC_TIMEVAL_T timeval;
  2686. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2687. myTime = timeval.sec + timeval.msec;
  2688. m2mb_rtc_close(fd);
  2689. }
  2690. return myTime;
  2691. }
  2692. #endif /* WOLFSSL_TLS13 */
  2693. #ifndef NO_CRYPT_BENCHMARK
  2694. double m2mb_xtime_bench(int reset)
  2695. {
  2696. double myTime = 0;
  2697. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2698. if (fd != -1) {
  2699. M2MB_RTC_TIMEVAL_T timeval;
  2700. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2701. myTime = (double)timeval.sec + ((double)timeval.msec / 1000);
  2702. m2mb_rtc_close(fd);
  2703. }
  2704. return myTime;
  2705. }
  2706. #endif /* !NO_CRYPT_BENCHMARK */
  2707. #endif /* WOLFSSL_TELIT_M2MB */
  2708. #if defined(WOLFSSL_LINUXKM)
  2709. time_t time(time_t * timer)
  2710. {
  2711. time_t ret;
  2712. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  2713. struct timespec ts;
  2714. getnstimeofday(&ts);
  2715. ret = ts.tv_sec;
  2716. #else
  2717. struct timespec64 ts;
  2718. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  2719. ts = current_kernel_time64();
  2720. #else
  2721. ktime_get_coarse_real_ts64(&ts);
  2722. #endif
  2723. ret = ts.tv_sec;
  2724. #endif
  2725. if (timer)
  2726. *timer = ret;
  2727. return ret;
  2728. }
  2729. #endif /* WOLFSSL_LINUXKM */
  2730. #ifdef HAL_RTC_MODULE_ENABLED
  2731. extern RTC_HandleTypeDef hrtc;
  2732. time_t stm32_hal_time(time_t *t1)
  2733. {
  2734. struct tm tm_time;
  2735. time_t ret;
  2736. RTC_TimeTypeDef time;
  2737. RTC_DateTypeDef date;
  2738. XMEMSET(&tm_time, 0, sizeof(struct tm));
  2739. /* order of GetTime followed by GetDate required here due to STM32 HW
  2740. * requirement */
  2741. HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN);
  2742. HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN);
  2743. /* RTC year is 0-99 and "struct tm" is 1900+, so assume after year 2000 */
  2744. tm_time.tm_year = date.Year + 100;
  2745. /* RTC month is 1-12 and "struct tm" is 0-12, so subtract 1 */
  2746. tm_time.tm_mon = date.Month - 1;
  2747. tm_time.tm_mday = date.Date;
  2748. tm_time.tm_hour = time.Hours;
  2749. tm_time.tm_min = time.Minutes;
  2750. tm_time.tm_sec = time.Seconds;
  2751. ret = mktime(&tm_time);
  2752. if (t1 != NULL)
  2753. *t1 = ret;
  2754. return ret;
  2755. }
  2756. #endif /* HAL_RTC_MODULE_ENABLED */
  2757. #endif /* !NO_ASN_TIME */
  2758. #if !defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)
  2759. char* mystrnstr(const char* s1, const char* s2, unsigned int n)
  2760. {
  2761. unsigned int s2_len = (unsigned int)XSTRLEN(s2);
  2762. if (s2_len == 0)
  2763. return (char*)s1;
  2764. while (n >= s2_len && s1[0]) {
  2765. if (s1[0] == s2[0])
  2766. if (XMEMCMP(s1, s2, s2_len) == 0)
  2767. return (char*)s1;
  2768. s1++;
  2769. n--;
  2770. }
  2771. return NULL;
  2772. }
  2773. #endif
  2774. /* custom memory wrappers */
  2775. #ifdef WOLFSSL_NUCLEUS_1_2
  2776. /* system memory pool */
  2777. extern NU_MEMORY_POOL System_Memory;
  2778. void* nucleus_malloc(unsigned long size, void* heap, int type)
  2779. {
  2780. STATUS status;
  2781. void* stack_ptr;
  2782. status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
  2783. NU_NO_SUSPEND);
  2784. if (status == NU_SUCCESS) {
  2785. return 0;
  2786. } else {
  2787. return stack_ptr;
  2788. }
  2789. }
  2790. void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
  2791. {
  2792. DM_HEADER* old_header;
  2793. word32 old_size, copy_size;
  2794. void* new_mem;
  2795. /* if ptr is NULL, behave like malloc */
  2796. new_mem = nucleus_malloc(size, NULL, 0);
  2797. if (new_mem == 0 || ptr == 0) {
  2798. return new_mem;
  2799. }
  2800. /* calculate old memory block size */
  2801. /* mem pointers stored in block headers (ref dm_defs.h) */
  2802. old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
  2803. old_size = (byte*)old_header->dm_next_memory - (byte*)ptr;
  2804. /* copy old to new */
  2805. if (old_size < size) {
  2806. copy_size = old_size;
  2807. } else {
  2808. copy_size = size;
  2809. }
  2810. XMEMCPY(new_mem, ptr, copy_size);
  2811. /* free old */
  2812. nucleus_free(ptr, NULL, 0);
  2813. return new_mem;
  2814. }
  2815. void nucleus_free(void* ptr, void* heap, int type)
  2816. {
  2817. if (ptr != NULL)
  2818. NU_Deallocate_Memory(ptr);
  2819. }
  2820. #endif /* WOLFSSL_NUCLEUS_1_2 */
  2821. #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
  2822. #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
  2823. #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
  2824. #endif
  2825. #if defined(WOLFSSL_CRYPTOCELL)
  2826. #define WOLFSSL_CRYPTOCELL_C
  2827. #include <wolfcrypt/src/port/arm/cryptoCell.c> /* CC310, RTC and RNG */
  2828. #if !defined(NO_SHA256)
  2829. #define WOLFSSL_CRYPTOCELL_HASH_C
  2830. #include <wolfcrypt/src/port/arm/cryptoCellHash.c> /* sha256 */
  2831. #endif
  2832. #endif
  2833. #ifndef SINGLE_THREADED
  2834. #if defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS)
  2835. int wolfSSL_NewThread(THREAD_TYPE* thread,
  2836. THREAD_CB cb, void* arg)
  2837. {
  2838. if (thread == NULL || cb == NULL)
  2839. return BAD_FUNC_ARG;
  2840. /* Use _beginthreadex instead of _beginthread because of:
  2841. * _beginthreadex is safer to use than _beginthread. If the thread
  2842. * that's generated by _beginthread exits quickly, the handle that's
  2843. * returned to the caller of _beginthread might be invalid or point
  2844. * to another thread. However, the handle that's returned by
  2845. * _beginthreadex has to be closed by the caller of _beginthreadex,
  2846. * so it's guaranteed to be a valid handle if _beginthreadex didn't
  2847. * return an error.*/
  2848. *thread = _beginthreadex(NULL, 0, cb, arg, 0, NULL);
  2849. if (*thread == 0) {
  2850. *thread = INVALID_THREAD_VAL;
  2851. return MEMORY_E;
  2852. }
  2853. return 0;
  2854. }
  2855. #ifdef WOLFSSL_THREAD_NO_JOIN
  2856. int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg)
  2857. {
  2858. THREAD_TYPE thread;
  2859. if (cb == NULL)
  2860. return BAD_FUNC_ARG;
  2861. thread = _beginthread(cb, 0, arg);
  2862. if (thread == -1L) {
  2863. return MEMORY_E;
  2864. }
  2865. return 0;
  2866. }
  2867. #endif
  2868. int wolfSSL_JoinThread(THREAD_TYPE thread)
  2869. {
  2870. int ret = 0;
  2871. if (thread == INVALID_THREAD_VAL)
  2872. return BAD_FUNC_ARG;
  2873. /* We still want to attempt to close the thread handle even on error */
  2874. if (WaitForSingleObject((HANDLE)thread, INFINITE) == WAIT_FAILED)
  2875. ret = MEMORY_E;
  2876. if (CloseHandle((HANDLE)thread) == 0)
  2877. ret = MEMORY_E;
  2878. return ret;
  2879. }
  2880. #ifdef WOLFSSL_COND
  2881. int wolfSSL_CondInit(COND_TYPE* cond)
  2882. {
  2883. if (cond == NULL)
  2884. return BAD_FUNC_ARG;
  2885. cond->cond = CreateEventA(NULL, FALSE, FALSE, NULL);
  2886. if (cond->cond == NULL)
  2887. return MEMORY_E;
  2888. if (wc_InitMutex(&cond->mutex) != 0) {
  2889. if (CloseHandle(cond->cond) == 0)
  2890. return MEMORY_E;
  2891. return MEMORY_E;
  2892. }
  2893. return 0;
  2894. }
  2895. int wolfSSL_CondFree(COND_TYPE* cond)
  2896. {
  2897. if (cond == NULL)
  2898. return BAD_FUNC_ARG;
  2899. if (CloseHandle(cond->cond) == 0)
  2900. return MEMORY_E;
  2901. return 0;
  2902. }
  2903. int wolfSSL_CondStart(COND_TYPE* cond)
  2904. {
  2905. if (cond == NULL)
  2906. return BAD_FUNC_ARG;
  2907. if (wc_LockMutex(&cond->mutex) != 0)
  2908. return BAD_MUTEX_E;
  2909. return 0;
  2910. }
  2911. int wolfSSL_CondSignal(COND_TYPE* cond)
  2912. {
  2913. if (cond == NULL)
  2914. return BAD_FUNC_ARG;
  2915. if (wc_UnLockMutex(&cond->mutex) != 0)
  2916. return BAD_MUTEX_E;
  2917. if (SetEvent(cond->cond) == 0)
  2918. return MEMORY_E;
  2919. if (wc_LockMutex(&cond->mutex) != 0)
  2920. return BAD_MUTEX_E;
  2921. return 0;
  2922. }
  2923. int wolfSSL_CondWait(COND_TYPE* cond)
  2924. {
  2925. if (cond == NULL)
  2926. return BAD_FUNC_ARG;
  2927. if (wc_UnLockMutex(&cond->mutex) != 0)
  2928. return BAD_MUTEX_E;
  2929. if (WaitForSingleObject(cond->cond, INFINITE) == WAIT_FAILED)
  2930. return MEMORY_E;
  2931. if (wc_LockMutex(&cond->mutex) != 0)
  2932. return BAD_MUTEX_E;
  2933. return 0;
  2934. }
  2935. int wolfSSL_CondEnd(COND_TYPE* cond)
  2936. {
  2937. if (cond == NULL)
  2938. return BAD_FUNC_ARG;
  2939. if (wc_UnLockMutex(&cond->mutex) != 0)
  2940. return BAD_MUTEX_E;
  2941. return 0;
  2942. }
  2943. #endif /* WOLFSSL_COND */
  2944. #elif defined(WOLFSSL_TIRTOS)
  2945. int wolfSSL_NewThread(THREAD_TYPE* thread,
  2946. THREAD_CB cb, void* arg)
  2947. {
  2948. /* Initialize the defaults and set the parameters. */
  2949. Task_Params taskParams;
  2950. Task_Params_init(&taskParams);
  2951. taskParams.arg0 = (UArg)arg;
  2952. taskParams.stackSize = 65535;
  2953. *thread = Task_create((Task_FuncPtr)cb, &taskParams, NULL);
  2954. if (*thread == NULL) {
  2955. return MEMORY_E;
  2956. }
  2957. Task_yield();
  2958. return 0;
  2959. }
  2960. int wolfSSL_JoinThread(THREAD_TYPE thread)
  2961. {
  2962. while(1) {
  2963. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  2964. Task_sleep(5);
  2965. break;
  2966. }
  2967. Task_yield();
  2968. }
  2969. return 0;
  2970. }
  2971. #elif defined(NETOS)
  2972. int wolfSSL_NewThread(THREAD_TYPE* thread,
  2973. THREAD_CB cb, void* arg)
  2974. {
  2975. /* For backwards compatibility allow using this declaration as well. */
  2976. #ifdef TESTSUITE_THREAD_STACK_SZ
  2977. #define WOLFSSL_NETOS_STACK_SZ TESTSUITE_THREAD_STACK_SZ
  2978. #endif
  2979. /* This can be adjusted by defining in user_settings.h, will default to
  2980. * 65k in the event it is undefined */
  2981. #ifndef WOLFSSL_NETOS_STACK_SZ
  2982. #define WOLFSSL_NETOS_STACK_SZ 65535
  2983. #endif
  2984. int result;
  2985. if (thread == NULL || cb == NULL)
  2986. return BAD_FUNC_ARG;
  2987. XMEMSET(thread, 0, sizeof(*thread));
  2988. thread->threadStack = (void *)XMALLOC(WOLFSSL_NETOS_STACK_SZ, NULL,
  2989. DYNAMIC_TYPE_TMP_BUFFER);
  2990. if (thread->threadStack == NULL)
  2991. return MEMORY_E;
  2992. /* first create the idle thread:
  2993. * ARGS:
  2994. * Param1: pointer to thread
  2995. * Param2: name
  2996. * Param3 and 4: entry function and input
  2997. * Param5: pointer to thread stack
  2998. * Param6: stack size
  2999. * Param7 and 8: priority level and preempt threshold
  3000. * Param9 and 10: time slice and auto-start indicator */
  3001. result = tx_thread_create(&thread->tid,
  3002. "wolfSSL thread",
  3003. (entry_functionType)cb, (ULONG)arg,
  3004. thread->threadStack,
  3005. TESTSUITE_THREAD_STACK_SZ,
  3006. 2, 2,
  3007. 1, TX_AUTO_START);
  3008. if (result != TX_SUCCESS) {
  3009. free(thread->threadStack);
  3010. thread->threadStack = NULL;
  3011. return MEMORY_E;
  3012. }
  3013. return 0;
  3014. }
  3015. int wolfSSL_JoinThread(THREAD_TYPE thread)
  3016. {
  3017. /* TODO: maybe have to use tx_thread_delete? */
  3018. free(thread.threadStack);
  3019. thread.threadStack = NULL;
  3020. return 0;
  3021. }
  3022. #elif defined(WOLFSSL_ZEPHYR)
  3023. int wolfSSL_NewThread(THREAD_TYPE* thread,
  3024. THREAD_CB cb, void* arg)
  3025. {
  3026. #ifndef WOLFSSL_ZEPHYR_STACK_SZ
  3027. #define WOLFSSL_ZEPHYR_STACK_SZ (24*1024)
  3028. #endif
  3029. if (thread == NULL || cb == NULL)
  3030. return BAD_FUNC_ARG;
  3031. XMEMSET(thread, 0, sizeof(*thread));
  3032. /* TODO: Use the following once k_thread_stack_alloc makes it into a
  3033. * release.
  3034. * thread->threadStack = k_thread_stack_alloc(WOLFSSL_ZEPHYR_STACK_SZ,
  3035. * 0);
  3036. */
  3037. thread->threadStack = (void*)XMALLOC(
  3038. Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), 0,
  3039. DYNAMIC_TYPE_TMP_BUFFER);
  3040. if (thread->threadStack == NULL)
  3041. return MEMORY_E;
  3042. /* k_thread_create does not return any error codes */
  3043. /* Casting to k_thread_entry_t should be fine since we just ignore the
  3044. * extra arguments being passed in */
  3045. k_thread_create(&thread->tid, thread->threadStack,
  3046. WOLFSSL_ZEPHYR_STACK_SZ, (k_thread_entry_t)cb, arg, NULL, NULL,
  3047. 5, 0, K_NO_WAIT);
  3048. return 0;
  3049. }
  3050. int wolfSSL_JoinThread(THREAD_TYPE thread)
  3051. {
  3052. int ret = 0;
  3053. int err;
  3054. err = k_thread_join(&thread.tid, K_FOREVER);
  3055. if (err != 0)
  3056. ret = MEMORY_E;
  3057. /* TODO: Use the following once k_thread_stack_free makes it into a
  3058. * release.
  3059. * err = k_thread_stack_free(thread.threadStack);
  3060. * if (err != 0)
  3061. * ret = MEMORY_E;
  3062. */
  3063. XFREE(thread.threadStack, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3064. thread.threadStack = NULL;
  3065. /* No thread resources to free. Everything is stored in thread.tid */
  3066. return ret;
  3067. }
  3068. #ifdef WOLFSSL_COND
  3069. /* Use the pthreads translation layer for signaling */
  3070. #endif /* WOLFSSL_COND */
  3071. #elif defined(WOLFSSL_PTHREADS)
  3072. int wolfSSL_NewThread(THREAD_TYPE* thread,
  3073. THREAD_CB cb, void* arg)
  3074. {
  3075. if (thread == NULL || cb == NULL)
  3076. return BAD_FUNC_ARG;
  3077. if (pthread_create(thread, NULL, cb, arg) != 0)
  3078. return MEMORY_E;
  3079. return 0;
  3080. }
  3081. #ifdef WOLFSSL_THREAD_NO_JOIN
  3082. int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg)
  3083. {
  3084. THREAD_TYPE thread;
  3085. int ret;
  3086. XMEMSET(&thread, 0, sizeof(thread));
  3087. ret = wolfSSL_NewThread(&thread, cb, arg);
  3088. if (ret == 0)
  3089. ret = pthread_detach(thread);
  3090. return ret;
  3091. }
  3092. #endif
  3093. int wolfSSL_JoinThread(THREAD_TYPE thread)
  3094. {
  3095. if (thread == INVALID_THREAD_VAL)
  3096. return BAD_FUNC_ARG;
  3097. if (pthread_join(thread, NULL) != 0)
  3098. return MEMORY_E;
  3099. return 0;
  3100. }
  3101. #ifdef WOLFSSL_COND
  3102. #ifndef __MACH__
  3103. /* Generic POSIX conditional */
  3104. int wolfSSL_CondInit(COND_TYPE* cond)
  3105. {
  3106. if (cond == NULL)
  3107. return BAD_FUNC_ARG;
  3108. if (pthread_mutex_init(&cond->mutex, NULL) != 0)
  3109. return MEMORY_E;
  3110. if (pthread_cond_init(&cond->cond, NULL) != 0) {
  3111. /* Keep compilers happy that we are using the return code */
  3112. if (pthread_mutex_destroy(&cond->mutex) != 0)
  3113. return MEMORY_E;
  3114. return MEMORY_E;
  3115. }
  3116. return 0;
  3117. }
  3118. int wolfSSL_CondFree(COND_TYPE* cond)
  3119. {
  3120. int ret = 0;
  3121. if (cond == NULL)
  3122. return BAD_FUNC_ARG;
  3123. if (pthread_mutex_destroy(&cond->mutex) != 0)
  3124. ret = MEMORY_E;
  3125. if (pthread_cond_destroy(&cond->cond) != 0)
  3126. ret = MEMORY_E;
  3127. return ret;
  3128. }
  3129. int wolfSSL_CondStart(COND_TYPE* cond)
  3130. {
  3131. if (cond == NULL)
  3132. return BAD_FUNC_ARG;
  3133. if (pthread_mutex_lock(&cond->mutex) != 0)
  3134. return BAD_MUTEX_E;
  3135. return 0;
  3136. }
  3137. int wolfSSL_CondSignal(COND_TYPE* cond)
  3138. {
  3139. if (cond == NULL)
  3140. return BAD_FUNC_ARG;
  3141. if (pthread_cond_signal(&cond->cond) != 0)
  3142. return MEMORY_E;
  3143. return 0;
  3144. }
  3145. int wolfSSL_CondWait(COND_TYPE* cond)
  3146. {
  3147. if (cond == NULL)
  3148. return BAD_FUNC_ARG;
  3149. if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0)
  3150. return MEMORY_E;
  3151. return 0;
  3152. }
  3153. int wolfSSL_CondEnd(COND_TYPE* cond)
  3154. {
  3155. if (cond == NULL)
  3156. return BAD_FUNC_ARG;
  3157. if (pthread_mutex_unlock(&cond->mutex) != 0)
  3158. return BAD_MUTEX_E;
  3159. return 0;
  3160. }
  3161. #else /* __MACH__ */
  3162. /* Apple style dispatch semaphore */
  3163. int wolfSSL_CondInit(COND_TYPE* cond)
  3164. {
  3165. if (cond == NULL)
  3166. return BAD_FUNC_ARG;
  3167. /* dispatch_release() fails hard, with Trace/BPT trap signal, if the
  3168. * sem's internal count is less than the value passed in with
  3169. * dispatch_semaphore_create(). work around this by initing
  3170. * with 0, then incrementing it afterwards.
  3171. */
  3172. cond->cond = dispatch_semaphore_create(0);
  3173. if (cond->cond == NULL)
  3174. return MEMORY_E;
  3175. if (wc_InitMutex(&cond->mutex) != 0) {
  3176. dispatch_release(cond->cond);
  3177. return MEMORY_E;
  3178. }
  3179. return 0;
  3180. }
  3181. int wolfSSL_CondFree(COND_TYPE* cond)
  3182. {
  3183. if (cond == NULL)
  3184. return BAD_FUNC_ARG;
  3185. dispatch_release(cond->cond);
  3186. cond->cond = NULL;
  3187. if (wc_FreeMutex(&cond->mutex) != 0) {
  3188. return MEMORY_E;
  3189. }
  3190. return 0;
  3191. }
  3192. int wolfSSL_CondStart(COND_TYPE* cond)
  3193. {
  3194. if (cond == NULL)
  3195. return BAD_FUNC_ARG;
  3196. if (wc_LockMutex(&cond->mutex) != 0)
  3197. return BAD_MUTEX_E;
  3198. return 0;
  3199. }
  3200. int wolfSSL_CondSignal(COND_TYPE* cond)
  3201. {
  3202. if (cond == NULL)
  3203. return BAD_FUNC_ARG;
  3204. if (wc_UnLockMutex(&cond->mutex) != 0)
  3205. return BAD_MUTEX_E;
  3206. dispatch_semaphore_signal(cond->cond);
  3207. if (wc_LockMutex(&cond->mutex) != 0)
  3208. return BAD_MUTEX_E;
  3209. return 0;
  3210. }
  3211. int wolfSSL_CondWait(COND_TYPE* cond)
  3212. {
  3213. if (cond == NULL)
  3214. return BAD_FUNC_ARG;
  3215. if (wc_UnLockMutex(&cond->mutex) != 0)
  3216. return BAD_MUTEX_E;
  3217. dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER);
  3218. if (wc_LockMutex(&cond->mutex) != 0)
  3219. return BAD_MUTEX_E;
  3220. return 0;
  3221. }
  3222. int wolfSSL_CondEnd(COND_TYPE* cond)
  3223. {
  3224. if (cond == NULL)
  3225. return BAD_FUNC_ARG;
  3226. if (wc_UnLockMutex(&cond->mutex) != 0)
  3227. return BAD_MUTEX_E;
  3228. return 0;
  3229. }
  3230. #endif /* __MACH__ */
  3231. #endif /* WOLFSSL_COND */
  3232. #endif
  3233. #endif /* SINGLE_THREADED */