wc_port.c 76 KB

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