wc_port.c 73 KB

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