| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942 |
- /* port.c
- *
- * Copyright (C) 2006-2023 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <wolfssl/wolfcrypt/settings.h>
- #include <wolfssl/wolfcrypt/types.h>
- #include <wolfssl/wolfcrypt/error-crypt.h>
- #include <wolfssl/wolfcrypt/logging.h>
- #include <wolfssl/wolfcrypt/wc_port.h>
- #ifdef HAVE_ECC
- #include <wolfssl/wolfcrypt/ecc.h>
- #endif
- #ifdef WOLFSSL_ASYNC_CRYPT
- #include <wolfssl/wolfcrypt/async.h>
- #endif
- /* IPP header files for library initialization */
- #ifdef HAVE_FAST_RSA
- #include <ipp.h>
- #include <ippcp.h>
- #endif
- #ifdef FREESCALE_LTC_TFM
- #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
- #endif
- #ifdef WOLFSSL_PSOC6_CRYPTO
- #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
- #endif
- #ifdef MAXQ10XX_MODULE_INIT
- #include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h>
- #endif
- #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
- defined(WOLFSSL_ATECC608A)
- #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
- #endif
- #if defined(WOLFSSL_RENESAS_TSIP)
- #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
- #endif
- #if defined(WOLFSSL_RENESAS_FSPSM)
- #include <wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h>
- #endif
- #if defined(WOLFSSL_RENESAS_RX64_HASH)
- #include <wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h>
- #endif
- #if defined(WOLFSSL_STSAFEA100)
- #include <wolfssl/wolfcrypt/port/st/stsafe.h>
- #endif
- #if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) \
- && !defined(WOLFCRYPT_ONLY)
- #include <wolfssl/openssl/evp.h>
- #endif
- #include <wolfssl/wolfcrypt/memory.h>
- #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
- #include <wolfssl/wolfcrypt/mem_track.h>
- #endif
- #if defined(WOLFSSL_CAAM)
- #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
- #endif
- #if defined(HAVE_ARIA)
- #include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h>
- #endif
- #if defined(WOLFSSL_DEVCRYPTO)
- #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
- #endif
- #ifdef WOLFSSL_IMXRT_DCP
- #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h>
- #endif
- #ifdef WOLF_CRYPTO_CB
- #include <wolfssl/wolfcrypt/cryptocb.h>
- #endif
- #ifdef HAVE_INTEL_QA_SYNC
- #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
- #endif
- #ifdef HAVE_CAVIUM_OCTEON_SYNC
- #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
- #endif
- #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT)
- #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
- #endif
- #ifdef WOLFSSL_SCE
- #include "hal_data.h"
- #endif
- #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
- #include "rpcmem.h"
- #endif
- #ifdef _MSC_VER
- /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
- #pragma warning(disable: 4996)
- #endif
- #if defined(WOLFSSL_HAVE_PSA)
- #include <wolfssl/wolfcrypt/port/psa/psa.h>
- #endif
- /* prevent multiple mutex initializations */
- static volatile int initRefCount = 0;
- /* Used to initialize state for wolfcrypt
- return 0 on success
- */
- WOLFSSL_ABI
- int wolfCrypt_Init(void)
- {
- int ret = 0;
- if (initRefCount == 0) {
- WOLFSSL_ENTER("wolfCrypt_Init");
- #ifdef WOLFSSL_CHECK_MEM_ZERO
- /* Initialize the mutex for access to the list of memory locations that
- * must be freed. */
- wc_MemZero_Init();
- #endif
- #ifdef WOLFSSL_MEM_FAIL_COUNT
- wc_MemFailCount_Init();
- #endif
- #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
- {
- word32 rngMallocFail;
- time_t seed = time(NULL);
- srand((word32)seed);
- rngMallocFail = rand() % 2000; /* max 2000 */
- fprintf(stderr, "\n--- RNG MALLOC FAIL AT %u ---\n", rngMallocFail);
- wolfSSL_SetMemFailCount(rngMallocFail);
- }
- #endif
- #ifdef WOLF_CRYPTO_CB
- wc_CryptoCb_Init();
- #endif
- #ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfAsync_HardwareStart();
- if (ret != 0) {
- WOLFSSL_MSG("Async hardware start failed");
- /* don't return failure, allow operation to continue */
- }
- #endif
- #if defined(WOLFSSL_RENESAS_TSIP)
- ret = tsip_Open( );
- if( ret != TSIP_SUCCESS ) {
- WOLFSSL_MSG("RENESAS TSIP Open failed");
- /* not return 1 since WOLFSSL_SUCCESS=1*/
- ret = -1;/* FATAL ERROR */
- return ret;
- }
- #endif
- #if defined(WOLFSSL_RENESAS_RX64_HASH)
- ret = rx64_hw_Open();
- if( ret != 0 ) {
- WOLFSSL_MSG("Renesas RX64 HW Open failed");
- /* not return 1 since WOLFSSL_SUCCESS=1*/
- ret = -1;/* FATAL ERROR */
- return ret;
- }
- #endif
- #if defined(WOLFSSL_RENESAS_FSPSM)
- ret = wc_fspsm_Open( );
- if( ret != FSP_SUCCESS ) {
- WOLFSSL_MSG("RENESAS SCE Open failed");
- /* not return 1 since WOLFSSL_SUCCESS=1*/
- ret = -1;/* FATAL ERROR */
- return ret;
- }
- #endif
- #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
- ret = InitMemoryTracker();
- if (ret != 0) {
- WOLFSSL_MSG("InitMemoryTracker failed");
- return ret;
- }
- #endif
- #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
- ret = allocate_wolfcrypt_linuxkm_fpu_states();
- if (ret != 0) {
- WOLFSSL_MSG("allocate_wolfcrypt_linuxkm_fpu_states failed");
- return ret;
- }
- #endif
- #if WOLFSSL_CRYPT_HW_MUTEX
- /* If crypto hardware mutex protection is enabled, then initialize it */
- ret = wolfSSL_CryptHwMutexInit();
- if (ret != 0) {
- WOLFSSL_MSG("Hw crypt mutex init failed");
- return ret;
- }
- #endif
- /* if defined have fast RSA then initialize Intel IPP */
- #ifdef HAVE_FAST_RSA
- WOLFSSL_MSG("Attempting to use optimized IPP Library");
- if ((ret = ippInit()) != ippStsNoErr) {
- /* possible to get a CPU feature support status on optimized IPP
- library but still use default library and see competitive speeds */
- WOLFSSL_MSG("Warning when trying to set up optimization");
- WOLFSSL_MSG(ippGetStatusString(ret));
- WOLFSSL_MSG("Using default fast IPP library");
- ret = 0;
- (void)ret; /* suppress not read warning */
- }
- #endif
- #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
- ret = ksdk_port_init();
- if (ret != 0) {
- WOLFSSL_MSG("KSDK port init failed");
- return ret;
- }
- #endif
- #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \
- defined(WOLFSSL_ATECC608A)
- ret = atmel_init();
- if (ret != 0) {
- WOLFSSL_MSG("CryptoAuthLib init failed");
- return ret;
- }
- #endif
- #if defined(WOLFSSL_CRYPTOCELL)
- /* enable and initialize the ARM CryptoCell 3xx runtime library */
- ret = cc310_Init();
- if (ret != 0) {
- WOLFSSL_MSG("CRYPTOCELL init failed");
- return ret;
- }
- #endif
- #if defined(WOLFSSL_STSAFEA100)
- stsafe_interface_init();
- #endif
- #if defined(WOLFSSL_PSOC6_CRYPTO)
- ret = psoc6_crypto_port_init();
- if (ret != 0) {
- WOLFSSL_MSG("PSoC6 crypto engine init failed");
- return ret;
- }
- #endif
- #ifdef MAXQ10XX_MODULE_INIT
- ret = maxq10xx_port_init();
- if (ret != 0) {
- WOLFSSL_MSG("MAXQ10xx port init failed");
- return ret;
- }
- #endif
- #ifdef WOLFSSL_SILABS_SE_ACCEL
- /* init handles if it is already initialized */
- ret = sl_se_init();
- #endif
- #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT)
- ret = wc_se050_init(NULL);
- if (ret != 0) {
- WOLFSSL_MSG("SE050 init failed");
- return ret;
- }
- #endif
- #ifdef WOLFSSL_ARMASM
- WOLFSSL_MSG("Using ARM hardware acceleration");
- #endif
- #ifdef WOLFSSL_AFALG
- WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
- #endif
- #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
- wolfSSL_EVP_init();
- #endif
- #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- if ((ret = wc_LoggingInit()) != 0) {
- WOLFSSL_MSG("Error creating logging mutex");
- return ret;
- }
- #endif
- #if defined(WOLFSSL_HAVE_PSA)
- if ((ret = wc_psa_init()) != 0)
- return ret;
- #endif
- #ifdef HAVE_ENTROPY_MEMUSE
- ret = Entropy_Init();
- if (ret != 0) {
- WOLFSSL_MSG("Error initializing entropy");
- return ret;
- }
- #endif
- #ifdef HAVE_ECC
- #ifdef FP_ECC
- wc_ecc_fp_init();
- #endif
- #ifdef ECC_CACHE_CURVE
- if ((ret = wc_ecc_curve_cache_init()) != 0) {
- WOLFSSL_MSG("Error creating curve cache");
- return ret;
- }
- #endif
- #endif
- #ifdef WOLFSSL_SCE
- ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(
- WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg);
- if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) {
- WOLFSSL_MSG("SCE already open");
- ret = 0;
- }
- if (ret != SSP_SUCCESS) {
- WOLFSSL_MSG("Error opening SCE");
- return -1; /* FATAL_ERROR */
- }
- #endif
- #if defined(WOLFSSL_DEVCRYPTO)
- if ((ret = wc_DevCryptoInit()) != 0) {
- return ret;
- }
- #endif
- #if defined(WOLFSSL_CAAM)
- if ((ret = wc_caamInit()) != 0) {
- return ret;
- }
- #endif
- #if defined(HAVE_ARIA)
- if ((ret = wc_AriaInit()) != 0) {
- return ret;
- }
- #endif
- #ifdef WOLFSSL_IMXRT_DCP
- if ((ret = wc_dcp_init()) != 0) {
- return ret;
- }
- #endif
- #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
- if ((ret = wolfSSL_InitHandle()) != 0) {
- return ret;
- }
- rpcmem_init();
- #endif
- }
- initRefCount++;
- return ret;
- }
- #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
- long wolfCrypt_heap_peakAllocs_checkpoint(void) {
- long ret = ourMemStats.peakAllocsTripOdometer;
- ourMemStats.peakAllocsTripOdometer = ourMemStats.totalAllocs -
- ourMemStats.totalDeallocs;
- return ret;
- }
- long wolfCrypt_heap_peakBytes_checkpoint(void) {
- long ret = ourMemStats.peakBytesTripOdometer;
- ourMemStats.peakBytesTripOdometer = ourMemStats.currentBytes;
- return ret;
- }
- #endif
- /* return success value is the same as wolfCrypt_Init */
- WOLFSSL_ABI
- int wolfCrypt_Cleanup(void)
- {
- int ret = 0;
- initRefCount--;
- if (initRefCount < 0)
- initRefCount = 0;
- if (initRefCount == 0) {
- WOLFSSL_ENTER("wolfCrypt_Cleanup");
- #ifdef HAVE_ECC
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #ifdef ECC_CACHE_CURVE
- wc_ecc_curve_cache_free();
- #endif
- #endif /* HAVE_ECC */
- #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- ret = wc_LoggingCleanup();
- #endif
- #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
- ShowMemoryTracker();
- #endif
- #ifdef WOLFSSL_ASYNC_CRYPT
- wolfAsync_HardwareStop();
- #endif
- #ifdef WOLFSSL_RENESAS_TSIP
- tsip_Close();
- #endif
- #if defined(WOLFSSL_RENESAS_RX64_HASH)
- rx64_hw_Close();
- #endif
- #if defined(WOLFSSL_RENESAS_FSPSM)
- wc_fspsm_Close();
- #endif
- #ifdef WOLFSSL_SCE
- WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl);
- #endif
- #if defined(WOLFSSL_CAAM)
- wc_caamFree();
- #endif
- #if defined(WOLFSSL_CRYPTOCELL)
- cc310_Free();
- #endif
- #ifdef WOLFSSL_SILABS_SE_ACCEL
- ret = sl_se_deinit();
- #endif
- #if defined(WOLFSSL_RENESAS_TSIP)
- tsip_Close();
- #endif
- #if defined(WOLFSSL_DEVCRYPTO)
- wc_DevCryptoCleanup();
- #endif
- #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
- rpcmem_deinit();
- wolfSSL_CleanupHandle();
- #endif
- #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
- free_wolfcrypt_linuxkm_fpu_states();
- #endif
- #ifdef HAVE_ENTROPY_MEMUSE
- Entropy_Final();
- #endif
- #ifdef WOLF_CRYPTO_CB
- wc_CryptoCb_Cleanup();
- #endif
- #if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY)
- wc_MemFailCount_Free();
- #endif
- #ifdef WOLFSSL_CHECK_MEM_ZERO
- /* Free the mutex for access to the list of memory locations that
- * must be freed. */
- wc_MemZero_Free();
- #endif
- }
- return ret;
- }
- #ifndef NO_FILESYSTEM
- /* Helpful function to load file into allocated buffer */
- int wc_FileLoad(const char* fname, unsigned char** buf, size_t* bufLen,
- void* heap)
- {
- int ret;
- ssize_t fileSz;
- XFILE f;
- if (fname == NULL || buf == NULL || bufLen == NULL) {
- return BAD_FUNC_ARG;
- }
- /* set defaults */
- *buf = NULL;
- *bufLen = 0;
- /* open file (read-only binary) */
- f = XFOPEN(fname, "rb");
- if (!f) {
- WOLFSSL_MSG("wc_LoadFile file load error");
- return BAD_PATH_ERROR;
- }
- if (XFSEEK(f, 0, XSEEK_END) != 0) {
- WOLFSSL_MSG("wc_LoadFile file seek error");
- XFCLOSE(f);
- return BAD_PATH_ERROR;
- }
- fileSz = XFTELL(f);
- if (fileSz < 0) {
- WOLFSSL_MSG("wc_LoadFile ftell error");
- XFCLOSE(f);
- return BAD_PATH_ERROR;
- }
- if (XFSEEK(f, 0, XSEEK_SET) != 0) {
- WOLFSSL_MSG("wc_LoadFile file seek error");
- XFCLOSE(f);
- return BAD_PATH_ERROR;
- }
- if (fileSz > 0) {
- *bufLen = (size_t)fileSz;
- *buf = (byte*)XMALLOC(*bufLen, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (*buf == NULL) {
- WOLFSSL_MSG("wc_LoadFile memory error");
- ret = MEMORY_E;
- }
- else {
- size_t readLen = XFREAD(*buf, 1, *bufLen, f);
- /* check response code */
- ret = (readLen == *bufLen) ? 0 : -1;
- }
- }
- else {
- ret = BUFFER_E;
- }
- XFCLOSE(f);
- (void)heap;
- return ret;
- }
- #if !defined(NO_WOLFSSL_DIR) && \
- !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
- /* File Handling Helper */
- /* returns 0 if file exists, WC_ISFILEEXIST_NOFILE if file doesn't exist */
- int wc_FileExists(const char* fname)
- {
- struct ReadDirCtx ctx;
- XMEMSET(&ctx, 0, sizeof(ctx));
- if (fname == NULL)
- return 0;
- if (XSTAT(fname, &ctx.s) != 0) {
- WOLFSSL_MSG("stat on name failed");
- return BAD_PATH_ERROR;
- } else {
- #if defined(USE_WINDOWS_API)
- if (XS_ISREG(ctx.s.st_mode)) {
- return 0;
- }
- #elif defined(WOLFSSL_ZEPHYR)
- if (XS_ISREG(ctx.s.type)) {
- return 0;
- }
- #elif defined(WOLFSSL_TELIT_M2MB)
- if (XS_ISREG(ctx.s.st_mode)) {
- return 0;
- }
- #else
- if (XS_ISREG(ctx.s.st_mode)) {
- return 0;
- }
- #endif
- }
- return WC_ISFILEEXIST_NOFILE;
- }
- /* File Handling Helpers */
- /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
- int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
- {
- int ret = WC_READDIR_NOFILE; /* default to no files found */
- int pathLen = 0;
- if (name)
- *name = NULL;
- if (ctx == NULL || path == NULL) {
- return BAD_FUNC_ARG;
- }
- XMEMSET(ctx, 0, sizeof(ReadDirCtx));
- pathLen = (int)XSTRLEN(path);
- #ifdef USE_WINDOWS_API
- if (pathLen > MAX_FILENAME_SZ - 3)
- return BAD_PATH_ERROR;
- XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
- XSTRNCPY(ctx->name + pathLen, "\\*", (size_t)(MAX_FILENAME_SZ - pathLen));
- ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
- if (ctx->hFind == INVALID_HANDLE_VALUE) {
- WOLFSSL_MSG("FindFirstFile for path verify locations failed");
- return BAD_PATH_ERROR;
- }
- do {
- if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
- int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
- if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
- return BAD_PATH_ERROR;
- }
- XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
- ctx->name[pathLen] = '\\';
- XSTRNCPY(ctx->name + pathLen + 1,
- ctx->FindFileData.cFileName,
- (size_t)(MAX_FILENAME_SZ - pathLen - 1));
- if (name)
- *name = ctx->name;
- return 0;
- }
- } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
- #elif defined(INTIME_RTOS)
- if (pathLen > MAX_FILENAME_SZ - 3)
- return BAD_PATH_ERROR;
- XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
- XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
- if (!IntimeFindFirst(ctx->name, &ctx->FindFileData)) {
- WOLFSSL_MSG("FindFirstFile for path verify locations failed");
- return BAD_PATH_ERROR;
- }
- do {
- int dnameLen = (int)XSTRLEN(IntimeFilename(ctx));
- if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
- return BAD_PATH_ERROR;
- }
- XSTRNCPY(ctx->name, path, pathLen + 1);
- ctx->name[pathLen] = '\\';
- XSTRNCPY(ctx->name + pathLen + 1,
- IntimeFilename(ctx),
- MAX_FILENAME_SZ - pathLen - 1);
- if (0 == wc_FileExists(ctx->name)) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- } while (IntimeFindNext(&ctx->FindFileData));
- #elif defined(WOLFSSL_ZEPHYR)
- if (fs_opendir(&ctx->dir, path) != 0) {
- WOLFSSL_MSG("opendir path verify locations failed");
- return BAD_PATH_ERROR;
- }
- ctx->dirp = &ctx->dir;
- while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
- int dnameLen = (int)XSTRLEN(ctx->entry.name);
- if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
- ret = BAD_PATH_ERROR;
- break;
- }
- XSTRNCPY(ctx->name, path, pathLen + 1);
- ctx->name[pathLen] = '/';
- /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
- * of earlier check it is known that dnameLen is less than
- * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
- XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
- if ((ret = wc_FileExists(ctx->name)) == 0) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #elif defined(WOLFSSL_TELIT_M2MB)
- ctx->dir = m2mb_fs_opendir((const CHAR*)path);
- if (ctx->dir == NULL) {
- WOLFSSL_MSG("opendir path verify locations failed");
- return BAD_PATH_ERROR;
- }
- while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
- int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
- if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
- ret = BAD_PATH_ERROR;
- break;
- }
- XSTRNCPY(ctx->name, path, pathLen + 1);
- ctx->name[pathLen] = '/';
- /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
- * of earlier check it is known that dnameLen is less than
- * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
- XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
- if ((ret = wc_FileExists(ctx->name)) == 0) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #else
- ctx->dir = opendir(path);
- if (ctx->dir == NULL) {
- WOLFSSL_MSG("opendir path verify locations failed");
- return BAD_PATH_ERROR;
- }
- while ((ctx->entry = readdir(ctx->dir)) != NULL) {
- int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
- if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
- ret = BAD_PATH_ERROR;
- break;
- }
- XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
- ctx->name[pathLen] = '/';
- /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
- * of earlier check it is known that dnameLen is less than
- * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
- XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1);
- if ((ret = wc_FileExists(ctx->name)) == 0) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #endif
- wc_ReadDirClose(ctx);
- return ret;
- }
- /* returns 0 if file found, WC_READDIR_NOFILE if no more files */
- int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
- {
- int ret = WC_READDIR_NOFILE; /* default to no file found */
- int pathLen = 0;
- if (name)
- *name = NULL;
- if (ctx == NULL || path == NULL) {
- return BAD_FUNC_ARG;
- }
- XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
- pathLen = (int)XSTRLEN(path);
- #ifdef USE_WINDOWS_API
- while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
- if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
- int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
- if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
- return BAD_PATH_ERROR;
- }
- XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
- ctx->name[pathLen] = '\\';
- XSTRNCPY(ctx->name + pathLen + 1,
- ctx->FindFileData.cFileName,
- (size_t)(MAX_FILENAME_SZ - pathLen - 1));
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #elif defined(INTIME_RTOS)
- while (IntimeFindNext(&ctx->FindFileData)) {
- int dnameLen = (int)XSTRLEN(IntimeFilename(ctx));
- if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
- return BAD_PATH_ERROR;
- }
- XSTRNCPY(ctx->name, path, pathLen + 1);
- ctx->name[pathLen] = '\\';
- XSTRNCPY(ctx->name + pathLen + 1,
- IntimeFilename(ctx),
- MAX_FILENAME_SZ - pathLen - 1);
- if (0 == wc_FileExists(ctx->name)) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #elif defined(WOLFSSL_ZEPHYR)
- while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
- int dnameLen = (int)XSTRLEN(ctx->entry.name);
- if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
- ret = BAD_PATH_ERROR;
- break;
- }
- XSTRNCPY(ctx->name, path, pathLen + 1);
- ctx->name[pathLen] = '/';
- /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
- * of earlier check it is known that dnameLen is less than
- * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
- XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
- if ((ret = wc_FileExists(ctx->name)) == 0) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #elif defined(WOLFSSL_TELIT_M2MB)
- while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
- int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
- if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
- ret = BAD_PATH_ERROR;
- break;
- }
- XSTRNCPY(ctx->name, path, pathLen + 1);
- ctx->name[pathLen] = '/';
- /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
- * of earlier check it is known that dnameLen is less than
- * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
- XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
- if ((ret = wc_FileExists(ctx->name)) == 0) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #else
- while ((ctx->entry = readdir(ctx->dir)) != NULL) {
- int dnameLen = (int)XSTRLEN(ctx->entry->d_name);
- if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
- ret = BAD_PATH_ERROR;
- break;
- }
- XSTRNCPY(ctx->name, path, (size_t)pathLen + 1);
- ctx->name[pathLen] = '/';
- /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
- * of earlier check it is known that dnameLen is less than
- * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
- XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1);
- if ((ret = wc_FileExists(ctx->name)) == 0) {
- if (name)
- *name = ctx->name;
- return 0;
- }
- }
- #endif
- wc_ReadDirClose(ctx);
- return ret;
- }
- void wc_ReadDirClose(ReadDirCtx* ctx)
- {
- if (ctx == NULL) {
- return;
- }
- #ifdef USE_WINDOWS_API
- if (ctx->hFind != INVALID_HANDLE_VALUE) {
- FindClose(ctx->hFind);
- ctx->hFind = INVALID_HANDLE_VALUE;
- }
- #elif defined(INTIME_RTOS)
- IntimeFindClose(&ctx->FindFileData);
- #elif defined(WOLFSSL_ZEPHYR)
- if (ctx->dirp) {
- fs_closedir(ctx->dirp);
- ctx->dirp = NULL;
- }
- #elif defined(WOLFSSL_TELIT_M2MB)
- if (ctx->dir) {
- m2mb_fs_closedir(ctx->dir);
- ctx->dir = NULL;
- }
- #else
- if (ctx->dir) {
- if (closedir(ctx->dir) < 0)
- WOLFSSL_MSG("closedir() failed");
- ctx->dir = NULL;
- }
- #endif
- }
- #endif /* !NO_WOLFSSL_DIR */
- #endif /* !NO_FILESYSTEM */
- #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_ZEPHYR)
- XFILE z_fs_open(const char* filename, const char* mode)
- {
- XFILE file;
- fs_mode_t flags = 0;
- if (mode == NULL)
- return NULL;
- /* Parse mode */
- switch (*mode++) {
- case 'r':
- flags |= FS_O_READ;
- break;
- case 'w':
- flags |= FS_O_WRITE|FS_O_CREATE;
- break;
- case 'a':
- flags |= FS_O_APPEND|FS_O_CREATE;
- break;
- default:
- return NULL;
- }
- /* Ignore binary flag */
- if (*mode == 'b')
- mode++;
- if (*mode == '+') {
- flags |= FS_O_READ;
- /* Don't add write flag if already appending */
- if (!(flags & FS_O_APPEND))
- flags |= FS_O_RDWR;
- }
- /* Ignore binary flag */
- if (*mode == 'b')
- mode++;
- /* Incorrect mode string */
- if (*mode != '\0')
- return NULL;
- file = (XFILE)XMALLOC(sizeof(*file), NULL, DYNAMIC_TYPE_FILE);
- if (file != NULL) {
- fs_file_t_init(file);
- if (fs_open(file, filename, flags) != 0) {
- XFREE(file, NULL, DYNAMIC_TYPE_FILE);
- file = NULL;
- }
- }
- return file;
- }
- int z_fs_close(XFILE file)
- {
- int ret;
- if (file == NULL)
- return -1;
- ret = (fs_close(file) == 0) ? 0 : -1;
- XFREE(file, NULL, DYNAMIC_TYPE_FILE);
- return ret;
- }
- #endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */
- #if !defined(WOLFSSL_USER_MUTEX)
- wolfSSL_Mutex* wc_InitAndAllocMutex(void)
- {
- wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
- DYNAMIC_TYPE_MUTEX);
- if (m != NULL) {
- if (wc_InitMutex(m) != 0) {
- WOLFSSL_MSG("Init Mutex failed");
- XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
- m = NULL;
- }
- }
- else {
- WOLFSSL_MSG("Memory error with Mutex allocation");
- }
- return m;
- }
- #endif
- #ifdef USE_WOLF_STRTOK
- /* String token (delim) search. If str is null use nextp. */
- char* wc_strtok(char *str, const char *delim, char **nextp)
- {
- char* ret;
- int i, j;
- /* Use next if str is NULL */
- if (str == NULL && nextp)
- str = *nextp;
- /* verify str input */
- if (str == NULL || *str == '\0')
- return NULL;
- /* match on entire delim */
- for (i = 0; str[i]; i++) {
- for (j = 0; delim[j]; j++) {
- if (delim[j] == str[i])
- break;
- }
- if (!delim[j])
- break;
- }
- str += i;
- /* if end of string, not found so return NULL */
- if (*str == '\0')
- return NULL;
- ret = str;
- /* match on first delim */
- for (i = 0; str[i]; i++) {
- for (j = 0; delim[j]; j++) {
- if (delim[j] == str[i])
- break;
- }
- if (delim[j] == str[i])
- break;
- }
- str += i;
- /* null terminate found string */
- if (*str)
- *str++ = '\0';
- /* return pointer to next */
- if (nextp)
- *nextp = str;
- return ret;
- }
- #endif /* USE_WOLF_STRTOK */
- #ifdef USE_WOLF_STRSEP
- char* wc_strsep(char **stringp, const char *delim)
- {
- char *s, *tok;
- const char *spanp;
- /* null check */
- if (stringp == NULL || *stringp == NULL)
- return NULL;
- s = *stringp;
- for (tok = s; *tok; ++tok) {
- for (spanp = delim; *spanp; ++spanp) {
- /* found delimiter */
- if (*tok == *spanp) {
- *tok = '\0'; /* replace delim with null term */
- *stringp = tok + 1; /* return past delim */
- return s;
- }
- }
- }
- *stringp = NULL;
- return s;
- }
- #endif /* USE_WOLF_STRSEP */
- #ifdef USE_WOLF_STRLCPY
- size_t wc_strlcpy(char *dst, const char *src, size_t dstSize)
- {
- size_t i;
- if (!dstSize)
- return 0;
- /* Always have to leave a space for NULL */
- for (i = 0; i < (dstSize - 1) && *src != '\0'; i++) {
- *dst++ = *src++;
- }
- *dst = '\0';
- return i; /* return length without NULL */
- }
- #endif /* USE_WOLF_STRLCPY */
- #ifdef USE_WOLF_STRLCAT
- size_t wc_strlcat(char *dst, const char *src, size_t dstSize)
- {
- size_t dstLen;
- if (!dstSize)
- return 0;
- dstLen = XSTRLEN(dst);
- if (dstSize < dstLen)
- return dstLen + XSTRLEN(src);
- return dstLen + wc_strlcpy(dst + dstLen, src, dstSize - dstLen);
- }
- #endif /* USE_WOLF_STRLCAT */
- #ifdef USE_WOLF_STRCASECMP
- int wc_strcasecmp(const char *s1, const char *s2)
- {
- char c1, c2;
- for (;;++s1, ++s2) {
- c1 = *s1;
- if ((c1 >= 'a') && (c1 <= 'z'))
- c1 -= ('a' - 'A');
- c2 = *s2;
- if ((c2 >= 'a') && (c2 <= 'z'))
- c2 -= ('a' - 'A');
- if ((c1 != c2) || (c1 == 0))
- break;
- }
- return (c1 - c2);
- }
- #endif /* USE_WOLF_STRCASECMP */
- #ifdef USE_WOLF_STRNCASECMP
- int wc_strncasecmp(const char *s1, const char *s2, size_t n)
- {
- char c1, c2;
- for (c1 = 0, c2 = 0; n > 0; --n, ++s1, ++s2) {
- c1 = *s1;
- if ((c1 >= 'a') && (c1 <= 'z'))
- c1 -= ('a' - 'A');
- c2 = *s2;
- if ((c2 >= 'a') && (c2 <= 'z'))
- c2 -= ('a' - 'A');
- if ((c1 != c2) || (c1 == 0))
- break;
- }
- return (c1 - c2);
- }
- #endif /* USE_WOLF_STRNCASECMP */
- #ifdef WOLFSSL_ATOMIC_OPS
- #ifdef HAVE_C___ATOMIC
- /* Atomic ops using standard C lib */
- #ifdef __cplusplus
- /* C++ using direct calls to compiler built-in functions */
- void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
- {
- *c = i;
- }
- int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
- {
- return __atomic_fetch_add(c, i, __ATOMIC_RELAXED);
- }
- int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
- {
- return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED);
- }
- #else
- /* Default C Implementation */
- void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
- {
- atomic_init(c, i);
- }
- int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
- {
- return atomic_fetch_add_explicit(c, i, memory_order_relaxed);
- }
- int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
- {
- return atomic_fetch_sub_explicit(c, i, memory_order_relaxed);
- }
- #endif /* __cplusplus */
- #elif defined(_MSC_VER)
- /* Default C Implementation */
- void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
- {
- *c = i;
- }
- int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
- {
- return (int)_InterlockedExchangeAdd(c, (long)i);
- }
- int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
- {
- return (int)_InterlockedExchangeAdd(c, (long)-i);
- }
- #endif
- #endif /* WOLFSSL_ATOMIC_OPS */
- #if !defined(SINGLE_THREADED) && !defined(WOLFSSL_ATOMIC_OPS)
- void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err)
- {
- int ret = wc_InitMutex(&ref->mutex);
- if (ret != 0) {
- WOLFSSL_MSG("Failed to create mutex for reference counting!");
- }
- ref->count = 1;
- *err = ret;
- }
- void wolfSSL_RefFree(wolfSSL_Ref* ref)
- {
- if (wc_FreeMutex(&ref->mutex) != 0) {
- WOLFSSL_MSG("Failed to free mutex of reference counting!");
- }
- }
- void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err)
- {
- int ret = wc_LockMutex(&ref->mutex);
- if (ret != 0) {
- WOLFSSL_MSG("Failed to lock mutex for reference increment!");
- }
- else {
- ref->count++;
- wc_UnLockMutex(&ref->mutex);
- }
- *err = ret;
- }
- void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err)
- {
- int ret = wc_LockMutex(&ref->mutex);
- if (ret != 0) {
- WOLFSSL_MSG("Failed to lock mutex for reference decrement!");
- /* Can't say count is zero. */
- *isZero = 0;
- }
- else {
- if (ref->count > 0) {
- ref->count--;
- }
- *isZero = (ref->count == 0);
- wc_UnLockMutex(&ref->mutex);
- }
- *err = ret;
- }
- #endif
- #if WOLFSSL_CRYPT_HW_MUTEX
- /* Mutex for protection of cryptography hardware */
- static wolfSSL_Mutex wcCryptHwMutex;
- static int wcCryptHwMutexInit = 0;
- int wolfSSL_CryptHwMutexInit(void)
- {
- int ret = 0;
- if (wcCryptHwMutexInit == 0) {
- ret = wc_InitMutex(&wcCryptHwMutex);
- if (ret == 0) {
- wcCryptHwMutexInit = 1;
- }
- }
- return ret;
- }
- int wolfSSL_CryptHwMutexLock(void)
- {
- int ret = BAD_MUTEX_E;
- /* Make sure HW Mutex has been initialized */
- ret = wolfSSL_CryptHwMutexInit();
- if (ret == 0) {
- ret = wc_LockMutex(&wcCryptHwMutex);
- }
- return ret;
- }
- int wolfSSL_CryptHwMutexUnLock(void)
- {
- int ret = BAD_MUTEX_E;
- if (wcCryptHwMutexInit) {
- ret = wc_UnLockMutex(&wcCryptHwMutex);
- }
- return ret;
- }
- #endif /* WOLFSSL_CRYPT_HW_MUTEX */
- /* ---------------------------------------------------------------------------*/
- /* Mutex Ports */
- /* ---------------------------------------------------------------------------*/
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- static mutex_cb* compat_mutex_cb = NULL;
- /* Function that locks or unlocks a mutex based on the flag passed in.
- *
- * flag lock or unlock i.e. CRYPTO_LOCK
- * type the type of lock to unlock or lock
- * file name of the file calling
- * line the line number from file calling
- */
- int wc_LockMutex_ex(int flag, int type, const char* file, int line)
- {
- if (compat_mutex_cb != NULL) {
- compat_mutex_cb(flag, type, file, line);
- return 0;
- }
- else {
- WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
- return BAD_STATE_E;
- }
- }
- /* Set the callback function to use for locking/unlocking mutex
- *
- * cb callback function to use
- */
- int wc_SetMutexCb(mutex_cb* cb)
- {
- compat_mutex_cb = cb;
- return 0;
- }
- /* Gets the current callback function in use for locking/unlocking mutex
- *
- */
- mutex_cb* wc_GetMutexCb(void)
- {
- return compat_mutex_cb;
- }
- #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
- #ifdef SINGLE_THREADED
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- (void)m;
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex *m)
- {
- (void)m;
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex *m)
- {
- (void)m;
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex *m)
- {
- (void)m;
- return 0;
- }
- #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
- defined(FREESCALE_FREE_RTOS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int iReturn;
- *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
- if( *m != NULL )
- iReturn = 0;
- else
- iReturn = BAD_MUTEX_E;
- return iReturn;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- vSemaphoreDelete( *m );
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- /* Assume an infinite block, or should there be zero block? */
- xSemaphoreTake( *m, portMAX_DELAY );
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- xSemaphoreGive( *m );
- return 0;
- }
- #elif defined(RTTHREAD)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int iReturn;
- *m = ( wolfSSL_Mutex ) rt_mutex_create("mutex",RT_IPC_FLAG_FIFO);
- if( *m != NULL )
- iReturn = 0;
- else
- iReturn = BAD_MUTEX_E;
- return iReturn;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- rt_mutex_delete( *m );
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- /* Assume an infinite block, or should there be zero block? */
- return rt_mutex_take( *m, RT_WAITING_FOREVER );
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- return rt_mutex_release( *m );
- }
- #elif defined(WOLFSSL_SAFERTOS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
- if (m->mutex == NULL)
- return BAD_MUTEX_E;
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- (void)m;
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- /* Assume an infinite block */
- xSemaphoreTake(m->mutex, portMAX_DELAY);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- xSemaphoreGive(m->mutex);
- return 0;
- }
- #elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- InitializeCriticalSection(m);
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- DeleteCriticalSection(m);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- EnterCriticalSection(m);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- LeaveCriticalSection(m);
- return 0;
- }
- #elif defined(MAXQ10XX_MUTEX)
- static pthread_mutex_t *wcCryptHwSharedMutexPtr;
- static pthread_once_t key_once_own_hw_mutex = PTHREAD_ONCE_INIT;
- static pthread_key_t key_own_hw_mutex;
- static void destruct_key(void *buf)
- {
- if (buf != NULL) {
- free(buf);
- }
- }
- static void make_key_own_hw_mutex(void)
- {
- (void)pthread_key_create(&key_own_hw_mutex, destruct_key);
- }
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int created = 0;
- void *addr = NULL;
- if (m != &wcCryptHwMutex) {
- if (pthread_mutex_init(m, 0) == 0) {
- return 0;
- }
- return BAD_MUTEX_E;
- }
- /* try to open mutex memory */
- int shm_fd = shm_open("/maxq-mutex", O_RDWR, 0666);
- if (shm_fd < 0) {
- /* create mutex memory */
- shm_fd = shm_open("/maxq-mutex", O_RDWR | O_CREAT | O_EXCL, 0666);
- created = 1;
- }
- if (shm_fd < 0) {
- WOLFSSL_MSG("wc_InitMutex: shm_open() failed");
- return BAD_MUTEX_E;
- }
- if (ftruncate(shm_fd, sizeof(pthread_mutex_t))) {
- WOLFSSL_MSG("wc_InitMutex: ftruncate() failed");
- return BAD_MUTEX_E;
- }
- addr = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE,
- MAP_SHARED, shm_fd, 0);
- if (addr == MAP_FAILED) {
- WOLFSSL_MSG("wc_InitMutex: mmap() failed");
- return BAD_MUTEX_E;
- }
- wcCryptHwSharedMutexPtr = (pthread_mutex_t *)addr;
- if (close(shm_fd)) {
- WOLFSSL_MSG("wc_InitMutex: close() failed");
- return BAD_MUTEX_E;
- }
- if (created) {
- /* initialize mutex */
- pthread_mutexattr_t attr;
- if (pthread_mutexattr_init(&attr)) {
- WOLFSSL_MSG("wc_InitMutex: pthread_mutexattr_init() failed");
- return BAD_MUTEX_E;
- }
- if (pthread_mutexattr_setpshared(&attr,
- PTHREAD_PROCESS_SHARED)) {
- WOLFSSL_MSG(
- "wc_InitMutex: pthread_mutexattr_setpshared() failed");
- return BAD_MUTEX_E;
- }
- if (pthread_mutex_init(wcCryptHwSharedMutexPtr, &attr)) {
- WOLFSSL_MSG("wc_InitMutex: pthread_mutex_init() failed");
- return BAD_MUTEX_E;
- }
- }
- if (pthread_once(&key_once_own_hw_mutex, make_key_own_hw_mutex)) {
- WOLFSSL_MSG("wc_InitMutex: pthread_once() failed");
- return BAD_MUTEX_E;
- }
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- void *key_ptr = NULL;
- if (m != &wcCryptHwMutex) {
- if (pthread_mutex_destroy(m) == 0) {
- return 0;
- }
- return BAD_MUTEX_E;
- }
- if (wcCryptHwSharedMutexPtr) {
- if (munmap((void *)wcCryptHwSharedMutexPtr,
- sizeof(pthread_mutex_t))) {
- WOLFSSL_MSG("wc_FreeMutex: munmap() failed");
- return BAD_MUTEX_E;
- }
- wcCryptHwSharedMutexPtr = NULL;
- }
- key_ptr = pthread_getspecific(key_own_hw_mutex);
- if (key_ptr) {
- *((int *)key_ptr) = 0;
- }
- return 0;
- }
- static int maxq_LockMutex(wolfSSL_Mutex* m, int trylock)
- {
- void *key_ptr = NULL;
- int ret = 0;
- if (m != &wcCryptHwMutex) {
- if (pthread_mutex_lock(m) == 0) {
- return 0;
- }
- return BAD_MUTEX_E;
- }
- if (wcCryptHwSharedMutexPtr == NULL) {
- return BAD_MUTEX_E;
- }
- key_ptr = pthread_getspecific(key_own_hw_mutex);
- if (key_ptr == NULL) {
- key_ptr = malloc(sizeof(int));
- if (key_ptr == NULL) {
- return MEMORY_E;
- }
- memset(key_ptr, 0, sizeof(int));
- if (pthread_setspecific(key_own_hw_mutex, key_ptr)) {
- return THREAD_STORE_SET_E;
- }
- }
- else {
- if ((trylock == 0) && (*((int *)key_ptr) > 0)) {
- *((int *)key_ptr) = *((int *)key_ptr) + 1;
- return 0;
- }
- }
- if (trylock) {
- ret = pthread_mutex_trylock(wcCryptHwSharedMutexPtr);
- }
- else {
- ret = pthread_mutex_lock(wcCryptHwSharedMutexPtr);
- }
- if (ret != 0) {
- return BAD_MUTEX_E;
- }
- *((int *)key_ptr) = 1;
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- return maxq_LockMutex(m, 0);
- }
- int maxq_CryptHwMutexTryLock()
- {
- int ret = BAD_MUTEX_E;
- /* Make sure HW Mutex has been initialized */
- ret = wolfSSL_CryptHwMutexInit();
- if (ret == 0) {
- ret = maxq_LockMutex(&wcCryptHwMutex, 1);
- }
- return ret;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- void *key_ptr = NULL;
- if (m != &wcCryptHwMutex) {
- if (pthread_mutex_unlock(m) == 0) {
- return 0;
- }
- return BAD_MUTEX_E;
- }
- if (wcCryptHwSharedMutexPtr == NULL) {
- return BAD_MUTEX_E;
- }
- key_ptr = pthread_getspecific(key_own_hw_mutex);
- if (key_ptr) {
- if (*((int *)key_ptr) > 0) {
- *((int *)key_ptr) = *((int *)key_ptr) - 1;
- if (*((int *)key_ptr) > 0) {
- return 0;
- }
- }
- }
- if (pthread_mutex_unlock(wcCryptHwSharedMutexPtr) != 0) {
- return BAD_MUTEX_E;
- }
- return 0;
- }
- #elif defined(WOLFSSL_PTHREADS)
- #ifdef WOLFSSL_USE_RWLOCK
- int wc_InitRwLock(wolfSSL_RwLock* m)
- {
- if (pthread_rwlock_init(m, NULL) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeRwLock(wolfSSL_RwLock* m)
- {
- if (pthread_rwlock_destroy(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockRwLock_Wr(wolfSSL_RwLock* m)
- {
- if (pthread_rwlock_wrlock(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockRwLock_Rd(wolfSSL_RwLock* m)
- {
- if (pthread_rwlock_rdlock(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockRwLock(wolfSSL_RwLock* m)
- {
- if (pthread_rwlock_unlock(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- #endif
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (pthread_mutex_init(m, NULL) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- if (pthread_mutex_destroy(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- if (pthread_mutex_lock(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- if (pthread_mutex_unlock(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- #elif defined(WOLFSSL_LINUXKM)
- /* Linux kernel mutex routines are voids, alas. */
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- mutex_init(m);
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- mutex_destroy(m);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- mutex_lock(m);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- mutex_unlock(m);
- return 0;
- }
- #elif defined(WOLFSSL_VXWORKS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (m) {
- if ((*m = semMCreate(0)) != SEM_ID_NULL)
- return 0;
- }
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- if (m) {
- if (semDelete(*m) == OK)
- return 0;
- }
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- if (m) {
- if (semTake(*m, WAIT_FOREVER) == OK)
- return 0;
- }
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- if (m) {
- if (semGive(*m) == OK)
- return 0;
- }
- return BAD_MUTEX_E;
- }
- #elif defined(THREADX)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- if (tx_mutex_delete(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- if (tx_mutex_put(m) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- #elif defined(WOLFSSL_DEOS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- mutexStatus mutStat;
- /*
- The empty string "" denotes an anonymous mutex, so objects do not cause name collisions.
- `protectWolfSSLTemp` in an XML configuration element template describing a mutex.
- */
- if (m) {
- mutStat = createMutex("", "protectWolfSSLTemp", m);
- if (mutStat == mutexSuccess)
- return 0;
- else{
- WOLFSSL_MSG("wc_InitMutex failed");
- return mutStat;
- }
- }
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- mutexStatus mutStat;
- if (m) {
- mutStat = deleteMutex(*m);
- if (mutStat == mutexSuccess)
- return 0;
- else{
- WOLFSSL_MSG("wc_FreeMutex failed");
- return mutStat;
- }
- }
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- mutexStatus mutStat;
- if (m) {
- mutStat = lockMutex(*m);
- if (mutStat == mutexSuccess)
- return 0;
- else{
- WOLFSSL_MSG("wc_LockMutex failed");
- return mutStat;
- }
- }
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- mutexStatus mutStat;
- if (m) {
- mutStat = unlockMutex(*m);
- if (mutStat== mutexSuccess)
- return 0;
- else{
- WOLFSSL_MSG("wc_UnLockMutex failed");
- return mutStat;
- }
- }
- return BAD_MUTEX_E;
- }
- #elif defined(MICRIUM)
- #if (OS_VERSION < 50000)
- #define MICRIUM_ERR_TYPE OS_ERR
- #define MICRIUM_ERR_NONE OS_ERR_NONE
- #define MICRIUM_ERR_CODE(err) err
- #else
- #define MICRIUM_ERR_TYPE RTOS_ERR
- #define MICRIUM_ERR_NONE RTOS_ERR_NONE
- #define MICRIUM_ERR_CODE(err) RTOS_ERR_CODE_GET(err)
- #endif
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- MICRIUM_ERR_TYPE err;
- OSMutexCreate(m, "wolfSSL Mutex", &err);
- if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
- MICRIUM_ERR_TYPE err;
- OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
- if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
- return 0;
- else
- return BAD_MUTEX_E;
- #else
- (void)m;
- return 0;
- #endif
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- MICRIUM_ERR_TYPE err;
- OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
- if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- MICRIUM_ERR_TYPE err;
- OSMutexPost(m, OS_OPT_POST_NONE, &err);
- if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- #elif defined(EBSNET)
- #if (defined(RTPLATFORM) && (RTPLATFORM != 0))
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
- return BAD_MUTEX_E;
- else
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- rtp_sig_mutex_free(*m);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- rtp_sig_mutex_release(*m);
- return 0;
- }
- int ebsnet_fseek(int a, long b, int c)
- {
- int retval;
- retval = vf_lseek(a, b, c);
- if (retval > 0)
- retval = 0;
- else
- retval = -1;
- return(retval);
- }
- #else
- static int rtip_semaphore_build(wolfSSL_Mutex *m)
- {
- KS_SEMAPHORE_BUILD(m)
- return(RTP_TRUE);
- }
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (rtip_semaphore_build(m) == RTP_FALSE)
- return BAD_MUTEX_E;
- else
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- KS_SEMAPHORE_FREE(*m);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- if (KS_SEMAPHORE_GET(*m))
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- KS_SEMAPHORE_GIVE(*m);
- return 0;
- }
- #endif
- int ebsnet_fseek(int a, long b, int c)
- {
- int retval;
- retval = (int)vf_lseek(a, b, c);
- if (retval > 0)
- retval = 0;
- else
- retval = -1;
- return(retval);
- }
- int strcasecmp(const char *s1, const char *s2)
- {
- while (rtp_tolower(*s1) == rtp_tolower(*s2)) {
- if (*s1 == '\0' || *s2 == '\0')
- break;
- s1++;
- s2++;
- }
- return rtp_tolower(*(unsigned char *) s1) -
- rtp_tolower(*(unsigned char *) s2);
- }
- #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (_mutex_init(m, NULL) == MQX_EOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- if (_mutex_destroy(m) == MQX_EOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- if (_mutex_lock(m) == MQX_EOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- if (_mutex_unlock(m) == MQX_EOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- #elif defined(WOLFSSL_TIRTOS)
- #include <xdc/runtime/Error.h>
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- Semaphore_Params params;
- Error_Block eb;
- Error_init(&eb);
- Semaphore_Params_init(¶ms);
- params.mode = Semaphore_Mode_BINARY;
- *m = Semaphore_create(1, ¶ms, &eb);
- if (Error_check(&eb)) {
- Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
- NULL);
- return BAD_MUTEX_E;
- }
- else
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- Semaphore_delete(m);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- Semaphore_pend(*m, BIOS_WAIT_FOREVER);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- Semaphore_post(*m);
- return 0;
- }
- #elif defined(WOLFSSL_uITRON4)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int iReturn;
- m->sem.sematr = TA_TFIFO;
- m->sem.isemcnt = 1;
- m->sem.maxsem = 1;
- m->sem.name = NULL;
- m->id = acre_sem(&m->sem);
- if( m->id != E_OK )
- iReturn = 0;
- else
- iReturn = BAD_MUTEX_E;
- return iReturn;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- del_sem( m->id );
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- wai_sem(m->id);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- sig_sem(m->id);
- return 0;
- }
- /**** uITRON malloc/free ***/
- static ID ID_wolfssl_MPOOL = 0;
- static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
- int uITRON4_minit(size_t poolsz) {
- ER ercd;
- wolfssl_MPOOL.mplsz = poolsz;
- ercd = acre_mpl(&wolfssl_MPOOL);
- if (ercd > 0) {
- ID_wolfssl_MPOOL = ercd;
- return 0;
- } else {
- return -1;
- }
- }
- void *uITRON4_malloc(size_t sz) {
- ER ercd;
- void *p = NULL;
- ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
- if (ercd == E_OK) {
- return p;
- } else {
- return 0;
- }
- }
- void *uITRON4_realloc(void *p, size_t sz) {
- ER ercd;
- void *newp = NULL;
- if(p) {
- ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
- if ((ercd == E_OK) && (newp != NULL)) {
- XMEMCPY(newp, p, sz);
- ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
- if (ercd == E_OK) {
- return newp;
- }
- }
- }
- return 0;
- }
- void uITRON4_free(void *p) {
- ER ercd;
- ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
- if (ercd == E_OK) {
- return;
- } else {
- return;
- }
- }
- #elif defined(WOLFSSL_uTKERNEL2)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int iReturn;
- m->sem.sematr = TA_TFIFO;
- m->sem.isemcnt = 1;
- m->sem.maxsem = 1;
- m->id = tk_cre_sem(&m->sem);
- if( m->id != NULL )
- iReturn = 0;
- else
- iReturn = BAD_MUTEX_E;
- return iReturn;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- tk_del_sem(m->id);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- tk_wai_sem(m->id, 1, TMO_FEVR);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- tk_sig_sem(m->id, 1);
- return 0;
- }
- /**** uT-Kernel malloc/free ***/
- static ID ID_wolfssl_MPOOL = 0;
- static T_CMPL wolfssl_MPOOL = {
- NULL, /* Extended information */
- TA_TFIFO, /* Memory pool attribute */
- 0, /* Size of whole memory pool (byte) */
- "wolfSSL" /* Object name (max 8-char) */
- };
- int uTKernel_init_mpool(unsigned int sz) {
- ER ercd;
- wolfssl_MPOOL.mplsz = sz;
- ercd = tk_cre_mpl(&wolfssl_MPOOL);
- if (ercd > 0) {
- ID_wolfssl_MPOOL = ercd;
- return 0;
- } else {
- return (int)ercd;
- }
- }
- void *uTKernel_malloc(unsigned int sz) {
- ER ercd;
- void *p = NULL;
- ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
- if (ercd == E_OK) {
- return p;
- } else {
- return 0;
- }
- }
- void *uTKernel_realloc(void *p, unsigned int sz) {
- ER ercd;
- void *newp = NULL;
- if (p) {
- ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
- if ((ercd == E_OK) && (newp != NULL)) {
- XMEMCPY(newp, p, sz);
- ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
- if (ercd == E_OK) {
- return newp;
- }
- }
- }
- return 0;
- }
- void uTKernel_free(void *p) {
- ER ercd;
- ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
- if (ercd == E_OK) {
- return;
- } else {
- return;
- }
- }
- #elif defined (WOLFSSL_FROSTED)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- *m = mutex_init();
- if (*m)
- return 0;
- else
- return -1;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- mutex_destroy(*m);
- return(0);
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- mutex_lock(*m);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- mutex_unlock(*m);
- return 0;
- }
- #elif defined(WOLFSSL_CMSIS_RTOS)
- #define CMSIS_NMUTEX 10
- osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
- osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
- osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
- osMutexDef(wolfSSL_mt9);
- static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
- osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
- osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
- osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
- static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int i;
- for (i=0; i<CMSIS_NMUTEX; i++) {
- if(CMSIS_mutexID[i] == 0) {
- CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
- (*m) = CMSIS_mutexID[i];
- return 0;
- }
- }
- return -1;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- int i;
- osMutexDelete (*m);
- for (i=0; i<CMSIS_NMUTEX; i++) {
- if(CMSIS_mutexID[i] == (*m)) {
- CMSIS_mutexID[i] = 0;
- return(0);
- }
- }
- return(-1);
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- osMutexWait(*m, osWaitForever);
- return(0);
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- osMutexRelease (*m);
- return 0;
- }
- #elif defined(WOLFSSL_CMSIS_RTOSv2)
- int wc_InitMutex(wolfSSL_Mutex *m)
- {
- static const osMutexAttr_t attr = {
- "wolfSSL_mutex", osMutexRecursive, NULL, 0};
- if ((*m = osMutexNew(&attr)) != NULL)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex *m)
- {
- if (osMutexDelete(*m) == osOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex *m)
- {
- if (osMutexAcquire(*m, osWaitForever) == osOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex *m)
- {
- if (osMutexRelease(*m) == osOK)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- #elif defined(WOLFSSL_MDK_ARM)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- os_mut_init (m);
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- return(0);
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- os_mut_wait (m, 0xffff);
- return(0);
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- os_mut_release (m);
- return 0;
- }
- #elif defined(INTIME_RTOS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int ret = 0;
- if (m == NULL)
- return BAD_FUNC_ARG;
- *m = CreateRtSemaphore(
- 1, /* initial unit count */
- 1, /* maximum unit count */
- PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
- );
- if (*m == BAD_RTHANDLE) {
- ret = GetLastRtError();
- if (ret != E_OK)
- ret = BAD_MUTEX_E;
- }
- return ret;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- int ret = 0;
- BOOLEAN del;
- if (m == NULL)
- return BAD_FUNC_ARG;
- del = DeleteRtSemaphore(
- *m /* handle for RT semaphore */
- );
- if (del != TRUE)
- ret = BAD_MUTEX_E;
- return ret;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- int ret = 0;
- DWORD lck;
- if (m == NULL)
- return BAD_FUNC_ARG;
- lck = WaitForRtSemaphore(
- *m, /* handle for RT semaphore */
- 1, /* number of units to wait for */
- WAIT_FOREVER /* number of milliseconds to wait for units */
- );
- if (lck == WAIT_FAILED) {
- ret = GetLastRtError();
- if (ret != E_OK)
- ret = BAD_MUTEX_E;
- }
- return ret;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- int ret = 0;
- BOOLEAN rel;
- if (m == NULL)
- return BAD_FUNC_ARG;
- rel = ReleaseRtSemaphore(
- *m, /* handle for RT semaphore */
- 1 /* number of units to release to semaphore */
- );
- if (rel != TRUE)
- ret = BAD_MUTEX_E;
- return ret;
- }
- #elif defined(WOLFSSL_NUCLEUS_1_2)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- /* Call the Nucleus function to create the semaphore */
- if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
- NU_PRIORITY) == NU_SUCCESS) {
- return 0;
- }
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- if (NU_Delete_Semaphore(m) == NU_SUCCESS)
- return 0;
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- /* passing suspend task option */
- if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
- return 0;
- return BAD_MUTEX_E;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- if (NU_Release_Semaphore(m) == NU_SUCCESS)
- return 0;
- return BAD_MUTEX_E;
- }
- #elif defined(WOLFSSL_ZEPHYR)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- k_mutex_init(m);
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- int ret = 0;
- if (k_mutex_lock(m, K_FOREVER) != 0)
- ret = BAD_MUTEX_E;
- return ret;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- k_mutex_unlock(m);
- return 0;
- }
- #elif defined(WOLFSSL_TELIT_M2MB)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- M2MB_OS_RESULT_E osRes;
- M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle;
- UINT32 inheritVal = 1;
- osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle,
- CMDS_ARGS(
- M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL,
- M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx",
- M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal
- )
- );
- if (osRes != M2MB_OS_SUCCESS) {
- return BAD_MUTEX_E;
- }
- osRes = m2mb_os_mtx_init(m, &mtxAttrHandle);
- if (osRes != M2MB_OS_SUCCESS) {
- return BAD_MUTEX_E;
- }
- return 0;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- M2MB_OS_RESULT_E osRes;
- if (m == NULL)
- return BAD_MUTEX_E;
- osRes = m2mb_os_mtx_deinit(*m);
- if (osRes != M2MB_OS_SUCCESS) {
- return BAD_MUTEX_E;
- }
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- M2MB_OS_RESULT_E osRes;
- if (m == NULL)
- return BAD_MUTEX_E;
- osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER);
- if (osRes != M2MB_OS_SUCCESS) {
- return BAD_MUTEX_E;
- }
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- M2MB_OS_RESULT_E osRes;
- if (m == NULL)
- return BAD_MUTEX_E;
- osRes = m2mb_os_mtx_put(*m);
- if (osRes != M2MB_OS_SUCCESS) {
- return BAD_MUTEX_E;
- }
- return 0;
- }
- #elif defined(WOLFSSL_EMBOS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- int ret;
- OS_MUTEX_Create((OS_MUTEX*) m);
- if (m != NULL)
- ret = 0;
- else
- ret = BAD_MUTEX_E;
- return ret;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- OS_MUTEX_Delete((OS_MUTEX*) m);
- return 0;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- OS_MUTEX_LockBlocked((OS_MUTEX*) m);
- return 0;
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- OS_MUTEX_Unlock((OS_MUTEX*) m);
- return 0;
- }
- #elif defined(NETOS)
- int wc_InitMutex(wolfSSL_Mutex* m)
- {
- if (tx_mutex_create(&ready->mutex, "wolfSSL Lock", TX_INHERIT)
- == TX_SUCCESS)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_FreeMutex(wolfSSL_Mutex* m)
- {
- if (tx_mutex_delete(&ready->mutex) == TX_SUCCESS)
- return 0;
- else
- return BAD_MUTEX_E;
- }
- int wc_LockMutex(wolfSSL_Mutex* m)
- {
- }
- int wc_UnLockMutex(wolfSSL_Mutex* m)
- {
- }
- #elif defined(WOLFSSL_USER_MUTEX)
- /* Use user own mutex */
- /*
- int wc_InitMutex(wolfSSL_Mutex* m) { ... }
- int wc_FreeMutex(wolfSSL_Mutex *m) { ... }
- int wc_LockMutex(wolfSSL_Mutex *m) { ... }
- int wc_UnLockMutex(wolfSSL_Mutex *m) { ... }
- */
- #else
- #warning No mutex handling defined
- #endif
- #if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED)
- int wc_InitRwLock(wolfSSL_RwLock* m)
- {
- return wc_InitMutex(m);
- }
- int wc_FreeRwLock(wolfSSL_RwLock* m)
- {
- return wc_FreeMutex(m);
- }
- int wc_LockRwLock_Wr(wolfSSL_RwLock* m)
- {
- return wc_LockMutex(m);
- }
- int wc_LockRwLock_Rd(wolfSSL_RwLock* m)
- {
- return wc_LockMutex(m);
- }
- int wc_UnLockRwLock(wolfSSL_RwLock* m)
- {
- return wc_UnLockMutex(m);
- }
- #endif
- #ifndef NO_ASN_TIME
- #if defined(_WIN32_WCE)
- time_t windows_time(time_t* timer)
- {
- SYSTEMTIME sysTime;
- FILETIME fTime;
- ULARGE_INTEGER intTime;
- GetSystemTime(&sysTime);
- SystemTimeToFileTime(&sysTime, &fTime);
- XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
- /* subtract EPOCH */
- intTime.QuadPart -= 0x19db1ded53e8000;
- /* to secs */
- intTime.QuadPart /= 10000000;
- if (timer != NULL)
- *timer = (time_t)intTime.QuadPart;
- return (time_t)intTime.QuadPart;
- }
- #endif /* _WIN32_WCE */
- #if defined(WOLFSSL_APACHE_MYNEWT)
- #include "os/os_time.h"
- time_t mynewt_time(time_t* timer)
- {
- time_t now;
- struct os_timeval tv;
- os_gettimeofday(&tv, NULL);
- now = (time_t)tv.tv_sec;
- if(timer != NULL) {
- *timer = now;
- }
- return now;
- }
- #endif /* WOLFSSL_APACHE_MYNEWT */
- #if defined(WOLFSSL_GMTIME)
- struct tm* gmtime_r(const time_t* timer, struct tm *ret)
- {
- #define YEAR0 1900
- #define EPOCH_YEAR 1970
- #define SECS_DAY (24L * 60L * 60L)
- #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
- #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
- static const int _ytab[2][12] =
- {
- {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
- {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
- };
- time_t secs = *timer;
- unsigned long dayclock, dayno;
- int year = EPOCH_YEAR;
- dayclock = (unsigned long)secs % SECS_DAY;
- dayno = (unsigned long)secs / SECS_DAY;
- ret->tm_sec = (int) dayclock % 60;
- ret->tm_min = (int)(dayclock % 3600) / 60;
- ret->tm_hour = (int) dayclock / 3600;
- ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
- while(dayno >= (unsigned long)YEARSIZE(year)) {
- dayno -= YEARSIZE(year);
- year++;
- }
- ret->tm_year = year - YEAR0;
- ret->tm_yday = (int)dayno;
- ret->tm_mon = 0;
- while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
- dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
- ret->tm_mon++;
- }
- ret->tm_mday = (int)++dayno;
- #ifndef WOLFSSL_LINUXKM
- ret->tm_isdst = 0;
- #endif
- return ret;
- }
- struct tm* gmtime(const time_t* timer) {
- static struct tm st_time;
- return gmtime_r(timer, &st_time);
- }
- #endif /* WOLFSSL_GMTIME */
- #if defined(HAVE_RTP_SYS)
- #define YEAR0 1900
- struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
- {
- static struct tm st_time;
- struct tm* ret = &st_time;
- DC_RTC_CALENDAR cal;
- dc_rtc_time_get(&cal, TRUE);
- ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
- ret->tm_mon = cal.month - 1; /* gm starts at 0 */
- ret->tm_mday = cal.day;
- ret->tm_hour = cal.hour;
- ret->tm_min = cal.minute;
- ret->tm_sec = cal.second;
- return ret;
- }
- #endif /* HAVE_RTP_SYS */
- #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
- /*
- * time() is just a stub in Microchip libraries. We need our own
- * implementation. Use SNTP client to get seconds since epoch.
- */
- time_t pic32_time(time_t* timer)
- {
- #ifdef MICROCHIP_TCPIP_V5
- DWORD sec = 0;
- #else
- word32 sec = 0;
- #endif
- #ifdef MICROCHIP_MPLAB_HARMONY
- sec = TCPIP_SNTP_UTCSecondsGet();
- #else
- sec = SNTPGetUTCSeconds();
- #endif
- if (timer != NULL)
- *timer = (time_t)sec;
- return (time_t)sec;
- }
- #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
- #if defined(WOLFSSL_DEOS) || defined(WOLFSSL_DEOS_RTEMS)
- time_t deos_time(time_t* timer)
- {
- const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
- const volatile word32 *systemTickPtr = systemTickPointer();
- if (timer != NULL)
- *timer = *systemTickPtr/systemTickTimeInHz;
- #if defined(CURRENT_UNIX_TIMESTAMP)
- /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */
- return (time_t) (*systemTickPtr/systemTickTimeInHz) + CURRENT_UNIX_TIMESTAMP;
- #else
- return (time_t) *systemTickPtr/systemTickTimeInHz;
- #endif
- }
- #endif /* WOLFSSL_DEOS || WOLFSSL_DEOS_RTEMS */
- #if defined(FREESCALE_RTC)
- #include "fsl_rtc.h"
- time_t fsl_time(time_t* t)
- {
- *t = RTC_GetSecondsTimerCount(RTC);
- return *t;
- }
- #endif
- #if defined(FREESCALE_SNVS_RTC)
- time_t fsl_time(time_t* t)
- {
- struct tm tm_time;
- time_t ret;
- snvs_hp_rtc_datetime_t rtcDate;
- snvs_hp_rtc_config_t snvsRtcConfig;
- SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
- SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
- SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
- tm_time.tm_year = rtcDate.year;
- tm_time.tm_mon = rtcDate.month;
- tm_time.tm_mday = rtcDate.day;
- tm_time.tm_hour = rtcDate.hour;
- tm_time.tm_min = rtcDate.minute;
- tm_time.tm_sec = rtcDate.second;
- ret = mktime(&tm_time);
- if (t != NULL)
- *t = ret;
- return ret;
- }
- #endif
- #if defined(MICRIUM)
- time_t micrium_time(time_t* timer)
- {
- CLK_TS_SEC sec;
- Clk_GetTS_Unix(&sec);
- if (timer != NULL)
- *timer = sec;
- return (time_t) sec;
- }
- #endif /* MICRIUM */
- #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
- time_t mqx_time(time_t* timer)
- {
- TIME_STRUCT time_s;
- _time_get(&time_s);
- if (timer != NULL)
- *timer = (time_t)time_s.SECONDS;
- return (time_t)time_s.SECONDS;
- }
- #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
- #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME)
- time_t XTIME(time_t * timer)
- {
- time_t sec = 0;
- sec = (time_t) Seconds_get();
- if (timer != NULL)
- *timer = sec;
- return sec;
- }
- #endif /* WOLFSSL_TIRTOS */
- #if defined(WOLFSSL_XILINX)
- #include "xrtcpsu.h"
- time_t xilinx_time(time_t * timer)
- {
- time_t sec = 0;
- XRtcPsu_Config* con;
- XRtcPsu rtc;
- con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
- if (con != NULL) {
- if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
- sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
- }
- else {
- WOLFSSL_MSG("Unable to initialize RTC");
- }
- }
- if (timer != NULL)
- *timer = sec;
- return sec;
- }
- #endif /* WOLFSSL_XILINX */
- #if defined(WOLFSSL_ZEPHYR)
- time_t z_time(time_t * timer)
- {
- struct timespec ts;
- #if defined(CONFIG_RTC) && \
- (defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC))
- /* Try to obtain the actual time from an RTC */
- static const struct device *rtc = DEVICE_DT_GET(DT_NODELABEL(rtc));
- if (device_is_ready(rtc)) {
- struct rtc_time rtc_time;
- struct tm *tm_time = rtc_time_to_tm(&rtc_time);
- int ret = rtc_get_time(rtc, &rtc_time);
- if (ret == 0) {
- time_t epochTime = mktime(tm_time);
- if (timer != NULL)
- *timer = epochTime;
- return epochTime;
- }
- }
- #endif
- /* Fallback to uptime since boot. This works for relative times, but
- * not for ASN.1 date validation */
- if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
- if (timer != NULL)
- *timer = ts.tv_sec;
- return ts.tv_sec;
- }
- #endif /* WOLFSSL_ZEPHYR */
- #if defined(WOLFSSL_WICED)
- #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME
- #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time.
- #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */
- time_t wiced_pseudo_unix_epoch_time(time_t * timer)
- {
- time_t epoch_time;
- /* The time() function return uptime on WICED platform. */
- epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME;
- if (timer != NULL) {
- *timer = epoch_time;
- }
- return epoch_time;
- }
- #endif /* WOLFSSL_WICED */
- #ifdef WOLFSSL_TELIT_M2MB
- time_t m2mb_xtime(time_t * timer)
- {
- time_t myTime = 0;
- INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
- if (fd != -1) {
- M2MB_RTC_TIMEVAL_T timeval;
- m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
- myTime = timeval.sec;
- m2mb_rtc_close(fd);
- }
- return myTime;
- }
- #ifdef WOLFSSL_TLS13
- time_t m2mb_xtime_ms(time_t * timer)
- {
- time_t myTime = 0;
- INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
- if (fd != -1) {
- M2MB_RTC_TIMEVAL_T timeval;
- m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
- myTime = timeval.sec + timeval.msec;
- m2mb_rtc_close(fd);
- }
- return myTime;
- }
- #endif /* WOLFSSL_TLS13 */
- #ifndef NO_CRYPT_BENCHMARK
- double m2mb_xtime_bench(int reset)
- {
- double myTime = 0;
- INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
- if (fd != -1) {
- M2MB_RTC_TIMEVAL_T timeval;
- m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
- myTime = (double)timeval.sec + ((double)timeval.msec / 1000);
- m2mb_rtc_close(fd);
- }
- return myTime;
- }
- #endif /* !NO_CRYPT_BENCHMARK */
- #endif /* WOLFSSL_TELIT_M2MB */
- #if defined(WOLFSSL_LINUXKM)
- time_t time(time_t * timer)
- {
- time_t ret;
- #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
- struct timespec ts;
- getnstimeofday(&ts);
- ret = ts.tv_sec;
- #else
- struct timespec64 ts;
- #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
- ts = current_kernel_time64();
- #else
- ktime_get_coarse_real_ts64(&ts);
- #endif
- ret = ts.tv_sec;
- #endif
- if (timer)
- *timer = ret;
- return ret;
- }
- #endif /* WOLFSSL_LINUXKM */
- #ifdef HAL_RTC_MODULE_ENABLED
- extern RTC_HandleTypeDef hrtc;
- time_t stm32_hal_time(time_t *t1)
- {
- struct tm tm_time;
- time_t ret;
- RTC_TimeTypeDef time;
- RTC_DateTypeDef date;
- XMEMSET(&tm_time, 0, sizeof(struct tm));
- /* order of GetTime followed by GetDate required here due to STM32 HW
- * requirement */
- HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN);
- HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN);
- /* RTC year is 0-99 and "struct tm" is 1900+, so assume after year 2000 */
- tm_time.tm_year = date.Year + 100;
- /* RTC month is 1-12 and "struct tm" is 0-12, so subtract 1 */
- tm_time.tm_mon = date.Month - 1;
- tm_time.tm_mday = date.Date;
- tm_time.tm_hour = time.Hours;
- tm_time.tm_min = time.Minutes;
- tm_time.tm_sec = time.Seconds;
- ret = mktime(&tm_time);
- if (t1 != NULL)
- *t1 = ret;
- return ret;
- }
- #endif /* HAL_RTC_MODULE_ENABLED */
- #endif /* !NO_ASN_TIME */
- #if !defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)
- char* mystrnstr(const char* s1, const char* s2, unsigned int n)
- {
- unsigned int s2_len = (unsigned int)XSTRLEN(s2);
- if (s2_len == 0)
- return (char*)s1;
- while (n >= s2_len && s1[0]) {
- if (s1[0] == s2[0])
- if (XMEMCMP(s1, s2, s2_len) == 0)
- return (char*)s1;
- s1++;
- n--;
- }
- return NULL;
- }
- #endif
- /* custom memory wrappers */
- #ifdef WOLFSSL_NUCLEUS_1_2
- /* system memory pool */
- extern NU_MEMORY_POOL System_Memory;
- void* nucleus_malloc(unsigned long size, void* heap, int type)
- {
- STATUS status;
- void* stack_ptr;
- status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
- NU_NO_SUSPEND);
- if (status == NU_SUCCESS) {
- return 0;
- } else {
- return stack_ptr;
- }
- }
- void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
- {
- DM_HEADER* old_header;
- word32 old_size, copy_size;
- void* new_mem;
- /* if ptr is NULL, behave like malloc */
- new_mem = nucleus_malloc(size, NULL, 0);
- if (new_mem == 0 || ptr == 0) {
- return new_mem;
- }
- /* calculate old memory block size */
- /* mem pointers stored in block headers (ref dm_defs.h) */
- old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
- old_size = (byte*)old_header->dm_next_memory - (byte*)ptr;
- /* copy old to new */
- if (old_size < size) {
- copy_size = old_size;
- } else {
- copy_size = size;
- }
- XMEMCPY(new_mem, ptr, copy_size);
- /* free old */
- nucleus_free(ptr, NULL, 0);
- return new_mem;
- }
- void nucleus_free(void* ptr, void* heap, int type)
- {
- if (ptr != NULL)
- NU_Deallocate_Memory(ptr);
- }
- #endif /* WOLFSSL_NUCLEUS_1_2 */
- #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
- #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
- #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
- #endif
- #if defined(WOLFSSL_CRYPTOCELL)
- #define WOLFSSL_CRYPTOCELL_C
- #include <wolfcrypt/src/port/arm/cryptoCell.c> /* CC310, RTC and RNG */
- #if !defined(NO_SHA256)
- #define WOLFSSL_CRYPTOCELL_HASH_C
- #include <wolfcrypt/src/port/arm/cryptoCellHash.c> /* sha256 */
- #endif
- #endif
- #ifndef SINGLE_THREADED
- #if defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS)
- int wolfSSL_NewThread(THREAD_TYPE* thread,
- THREAD_CB cb, void* arg)
- {
- if (thread == NULL || cb == NULL)
- return BAD_FUNC_ARG;
- /* Use _beginthreadex instead of _beginthread because of:
- * _beginthreadex is safer to use than _beginthread. If the thread
- * that's generated by _beginthread exits quickly, the handle that's
- * returned to the caller of _beginthread might be invalid or point
- * to another thread. However, the handle that's returned by
- * _beginthreadex has to be closed by the caller of _beginthreadex,
- * so it's guaranteed to be a valid handle if _beginthreadex didn't
- * return an error.*/
- *thread = _beginthreadex(NULL, 0, cb, arg, 0, NULL);
- if (*thread == 0) {
- *thread = INVALID_THREAD_VAL;
- return MEMORY_E;
- }
- return 0;
- }
- #ifdef WOLFSSL_THREAD_NO_JOIN
- int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg)
- {
- THREAD_TYPE thread;
- if (cb == NULL)
- return BAD_FUNC_ARG;
- thread = _beginthread(cb, 0, arg);
- if (thread == -1L) {
- return MEMORY_E;
- }
- return 0;
- }
- #endif
- int wolfSSL_JoinThread(THREAD_TYPE thread)
- {
- int ret = 0;
- if (thread == INVALID_THREAD_VAL)
- return BAD_FUNC_ARG;
- /* We still want to attempt to close the thread handle even on error */
- if (WaitForSingleObject((HANDLE)thread, INFINITE) == WAIT_FAILED)
- ret = MEMORY_E;
- if (CloseHandle((HANDLE)thread) == 0)
- ret = MEMORY_E;
- return ret;
- }
- #ifdef WOLFSSL_COND
- int wolfSSL_CondInit(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- cond->cond = CreateEventA(NULL, FALSE, FALSE, NULL);
- if (cond->cond == NULL)
- return MEMORY_E;
- if (wc_InitMutex(&cond->mutex) != 0) {
- if (CloseHandle(cond->cond) == 0)
- return MEMORY_E;
- return MEMORY_E;
- }
- return 0;
- }
- int wolfSSL_CondFree(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (CloseHandle(cond->cond) == 0)
- return MEMORY_E;
- return 0;
- }
- int wolfSSL_CondStart(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_LockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondSignal(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_UnLockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- if (SetEvent(cond->cond) == 0)
- return MEMORY_E;
- if (wc_LockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondWait(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_UnLockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- if (WaitForSingleObject(cond->cond, INFINITE) == WAIT_FAILED)
- return MEMORY_E;
- if (wc_LockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondEnd(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_UnLockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- #endif /* WOLFSSL_COND */
- #elif defined(WOLFSSL_TIRTOS)
- int wolfSSL_NewThread(THREAD_TYPE* thread,
- THREAD_CB cb, void* arg)
- {
- /* Initialize the defaults and set the parameters. */
- Task_Params taskParams;
- Task_Params_init(&taskParams);
- taskParams.arg0 = (UArg)arg;
- taskParams.stackSize = 65535;
- *thread = Task_create((Task_FuncPtr)cb, &taskParams, NULL);
- if (*thread == NULL) {
- return MEMORY_E;
- }
- Task_yield();
- return 0;
- }
- int wolfSSL_JoinThread(THREAD_TYPE thread)
- {
- while(1) {
- if (Task_getMode(thread) == Task_Mode_TERMINATED) {
- Task_sleep(5);
- break;
- }
- Task_yield();
- }
- return 0;
- }
- #elif defined(NETOS)
- int wolfSSL_NewThread(THREAD_TYPE* thread,
- THREAD_CB cb, void* arg)
- {
- /* For backwards compatibility allow using this declaration as well. */
- #ifdef TESTSUITE_THREAD_STACK_SZ
- #define WOLFSSL_NETOS_STACK_SZ TESTSUITE_THREAD_STACK_SZ
- #endif
- /* This can be adjusted by defining in user_settings.h, will default to
- * 65k in the event it is undefined */
- #ifndef WOLFSSL_NETOS_STACK_SZ
- #define WOLFSSL_NETOS_STACK_SZ 65535
- #endif
- int result;
- if (thread == NULL || cb == NULL)
- return BAD_FUNC_ARG;
- XMEMSET(thread, 0, sizeof(*thread));
- thread->threadStack = (void *)XMALLOC(WOLFSSL_NETOS_STACK_SZ, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (thread->threadStack == NULL)
- return MEMORY_E;
- /* first create the idle thread:
- * ARGS:
- * Param1: pointer to thread
- * Param2: name
- * Param3 and 4: entry function and input
- * Param5: pointer to thread stack
- * Param6: stack size
- * Param7 and 8: priority level and preempt threshold
- * Param9 and 10: time slice and auto-start indicator */
- result = tx_thread_create(&thread->tid,
- "wolfSSL thread",
- (entry_functionType)cb, (ULONG)arg,
- thread->threadStack,
- TESTSUITE_THREAD_STACK_SZ,
- 2, 2,
- 1, TX_AUTO_START);
- if (result != TX_SUCCESS) {
- free(thread->threadStack);
- thread->threadStack = NULL;
- return MEMORY_E;
- }
- return 0;
- }
- int wolfSSL_JoinThread(THREAD_TYPE thread)
- {
- /* TODO: maybe have to use tx_thread_delete? */
- free(thread.threadStack);
- thread.threadStack = NULL;
- return 0;
- }
- #elif defined(WOLFSSL_ZEPHYR)
- int wolfSSL_NewThread(THREAD_TYPE* thread,
- THREAD_CB cb, void* arg)
- {
- #ifndef WOLFSSL_ZEPHYR_STACK_SZ
- #define WOLFSSL_ZEPHYR_STACK_SZ (24*1024)
- #endif
- if (thread == NULL || cb == NULL)
- return BAD_FUNC_ARG;
- XMEMSET(thread, 0, sizeof(*thread));
- /* TODO: Use the following once k_thread_stack_alloc makes it into a
- * release.
- * thread->threadStack = k_thread_stack_alloc(WOLFSSL_ZEPHYR_STACK_SZ,
- * 0);
- */
- thread->threadStack = (void*)XMALLOC(
- Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), 0,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (thread->threadStack == NULL)
- return MEMORY_E;
- /* k_thread_create does not return any error codes */
- /* Casting to k_thread_entry_t should be fine since we just ignore the
- * extra arguments being passed in */
- k_thread_create(&thread->tid, thread->threadStack,
- WOLFSSL_ZEPHYR_STACK_SZ, (k_thread_entry_t)cb, arg, NULL, NULL,
- 5, 0, K_NO_WAIT);
- return 0;
- }
- int wolfSSL_JoinThread(THREAD_TYPE thread)
- {
- int ret = 0;
- int err;
- err = k_thread_join(&thread.tid, K_FOREVER);
- if (err != 0)
- ret = MEMORY_E;
- /* TODO: Use the following once k_thread_stack_free makes it into a
- * release.
- * err = k_thread_stack_free(thread.threadStack);
- * if (err != 0)
- * ret = MEMORY_E;
- */
- XFREE(thread.threadStack, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- thread.threadStack = NULL;
- /* No thread resources to free. Everything is stored in thread.tid */
- return ret;
- }
- #ifdef WOLFSSL_COND
- /* Use the pthreads translation layer for signaling */
- #endif /* WOLFSSL_COND */
- #elif defined(WOLFSSL_PTHREADS)
- int wolfSSL_NewThread(THREAD_TYPE* thread,
- THREAD_CB cb, void* arg)
- {
- if (thread == NULL || cb == NULL)
- return BAD_FUNC_ARG;
- if (pthread_create(thread, NULL, cb, arg) != 0)
- return MEMORY_E;
- return 0;
- }
- #ifdef WOLFSSL_THREAD_NO_JOIN
- int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg)
- {
- THREAD_TYPE thread;
- int ret;
- XMEMSET(&thread, 0, sizeof(thread));
- ret = wolfSSL_NewThread(&thread, cb, arg);
- if (ret == 0)
- ret = pthread_detach(thread);
- return ret;
- }
- #endif
- int wolfSSL_JoinThread(THREAD_TYPE thread)
- {
- if (thread == INVALID_THREAD_VAL)
- return BAD_FUNC_ARG;
- if (pthread_join(thread, NULL) != 0)
- return MEMORY_E;
- return 0;
- }
- #ifdef WOLFSSL_COND
- #ifndef __MACH__
- /* Generic POSIX conditional */
- int wolfSSL_CondInit(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (pthread_mutex_init(&cond->mutex, NULL) != 0)
- return MEMORY_E;
- if (pthread_cond_init(&cond->cond, NULL) != 0) {
- /* Keep compilers happy that we are using the return code */
- if (pthread_mutex_destroy(&cond->mutex) != 0)
- return MEMORY_E;
- return MEMORY_E;
- }
- return 0;
- }
- int wolfSSL_CondFree(COND_TYPE* cond)
- {
- int ret = 0;
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (pthread_mutex_destroy(&cond->mutex) != 0)
- ret = MEMORY_E;
- if (pthread_cond_destroy(&cond->cond) != 0)
- ret = MEMORY_E;
- return ret;
- }
- int wolfSSL_CondStart(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (pthread_mutex_lock(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondSignal(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (pthread_cond_signal(&cond->cond) != 0)
- return MEMORY_E;
- return 0;
- }
- int wolfSSL_CondWait(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0)
- return MEMORY_E;
- return 0;
- }
- int wolfSSL_CondEnd(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (pthread_mutex_unlock(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- #else /* __MACH__ */
- /* Apple style dispatch semaphore */
- int wolfSSL_CondInit(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- /* dispatch_release() fails hard, with Trace/BPT trap signal, if the
- * sem's internal count is less than the value passed in with
- * dispatch_semaphore_create(). work around this by initing
- * with 0, then incrementing it afterwards.
- */
- cond->cond = dispatch_semaphore_create(0);
- if (cond->cond == NULL)
- return MEMORY_E;
- if (wc_InitMutex(&cond->mutex) != 0) {
- dispatch_release(cond->cond);
- return MEMORY_E;
- }
- return 0;
- }
- int wolfSSL_CondFree(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- dispatch_release(cond->cond);
- cond->cond = NULL;
- if (wc_FreeMutex(&cond->mutex) != 0) {
- return MEMORY_E;
- }
- return 0;
- }
- int wolfSSL_CondStart(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_LockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondSignal(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_UnLockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- dispatch_semaphore_signal(cond->cond);
- if (wc_LockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondWait(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_UnLockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER);
- if (wc_LockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- int wolfSSL_CondEnd(COND_TYPE* cond)
- {
- if (cond == NULL)
- return BAD_FUNC_ARG;
- if (wc_UnLockMutex(&cond->mutex) != 0)
- return BAD_MUTEX_E;
- return 0;
- }
- #endif /* __MACH__ */
- #endif /* WOLFSSL_COND */
- #endif
- #endif /* SINGLE_THREADED */
|