wc_port.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286
  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. int wc_InitMutex(wolfSSL_Mutex* m)
  1717. {
  1718. if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
  1719. return BAD_MUTEX_E;
  1720. else
  1721. return 0;
  1722. }
  1723. int wc_FreeMutex(wolfSSL_Mutex* m)
  1724. {
  1725. rtp_sig_mutex_free(*m);
  1726. return 0;
  1727. }
  1728. int wc_LockMutex(wolfSSL_Mutex* m)
  1729. {
  1730. if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
  1731. return 0;
  1732. else
  1733. return BAD_MUTEX_E;
  1734. }
  1735. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1736. {
  1737. rtp_sig_mutex_release(*m);
  1738. return 0;
  1739. }
  1740. int ebsnet_fseek(int a, long b, int c)
  1741. {
  1742. int retval;
  1743. retval = vf_lseek(a, b, c);
  1744. if (retval > 0)
  1745. retval = 0;
  1746. else
  1747. retval = -1;
  1748. return(retval);
  1749. }
  1750. #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  1751. int wc_InitMutex(wolfSSL_Mutex* m)
  1752. {
  1753. if (_mutex_init(m, NULL) == MQX_EOK)
  1754. return 0;
  1755. else
  1756. return BAD_MUTEX_E;
  1757. }
  1758. int wc_FreeMutex(wolfSSL_Mutex* m)
  1759. {
  1760. if (_mutex_destroy(m) == MQX_EOK)
  1761. return 0;
  1762. else
  1763. return BAD_MUTEX_E;
  1764. }
  1765. int wc_LockMutex(wolfSSL_Mutex* m)
  1766. {
  1767. if (_mutex_lock(m) == MQX_EOK)
  1768. return 0;
  1769. else
  1770. return BAD_MUTEX_E;
  1771. }
  1772. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1773. {
  1774. if (_mutex_unlock(m) == MQX_EOK)
  1775. return 0;
  1776. else
  1777. return BAD_MUTEX_E;
  1778. }
  1779. #elif defined(WOLFSSL_TIRTOS)
  1780. #include <xdc/runtime/Error.h>
  1781. int wc_InitMutex(wolfSSL_Mutex* m)
  1782. {
  1783. Semaphore_Params params;
  1784. Error_Block eb;
  1785. Error_init(&eb);
  1786. Semaphore_Params_init(&params);
  1787. params.mode = Semaphore_Mode_BINARY;
  1788. *m = Semaphore_create(1, &params, &eb);
  1789. if (Error_check(&eb)) {
  1790. Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
  1791. NULL);
  1792. return BAD_MUTEX_E;
  1793. }
  1794. else
  1795. return 0;
  1796. }
  1797. int wc_FreeMutex(wolfSSL_Mutex* m)
  1798. {
  1799. Semaphore_delete(m);
  1800. return 0;
  1801. }
  1802. int wc_LockMutex(wolfSSL_Mutex* m)
  1803. {
  1804. Semaphore_pend(*m, BIOS_WAIT_FOREVER);
  1805. return 0;
  1806. }
  1807. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1808. {
  1809. Semaphore_post(*m);
  1810. return 0;
  1811. }
  1812. #elif defined(WOLFSSL_uITRON4)
  1813. int wc_InitMutex(wolfSSL_Mutex* m)
  1814. {
  1815. int iReturn;
  1816. m->sem.sematr = TA_TFIFO;
  1817. m->sem.isemcnt = 1;
  1818. m->sem.maxsem = 1;
  1819. m->sem.name = NULL;
  1820. m->id = acre_sem(&m->sem);
  1821. if( m->id != E_OK )
  1822. iReturn = 0;
  1823. else
  1824. iReturn = BAD_MUTEX_E;
  1825. return iReturn;
  1826. }
  1827. int wc_FreeMutex(wolfSSL_Mutex* m)
  1828. {
  1829. del_sem( m->id );
  1830. return 0;
  1831. }
  1832. int wc_LockMutex(wolfSSL_Mutex* m)
  1833. {
  1834. wai_sem(m->id);
  1835. return 0;
  1836. }
  1837. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1838. {
  1839. sig_sem(m->id);
  1840. return 0;
  1841. }
  1842. /**** uITRON malloc/free ***/
  1843. static ID ID_wolfssl_MPOOL = 0;
  1844. static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
  1845. int uITRON4_minit(size_t poolsz) {
  1846. ER ercd;
  1847. wolfssl_MPOOL.mplsz = poolsz;
  1848. ercd = acre_mpl(&wolfssl_MPOOL);
  1849. if (ercd > 0) {
  1850. ID_wolfssl_MPOOL = ercd;
  1851. return 0;
  1852. } else {
  1853. return -1;
  1854. }
  1855. }
  1856. void *uITRON4_malloc(size_t sz) {
  1857. ER ercd;
  1858. void *p = NULL;
  1859. ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
  1860. if (ercd == E_OK) {
  1861. return p;
  1862. } else {
  1863. return 0;
  1864. }
  1865. }
  1866. void *uITRON4_realloc(void *p, size_t sz) {
  1867. ER ercd;
  1868. void *newp = NULL;
  1869. if(p) {
  1870. ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
  1871. if ((ercd == E_OK) && (newp != NULL)) {
  1872. XMEMCPY(newp, p, sz);
  1873. ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1874. if (ercd == E_OK) {
  1875. return newp;
  1876. }
  1877. }
  1878. }
  1879. return 0;
  1880. }
  1881. void uITRON4_free(void *p) {
  1882. ER ercd;
  1883. ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1884. if (ercd == E_OK) {
  1885. return;
  1886. } else {
  1887. return;
  1888. }
  1889. }
  1890. #elif defined(WOLFSSL_uTKERNEL2)
  1891. int wc_InitMutex(wolfSSL_Mutex* m)
  1892. {
  1893. int iReturn;
  1894. m->sem.sematr = TA_TFIFO;
  1895. m->sem.isemcnt = 1;
  1896. m->sem.maxsem = 1;
  1897. m->id = tk_cre_sem(&m->sem);
  1898. if( m->id != NULL )
  1899. iReturn = 0;
  1900. else
  1901. iReturn = BAD_MUTEX_E;
  1902. return iReturn;
  1903. }
  1904. int wc_FreeMutex(wolfSSL_Mutex* m)
  1905. {
  1906. tk_del_sem(m->id);
  1907. return 0;
  1908. }
  1909. int wc_LockMutex(wolfSSL_Mutex* m)
  1910. {
  1911. tk_wai_sem(m->id, 1, TMO_FEVR);
  1912. return 0;
  1913. }
  1914. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1915. {
  1916. tk_sig_sem(m->id, 1);
  1917. return 0;
  1918. }
  1919. /**** uT-Kernel malloc/free ***/
  1920. static ID ID_wolfssl_MPOOL = 0;
  1921. static T_CMPL wolfssl_MPOOL = {
  1922. NULL, /* Extended information */
  1923. TA_TFIFO, /* Memory pool attribute */
  1924. 0, /* Size of whole memory pool (byte) */
  1925. "wolfSSL" /* Object name (max 8-char) */
  1926. };
  1927. int uTKernel_init_mpool(unsigned int sz) {
  1928. ER ercd;
  1929. wolfssl_MPOOL.mplsz = sz;
  1930. ercd = tk_cre_mpl(&wolfssl_MPOOL);
  1931. if (ercd > 0) {
  1932. ID_wolfssl_MPOOL = ercd;
  1933. return 0;
  1934. } else {
  1935. return (int)ercd;
  1936. }
  1937. }
  1938. void *uTKernel_malloc(unsigned int sz) {
  1939. ER ercd;
  1940. void *p = NULL;
  1941. ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
  1942. if (ercd == E_OK) {
  1943. return p;
  1944. } else {
  1945. return 0;
  1946. }
  1947. }
  1948. void *uTKernel_realloc(void *p, unsigned int sz) {
  1949. ER ercd;
  1950. void *newp = NULL;
  1951. if (p) {
  1952. ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
  1953. if ((ercd == E_OK) && (newp != NULL)) {
  1954. XMEMCPY(newp, p, sz);
  1955. ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1956. if (ercd == E_OK) {
  1957. return newp;
  1958. }
  1959. }
  1960. }
  1961. return 0;
  1962. }
  1963. void uTKernel_free(void *p) {
  1964. ER ercd;
  1965. ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
  1966. if (ercd == E_OK) {
  1967. return;
  1968. } else {
  1969. return;
  1970. }
  1971. }
  1972. #elif defined (WOLFSSL_FROSTED)
  1973. int wc_InitMutex(wolfSSL_Mutex* m)
  1974. {
  1975. *m = mutex_init();
  1976. if (*m)
  1977. return 0;
  1978. else
  1979. return -1;
  1980. }
  1981. int wc_FreeMutex(wolfSSL_Mutex* m)
  1982. {
  1983. mutex_destroy(*m);
  1984. return(0);
  1985. }
  1986. int wc_LockMutex(wolfSSL_Mutex* m)
  1987. {
  1988. mutex_lock(*m);
  1989. return 0;
  1990. }
  1991. int wc_UnLockMutex(wolfSSL_Mutex* m)
  1992. {
  1993. mutex_unlock(*m);
  1994. return 0;
  1995. }
  1996. #elif defined(WOLFSSL_CMSIS_RTOS)
  1997. #define CMSIS_NMUTEX 10
  1998. osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
  1999. osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
  2000. osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
  2001. osMutexDef(wolfSSL_mt9);
  2002. static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
  2003. osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
  2004. osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
  2005. osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
  2006. static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
  2007. int wc_InitMutex(wolfSSL_Mutex* m)
  2008. {
  2009. int i;
  2010. for (i=0; i<CMSIS_NMUTEX; i++) {
  2011. if(CMSIS_mutexID[i] == 0) {
  2012. CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
  2013. (*m) = CMSIS_mutexID[i];
  2014. return 0;
  2015. }
  2016. }
  2017. return -1;
  2018. }
  2019. int wc_FreeMutex(wolfSSL_Mutex* m)
  2020. {
  2021. int i;
  2022. osMutexDelete (*m);
  2023. for (i=0; i<CMSIS_NMUTEX; i++) {
  2024. if(CMSIS_mutexID[i] == (*m)) {
  2025. CMSIS_mutexID[i] = 0;
  2026. return(0);
  2027. }
  2028. }
  2029. return(-1);
  2030. }
  2031. int wc_LockMutex(wolfSSL_Mutex* m)
  2032. {
  2033. osMutexWait(*m, osWaitForever);
  2034. return(0);
  2035. }
  2036. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2037. {
  2038. osMutexRelease (*m);
  2039. return 0;
  2040. }
  2041. #elif defined(WOLFSSL_CMSIS_RTOSv2)
  2042. int wc_InitMutex(wolfSSL_Mutex *m)
  2043. {
  2044. static const osMutexAttr_t attr = {
  2045. "wolfSSL_mutex", osMutexRecursive, NULL, 0};
  2046. if ((*m = osMutexNew(&attr)) != NULL)
  2047. return 0;
  2048. else
  2049. return BAD_MUTEX_E;
  2050. }
  2051. int wc_FreeMutex(wolfSSL_Mutex *m)
  2052. {
  2053. if (osMutexDelete(*m) == osOK)
  2054. return 0;
  2055. else
  2056. return BAD_MUTEX_E;
  2057. }
  2058. int wc_LockMutex(wolfSSL_Mutex *m)
  2059. {
  2060. if (osMutexAcquire(*m, osWaitForever) == osOK)
  2061. return 0;
  2062. else
  2063. return BAD_MUTEX_E;
  2064. }
  2065. int wc_UnLockMutex(wolfSSL_Mutex *m)
  2066. {
  2067. if (osMutexRelease(*m) == osOK)
  2068. return 0;
  2069. else
  2070. return BAD_MUTEX_E;
  2071. }
  2072. #elif defined(WOLFSSL_MDK_ARM)
  2073. int wc_InitMutex(wolfSSL_Mutex* m)
  2074. {
  2075. os_mut_init (m);
  2076. return 0;
  2077. }
  2078. int wc_FreeMutex(wolfSSL_Mutex* m)
  2079. {
  2080. return(0);
  2081. }
  2082. int wc_LockMutex(wolfSSL_Mutex* m)
  2083. {
  2084. os_mut_wait (m, 0xffff);
  2085. return(0);
  2086. }
  2087. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2088. {
  2089. os_mut_release (m);
  2090. return 0;
  2091. }
  2092. #elif defined(INTIME_RTOS)
  2093. int wc_InitMutex(wolfSSL_Mutex* m)
  2094. {
  2095. int ret = 0;
  2096. if (m == NULL)
  2097. return BAD_FUNC_ARG;
  2098. *m = CreateRtSemaphore(
  2099. 1, /* initial unit count */
  2100. 1, /* maximum unit count */
  2101. PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
  2102. );
  2103. if (*m == BAD_RTHANDLE) {
  2104. ret = GetLastRtError();
  2105. if (ret != E_OK)
  2106. ret = BAD_MUTEX_E;
  2107. }
  2108. return ret;
  2109. }
  2110. int wc_FreeMutex(wolfSSL_Mutex* m)
  2111. {
  2112. int ret = 0;
  2113. BOOLEAN del;
  2114. if (m == NULL)
  2115. return BAD_FUNC_ARG;
  2116. del = DeleteRtSemaphore(
  2117. *m /* handle for RT semaphore */
  2118. );
  2119. if (del != TRUE)
  2120. ret = BAD_MUTEX_E;
  2121. return ret;
  2122. }
  2123. int wc_LockMutex(wolfSSL_Mutex* m)
  2124. {
  2125. int ret = 0;
  2126. DWORD lck;
  2127. if (m == NULL)
  2128. return BAD_FUNC_ARG;
  2129. lck = WaitForRtSemaphore(
  2130. *m, /* handle for RT semaphore */
  2131. 1, /* number of units to wait for */
  2132. WAIT_FOREVER /* number of milliseconds to wait for units */
  2133. );
  2134. if (lck == WAIT_FAILED) {
  2135. ret = GetLastRtError();
  2136. if (ret != E_OK)
  2137. ret = BAD_MUTEX_E;
  2138. }
  2139. return ret;
  2140. }
  2141. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2142. {
  2143. int ret = 0;
  2144. BOOLEAN rel;
  2145. if (m == NULL)
  2146. return BAD_FUNC_ARG;
  2147. rel = ReleaseRtSemaphore(
  2148. *m, /* handle for RT semaphore */
  2149. 1 /* number of units to release to semaphore */
  2150. );
  2151. if (rel != TRUE)
  2152. ret = BAD_MUTEX_E;
  2153. return ret;
  2154. }
  2155. #elif defined(WOLFSSL_NUCLEUS_1_2)
  2156. int wc_InitMutex(wolfSSL_Mutex* m)
  2157. {
  2158. /* Call the Nucleus function to create the semaphore */
  2159. if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
  2160. NU_PRIORITY) == NU_SUCCESS) {
  2161. return 0;
  2162. }
  2163. return BAD_MUTEX_E;
  2164. }
  2165. int wc_FreeMutex(wolfSSL_Mutex* m)
  2166. {
  2167. if (NU_Delete_Semaphore(m) == NU_SUCCESS)
  2168. return 0;
  2169. return BAD_MUTEX_E;
  2170. }
  2171. int wc_LockMutex(wolfSSL_Mutex* m)
  2172. {
  2173. /* passing suspend task option */
  2174. if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
  2175. return 0;
  2176. return BAD_MUTEX_E;
  2177. }
  2178. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2179. {
  2180. if (NU_Release_Semaphore(m) == NU_SUCCESS)
  2181. return 0;
  2182. return BAD_MUTEX_E;
  2183. }
  2184. #elif defined(WOLFSSL_ZEPHYR)
  2185. int wc_InitMutex(wolfSSL_Mutex* m)
  2186. {
  2187. k_mutex_init(m);
  2188. return 0;
  2189. }
  2190. int wc_FreeMutex(wolfSSL_Mutex* m)
  2191. {
  2192. return 0;
  2193. }
  2194. int wc_LockMutex(wolfSSL_Mutex* m)
  2195. {
  2196. int ret = 0;
  2197. if (k_mutex_lock(m, K_FOREVER) != 0)
  2198. ret = BAD_MUTEX_E;
  2199. return ret;
  2200. }
  2201. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2202. {
  2203. k_mutex_unlock(m);
  2204. return 0;
  2205. }
  2206. #elif defined(WOLFSSL_TELIT_M2MB)
  2207. int wc_InitMutex(wolfSSL_Mutex* m)
  2208. {
  2209. M2MB_OS_RESULT_E osRes;
  2210. M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle;
  2211. UINT32 inheritVal = 1;
  2212. osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle,
  2213. CMDS_ARGS(
  2214. M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL,
  2215. M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx",
  2216. M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal
  2217. )
  2218. );
  2219. if (osRes != M2MB_OS_SUCCESS) {
  2220. return BAD_MUTEX_E;
  2221. }
  2222. osRes = m2mb_os_mtx_init(m, &mtxAttrHandle);
  2223. if (osRes != M2MB_OS_SUCCESS) {
  2224. return BAD_MUTEX_E;
  2225. }
  2226. return 0;
  2227. }
  2228. int wc_FreeMutex(wolfSSL_Mutex* m)
  2229. {
  2230. M2MB_OS_RESULT_E osRes;
  2231. if (m == NULL)
  2232. return BAD_MUTEX_E;
  2233. osRes = m2mb_os_mtx_deinit(*m);
  2234. if (osRes != M2MB_OS_SUCCESS) {
  2235. return BAD_MUTEX_E;
  2236. }
  2237. return 0;
  2238. }
  2239. int wc_LockMutex(wolfSSL_Mutex* m)
  2240. {
  2241. M2MB_OS_RESULT_E osRes;
  2242. if (m == NULL)
  2243. return BAD_MUTEX_E;
  2244. osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER);
  2245. if (osRes != M2MB_OS_SUCCESS) {
  2246. return BAD_MUTEX_E;
  2247. }
  2248. return 0;
  2249. }
  2250. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2251. {
  2252. M2MB_OS_RESULT_E osRes;
  2253. if (m == NULL)
  2254. return BAD_MUTEX_E;
  2255. osRes = m2mb_os_mtx_put(*m);
  2256. if (osRes != M2MB_OS_SUCCESS) {
  2257. return BAD_MUTEX_E;
  2258. }
  2259. return 0;
  2260. }
  2261. #elif defined(WOLFSSL_EMBOS)
  2262. int wc_InitMutex(wolfSSL_Mutex* m)
  2263. {
  2264. int ret;
  2265. OS_MUTEX_Create((OS_MUTEX*) m);
  2266. if (m != NULL)
  2267. ret = 0;
  2268. else
  2269. ret = BAD_MUTEX_E;
  2270. return ret;
  2271. }
  2272. int wc_FreeMutex(wolfSSL_Mutex* m)
  2273. {
  2274. OS_MUTEX_Delete((OS_MUTEX*) m);
  2275. return 0;
  2276. }
  2277. int wc_LockMutex(wolfSSL_Mutex* m)
  2278. {
  2279. OS_MUTEX_LockBlocked((OS_MUTEX*) m);
  2280. return 0;
  2281. }
  2282. int wc_UnLockMutex(wolfSSL_Mutex* m)
  2283. {
  2284. OS_MUTEX_Unlock((OS_MUTEX*) m);
  2285. return 0;
  2286. }
  2287. #elif defined(WOLFSSL_USER_MUTEX)
  2288. /* Use user own mutex */
  2289. /*
  2290. int wc_InitMutex(wolfSSL_Mutex* m) { ... }
  2291. int wc_FreeMutex(wolfSSL_Mutex *m) { ... }
  2292. int wc_LockMutex(wolfSSL_Mutex *m) { ... }
  2293. int wc_UnLockMutex(wolfSSL_Mutex *m) { ... }
  2294. */
  2295. #else
  2296. #warning No mutex handling defined
  2297. #endif
  2298. #if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
  2299. int wc_InitRwLock(wolfSSL_RwLock* m)
  2300. {
  2301. return wc_InitMutex(m);
  2302. }
  2303. int wc_FreeRwLock(wolfSSL_RwLock* m)
  2304. {
  2305. return wc_FreeMutex(m);
  2306. }
  2307. int wc_LockRwLock_Wr(wolfSSL_RwLock* m)
  2308. {
  2309. return wc_LockMutex(m);
  2310. }
  2311. int wc_LockRwLock_Rd(wolfSSL_RwLock* m)
  2312. {
  2313. return wc_LockMutex(m);
  2314. }
  2315. int wc_UnLockRwLock(wolfSSL_RwLock* m)
  2316. {
  2317. return wc_UnLockMutex(m);
  2318. }
  2319. #endif
  2320. #ifndef NO_ASN_TIME
  2321. #if defined(_WIN32_WCE)
  2322. time_t windows_time(time_t* timer)
  2323. {
  2324. SYSTEMTIME sysTime;
  2325. FILETIME fTime;
  2326. ULARGE_INTEGER intTime;
  2327. GetSystemTime(&sysTime);
  2328. SystemTimeToFileTime(&sysTime, &fTime);
  2329. XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
  2330. /* subtract EPOCH */
  2331. intTime.QuadPart -= 0x19db1ded53e8000;
  2332. /* to secs */
  2333. intTime.QuadPart /= 10000000;
  2334. if (timer != NULL)
  2335. *timer = (time_t)intTime.QuadPart;
  2336. return (time_t)intTime.QuadPart;
  2337. }
  2338. #endif /* _WIN32_WCE */
  2339. #if defined(WOLFSSL_APACHE_MYNEWT)
  2340. #include "os/os_time.h"
  2341. time_t mynewt_time(time_t* timer)
  2342. {
  2343. time_t now;
  2344. struct os_timeval tv;
  2345. os_gettimeofday(&tv, NULL);
  2346. now = (time_t)tv.tv_sec;
  2347. if(timer != NULL) {
  2348. *timer = now;
  2349. }
  2350. return now;
  2351. }
  2352. #endif /* WOLFSSL_APACHE_MYNEWT */
  2353. #if defined(WOLFSSL_GMTIME)
  2354. struct tm* gmtime(const time_t* timer)
  2355. {
  2356. #define YEAR0 1900
  2357. #define EPOCH_YEAR 1970
  2358. #define SECS_DAY (24L * 60L * 60L)
  2359. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
  2360. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  2361. static const int _ytab[2][12] =
  2362. {
  2363. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  2364. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  2365. };
  2366. static struct tm st_time;
  2367. struct tm* ret = &st_time;
  2368. time_t secs = *timer;
  2369. unsigned long dayclock, dayno;
  2370. int year = EPOCH_YEAR;
  2371. dayclock = (unsigned long)secs % SECS_DAY;
  2372. dayno = (unsigned long)secs / SECS_DAY;
  2373. ret->tm_sec = (int) dayclock % 60;
  2374. ret->tm_min = (int)(dayclock % 3600) / 60;
  2375. ret->tm_hour = (int) dayclock / 3600;
  2376. ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
  2377. while(dayno >= (unsigned long)YEARSIZE(year)) {
  2378. dayno -= YEARSIZE(year);
  2379. year++;
  2380. }
  2381. ret->tm_year = year - YEAR0;
  2382. ret->tm_yday = (int)dayno;
  2383. ret->tm_mon = 0;
  2384. while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
  2385. dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
  2386. ret->tm_mon++;
  2387. }
  2388. ret->tm_mday = (int)++dayno;
  2389. #ifndef WOLFSSL_LINUXKM
  2390. ret->tm_isdst = 0;
  2391. #endif
  2392. return ret;
  2393. }
  2394. #endif /* WOLFSSL_GMTIME */
  2395. #if defined(HAVE_RTP_SYS)
  2396. #define YEAR0 1900
  2397. struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
  2398. {
  2399. static struct tm st_time;
  2400. struct tm* ret = &st_time;
  2401. DC_RTC_CALENDAR cal;
  2402. dc_rtc_time_get(&cal, TRUE);
  2403. ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
  2404. ret->tm_mon = cal.month - 1; /* gm starts at 0 */
  2405. ret->tm_mday = cal.day;
  2406. ret->tm_hour = cal.hour;
  2407. ret->tm_min = cal.minute;
  2408. ret->tm_sec = cal.second;
  2409. return ret;
  2410. }
  2411. #endif /* HAVE_RTP_SYS */
  2412. #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
  2413. /*
  2414. * time() is just a stub in Microchip libraries. We need our own
  2415. * implementation. Use SNTP client to get seconds since epoch.
  2416. */
  2417. time_t pic32_time(time_t* timer)
  2418. {
  2419. #ifdef MICROCHIP_TCPIP_V5
  2420. DWORD sec = 0;
  2421. #else
  2422. word32 sec = 0;
  2423. #endif
  2424. #ifdef MICROCHIP_MPLAB_HARMONY
  2425. sec = TCPIP_SNTP_UTCSecondsGet();
  2426. #else
  2427. sec = SNTPGetUTCSeconds();
  2428. #endif
  2429. if (timer != NULL)
  2430. *timer = (time_t)sec;
  2431. return (time_t)sec;
  2432. }
  2433. #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
  2434. #if defined(WOLFSSL_DEOS) || defined(WOLFSSL_DEOS_RTEMS)
  2435. time_t deos_time(time_t* timer)
  2436. {
  2437. const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
  2438. const volatile word32 *systemTickPtr = systemTickPointer();
  2439. if (timer != NULL)
  2440. *timer = *systemTickPtr/systemTickTimeInHz;
  2441. #if defined(CURRENT_UNIX_TIMESTAMP)
  2442. /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */
  2443. return (time_t) (*systemTickPtr/systemTickTimeInHz) + CURRENT_UNIX_TIMESTAMP;
  2444. #else
  2445. return (time_t) *systemTickPtr/systemTickTimeInHz;
  2446. #endif
  2447. }
  2448. #endif /* WOLFSSL_DEOS || WOLFSSL_DEOS_RTEMS */
  2449. #if defined(FREESCALE_RTC)
  2450. #include "fsl_rtc.h"
  2451. time_t fsl_time(time_t* t)
  2452. {
  2453. *t = RTC_GetSecondsTimerCount(RTC);
  2454. return *t;
  2455. }
  2456. #endif
  2457. #if defined(FREESCALE_SNVS_RTC)
  2458. time_t fsl_time(time_t* t)
  2459. {
  2460. struct tm tm_time;
  2461. time_t ret;
  2462. snvs_hp_rtc_datetime_t rtcDate;
  2463. snvs_hp_rtc_config_t snvsRtcConfig;
  2464. SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
  2465. SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
  2466. SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
  2467. tm_time.tm_year = rtcDate.year;
  2468. tm_time.tm_mon = rtcDate.month;
  2469. tm_time.tm_mday = rtcDate.day;
  2470. tm_time.tm_hour = rtcDate.hour;
  2471. tm_time.tm_min = rtcDate.minute;
  2472. tm_time.tm_sec = rtcDate.second;
  2473. ret = mktime(&tm_time);
  2474. if (t != NULL)
  2475. *t = ret;
  2476. return ret;
  2477. }
  2478. #endif
  2479. #if defined(MICRIUM)
  2480. time_t micrium_time(time_t* timer)
  2481. {
  2482. CLK_TS_SEC sec;
  2483. Clk_GetTS_Unix(&sec);
  2484. if (timer != NULL)
  2485. *timer = sec;
  2486. return (time_t) sec;
  2487. }
  2488. #endif /* MICRIUM */
  2489. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  2490. time_t mqx_time(time_t* timer)
  2491. {
  2492. TIME_STRUCT time_s;
  2493. _time_get(&time_s);
  2494. if (timer != NULL)
  2495. *timer = (time_t)time_s.SECONDS;
  2496. return (time_t)time_s.SECONDS;
  2497. }
  2498. #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
  2499. #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME)
  2500. time_t XTIME(time_t * timer)
  2501. {
  2502. time_t sec = 0;
  2503. sec = (time_t) Seconds_get();
  2504. if (timer != NULL)
  2505. *timer = sec;
  2506. return sec;
  2507. }
  2508. #endif /* WOLFSSL_TIRTOS */
  2509. #if defined(WOLFSSL_XILINX)
  2510. #include "xrtcpsu.h"
  2511. time_t xilinx_time(time_t * timer)
  2512. {
  2513. time_t sec = 0;
  2514. XRtcPsu_Config* con;
  2515. XRtcPsu rtc;
  2516. con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
  2517. if (con != NULL) {
  2518. if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
  2519. sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
  2520. }
  2521. else {
  2522. WOLFSSL_MSG("Unable to initialize RTC");
  2523. }
  2524. }
  2525. if (timer != NULL)
  2526. *timer = sec;
  2527. return sec;
  2528. }
  2529. #endif /* WOLFSSL_XILINX */
  2530. #if defined(WOLFSSL_ZEPHYR)
  2531. time_t z_time(time_t * timer)
  2532. {
  2533. struct timespec ts;
  2534. if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
  2535. if (timer != NULL)
  2536. *timer = ts.tv_sec;
  2537. return ts.tv_sec;
  2538. }
  2539. #endif /* WOLFSSL_ZEPHYR */
  2540. #if defined(WOLFSSL_WICED)
  2541. #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME
  2542. #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time.
  2543. #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */
  2544. time_t wiced_pseudo_unix_epoch_time(time_t * timer)
  2545. {
  2546. time_t epoch_time;
  2547. /* The time() function return uptime on WICED platform. */
  2548. epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME;
  2549. if (timer != NULL) {
  2550. *timer = epoch_time;
  2551. }
  2552. return epoch_time;
  2553. }
  2554. #endif /* WOLFSSL_WICED */
  2555. #ifdef WOLFSSL_TELIT_M2MB
  2556. time_t m2mb_xtime(time_t * timer)
  2557. {
  2558. time_t myTime = 0;
  2559. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2560. if (fd != -1) {
  2561. M2MB_RTC_TIMEVAL_T timeval;
  2562. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2563. myTime = timeval.sec;
  2564. m2mb_rtc_close(fd);
  2565. }
  2566. return myTime;
  2567. }
  2568. #ifdef WOLFSSL_TLS13
  2569. time_t m2mb_xtime_ms(time_t * timer)
  2570. {
  2571. time_t myTime = 0;
  2572. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2573. if (fd != -1) {
  2574. M2MB_RTC_TIMEVAL_T timeval;
  2575. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2576. myTime = timeval.sec + timeval.msec;
  2577. m2mb_rtc_close(fd);
  2578. }
  2579. return myTime;
  2580. }
  2581. #endif /* WOLFSSL_TLS13 */
  2582. #ifndef NO_CRYPT_BENCHMARK
  2583. double m2mb_xtime_bench(int reset)
  2584. {
  2585. double myTime = 0;
  2586. INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
  2587. if (fd != -1) {
  2588. M2MB_RTC_TIMEVAL_T timeval;
  2589. m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
  2590. myTime = (double)timeval.sec + ((double)timeval.msec / 1000);
  2591. m2mb_rtc_close(fd);
  2592. }
  2593. return myTime;
  2594. }
  2595. #endif /* !NO_CRYPT_BENCHMARK */
  2596. #endif /* WOLFSSL_TELIT_M2MB */
  2597. #if defined(WOLFSSL_LINUXKM)
  2598. time_t time(time_t * timer)
  2599. {
  2600. time_t ret;
  2601. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  2602. struct timespec ts;
  2603. getnstimeofday(&ts);
  2604. ret = ts.tv_sec;
  2605. #else
  2606. struct timespec64 ts;
  2607. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  2608. ts = current_kernel_time64();
  2609. #else
  2610. ktime_get_coarse_real_ts64(&ts);
  2611. #endif
  2612. ret = ts.tv_sec;
  2613. #endif
  2614. if (timer)
  2615. *timer = ret;
  2616. return ret;
  2617. }
  2618. #endif /* WOLFSSL_LINUXKM */
  2619. #ifdef HAL_RTC_MODULE_ENABLED
  2620. extern RTC_HandleTypeDef hrtc;
  2621. time_t stm32_hal_time(time_t *t1)
  2622. {
  2623. struct tm tm_time;
  2624. time_t ret;
  2625. RTC_TimeTypeDef time;
  2626. RTC_DateTypeDef date;
  2627. XMEMSET(&tm_time, 0, sizeof(struct tm));
  2628. /* order of GetTime followed by GetDate required here due to STM32 HW
  2629. * requirement */
  2630. HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN);
  2631. HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN);
  2632. tm_time.tm_year = date.Year;
  2633. tm_time.tm_mon = date.Month - 1; /* gm starts at 0 */
  2634. tm_time.tm_mday = date.Date;
  2635. tm_time.tm_hour = time.Hours;
  2636. tm_time.tm_min = time.Minutes;
  2637. tm_time.tm_sec = time.Seconds;
  2638. ret = mktime(&tm_time);
  2639. if (t1 != NULL)
  2640. *t1 = ret;
  2641. return ret;
  2642. }
  2643. #endif /* HAL_RTC_MODULE_ENABLED */
  2644. #endif /* !NO_ASN_TIME */
  2645. #if !defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)
  2646. char* mystrnstr(const char* s1, const char* s2, unsigned int n)
  2647. {
  2648. unsigned int s2_len = (unsigned int)XSTRLEN(s2);
  2649. if (s2_len == 0)
  2650. return (char*)s1;
  2651. while (n >= s2_len && s1[0]) {
  2652. if (s1[0] == s2[0])
  2653. if (XMEMCMP(s1, s2, s2_len) == 0)
  2654. return (char*)s1;
  2655. s1++;
  2656. n--;
  2657. }
  2658. return NULL;
  2659. }
  2660. #endif
  2661. /* custom memory wrappers */
  2662. #ifdef WOLFSSL_NUCLEUS_1_2
  2663. /* system memory pool */
  2664. extern NU_MEMORY_POOL System_Memory;
  2665. void* nucleus_malloc(unsigned long size, void* heap, int type)
  2666. {
  2667. STATUS status;
  2668. void* stack_ptr;
  2669. status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
  2670. NU_NO_SUSPEND);
  2671. if (status == NU_SUCCESS) {
  2672. return 0;
  2673. } else {
  2674. return stack_ptr;
  2675. }
  2676. }
  2677. void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
  2678. {
  2679. DM_HEADER* old_header;
  2680. word32 old_size, copy_size;
  2681. void* new_mem;
  2682. /* if ptr is NULL, behave like malloc */
  2683. new_mem = nucleus_malloc(size, NULL, 0);
  2684. if (new_mem == 0 || ptr == 0) {
  2685. return new_mem;
  2686. }
  2687. /* calculate old memory block size */
  2688. /* mem pointers stored in block headers (ref dm_defs.h) */
  2689. old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
  2690. old_size = (byte*)old_header->dm_next_memory - (byte*)ptr;
  2691. /* copy old to new */
  2692. if (old_size < size) {
  2693. copy_size = old_size;
  2694. } else {
  2695. copy_size = size;
  2696. }
  2697. XMEMCPY(new_mem, ptr, copy_size);
  2698. /* free old */
  2699. nucleus_free(ptr, NULL, 0);
  2700. return new_mem;
  2701. }
  2702. void nucleus_free(void* ptr, void* heap, int type)
  2703. {
  2704. if (ptr != NULL)
  2705. NU_Deallocate_Memory(ptr);
  2706. }
  2707. #endif /* WOLFSSL_NUCLEUS_1_2 */
  2708. #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
  2709. #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
  2710. #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
  2711. #endif
  2712. #if defined(WOLFSSL_CRYPTOCELL)
  2713. #define WOLFSSL_CRYPTOCELL_C
  2714. #include <wolfcrypt/src/port/arm/cryptoCell.c> /* CC310, RTC and RNG */
  2715. #if !defined(NO_SHA256)
  2716. #define WOLFSSL_CRYPTOCELL_HASH_C
  2717. #include <wolfcrypt/src/port/arm/cryptoCellHash.c> /* sha256 */
  2718. #endif
  2719. #endif