wc_port.c 93 KB

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