wc_port.c 79 KB

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