wc_port.c 66 KB

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